rsync command

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 syntax

Rsync commands are written in the following format:

rsync -options [source] [destination].

Here is an example:

rsync -avz data/ /media/backups/data

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.

Useful rsync Options

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.

With the -e option you can, for example, use ssh to transfer data with rsync. It would look something like this:

rsync -avze ssh data [email protected]:/home/backups/

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.

See Also




Contact Me

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!

Recent Tweets