Transferring files between directories and servers with Rsync

Let’s say you have a huge amount of files in 1 directory that you want to synchronise with another directory, either locally or on a remote server, and it is widely accepted that the best tool for this job is rsync.

The rsync can be installed on most distributions simply by using the package manager, e.g. yum or apt.

To sync all the contents of /home/billy/music to /home/bob/music, then you can run:

rsync -a /home/billy/music/ /home/bob/music
In the example above, the -a means recursive (all files and folders and files and folders within folders) while preserving group/owner and permissions and preserving any symbolic links.
Also note that in the first directory, the source has a trailing slash while the destination does not, this is important, if you remove the trailing slash from the source then the music directory itself would be moved into the destination music directory so you would end up with /home/bob/music/musicIf you just want to test your rsync command without actually executing it to see what it would do you can change -a to -anv, this will do what is known as a dry run with verbose output so you can see what would have happened if you ran just -a this can be helpful as rsync can at times be potentially destructive depending on your command string.

Now let us say you want to sync up your music collection between servers/computers, first of all, you will need to install rsync on both sides then the command is a little bit different:

rsync -a /home/billy/music/ billy@remote-server-ip-address:/home/billy/music

In the above command again note the trailing slash on the local directory to prevent the directory itself from being copied, then it is your username and password (ssh) for the remote server.

If there are a lot of files and bandwidth is a factor you can use -az instead of just -a, this will compress the data in transit, while that may sound like a good option generally it can actually slow things down in practice so only worth using if the amount of data is large and bandwidth volume is a factor.

If you want to see the progress in real-time you can use the P flag e.g. rsync -azP will recursively copy preserving permissions etc (a), compress the data in transmission (z), and show you the progress as it goes (P).

Now, it gets dangerous, so please make sure you fully understand everything above before we get into true synchronisation, including deletion. You can add –delete to your command string to delete files on the destination that do not exist on the source.

Example:

rsync -azP --delete /home/billy/music/ billy@remote-server-ip-address:/home/billy/music

This will delete anything on the destination server that is not present on the source. For this reason, please always do a dry run using -anvzP first. Adding the nv will do a test run with verbose output.

If you are using rsync as a backup tool, then why not consider something a bit more enterprise-grade? Clouvider provides 50GB of free Acronis Cyber backup space with all dedicated servers, details here: https://www.clouvider.co.uk/acronis-cyber-cloud-backup/