How to Use the rsync Command to Securely Backup Linux Servers

Copying a 50GB folder from one Linux server to another using the standard cp or scp commands is a terrifying gamble. If your SSH connection drops at 49GB, the transfer fails completely, and you have to start over from zero. To transfer massive amounts of data safely and efficiently, seasoned system administrators abandon basic copy commands and use the legendary rsync.

The rsync (Remote Sync) command is an advanced file copying tool. Instead of blindly copying every single byte, it analyzes both the source and the destination. If a transfer fails, it simply picks up exactly where it left off. If you update a single text file inside a massive folder, it only transfers that tiny changed file, not the entire directory. In this guide, you will learn how to wield this essential backup tool.

Step 1: The Basic Local Copy

While rsync is famous for remote transfers, it is equally powerful for backing up files to a local external hard drive.

The basic syntax is always rsync [options] [source] [destination].

rsync -av /home/user/documents/ /mnt/backup_drive/

Let’s break down those crucial flags:

  • -a (Archive): This is the most important flag. It tells rsync to preserve exactly how the folder looks. It copies subdirectories recursively, and it preserves symlinks, file permissions, and ownership timestamps.
  • -v (Verbose): This forces the command to print out the name of every file it copies to your terminal, so you can visually confirm the backup is actually happening.

Crucial Note: Notice the trailing slash on /documents/. If you include the slash, it copies the contents of the folder. If you omit the slash (/documents), it copies the folder itself into the destination.

Step 2: Syncing to a Remote Server

Pushing your data to a remote backup server over the internet is where rsync truly shines. It uses the secure SSH protocol by default.

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

We added one new flag here:

  • -z (Zip/Compress): This tells rsync to compress the file data before sending it over the network, and decompress it on the other side. This drastically reduces bandwidth usage and speeds up the transfer of text-heavy files like logs and code.

When you run this command, it will prompt you for the root password of 192.168.1.50. Once entered, the transfer begins.

Step 3: The Danger of the Delete Flag

By default, rsync only adds files. If you delete a photo on your laptop, and then run an rsync backup to your server, that photo will still exist on the server. If you want the destination to perfectly mirror the source (including deletions), you must use the --delete flag.

rsync -av --delete /local/folder/ /remote/folder/

Warning: Use this flag with extreme caution. If you accidentally put an empty folder as the source, rsync will happily delete every single file in the destination to make them match.

By mastering the rsync command, you guarantee that your critical server backups are fast, bandwidth-efficient, and entirely resilient against network failures.

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.

Receive our best articles and tips delivered straight to your inbox.