rsync is an efficient file synchronisation tool that only transfers changed portions of files, making it ideal for incremental backups. Combined with cron for scheduling and SSH for remote transfers, rsync provides a robust, scriptable backup solution. This guide automates backups on Ubuntu 26.04 LTS.

Tested and valid on:

  • Ubuntu 26.04 LTS

Prerequisites

  • Ubuntu 26.04 LTS
  • rsync installed (sudo apt install rsync)
  • SSH key access to the backup destination (for remote backups)

Step 1 – Install rsync

sudo apt install rsync -y
rsync --version

Step 2 – Basic rsync Commands

# Local copy:
rsync -avz /var/www/mysite/ /backup/mysite/
# Remote backup over SSH:
rsync -avz -e ssh /var/www/mysite/ user@backup-server:/backup/mysite/
# Dry run (preview without changes):
rsync -avz --dry-run /var/www/ /backup/www/

Step 3 – Create an Incremental Backup Script

sudo nano /usr/local/bin/backup.sh

Add:

#!/bin/bash
DATE=$(date +%Y-%m-%d)
SRC='/var/www'
DST='/backup/www'
LOG='/var/log/backup.log'

mkdir -p "$DST/$DATE"
rsync -avz --delete 
    --link-dest="$DST/latest" 
    --exclude='*.tmp' 
    --exclude='node_modules' 
    "$SRC/" "$DST/$DATE/" >> "$LOG" 2>&1

rm -f "$DST/latest"
ln -s "$DST/$DATE" "$DST/latest"

# Remove backups older than 30 days
find "$DST" -maxdepth 1 -type d -mtime +30 -exec rm -rf {} + >> "$LOG" 2>&1
echo "Backup completed: $(date)" >> "$LOG"
sudo chmod +x /usr/local/bin/backup.sh

Step 4 – Test the Backup Script

sudo /usr/local/bin/backup.sh
ls /backup/www/

Step 5 – Schedule with cron

sudo crontab -e

Add (runs daily at 2 AM):

0 2 * * * /usr/local/bin/backup.sh

Step 6 – MySQL Database Backups

sudo nano /usr/local/bin/db-backup.sh

Add:

#!/bin/bash
DATE=$(date +%Y-%m-%d)
mkdir -p /backup/mysql/$DATE
mysqldump -u root --all-databases | gzip > /backup/mysql/$DATE/all-databases.sql.gz
sudo chmod +x /usr/local/bin/db-backup.sh

Step 7 – Verify Backup Integrity

rsync -avz --checksum /var/www/mysite/ /backup/verify/
ls -la /backup/www/latest/

Conclusion

Automated rsync backups are configured on Ubuntu 26.04 LTS. The incremental backup strategy using hard links is space-efficient while maintaining full daily snapshots. Test restores regularly to verify backup integrity.