How to use rsync

Try it in our public cloud & Get $50 Credit
CLAIM NOW

Rsync is a versatile tool. Similar in some ways to the generic cp command, rsync does so much more. It can make and restore backups, copy files with metadata intact, and perform a host of other tasks where file synchronization and integrity are important. It also uses its own protocol or works via SSH, making it a great tool for everything from backing up local laptops to administering fleets of servers. Even so, it is complicated. Below you’ll find an introduction to some of its most common features and command line flags.

Getting Started

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) with a clean version of Linux installed.
• Later steps will require a second Linux server with which to synchronize files.

Tutorial

We’ll start by making two directories for synchronizing files.

cd /root
mkdir test1
mkdir test2

Now let’s create a file in test1 and see how rsync copies it to test2.

touch /root/test1/test-file.txt
rsync -r /root/test1/ /root/test2/

“-r” means “recursive.” It copies all the files in the source directory to the second, recursing into subdirectories. Another convenient shortcut is the “-a” option. This switch archives the source to the destination, activating the “-r” flag and also copying metadata such as permissions and modification times.

rsync -a /root/test1/ /root/test2/

After this command, you’ll see your files synchronized to /root/test2.

ls /root/test2/
test-file.txt

Next let’s add a remote server into the mix. This example copies a complete directory from one server to another.

rsync -a /root/test1/ username@destination_ip:/destination/folder/

Here are some additional helpful command line flags to increase rsync’s usefulness:

“-v”: This flag increases verbosity, providing additional information on the stages of the synchronization.

rsync -av /root/test1/ /root/test2/
sending incremental file list
./
test-file.txt1
test-file.txt10
test-file.txt11
test-file.txt12
test-file.txt13
test-file.txt14
test-file.txt15
test-file.txt16
test-file.txt17
test-file.txt18
test-file.txt19
test-file.txt2
test-file.txt3
test-file.txt4
test-file.txt5
test-file.txt6
test-file.txt7
test-file.txt8
test-file.txt9
sent 922 bytes received 376 bytes 2596.00 bytes/sec
total size is 0 speedup is 0.00

“–delete”: Say test2 has 20 files, and test1 has only 19 because one was removed. This option synchronizes both directories, deleting any files not present in each. In the above scenario, both test1 and test2 will end up with 19 files because the operation will delete the missing file from test2.

touch /root/test1/test-file.txt{1..19}
touch /root/test2/test-file.txt{1..20}
ls /root/test1/
test-file.txt1 test-file.txt11 test-file.txt13 test-file.txt15 test-file.txt17 test-file.txt19 test-file.txt3 test-file.txt5 test-file.txt7 test-file.txt9
test-file.txt10 test-file.txt12 test-file.txt14 test-file.txt16 test-file.txt18 test-file.txt2 test-file.txt4 test-file.txt6 test-file.txt8

ls /root/test2/
test-file.txt1 test-file.txt11 test-file.txt13 test-file.txt15 test-file.txt17 test-file.txt19 test-file.txt20 test-file.txt4 test-file.txt6 test-file.txt8
test-file.txt10 test-file.txt12 test-file.txt14 test-file.txt16 test-file.txt18 test-file.txt2 test-file.txt3 test-file.txt5 test-file.txt7 test-file.txt9

rsync -a --delete /root/test1/ /root/test2/
ls /root/test2/
test-file.txt1 test-file.txt11 test-file.txt13 test-file.txt15 test-file.txt17 test-file.txt19 test-file.txt3 test-file.txt5 test-file.txt7 test-file.txt9
test-file.txt10 test-file.txt12 test-file.txt14 test-file.txt16 test-file.txt18 test-file.txt2 test-file.txt4 test-file.txt6 test-file.txt8

As with any other rsync flags, “–delete” works across server boundaries.

rsync -a --delete /root/test1/ username@destination_ip/root/test2/

Conclusion

A good understanding of rsync is beneficial for any developer or system administrator. It can both perform a quick copy or orchestrate a full backup, and is invaluable for many day-to-day Linux administrative tasks. If this guide was helpful to you, kindly share it with others who may also be interested.