The man page calls Rsync a fast and versatile remote and local file copying tool. Rsync is a powerful utility baked into many Linux systems and widely used by administrators for backups. Rsync is famous for its ability to send over only the changed portions of the files resulting in extremely fast copy times. You can make a complete backup just once and future backups will only copy the changed bits. Rsync can copy files to and from a local/remote host as well as between 2 locations on a local host. (Rsync does not support copying between 2 remote hosts.)
To use Rsync, you must have shell access to the remote machine (typically SSH) and Rsync installed on both client and server. When connecting to a machine with Rsync you will be prompted for login credentials unless you have SSH keys setup. Alternatively, the Rsync client can connect to any machine running the Rsync daemon. The Rsync daemon is useful when you don’t want to allow user level SSH access to the server.
There are a lot of options for Rsync but most administrators only use a handful of them. Below are the most popular options.
|-a||Archive mode. Back up recursively and preserve permissions etc.|
|-v||verbose. Increase output to terminal.|
|-z||Compress data before transferring|
|--progress||Show progress bar|
|--exclude||Exclude specific files from transfer|
|--remove-source-files||Remove files from source once data is transferred|
|--dry-run||Output files to be copied/skipped. Does not actually run.|
1.) Sync contents of 2 folders
At the most basic level Rsync can sync the contents of 2 directories. This isn’t exactly a backup but allows you to explore the basics of what Rsync has to offer.
Here we use Rsync with no options to sync the contents of 2 folders:
rsync /path/to/folder1/ /path/to/folder2/
2.) Sync entire contents of 2 directories including sub directories
With no options specified Rsync will only copy files in a directory. By default it does not copy sub directories. The -r switch tells Rsync to copy everything including all directories and sub directories.
rsync -r /path/to/folder1/ /path/to/folder2/
3.) Copy everything in archive mode
The -a option is the most popular option to use with Rsync. This option is actually many options strung together. -a is a full archive backup and backs up everything including time stamps, permissions, and modifications. -a is the same as using the options -rlptgoD.
rsync -a /path/to/folder1/ /path/to/folder2/
4.) Sync a remote folder to a local folder
Rsyncs true power comes from its ability to securely sync a remote folder to a local folder. Useful for backing up a web server. Here we sync a remote folder called folder1 to a local directory folder2:
rsync -a firstname.lastname@example.org:/var/www/folder1 /var/www/folder2
5.) Sync a local folder with a remote folder and compress before sending
Rsync can compress data before it transfers over the network. This is useful for slow networks but should be avoided if CPU power is limited. Compression is executed with the -z option.
rsync -az /var/www/folder1 email@example.com:/var/www
6.) Do a dry run first
Rsync can tell you what will be copied without actually copying any data. This can provide a visual overview of what is missing in the destination as well as how much data will be transferred. It can also save your bacon especially when using the –delete option! Here we also include the -v (verbose) so we can see the output in the terminal.
rsync -av --dry-run /var/www/folder1 firstname.lastname@example.org:/var/www
7.) Delete files in destination that are not in the source
Sometimes you want to completely mirror a directory and remove extra files. The –delete option tells Rsync to delete everything in the destination that is not in the source. Be careful with this one! If you are accidentally in an empty directory you can completely wipe out your destination files.
rsync -av --delete /var/www/folder1 email@example.com:/var/www
8.) Delete files from source once synchronized
When you no longer need the source files.
rsync --remove-source-files -avz /var/www/folder1 firstname.lastname@example.org:/var/www
9.) Exclude specific files
In this example we exclude every document with a .docx file extension
rsync -avz --exclude '*.docx' email@example.com:/var/www/folder1/ /var/www/folder1/
10.) Show a progress bar
For those who gain deep satisfaction from watching progress bars tick by the screen. Here we use the –progress option to create a nice little progress report of each transfer in the terminal.
rsync -avz --progress /var/www/folder1 firstname.lastname@example.org:/var/www