Automated WordPress.com Backup – Part 2

•November 12, 2008 • Leave a Comment

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.

Automated WordPress.com Backup – Part 1

•November 8, 2008 • 5 Comments

Updated on 12 November 2008: added Part2(how to setup cronjob).

I was going to write my first post on another topic, but then I realized that I’m not able to backup this blog automatically. So I wrote a simple perl script. All it does is login, download the xml backup file and log off.

Although I created it specially for wordpress.com blogs it works with standard wordpress installations too.

This script has been tested on debian. It should work on any UNIX-like OS with perl. I think, it would also run on windows, with little or no modification.

All you need to do is install the WWW::Mechanize Perl module.

To install it under debian(ubuntu) do:

 # apt-get install libwww-mechanize-perl 

or
from CPAN:

 #perl -MCPAN -e 'install WWW::Mechanize' 

Don’t forget to change the settings!

Feel free to modify it to your needs. Any suggestions for improvement are welcome.


#!/usr/bin/perl -w
use WWW::Mechanize;
#this file is named wordpress_backup.perl

#Warning:
#1. If a file with a given name exists it'll be overwritten.
#2. Don't forget to escape special characters like @ in the password
#   variable.  Example:   q6@7mb   =>  q6\@7mb

##############  Settings  #####################
my $username="YourUsername";
my $password ="YourPassword";
my $path_to_file="/path/to/file/";
my $url="https://$username.wordpress.com/";
my $author="all";
#Filename format: fileprefix.date.xml
my $fileprefix="wordpress";
###############################################

#Change that if you want
my $agent="unixwayoflife/1.0";

my $date=((localtime)[5] +1900)."-".((localtime)[4] +1)."-".(localtime)[3];
my $mech = WWW::Mechanize->new( agent => $agent );
$mech->get( $url."wp-login.php" );

##Log in
$mech->submit_form(
        form_name => 'loginform',
        fields    => { log  => $username, pwd => $password },
        button    => 'wp-submit'
   );
die "ERROR: Incorrect password. Aborted." if $mech->content =~ m/Incorrect password/;
print ("Login in \t\t\t[OK]\n");

#optional
sleep(2);

##Download the file
$mech->get($url."wp-admin/export.php?author=$author&submit=Download+Export+File&download=true");
$file_name="$fileprefix.$date.xml";
$mech->save_content( $path_to_file.$file_name );
print ("Download \t\t\t[OK]\n");

#optional
sleep(2);

##Log Out
$mech->follow_link( text => 'Log Out' );
print ("Login out \t\t\t[OK]\n");

print("File successfully saved in $path_to_file$file_name\n");
exit 0;

Run it:

 perl wordpress_backup.perl 

Sample output:

 username@debian:~$ perl wordpress_backup.perl
Login in                        [OK]
Download                     [OK]
Loging out                     [OK]
File successfully saved in /home/user/backups/wordpress.2008-11-12.xml