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  
About these ads

~ by unixwayoflife on November 8, 2008.

5 Responses to “Automated WordPress.com Backup – Part 1”

  1. […] 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, […]

  2. 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.

  3. 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.

  4. Thank you so much that’s really great will link to you when trying to port your script to rebol ;)

  5. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: