How to Use the Linux rsync Command for Secure, Resumable Remote Backups

The Danger of Standard File Transfers

In Linux administration, transferring files between servers is a daily requirement. If a web developer needs to push 50GB of new image assets from a staging server to a production cluster, the common instinct is to use the scp (Secure Copy) command or an SFTP client.

While scp is secure, it is incredibly unintelligent. If the network connection drops after 49GB have been transferred, scp fails. When you restart the command, scp blindly starts transferring all 50GB from the very beginning, wasting hours of time and massive amounts of bandwidth. Furthermore, scp cannot synchronize directories; it simply overwrites files.

The definitive solution for robust file transfer in Linux is rsync (Remote Sync). rsync utilizes an advanced delta-transfer algorithm. It compares the files on the source server with the files on the destination server, identifies exactly which bytes have changed, and transmits only those specific changes over a secure SSH tunnel. If a transfer is interrupted, rsync resumes instantly from the exact point of failure.

Step 1: The Basic Syntax and Archive Mode

The structure of an rsync command mimics scp: you define the source, followed by the destination.

rsync [options] /path/to/source username@remote_host:/path/to/destination

The most critical option to memorize is -a (Archive mode). This is a meta-flag that combines several essential behaviors into one: it enables recursive copying (directories), preserves symbolic links, preserves file permissions, preserves ownership (user and group), and preserves precise modification timestamps. Without the -a flag, a backup is useless because it destroys the original file metadata.

To transfer a local directory to a remote backup server while seeing a progress bar (-v for verbose, -P for progress and partial resumption):

rsync -avP /var/www/html/ [email protected]:/backup/web_assets/

Step 2: The Trailing Slash Anomaly (Crucial)

The most common mistake administrators make with rsync is misunderstanding the trailing slash on the source directory.

  • rsync -a /var/www/html ... (No trailing slash): This tells rsync to copy the directory itself. It will create a folder named html inside the destination.
  • rsync -a /var/www/html/ ... (With trailing slash): This tells rsync to copy the contents of the directory. It will dump all the files inside html directly into the destination path, without creating a parent folder.

A single misplaced slash can completely destroy a complex directory structure on the destination server.

Step 3: Secure Transfer over Non-Standard SSH Ports

By default, rsync establishes a secure tunnel using the standard SSH protocol on port 22. However, secure enterprise environments almost always change the default SSH port to thwart automated brute-force attacks (e.g., port 2222).

To instruct rsync to use a custom SSH port (and to explicitly define the SSH key for passwordless automation), use the -e (execute) flag:

rsync -avP -e "ssh -p 2222 -i /root/.ssh/backup_key" /var/www/html/ [email protected]:/backup/web_assets/

Step 4: Ensuring True Synchronization (The –delete Flag)

By default, rsync is additive. It will copy new files and update changed files, but it will never delete files on the destination.

Suppose a developer deletes 100 obsolete images from the staging server. If you run a standard rsync to push the changes to production, those 100 images will remain on the production server forever, eventually filling up the hard drive.

To force the destination server to be an exact, perfect mirror of the source server, you must append the --delete flag. This instructs rsync to ruthlessly delete any file on the destination that no longer exists on the source.

rsync -avP --delete /var/www/html/ [email protected]:/var/www/html/

Warning: The --delete flag is incredibly dangerous. If you accidentally type the source path incorrectly (e.g., point it at an empty directory), rsync will instantly delete everything on your production server. Always test with the --dry-run flag first.

Step 5: Safely Testing with Dry Runs

Before executing a massive, destructive synchronization (especially one utilizing --delete), you must verify what rsync is about to do.

Append the --dry-run (or -n) flag to the command:

rsync -avP --delete --dry-run /var/www/html/ [email protected]:/var/www/html/

rsync will perform the entire mathematical delta-comparison algorithm and print out an exact list of every file it intends to transfer, update, or delete, without actually transmitting a single byte of data or altering any files. Once you verify the list is correct, simply remove the --dry-run flag and execute the real command.

Conclusion

The rsync command is the absolute standard for data synchronization in Linux environments. By utilizing its delta-transfer algorithm, SSH tunneling, and precise mirror synchronization logic, administrators can build robust, highly efficient, and fully resumable disaster recovery pipelines that can safely transfer terabytes of data across the internet.

RELATED POSTS

  • How to Verify File Integrity Using the md5sum Command in Linux
  • How to Use the iperf3 Command to Measure Network Bandwidth in Linux
  • How to View the Contents of a Compressed Archive Using the zcat Command in Linux
  • How to Add Line Numbers to Text Files in Linux Using the nl Command
  • How to Use the find Command to Locate Files Modified in the Last 24 Hours in Linux
  • Get the best tech tips delivered straight to your inbox.

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