Creating a Backup Script
Introduction
This document describes how you might create a simple backup script which gets run automatically via a cron job. Note that it keeps the backup on the same machine as the files that it is protecting, so it is no good against disk failures! Consider using rsync> to your home machine or see the Shack Wiki for more ideas.Creating the Script
None of this needs to be done by root (and is better not!) Start by creating a directory to hold the script and one to hold the backup files:
cd mkdir bin mkdir backups cd bin
Now start your favourite text editor (vi, emacs, etc, not a word-processor...) and paste in the following script:
#!/bin/sh # # Backup script # Saves a tar image of the Mail directory PATH=/usr/bin:/usr/ucb:/bin export PATH cd BACKUP=backups/backup.`date +%Y%m%d-%H%M` tar -c -f "${BACKUP}" Mail # Optional bit to expire backup files over 20 days old # find backups -type f -mtime +20 -exec rm '{}' ';'
Save it as 'do-backup' and exit from the editor. Now make the script executable:
chmod +x do-backup
Check that the script is syntactically correct:
sh -n do-backup
Now try running it:
./do-backup
If that works you should get a file created in the ~/backups directory.
Automating the Backup
To automate the process you need to get cron to run the script each night:
crontab -e
This will start an editor. Paste in this line:
5 3 * * * bin/do-backup
Be careful not to leave any blank lines in the file. Write the file back and exit from the editor. Cron will now run your script every night at 03:05. Wait a day, and check that another backup file has appeared.
Remember that the backups directory will grow forever unless you uncomment the cleanup line in the script.