How to Use rsync with SSH Keys and Partial Resumes in Linux Terminal

Transferring multi-gigabyte virtual machine images, database dumps, or website trees across remote servers using basic tools like scp or sftp presents significant hazards: network disconnections cancel entire transfers without resume capabilities, and repeated transfers re-copy identical unchanged data. rsync (Remote Sync) solves file synchronization in Linux by using a rolling-checksum delta transfer algorithm. It computes differences between source and destination files, transmitting only modified data blocks while supporting automated SSH key authentication and partial transfer resumes.

Essential Flags for Production Synchronization

The standard best-practice flag combination for robust system mirroring is -avzP:

rsync -avzP /var/www/html/ [email protected]:/var/www/html/
  • -a (Archive): Preserves recursion, symbolic links, file permissions, modification timestamps, group ownership, and owner IDs.
  • -v (Verbose): Displays detailed file transfer progress in the console.
  • -z (Compress): Compresses file data during network transit, saving bandwidth on slower uplinks.
  • -P (Progress & Partial): Combines --progress (live transfer speed and time remaining) with --partial (keeps partially transferred files if the connection drops).

The Crucial Trailing Slash Difference in Source Paths

A classic pitfall in rsync involves whether a trailing slash is appended to the source directory:

  • With Trailing Slash (/src/dir/): Copies the contents of dir into the destination directory without creating an extra dir container.
  • Without Trailing Slash (/src/dir): Copies the directory itself into the destination, creating destination/dir.

Enforcing SSH Key Authentication and Custom Ports

By default, rsync uses SSH as its remote shell transport. To specify a non-standard SSH port or dedicated private key:

# Use a custom SSH port (e.g., port 2222) and specific private key
rsync -avzP -e "ssh -p 2222 -i ~/.ssh/backup_deploy_key" /data/ [email protected]:/backup/

Resuming Interrupted Multi-Gigabyte Transfers Seamlessly

When transferring 50 GB database dumps across unstable WAN connections, network drops can sever active sessions. Use the --partial and --append-verify flags to avoid re-transferring previously received data:

rsync -avzP --append-verify -e ssh large_archive.tar.gz user@remote:/storage/

If disconnected, re-executing the identical command verifies the destination file’s checksum and resumes uploading exactly at the point of failure.

Safe Deletions and Testing with Dry-Run

To mirror destination folders exactly—deleting files at the destination that no longer exist on the source—add --delete. Always test with --dry-run first to prevent catastrophic data loss:

# Preview planned sync and deletion actions without modifying files
rsync -avzP --delete --dry-run /source/dir/ user@remote:/dest/dir/

# Execute true deletion sync once validated
rsync -avzP --delete /source/dir/ user@remote:/dest/dir/

Get the best tech tips delivered straight to your inbox.

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