Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Rsync Backup Guide: Sync Files Between Servers

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Rsync is a powerful command-line tool that synchronizes files and directories between two servers over SSH. It copies only the changes, saving bandwidth and time, making it ideal for backups and mirroring. This guide walks you through setting up and using rsync for reliable server-to-server file transfers.

What You'll Need

  • Two servers (source and destination) with SSH access.
  • Rsync installed on both (usually pre-installed on Linux; on macOS it's included, on Windows you may need WSL or a tool like cwRsync).
  • SSH keys set up for passwordless login (recommended for automation).
  • Basic command-line familiarity.

Step-by-Step

  1. Verify rsync is installed on both servers. Run rsync --version. If missing, install it via your package manager (e.g., sudo apt install rsync on Debian/Ubuntu, sudo yum install rsync on RHEL/CentOS).
  2. Set up SSH keys for passwordless authentication. On the source server, generate a key pair if you don't have one: ssh-keygen -t rsa -b 4096. Then copy the public key to the destination server: ssh-copy-id user@destination-server. Test with ssh user@destination-server.
  3. Run a basic sync command from the source server. The syntax is: rsync -avz /path/to/source/ user@destination-server:/path/to/destination/. The -a (archive) preserves permissions, timestamps, and symlinks; -v is verbose; -z compresses data during transfer. Note the trailing slash on the source: it copies the contents of the directory, not the directory itself.
  4. Test with a dry run first: add --dry-run to see what would be transferred without actually doing it. This is crucial for verifying paths and avoiding accidental deletions.
  5. Enable deletion to make the destination an exact mirror. Add --delete to remove files on the destination that no longer exist on the source. Be careful: this can delete data if the source is incomplete. Always dry-run first.
  6. Use incremental backups with hard links. To keep multiple versions without duplicating unchanged files, use --link-dest pointing to a previous backup. For example: rsync -av --link-dest=/backups/backup1 /data/ /backups/backup2/. This creates backup2 as a full copy but only stores changed files; unchanged files are hard-linked to backup1.
  7. Exclude unnecessary files with --exclude. For instance, --exclude='*.tmp' or --exclude='cache/'. You can also use --exclude-from=file to list patterns in a file.
  8. Automate with cron. Edit your crontab (crontab -e) and add a line like 0 2 * * * rsync -avz --delete /data/ user@dest:/backups/current/ to run daily at 2 AM. Redirect output to a log file: >> /var/log/rsync.log 2>&1.

Common Mistakes

  • Forgetting the trailing slash on the source path. /data copies the directory itself, creating /dest/data; /data/ copies its contents, creating /dest/contents. This is the #1 cause of unexpected directory nesting.
  • Using --delete without a dry run. A typo in the source path can wipe out the destination. Always test first.
  • Not preserving permissions or ownership. If you omit -a, files may lose their original permissions, causing issues for services.
  • Ignoring SSH key permissions. If your private key has too-open permissions (e.g., 644), SSH will refuse to use it. Fix with chmod 600 ~/.ssh/id_rsa.
  • Running rsync as root unnecessarily. This can change ownership of files on the destination, causing problems. Use a regular user with appropriate permissions.

Tips

  • Use --progress to see transfer progress for large files.
  • For large transfers over slow connections, consider --bwlimit=RATE (e.g., --bwlimit=1000 for 1000 KB/s) to avoid saturating bandwidth.
  • Use --partial to keep partially transferred files if the connection drops, so you don't restart from scratch.
  • For very large directories, use --info=progress2 to see overall progress.
  • Test your backup restoration regularly. A backup you can't restore is worthless.

Troubleshooting

  • Connection refused or timeout: Check that SSH is running on the destination (sudo systemctl status sshd or similar). Ensure the firewall allows port 22 (or your custom SSH port). Test with ssh user@dest.
  • Permission denied (publickey): Your SSH key isn't authorized. Re-run ssh-copy-id, or manually append your public key to ~/.ssh/authorized_keys on the destination. Ensure the .ssh directory has 700 permissions and authorized_keys has 600.
  • Rsync not found on remote: The destination server may not have rsync installed. Install it there, or use --rsync-path="sudo rsync" if you need elevated privileges (but that's a security risk).
  • Files not updating: Check if you're using --checksum (which compares checksums instead of size/time) – it's slower but catches changes that preserve size and timestamp. Also ensure you're not accidentally excluding the files.
  • Broken pipe or interrupted transfer: Re-run the command; rsync resumes where it left off only if you used --partial. Without it, it restarts the file.

Frequently Asked Questions

Can rsync sync files in real-time?

No, rsync is not a real-time sync tool. It runs when you invoke it (or via cron). For real-time syncing, use tools like inotifywait (to trigger rsync on file changes) or dedicated sync solutions like Syncthing.

Does rsync compress data by default?

No, compression is off by default. Use the -z flag to enable it, which is beneficial for text files but can slow down transfers of already-compressed files (like .jpg or .zip).

How do I sync only new files, not deleted ones?

Omit the --delete flag. Rsync will copy new and changed files but will not remove files that exist on the destination but not the source. This is a safe way to accumulate backups without losing old files.

Can I use rsync over a non-standard SSH port?

Yes, use the -e option to specify the SSH command with the port: rsync -avz -e "ssh -p 2222" /data/ user@dest:/backup/.

What's the difference between -a and -r?

-r (recursive) only copies directories recursively. -a (archive) implies -r and also preserves symlinks, permissions, timestamps, and other attributes. Always use -a unless you have a specific reason not to.

How do I backup a remote server to my local machine?

Run rsync from your local machine, with the remote as the source: rsync -avz user@remote:/data/ /local/backup/. You'll need SSH access from your local to the remote.