Backing up your VPS is essential, but manual backups are unreliable — you might forget or skip them when busy. This guide shows you how to use rsync with cron to create fully automated daily backups that run without any intervention.
rsync (Remote Sync) is a command-line tool built into Linux for synchronizing files locally and over SSH. Its key advantages for backup include:
rsync is usually pre-installed. Check and install if needed:
# Check if rsync is installed
rsync --version
# Install on Ubuntu/Debian
sudo apt update && sudo apt install rsync -y
# Install on CentOS/AlmaLinux/Rocky Linux
sudo yum install rsync -y
Start with a local backup to understand rsync syntax before adding SSH complexity:
rsync [options] source/ destination/
| Option | Meaning |
|---|---|
-a | Archive mode — preserves permissions, owner, symlinks, timestamps |
-v | Verbose — shows file list being transferred |
-z | Compress data during transfer |
--delete | Delete files at destination not in source |
--exclude | Exclude files or directories matching pattern |
--dry-run | Test run without actual file transfer |
-P | Show progress bar |
# Backup /var/www to /backup/www
rsync -avz /var/www/ /backup/www/
# Backup excluding temp files and cache
rsync -avz --exclude='*.tmp' --exclude='cache/' /var/www/ /backup/www/
# Dry run to preview changes
rsync -avz --dry-run /var/www/ /backup/www/
/var/www/ (with slash) = copy contents of the directory./var/www (without slash) = copy the directory itself. Results differ significantly.
Backing up to a separate server is the most resilient approach — if your primary server fails, data survives elsewhere.
# rsync over SSH to remote server
rsync -avz -e ssh /var/www/ user@remote-server:/backup/www/
# Specify non-standard SSH port
rsync -avz -e "ssh -p 2222" /var/www/ user@remote-server:/backup/www/
# Use specific SSH key file
rsync -avz -e "ssh -i /root/.ssh/backup_key" /var/www/ user@remote-server:/backup/www/
Cron cannot interactively enter passwords — SSH key authentication is required.
# 1. Generate SSH key pair (no passphrase for cron use)
ssh-keygen -t ed25519 -f /root/.ssh/backup_key -N ""
# 2. Copy public key to remote server
ssh-copy-id -i /root/.ssh/backup_key.pub user@remote-server
# 3. Test passwordless connection
ssh -i /root/.ssh/backup_key user@remote-server "echo 'SSH key OK'"
#!/bin/bash
# /root/scripts/backup.sh
BACKUP_SOURCE="/var/www /etc /home"
BACKUP_DEST="backup-user@backup-server:/backups/$(hostname)"
SSH_KEY="/root/.ssh/backup_key"
LOG_FILE="/var/log/backup.log"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
log "=== Backup started ==="
ERRORS=0
for SRC in $BACKUP_SOURCE; do
log "Backing up $SRC..."
rsync -az \
--delete \
--exclude='*.tmp' \
--exclude='cache/' \
-e "ssh -i $SSH_KEY -o StrictHostKeyChecking=no" \
"$SRC/" \
"$BACKUP_DEST${SRC}/"
if [ $? -eq 0 ]; then
log "OK: $SRC"
else
log "ERROR: Failed $SRC"
ERRORS=$((ERRORS + 1))
fi
done
log "=== Backup finished (errors: $ERRORS) ==="
exit $ERRORS
chmod +x /root/scripts/backup.sh
# Open crontab
crontab -e
# Run backup daily at 2:00 AM
0 2 * * * /root/scripts/backup.sh >> /var/log/backup.log 2>&1
| Field | Value | Meaning |
|---|---|---|
| Minute | 0 | At minute 0 |
| Hour | 2 | At 2 AM |
| Day | * | Every day |
| Month | * | Every month |
| Weekday | * | Every weekday |
#!/bin/bash
DATE=$(date +%Y-%m-%d)
BACKUP_BASE="/backup"
BACKUP_DIR="$BACKUP_BASE/$DATE"
mkdir -p "$BACKUP_DIR"
rsync -az /var/www/ "$BACKUP_DIR/www/"
rsync -az /etc/ "$BACKUP_DIR/etc/"
# Delete backups older than 30 days
find "$BACKUP_BASE" -maxdepth 1 -type d -mtime +30 -exec rm -rf {} \;
# Check today's log
tail -50 /var/log/backup.log
# Find errors
grep "ERROR" /var/log/backup.log | tail -20
# Test restore
rsync -avz backup-user@backup-server:/backups/hostname/var/www/index.html /tmp/test-restore/
diff /var/www/index.html /tmp/test-restore/index.html
Automating VPS backups with rsync + cron is simple, reliable, and cost-effective. Summary:
Looking for reliable VPS hosting? AsiaGB VPS offers Thailand and Singapore locations with SSD storage, 99% uptime, and expert support.