Bash Backup Automation
Scheduled, rotated, alerting backups in pure Bash.
0 of 6 steps
What you will end up with
An idempotent backup script
Scheduled rotation
Failure alerting
Build steps
0/6A Bash script that tars a directory into a timestamped file and checks its own exit code before declaring success. Safe means it checks its own work before it takes a bow.
Use something like tar czf backup-$(date +%F).tar.gz /data so every file carries today’s date in its name. The date in the filename works like a milk carton’s expiry stamp, easy to sort by age.
Delete anything older than a set number of days, so the shelf never overflows. Rotation is simply one out for every one in.
Add a line to crontab -e, like 0 2 * * * /path/backup.sh, to run it at 2am daily. Cron is an alarm clock that runs a script instead of waking a person up.
Check the exit code after the backup runs and send a webhook or email if it is non-zero. It is a smoke alarm: silent when everything is fine, loud the moment something breaks.
Actually untar a backup into a scratch folder and confirm the files open correctly. A backup nobody has ever restored from is a smoke detector nobody has ever tested.
Before you start