How to Use the Linux rsync Command for Backups

The Bandwidth Problem

You manage a Linux web server in New York, and it contains 500 gigabytes of high-resolution images. Every night, you need to back up that massive directory to a secondary server in London for disaster recovery.

If you use a basic copy command (like cp or scp), the system will mindlessly attempt to transfer all 500 gigabytes over the internet every single night. This will take twelve hours, crush your network bandwidth, and cost you a fortune in cloud data transfer fees. It is a wildly inefficient way to manage backups because, on any given day, you probably only added or modified 5 megabytes of new images.

To solve this, system administrators rely on a powerful utility called rsync (Remote Sync). Instead of blindly copying everything, rsync uses a brilliant mathematical algorithm to compare the source folder in New York with the destination folder in London. It calculates exactly which files are new, which have changed, and which were deleted, and then it only transfers those specific changes. What used to take twelve hours now takes twelve seconds.

The Basic rsync Syntax

The syntax for rsync looks very similar to a standard copy command: rsync [options] [source] [destination].

The true power of the command comes from the flags you use. The absolute gold standard combination of flags used by every sysadmin is -avz.

  • -a (Archive): This is the most important flag. It preserves all file permissions, ownership, timestamps, and symbolic links, ensuring the backup is an exact, functional clone of the original.
  • -v (Verbose): This forces the command to print out a detailed list of exactly which files it is currently transferring, so you are not staring at a blank screen.
  • -z (Compress): This compresses the file data during the transfer across the network, further reducing bandwidth usage, and decompresses it automatically on the other side.

Syncing Between Two Local Folders

Before transferring data across the ocean, it is best to understand how rsync works locally. If you want to back up a folder called /var/www/images to an external hard drive mounted at /mnt/backup, you would type:

rsync -avz /var/www/images/ /mnt/backup/

Crucial Detail: Pay close attention to the trailing slash on the source folder (images/). In the rsync language, a trailing slash means “copy the contents of this folder.” If you forget the slash (images), it means “copy the folder itself,” which will create a nested directory like /mnt/backup/images/.

The first time you run this command, it will copy everything. If you run it again five minutes later, it will finish instantly, reporting that 0 bytes were transferred because nothing changed.

Syncing to a Remote Server

To push your data to the backup server in London, rsync automatically uses the secure SSH protocol to encrypt the transfer.

You define the destination exactly like an SSH login (username@ip_address:/path/).

rsync -avz /var/www/images/ [email protected]:/var/backup/london_images/

The system will ask for the London server’s SSH password (unless you have SSH keys configured), compare the two directories across the internet, and securely transmit only the delta.

Deleting Stale Files (–delete)

By default, rsync only adds files. If you delete a bad image from the New York server, rsync will not delete it from the London server. Over time, your backup server will become bloated with thousands of files you thought you deleted.

To force the London server to remain an exact, 1:1 mirror of the New York server, you must add the –delete flag.

rsync -avz --delete /var/www/images/ [email protected]:/var/backup/london_images/

Now, if a file goes missing in New York, rsync will actively hunt it down and destroy it in London.

Conclusion

Stop wasting bandwidth by blindly copying entire directories. By mastering the Linux rsync command and its powerful archive and delete flags, you can establish lightning-fast, bandwidth-efficient, and perfectly synchronized remote backups for massive datasets.

Related posts

  1. Essential Linux Terminal Keyboard Shortcuts to Work Faster
  2. How to Find Large Files and Directories in Linux
  3. How to Create and Extract ZIP Files in the Linux Terminal

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.