This is an old revision of the document!
Table of Contents
rsync
rsync (or remote sync) is a command line utility for transferring and synchronizing files between any given “source” and “destination” pair. It is extremely popular for use in backup utilities, at it can synchronize backups between not only local filesystems, but also over the internet via SSH.
If you are already familiar with using Wget to make mirrors of entire websites, then use of rsync should come to you pretty naturally.
Useful Commands
Using rsync may seem obtuse to new users at first. However, once you know how to structure and alias your commands, it is one of the absolute fastest & reliable ways to run backup systems:
Built-in Documentation
rsync --h and man grep will tell you all the flags which are available to use. We will only detail some of the most important ones below:
Local Backups
To have rsync copy all files from one directory to another, use this command:
$ rsync -av /path/to/source/dir/ /path/to/destination/dir/
The -a flag tells rsync to not only recursively go into subdirectories, but it also ensures all attributes like timestamps & permissions are preserved. -v is for verbose output, so you can see what rsync is doing.
Remote & Network Backups
rsync supports the use of SSH for transferring backups over network connections, using the -e flag.
To back up files from your local shell to a remote machine via SSH:
$ rsync -avz -e ssh /source/directory/ user@remote:/destination/directory/
Here we are also using the -z flag, which compresses the data to speed up transfer.
To back up files from a remote machine to your local shell via SSH:
$ rsync -avz -e ssh user@remote:/source/directory/ /destination/directory/
In the case of both of these commands, “local shell” just means whatever terminal you are running rsync from. If you are running this from a remote machine, that machine is the “local shell” in this equation, and whatever machine it is SSH'ing into is the “remote shell.” Make sense? Good, because there's a few more things you have to grasp with this command, or you're going to make an absolute mess of your files.
Incremental Backups
If you are regularly backing up the same folders, drive, server, etc, then rsync needs to be tweaked further with two more flags, or over time your backups are not going to look like the folders you were backing up:
$ rsync -avu --delete /path/to/source/dir/ /path/to/destination/dir/
With the -u (or --update) flag, we can tell rsync to only copy files from the source that do not exist in the destination, or are newer than the same files in the destination, and skip all the others. With the --delete flag, we can tell rsync to delete files in the destination that no longer exist in the source.
This is the form of rsync that you want to use in automated backup scripts.
If you are looking for an even more failsafe way to ensure the quality of backups, use the -c flag to have rsync compare checksums of every conflicting file transfer instead of modification times. This method is a lot slower, but far more accurate when left on autopilot.
Excluding Things From Backups
If there are files/folders that you wish to have rsync skip over, use the --exclude flag like this:
$ rsync -av --exclude 'filename' --exclude 'path/' /path/to/source/dir/ /path/to/destination/dir/
If you have a lot of exclusions, you can list them all out one-per-line in a text file, and pass it to rsync with --exclude-from like this:
$ rsync -av --exclude-from 'exclusions.txt' /path/to/source/dir/ /path/to/destination/dir/
More Command Flags
There are more flags you can combine with these basic rsync commands to fine-tune how your backups are executed. Play around with them on some demo directories to see how they affect your files!
- Use the
-nor--dry-runflag in combination with-vto perform a simulated transfer. - Use
--progressto see real-time transfer status. - Use
--log-file=transfer.logto generate a log file of the transfer.
What About Automatic Backups?
rsync does not have a built-in scheduling service. The easiest way to set your rsync-powered backups on autopilot, is with the use of crontab.
Find this page online at: https://bestpoint.institute/tools/rsync