Note: this post builds on Part 1, so read it first.
In my previous post I explained how to download the backup xml from the command line without a password prompt. An automated backup system should run of course automatically, so we need to automate it. It can be done with a cron job.
Cron is the name of program that enables unix users to execute commands or scripts (groups of commands) automatically at a specified time/date.
For a more comprehensive(but easy to understand) cron tutorial go to http://ubuntuforums.org/showthread.php?t=102625.
Here is how to do it:
First make the perl file executable. Without that cron won’t run the script.
chmod u+x wordpress_backup.perl
Cron is by default installed on most linux system, including ubuntu.
There is a special format for entering cronjobs:
Minute Hour Day Month Day Task
Minute = Minute of the hour, 00 to 59. * Will indicate every minute
Hour = Hour of the day in 24-hour format, 00 to 23. * Will indicate every hour
Day = Day of the month, 1 to 31. * Will indicate every day
Month = Month of the year, 1 to 12. * Will indicate every month
Day = Day of the week, 3 chars – sun, mon, tue, … * Will indicate every day
Task = The command you want to execute
Note: each of the above must be separated by at least 1 space.
Run the following command (it opens an editor usually nano or pico):
crontab -e
Copy&paste the following two lines(change the time, email and path):
MAILTO=YourEmail@example.com 03 20 * * * /path/to/script/wordpress_backup.perl
For nano editor: CTRL+O to save the file and CTRL+X to exit the file.
The above example will run /path/to/script/wordpress_backup.perl at 8:03 pm everyday.
To test the setup use current time +2 minutes and wait for the backup file to
appear in the $path_to_file directory. If it is there it works! You can also double check the file by manually downloading it and comparing the two files.
Note: always use absolute pathnames (such as “/files/home/user/filename”) to specify the script in crontab. This is also true for any local scripts you use within other scripts and for any system commands you use within the script you are running.
Some more examples:
How do I run a task at 6PM every night?
00 18 * * * /command/to/execute
How do I run a perl script at 2am every Sunday?
00 02 * * sun /home/user/script.perl
This solution isn’t perfect. It will run the backup script at given hour only when the pc is on at that time. It also backups the wordpress xml file(posts, comments, …) only.
In Part 3 I’ll show how to backup images and (maybe) how to run it on windows.
