Archive · Linux

Backups with rsync and tar

A simple, restorable backup routine, and why a backup you have not restored is not yet a backup.

1 min read

  • Ubuntu 18.04.3 LTS
  • rsync 3.1
  • GNU tar 1.29
# mirror a directory to another disk or host, preserving permissions
rsync -aHAX --delete /srv/app/ backup@10.0.0.5:/backups/app/

# a dated, compressed archive
tar -czf /backups/etc-$(date +%F).tar.gz /etc

# list or restore
tar -tzf /backups/etc-2019-12-16.tar.gz | head
tar -xzf /backups/etc-2019-12-16.tar.gz -C /tmp/restore etc/ssh/sshd_config

The trailing slash in rsync matters: /srv/app/ copies the contents, /srv/app copies the folder itself. --delete makes the target an exact mirror, so test with --dry-run first.

Keep the 3-2-1 rule: three copies, on two kinds of storage, one off-site. Schedule the job with cron, log the result, and practise a restore every so often. A backup that has never been restored is only a hope.