rsync is a powerful command‑line tool for copying and synchronizing files locally or remotely. It only transfers changed parts of files, making backups fast and efficient.

This guide covers practical rsync commands for backing up your VPS.

Basic rsync Syntax

rsync [options] source destination

Common options:

  • -a : Archive mode (preserves permissions, timestamps, etc.)
  • -v : Verbose output
  • -z : Compress during transfer
  • --delete : Remove files in destination that don't exist in source
  • --exclude : Skip specific files or directories

Local Backups

Copy a directory to another location on the same server:

rsync -av /var/www/html/ /backups/html_backup/

Add --delete to mirror exactly (remove extra files):

rsync -av --delete /var/www/html/ /backups/html_backup/

Exclude certain files (e.g., cache):

rsync -av --exclude 'cache' --exclude '*.log' /var/www/html/ /backups/html_backup/

Remote Backups (Push)

Sync your local directory to a remote server:

rsync -avz -e ssh /var/www/html/ user@backupserver.com:/backups/html_backup/

The -e ssh option uses SSH for secure transfer.

Remote Backups (Pull)

Pull a remote directory to your local machine:

rsync -avz -e ssh user@remoteserver.com:/var/www/html/ /local/backups/

Incremental Snapshots with Date Folders

Create timestamped backup folders:

DATE=$(date +%Y%m%d_%H%M%S)
rsync -av /var/www/html/ /backups/snapshots/$DATE/

Delete old snapshots (e.g., keep last 7):

ls -t /backups/snapshots/ | tail -n +8 | xargs rm -rf

Use in a cron job.

Hard Link Snapshot Strategy

Create a full backup with hard links, then only new files take space:

rsync -av --link-dest=/backups/latest /var/www/html/ /backups/snapshots/$(date +%Y%m%d)
rm -f /backups/latest
ln -s /backups/snapshots/$(date +%Y%m%d) /backups/latest

This stores daily snapshots efficiently.

Bandwidth Limiting

Avoid saturating your uplink:

rsync -avz --bwlimit=1000 /var/www/html/ user@backupserver.com:/backups/

Limit to 1000 KB/s.

Testing with Dry Run

Before running, preview what would be transferred:

rsync -av --dry-run /var/www/html/ /backups/html_backup/

Full Backup Script Example

#!/bin/bash
BACKUP_DIR="/backups/website"
SOURCE="/var/www/html"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

mkdir -p $BACKUP_DIR
rsync -av --delete $SOURCE/ $BACKUP_DIR/current/
cp -al $BACKUP_DIR/current $BACKUP_DIR/snapshots/$TIMESTAMP
find $BACKUP_DIR/snapshots/ -maxdepth 1 -type d -mtime +30 -exec rm -rf {} \;

Save as /usr/local/bin/backup.sh, make executable, and set a cron job.

Troubleshooting

Permission denied: Ensure SSH keys are set up for remote backups. Use ssh-copy-id.

No space left: Check destination disk space. Use df -h.

Trailing slashes matter: rsync source/ destination/ copies contents. rsync source destination copies the folder itself.

Next Steps

rsync is one of the most reliable backup tools. Combine it with cron for automatic daily backups. Use remote servers or cloud storage (via mount) for off‑site safety.

Need a VPS to practice on? Check our recommended VPS providers.