So I realized today, reminded by this sad story, that it was time to test my backups again (if you're doing regular backups, which you should, then it's a good idea to test them every now and then. No point in having backups if they don't work). So my backups worked, but I noticed that a few things on my web server were not yet being backed up at all. No big deal, since the files in question weren't all that important, but still... So I updated my backup script and realized that, considering how old it is and how little I know about shell scripting, it's pretty darn convenient. So here it is:

Click anywhere in the code to remove line numbers.
 1  # Include config file
 2  source backup.conf
 3  
 4  # Handle errors
 5  function error {
 6  echo "Backup script was aborted prematurely due to a program error"|mail -s "Backup script error" $ERROR_EMAIL_ADDRESS
 7  exit 1
 8  }
 9  
10  trap error ERR
11  
12  # Delete the old backup copy
13  rm -rf ./snapshot/*-*-*/
14  
15  # Create a backup copy of the last snapshot
16  today=`date +%Y-%m-%d`
17  mv ./snapshot/current ./snapshot/$today
18  
19  # Create a new folder for the current snapshot
20  mkdir ./snapshot/current
21  
22  # Loop through all backup scripts
23  for file in ./tasks-enabled/*.sh; do
24  bash $file
25  done
26  
27  # Tar up the files in the current snapshot
28  tarfile=backup_$today.tar.bz2
29  cd ./snapshot/current/
30  tar -cjf ../$tarfile ./
31  cd ../../
32  
33  # Upload tar file to FTP
34  ncftpput -u $FTP_USER -p $FTP_PASSWORD $FTP_HOST / ./snapshot/$tarfile
35  
36  # Delete tar file
37  rm ./snapshot/$tarfile


Here's how it works: inside a folder named "tasks-enabled", there's a bunch of scripts that each back up one particular thing. Like for example there's a script that does a dump of all the MySQL databases. Similarly, there's a script that backs up the config files and so forth. Now the cool part is that if you need to back up something new, all you have to do is write a new script and put it in that folder. Actually, I have two folders ("tasks-available" and "tasks-enabled"), so that I can easily enable or disable tasks by creating or deleting links in the "tasks-enabled" folder (that's a common pattern, used for example by some versions of Apache). What's even better is that all the task scripts have to do is to copy their files to a folder called "snapshot/current". The main backup script then tars-up the folder and uploads it to my backup FTP server. It also keeps the last two backup "snapshots" around, so that you can easily restore something without having to go through the trouble of having to download it from the FTP site. For this script to work, you'll need the NcFTP Client. There's also a config file in which you have to set the following variables: FTP_HOST, FTP_USER, FTP_PASSWORD and ERROR_EMAIL_ADDRESS (an email is sent to that address in case something goes wrong). Now all you have to do is create a crontab entry for it, and you're good.

Let me know if you see any flaws in this.