Script to backup WordPress from remote PC

Another auto-backup script 🙂

This time, I wanted to have a quick and easy way to login to my server from my windows laptop, compress and backup the wordpress blog content, as well as the wordpress mysql database, then copy everything back to my laptop. A one-click backup basically.

Here’s what I did. First up, you need to download pscp and plink. I mentioned pscp in previous backup scripting posts. Both program are part of the “putty family”. Plink is basically a CLI version of putty, and pscp is a secure copy program (transfers files over ssh).

So the next step is instructing my laptop to login to the server and run a series of commands. We do this via plink:

plink [email protected] -pw really_good_pass -P 22 -m wordpressback.txt

The above uses plink to connect to on port 22 (SSH) using username user and pasword “really_good_pass”. Once connected, the -m switch instructs plink to run the commands in wordpressback.txt. In this file, we list the commands that should be run on the server:

mysqldump -u root –password=pass wordpress > /data/var/backups/wordpress.sql
tar czf /data/var/backups/blog.tgz /var/www/blog/*
zip /data/blog /data/var/backups/blog.tgz
zip /data/blog  /data/var/backups/wordpress.sql
The first line backs up the mysql database. The second line uses tar to compress and archive the whole wordpress blog directory. The third and forth lines just archive the mysql dump and tar file into one convenient-to-transfer zip file.
The last step is calling pscp to transfer the file over from the server to my laptop:
pscp -pw really_good_pas -P [email protected]:/data/
So in conclusion we end up with two files. One is wordpressback.txt which holds the commands to run on the remote server as described above. The second file is a .BAT file which contains just two lines calling plink and pscp:
plink [email protected] -pw really_good_pass -P 22 -m wordpressback.txt
pscp -pw really_good_pas -P [email protected]:/data/

Leave a Reply

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

You are commenting using your 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