Schedule WordPress Backups Using Cron

I was contacted a couple of days back by a reader who wanted to add my super simple WordPress backup program to Cron, the Unix task scheduler, to create automated regular snapshots of his site. Backing up my website using the program is something I normally do by hand as part of my general system maintenance routine, but the program is easily incorporated in to Cron to run on a timer. Completely by coincidence, I suffered a WordPress plugin failure within a couple of days of creating the Cron job, and thanks to my now daily backups I was able to restore from a 10 hour old snapshot with the minimum of fuss.

Here’s how to run a daily WordPress backup on your server using Cron:

1. Firstly, find out if you have Cron running. To do so we use the ‘ps’ command piped into ‘grep’:

$ ps aux | grep cron

The results should look something like the following. The top result is the grep process with ‘cron’ as its argument, while the second is the cron process itself:

1003      6714  0.0  0.0   9140  1048 pts/1    S+   11:09   0:00 grep --color=auto cron
root    15538  0.0  0.0  18932  1024 ?        Ss   Jul21   0:00 cron

2. If cron is not running, you can start it using the following command:

$ /etc/init.d/cron start

3. Before we go any further with Cron, download the WordPress backup program either by right clicking the download link and ‘saving as’, or using the command line:

$ wget

Remember to make the file executable once it has downloaded:

$ chmod +x wordpressbackup

Move the executable file to the folder in which you want to create your backups using the ‘mv’ command.

4. It’s important to have a basic grasp on the anatomy of ‘Cron’ before proceeding. Cron is controlled by the ‘crontab’ file – a table containing the ‘cron jobs’ it is currently tasked with. To add a job to the list, you need to open the ‘crontab’ file and describe the task at hand. Just like reloading an Apache site descriptor, you must then instruct Cron to reload the edited ‘crontab’.

Here’s a typical crontab example:

# m h dom mon dow user command
17 * * * * root    cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

Let’s examine the Cron syntax.

The first five columns are the timing controls. In order, they represent: minute, hour, day of month, month and day of week. Using these columns, you can describe precise event frequencies – if you want to perform your backup at 8:32am on the 5th of every month, that’s just fine. The wildcard (*) character means ‘every’, so for the first entry in the example above the command will be executed at 17 minutes past every hour on every day of the month in every month on every day of the week.

Next, the user performing the command, ‘root’ in the above examples. This can be any of the users of the system – in our Cron job, this needs to be the owner of the WordPress installation, for example.

Finally, the command itself. Cron commands have the same syntax as any command line instructions, and can be run in several stages. If you wish to perform a command from a particular folder, the command will start with a ‘cd xyz123’ to navigate to the folder ‘xyz123’ before the command itself is issued (after an ‘&&’ option, which separates sections of the command).

That’s as much Cron syntax as we need to know for setting up our WordPress backup, so let’s put together the command.

5. With root privileges, we need to edit the ‘crontab’ file. It is located at ‘/etc/crontab’ (use ‘$ locate crontab‘  if you can’t find it). Enter the following command to load the crontab into GNU nano:

$ sudo nano /etc/crontab

6. In your text editor, create a new line underneath the bottom job in the crontab. We want to set the command to execute at 23:55 on every single day of the year. For this example cron job, the WordPress install is located at ‘/home/randymarsh/website/’ and the backup program is located at ‘/home/randymarsh/wordpressbackup’. The first part of the command will be navigating to the folder containing the executable, and then proceeding to perform the backup itself.

Make sure you know which user owns the folder you wish to create the backup in. In this example, ‘randymarsh’ owns the folder so he’ll be performing the backup.

55 23 * * *   randymarsh  cd /home/randymarsh/ && ./wordpressbackup website/

7. Save the crontab file and return to the command line. To complete the schedule, reload the cron configuration file:

$ /etc/init.d/cron reload

And we’re done! Make sure you check that you haven’t made any errors by seeing if the backup has been performed at the time you scheduled it for. Cron is a really powerful part of Unix and can be used for all sorts of things – automated backups are one of the most useful tasks it can take care of for you, but there’s tons of other maintenance which can be similarly automated.

This tutorial is written for Ubuntu Linux 11.04, however it should be very similar (if not identical) to many other Unix-type operating systems as well.

Leave a Reply