Automated WordPress.com Backup – Part 1
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

[...] wordpress blog I’ve been meaning to write about this forever, but now someone else has done all the work already! The script presented there works wonderfully. I’ve just made a tiny addition, [...]
Systematically backing up a wordpress blog « Learning in Linux said this on November 13, 2008 at 12:39 am
Works great here, thanks!! You’re quite a disciplined worker – the first thing you did upon starting this blog seems to be making backups!
Looks like you’ve disabled trackbacks, but I thought I’d mention I expanded your script by one line that
adds version control.
yungchin said this on November 13, 2008 at 12:49 am
I keep getting here:
Can't call method "url" on an undefined value at /usr/share/perl5/WWW/Mechanize.pm line 713.It prints “Download [OK]”
Seems to be failing at: $mech->follow_link( text => ‘Log Out’ );
libwww-mechanize-perl at version 1.58-1 in Debian.
GuySoft said this on September 2, 2009 at 1:39 pm
Thank you so much that’s really great will link to you when trying to port your script to rebol
rebtut said this on December 26, 2009 at 10:54 am
For cpanel hosted websites another method Automated WordPress.com Backup is using backupsmart for website backup
It is also fully automated and effective for website backup
Steve Robbins said this on October 30, 2010 at 2:30 am