The rsync command is an invaluable tool for advanced file/directory copying and syncing operations, particularly useful for efficiently making and maintaining backups (by synchronising the source and destination directories). It can be used to copy and sync files from any source and to any destination both locally and remotely. It is famous for its ability to copy only the differences in files rather than the whole files when they have been modified, which drastically reduces the required bandwidth.
Rsync commands are written in the following format:
Here is an example:
This will copy all the files and folders from the
data directory to
/media/backups/data, which could be on your external backup device. This example already contains three commonly used and useful options:
-a copies recursively (all directories and subdirectories) while preserving symbolic links, permissions, file ownerships and timestamps. Similar to -r except that -r does not preserve permissions and timestamps-
-v shows verbose output, telling you more of what it’s doing.
-z uses compression when transferring, which can speed up the transfer, especially over the network.
There are, of course, other useful options that can be added, such as:
-h displays numbers in a human readable form, easier to understand the amount of data transferred.
–stats shows statistics on file transfers if you want even more insight into the statistics of the transferred data.
-m prunes empty directories
-n so called “dry run”, that is, it simulates what it would do with a given command without actually making any changes. Could be very useful if you want to make sure you don’t screw up and end up losing your data.
-p preserve permissions, in case you’re using the -r option instead of -a or don’t use recursive syncing, but want to preserve permissions.
-e allows specifying the remote shell to use when you want to use the remote source or destination.
-e option you can, for example, use ssh to transfer data with rsync. It would look something like this:
When you run a command like this it will ask you for your remote SSH server password, and then copy over the contents of the data directory to /home/backups/ on the remote system.
As with most standard Linux and UNIX utilities, you can run man rsync to quickly get more information about what other options are available, but this should cover most important uses.