This post is part of a series on automatically backing up your website:
- Part 1: Getting Started with Amazon S3 and S3Fox
- Part 2: Using Amazon S3 from the Command Line with s3sync
- Part 3: Backing Up Your Data with Astrails-Safe
- Part 4: Automating Your Backup with Cron
What Is Cron?
cron is a daemon that will execute commands at a scheduled date and time. You use the
crontab command (short for cron table) to set up
cron to execute commands at regular intervals.
If you haven’t used
cron before, I recommend reading this excellent introduction to cron and crontab and looking at this example crontab file. You should have at least a basic understanding of
crontab before proceeding.
Now that you know how to backup your web site, let’s schedule automatic backups using crontab. Open up your crontab file (note: don’t edit your crontab file directly):
sudo crontab -e
Add a line that will trigger astrails-safe to backup your files. Here’s how I have techiferous.com’s backup set up:
15 04 * * 1,4 /usr/local/bin/astrails-safe /etc/astrails-safe/techiferous.rb > /var/log/cron/techiferous-`date "+\%Y\%m\%d_\%H\%M"`.log 2>&1
Let me break that down for you:
||This tells cron to run the command every Monday and Thursday at 4:15am.|
||This is the command that backs up your web site. Notice that I preceded
||This instructs stdout to be saved to a log file. That helps us debug
||This Unix idiom tells stderr to go to the same place as stdout. Otherwise, errors would not appear in the log file.|
If you set up your crontab similarly, make sure the
/var/log/cron directory exists or your backup will not run.
That’s it! Now you can sit back and relax while your web server backs up your website for you automatically.