I recently needed to add some better backup systems to my current pet project.
There are 3 pretty important things to consider backing up in the case of this system:
- The code
- The database data
- Some flat file uploads from users
I’ve arranged my code so there’s a single JSON file to control its configuration between environments. Beyond that there’s the whole code base, of course this is under version control and is available on my local machine and I have an extra backup process going on for my local machine’s copy. So the code is pretty safely backed up at the moment.
The main things I considered today were the second two matters. Backing up the database was pretty trivial in Cron, especially since the database data is incredibly small, most users will probably consume less than a megabyte but nonetheless that data is significant (auth details, relationships etc):
0 4 * * * mysqldump -u username -pdbpasswd dbname > /directory/to/store/databasebackup
Backing up the third was trickier, I really wanted a rolling window of backups for this data. By rolling window I mean that backups will be made at a regular interval but only the last X backups will be kept. In the example below I’m rolling the last 5 days of backups.
A quick search across StackOverflow led me to these two commands:
# Backup all data 0 4 * * * tar -zcf /directory/to/store/backups/data`date +\%F`.tar.gz /directory/to/backup # Remove all backups beyond 5 days 0 5 * * * cd /directory/to/store/backups/ && (ls -t|head -n 5;ls)|sort|uniq -u|xargs rm
The backslash in the Backup all data command is significant as Cron will interpret the % differently otherwise. And by differently I mean in a way you really don’t want
I’d like to see other people’s solutions, perhaps a more scattered backup schedule? Five backups for the past five days, one backup every week for four weeks? one backup every month for the past six months? These could be achieved easily by adapting the above with separate directories or perhaps prefixing the filenames.

Take a look at http://members.multimania.co.uk/wipe_out/automysqlbackup/ for a script that does daily, weekly, and monthly MySQL backups and rotates the files.