How to Do an Automated Remote Backup of Your WordPress Blog
This weekend, my website got hacked for the first time ever. I figured it was an event that was bound to happen eventually, but I still felt a bit shocked and more than a little surprised that someone would bother to do such a thing. I was lucky that I had a backup of my website which I had made about a month earlier, and since index.php never really changes unless I make a site design change, I was able to replace the defaced file with my original one, and no harm was done.
Still, it made me realize just how dangerous it is to make such infrequent backups. It also made me realize the extent of the damage that could have occurred if the database itself had got hacked – I could have lost over a month’s worth of blog posts.
Protecting Yourself With Automated WordPress Site Backups
The problem is I just don’t have time to go in every day, week, or even month, and back up my blog. I’m sure most of you reading this feel the same way. There really isn’t a nicely streamlined, automated solution that allows you to schedule a full backup of your files and database, and get the exported backup somewhere off the web server. Aibek listed some online tools that may help, but in those cases you are dependent on some outside service or website to make it all work.
Configuring Automatic Backups
I decided to spend half of my Sunday putting together a solution. The following setup makes use of two scheduling WordPress plugins, WordPress Backup and DB Manager. The first backs up your files, and the second backs up your database – both on a schedule. In the last part of the article, I’ll show you how to automate the FTP transfer of those files onto a remote server or PC.
Your first step is to install the WordPress Backup plugin, taking careful note of the installation instructions and implementing the .htaccess file where required. Then go to the configuration area for it. Here is where you can tell the plugin how often to do a backup of your wordpress uploaded images, theme files and plugins.
Make note of the backup directory, you’re going to need this later. The three files stored there will be plugins.zip, uploads.zip and themes.zip.
Next, install the DB Manager plugin. You could also use something like the database plugin Dean described. Pay attent to the .htaccess configuration instructions. Don’t skip the instructions or the backup won’t work properly. Go to the Database Options in your WordPress menu and make a note of the backup path.
There will be multiple files up to the backup archive you define. To FTP these files we’ll have to use wildcards (I’ll show you how).
Set up your database backup on a schedule by changing the Automatic Scheduling settings.
Now that your WordPress files and database will be backed up locally to a location on your web server, it’s time to schedule an FTP job to get those files off the server and onto your local PC.
Scheduling An FTP Transfer
A scheduled FTP transfer is not easy to come by. Most FTP clients that can do it want you to pay for their premium product to do so. To get around this, you need FTP scripting commands that you can schedule, and that’s exactly what WinSCP allows you to do.
Download and run the interactive WinSCP app. Once you’ve established an FTP connection, click on “Session” and save the session.
Once you’ve saved the session details, you can close the application and then open up a text file. Call it anything you like, but remember the name. You will want to type something similar to the following (replace the paths with the paths above that you’ve noted as you set up each plugin).
I created a folder on my local server under D:\backup\TSW_Files where I want to FTP all of those files. Under D:\backup\, I placed the following script file called myfile.txt:
option batch on option confirm off open topsecretwriters get public_html/wp-content/xxxxx/themes.zip d:\backup\TSW_Files\ get public_html/wp-content/xxxxx/uploads.zip d:\backup\TSW_Files\ get public_html/wp-content/xxxxx/plugins.zip d:\backup\TSW_Files\ get public_html/wp-content/xxxxxxx-db/*topsec6_topwp.sql.gz d:\backup\TSW_Files\ exit
The first path in the “get” command is your remote web server path, and the second path is your local backup PC/server path. Next, create a text batch job called backupWP.bat and store it in the c:\program files\WinSCP\ folder. That text file should have the following lines:
@echo off winscp.com /script=d:\backup\myfile.txt
The myfile.txt script can really be stored anywhere, so long as you keep the path listed in the batch job accurate.
Now, whenever the batch job launches, it will call WinSCP.com, which will in turn run the FTP script commands you defined in your txt file. All you have to do now is schedule backupWP.bat to run using Windows Scheduler (All Programs -> Accessories -> System Tools -> Windows Scheduler).
Now, you’ve basically set up a system where your WordPress block takes regular backups of important WordPress files and the WordPress database to folders on your web server, and you can FTP those backed-up files off your server to a remote location using your daily FTP script.
So, there you have it. Once you’ve set up the system above, you never have to give backing up your blog another thought. Automating the entire process will free you up to do more important things, like writing your first novel, or playing with your kids. Pretty cool, isn’t it?
Try the setup above with your own blog and let us know if you were able to make any improvements in the setup, or if you know of any plugins that work as well as these two for a WordPress site backup. Share your thoughts in the comments section below.
Image Credit: Gualtiero Boffi