There are a variety of ways to transfer content from one server to another. One of the most popular and versatile methods to use is rsync. The rsync command allows a user to execute commands locally on one server, and connect to another to transfer files, download files, or even mirror files using a few basic flags. This is extremely useful when transferring website content from a dev environment, or when managing similar website content across multiple hosts.
If this is your first time using rsync, you may wish to familiarize yourself with the flags available.
rsync –options
The most common rsync options include:
-v : Verbose
-a : archive mode
-r : run command recursively in directory
-z : compress data
The basic syntax for rsync is as follows:
rsync -options ...
The syntax for specifying the remote machine is user@host:/path, which could be the source or destination. The username is optional.
Using rsync to move content from one server to another
Rsync commands are executed through shell, so you’ll need an active Linux server, as well as an SSH client such as PuTTY (for Windows) or Terminal (Mac.) We recommend a GoDaddy Virtual Private Server if you’re just getting started, or a full dedicated server if you’re ready to take total control.
[requirements difficulty="1" time="10"]
The most common use for rsync is copying files from one machine to another. The examples below illustrate several ways to copy different sets of files to a new server, “Batman” (server names can vary; normally this will be an IP address of the remote host.)
rsync *.txt batman:/tmp
In this example all .txt files are being copied from the local machine to batman. This example assumes that the username for both source and destination is the same, if this is not the case the username must be specified, like so:
rsync *.txt username@batman:/tmp
To reverse the direction of the copy, swap around the arguments, using . to represent the current directory.
rsync batman:/tmp/file.txt .
Instead of specifying individual files to copy, you can also back up a whole directory tree by using a directory name as the source, with or without the -r option to recursively get subdirectories. Using this command, if the destination directory doesn't exist, it will be created.
rsync -r /tmp/ username@batman:/tmp/foo
To preserve symbolic links, file permissions and ownerships, and other file attributes, use the –a option to archive. Couple this with the -v option and you'll see more verbose output. This can be done to a remote system as shown in the first option, or a local directory as in the second example:
rsync -av /tmp/foo username@batman:/tmp/bar
rsync -av /tmp/foo /tmp/bar