Why Automate Backups with Rsync and Cron? – Automate Linux Backups with Rsync and
Every sysadmin learns the value of backups the hard way — usually after the first data loss. Rsync is the standard Linux tool for efficient file synchronization: it transfers only changed blocks, preserves permissions and timestamps, and works over SSH. Combined with cron, you can build a reliable automatic backup system on Linux with no paid software. This guide walks you through creating versioned daily backups that run while you sleep — plus retention strategies and restore testing, the two pieces most tutorials skip.
Prerequisites – Automate Linux Backups with Rsync and
- A Linux server (any modern distro) with
rsyncinstalled (sudo apt install rsync/sudo dnf install rsync) - A backup destination: a second disk, a mounted NAS, or a remote server reachable over SSH
- Root or
sudoaccess (needed to preserve ownership of system files)

Step 1 – Understand the Core Rsync Command
The workhorse command for a full mirrored backup. Run it manually once first to confirm source paths, destination permissions, exclude rules, and available disk space, and network bandwidth for remote targets before automating anything:
rsync -avz --delete /var/www/ /backup/www/
Flag breakdown:
-a(archive): preserves permissions, ownership, timestamps, symlinks-v(verbose): shows progress in logs-z(compress): compresses data in transit — useful over networks, skip for local disks--delete: removes files from the backup that no longer exist in the source (true mirror)
Trailing slash matters: /var/www/ copies the contents; /var/www copies the directory itself.
Step 2 – Create a Backup Script with Date-Stamped Snapshots
A plain mirror gives you only the latest state. For history, use --link-dest to create space-efficient daily snapshots (unchanged files are hard-linked, so each snapshot costs almost no extra disk):
sudo nano /usr/local/bin/backup.sh
#!/bin/bash
# Automatic daily backup with rsync + hard-link snapshots
SRC="/var/www/"
DST="/backup"
DATE=$(date +%F)
LATEST="$DST/latest"
mkdir -p "$DST/$DATE"
rsync -av --delete \
--link-dest="$LATEST" \
--exclude='cache/*' \
--exclude='*.tmp' \
"$SRC" "$DST/$DATE/" >> /var/log/backup.log 2>&1
# Update the 'latest' symlink for tomorrow's --link-dest
rm -f "$LATEST"
ln -s "$DST/$DATE" "$LATEST"
# Keep only the last 14 snapshots
ls -1d "$DST"/20* | sort | head -n -14 | xargs -r rm -rf
echo "[$(date)] Backup completed: $DATE" >> /var/log/backup.log
sudo chmod +x /usr/local/bin/backup.sh
This pairs well with database dumps — see our guide on importing and exporting MySQL/MariaDB databases to add a mysqldump step before the rsync run.
Step 3 – Back Up to a Remote Server over SSH
For off-site safety, push backups to a remote host. Set up passwordless SSH keys first, then use rsync over SSH:
rsync -avz --delete -e "ssh -p 22 -i /root/.ssh/backup_key" \
/var/www/ [email protected]:/backup/www/
In the script, replace the local $DST paths with the remote target. Using a dedicated SSH key restricted with command="rrsync" in authorized_keys limits what the backup key can do if compromised.
Step 4 – Schedule with Cron
Run the backup every night at 2:00 AM, when traffic is lowest:
sudo crontab -e
# Daily backup at 02:00, log output
0 2 * * * /usr/local/bin/backup.sh
Verify the cron job is registered:
sudo crontab -l
sudo grep CRON /var/log/syslog | tail -n 5
For long-running transfers you can launch once manually under nohup (see nohup command in Linux) to survive SSH disconnects.
Step 5 – Test Your Restore Procedure
A backup you have never restored is not a backup. Test monthly:
# Dry-run: show what WOULD be restored, change nothing
rsync -avn /backup/latest/ /var/www/
# Restore a single file
rsync -av /backup/2026-10-18/wp-config.php /var/www/wp-config.php
# Full restore (stop services first!)
sudo systemctl stop nginx php8.1-fpm
rsync -av --delete /backup/latest/ /var/www/
sudo systemctl start nginx php8.1-fpm
Troubleshooting
Permission denied / failed to set permissions
Run rsync as root on both ends, or use --no-owner --no-group --no-perms when backing up as an unprivileged user. For remote roots, ensure PermitRootLogin prohibit-password with key auth in sshd_config.
Disk fills up despite –link-dest
Hard links only deduplicate unchanged files. Large database files or VM images that change daily will consume a full copy per snapshot — exclude them or dump databases to SQL text instead (which deduplicates far better).
Cron runs but nothing happens
Cron uses a minimal environment: always use absolute paths in scripts, and redirect output to a log file. Check /var/log/syslog (or journalctl -u cron) for errors, and test the script manually first as the same user cron runs it as.
Fine-Tuning Excludes for Real-World Backups
Blindly copying everything wastes space and time. A production exclude list typically skips caches, logs, and transient data:
rsync -av --delete \
--exclude='/proc/*' \
--exclude='/sys/*' \
--exclude='/dev/*' \
--exclude='/tmp/*' \
--exclude='/var/cache/*' \
--exclude='/var/log/*.gz' \
--exclude='*.sock' \
/ /backup/system/
For full-system backups, also consider --one-file-system to avoid descending into other mounts (/proc, /sys, network shares). Store the exclude list in a file for readability:
printf '%s\n' '/proc/*' '/sys/*' '/tmp/*' '*.sock' | sudo tee /etc/rsync-excludes.txt
rsync -av --delete --exclude-from=/etc/rsync-excludes.txt / /backup/system/
Review excludes quarterly — application upgrades often introduce new cache directories worth skipping.
Backup Retention Strategy: GFS in Practice
Fourteen daily snapshots are a good start, but consider the Grandfather-Father-Son (GFS) rotation for longer history without ballooning disk usage: keep 7 daily, 4 weekly, and 6 monthly snapshots. With --link-dest hard links, monthly snapshots of slowly-changing data cost almost nothing extra.
Implement it with three cron entries calling the same script with different retention arguments:
# Daily at 02:00, keep 7
0 2 * * * /usr/local/bin/backup.sh daily 7
# Weekly on Sunday at 03:00, keep 4
0 3 * * 0 /usr/local/bin/backup.sh weekly 4
# Monthly on the 1st at 04:00, keep 6
0 4 1 * * /usr/local/bin/backup.sh monthly 6
Adapt the script to accept $1 (label) and $2 (retention count) so each tier manages its own directory tree. This gives you a full year of restore points for roughly the cost of a few full copies.
Conclusion
You now have a complete automatic backup solution for Linux: an rsync snapshot script with 14-day retention, remote SSH targets, nightly cron scheduling, and a tested restore workflow. This setup costs nothing, runs unattended every night, and has saved countless servers from disaster over the years. For extra resilience, always keep at least one copy off-site and periodically verify restores — the official rsync documentation covers advanced filters and daemon mode when your growing infrastructure outgrows this straightforward setup.