BackupsRsyncAutomation8 min read

Configure automatic backups with rsync

Automate incremental backups with rsync and cron to protect your data on a remote server.


rsync performs efficient incremental copies: it only transfers files that changed. Combined with cron, you get automatic backups without intervention.

Step 1 — Install rsync

bash
sudo apt update
sudo apt install rsync -y

Step 2 — Configure passwordless SSH keys

For unattended backup execution:

bash
ssh-keygen -t ed25519 -f ~/.ssh/backup_key -N ""
ssh-copy-id -i ~/.ssh/backup_key.pub user@BACKUP_SERVER

Step 3 — Create the backup script

Create /usr/local/bin/backup.sh:

bash
#!/bin/bash
DATE=$(date +%Y-%m-%d_%H%M)
SOURCE="/var/www /home /etc"
DEST="user@BACKUP_SERVER:/backups/my-vps/"
LOG="/var/log/backup-$DATE.log"

echo "Backup started: $DATE" > $LOG

for dir in $SOURCE; do
  rsync -avz --delete \
    -e "ssh -i /root/.ssh/backup_key" \
    $dir $DEST \
    >> $LOG 2>&1
done

echo "Backup finished: $(date)" >> $LOG

Set permissions:

bash
sudo chmod +x /usr/local/bin/backup.sh

Step 4 — Schedule with cron

bash
sudo crontab -e

Add (daily backup at 3 AM):

terminal
0 3 * * * /usr/local/bin/backup.sh

Step 5 — Backup with date retention

To keep historical backups:

bash
#!/bin/bash
DATE=$(date +%Y-%m-%d)
DEST="user@BACKUP_SERVER:/backups/my-vps/$DATE/"

rsync -avz --delete \
  -e "ssh -i /root/.ssh/backup_key" \
  /var/www/ $DEST

Step 6 — Verify backups

bash
# Dry-run simulation (no actual transfer)
rsync -avzn --delete /var/www/ user@BACKUP_SERVER:/backups/test/

# View latest log
ls -lt /var/log/backup-*.log | head -1 | xargs cat

Useful rsync options

bash
--exclude='*.log'        # Exclude files
--exclude='node_modules' # Exclude directories
--bwlimit=5000           # Limit bandwidth (KB/s)
--compress               # Compress during transfer

With this setup, your Baires Host VPS maintains automatic secure backups on an external server.


Was this guide helpful?