How to backup web hosts

April 7th, 2008 | by David |

Do you backup your personal data regularly? I hope so. If you don’t, stop reading here and go backup your data.

If you are still reading this, you are really doing backups of your personal data. But do you have regular backups of your web host data (e.g. blog, forum, online shop) including files AND databases? Perhaps you are using stuff like phpMyAdmin to backup your databases from time to time.
And if you DO have backups of your web host (and I’m sure that there are not many people out there doing this, especially for their private hosts), do you have an archive of previous versions? Yes? You’re great, welcome on board, skip this blog posting (or don’t if you want to learn how I do it).

Everyone else who wants to know how to perform fully automated incremental backups of your web host including files and mysql databases and an archive of previous backups and do all this on Windows (of course I know that the Linux guys have advantages in that area) go and read on…

What you need for backing up your web host

To perform the automated backup described here you need the following things:

Setup phpMyBackupPro

Download the latest version (currently 2.1) and any language file if you prefer non-English interfaces. The installation is quite easy: just follow the instructions in the INSTALL.txt file. The moving/renaming of the configuration file is security through obscurity: it should be fine without this, but making assurance double sure is no bad way of thinking in terms of security. I have secured my export directory through a .htaccess file, so no one can access anything in there from the web. The .htaccess file is included in my backup package.

After successfully entering your configuration data in phpMyBackupPro you are ready to proceed. Go to the “configuration” and choose “Extended configuration” and set up the values for automatic deletion of files (I have marked the fields with big red dots):

phpMyBackupPro Extended configuration

These values depend basically on your personal preferences and on the size of your databases and available web host space. I’m using 14 days, so there are always 14 days of previous database backups directly on the web server. I think 5 or 7 would be enough, but as long as there is enough space… however. Let’s go on!

Create a schedule job file

phpMyBackupPro has a nifty little feature to create semi-automated backups. Go to “schedule backup” and set the following options:

  • Backup period: 1 day
  • Directory: choose any directory on your server where you might want to put the caller script, I usually just put it into the phpMyBackupPro directory
  • Databases: Select all databases you want to include in the backup
  • Comments: I always enter “daily backup” here
  • Make sure all the checkboxes below the comments area are checked. For the compression i prefer gzip since zip does not always work. If your web server does not support compression select none.

After clicking “Show script” you should get something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
// This code was created by phpMyBackupPro v.2.1 
// http://www.phpMyBackupPro.net
$_POST['db']=array("database_1", );
$_POST['comments']="daily backup";
$_POST['tables']="on";
$_POST['data']="on";
$_POST['drop']="on";
$_POST['zip']="gzip";
$period=(3600*24)*1;
$security_key="eae9fe894401239614a469f87e7ec638";
// This is the relative path to the phpMyBackupPro v.2.1 directory
@chdir("../phpMybackupPro/");
@include("backup.php");
?>

Because the backup always takes a few seconds the full 24 hours setting for the period would work only every 2nd day in average. Therefore I change it to 22 hours (this also makes sure no DST change overrules a backup):

10
$period=3600*22;

Now take the content and put it into a file called something like “auto_backup.php” and upload it onto your server – to the directory you specified before. Now it’s time for the first test: type the address to the auto_backup.php into your browser and hit enter:

http://www.yourhost.com/your-path/auto_backup.php

You will see nothing but a blank page in the browser. To check if the backup worked, go back to your phpMyBackupPro installation and select “import”. There you should see something like database_1.1206988550.sql.gz. If the list is empty, check your script for an invalid relative path in the @chdir or try to perform a manual backup in phpMyBackupPro. To perform several test backups in a row you must change the period value in the auto_backup.php to something much smaller like 30 seconds or so. Don’t forget to set it back to 22 hours afterwards!

How to get the files from your web host to your PC

This is where I had so search quite a long time myself (there are lots of automated FTP mirror tools, most of them costing too much money and not providing what I need here) until I found a solution that is so unbelievably easy that I don’t know why I didn’t get it earlier. The point is not only in loading everything from your server but also 2 other things:

  • Getting only those files that are new or have changed
  • Getting rid of stuff that also does not exist on the server anymore (full one-way synchronization)

There are tools like rsync out there, but they are too Unix-oriented for my taste and often require shell access that can’t be provided. What I needed had to work with any regular FTP server.

I found out that Wget, the command-line HTTP download tool from the Unix world, is also available for Windows and can download complete FTP structures too. There are of course thousands of people that have known this for years, but I did not since I’m using mostly Windows. Knowledge still equals power.

So what I do is mirroring the FTP server locally using Wget. Of course those readers who know that Wget only does mirroring, not synchronizing, will ask what the hell is he braying about, that’s not what we need!. Well, it was not what I needed either. But it was the main part to be followed by some work of myself.

How to get rid of files that don’t exist anymore

Trash me, baby!
The whole topic of synchronization is about getting the things that have changed (this we have already achieved) and then getting rid of the stuff that’s not on the other side anymore (e.g. old database backups that have been removed or folders that have been renamed). Wget comes in handy in this situation: when downloading from a server, it can provide a file called .listing that names all files that have been downloaded. So the conclusion is: everything that is NOT in this .listing file is outdated and ready to be trashed.

To parse these .listing files and remove outdated files and folders I’m using an Eclipse-based file organization workspace that is strictly object oriented and needs 450 Megabytes of space on your hard drive. Ha, got you there, just joking! It’s a dirty little VBScript, and I’m sure there are lots of ways to improve it, but it works fine for me and that’s basically why I want to share it with people out there who need something that might be working fine for them.

How to get an archive of previous versions

After moving the old files away my script calls 7-zip to create a compressed self-extracting archive. By using the self-extractor feature you don’t have to worry if you don’t have 7-zip any more one day – the files will be available as long as something can execute win32 binaries. There is a new archive created for every day (using the current date when you are executing the script), so you can spot your old data easily.

Summary

So this is what my backup does in the same order:

  • Start the database backup online using Wget
  • Grab everything that has changed (including the latest database image) using Wget
  • Move everything away that is not on the web server anymore using my VBScript
  • Create a compressed archive bearing the current date in its name using my VBScript and 7-zip

Use the Windows Task Manager to kick this off every day at the same time and that’s it!

Yeah, alright, I just want to download this thing and go on!

In the end this is all you need besides 7-zip and phpMyBackupPro (which you don’t need in case you don’t have a database to backup):

David’s Web Host Backup Script v1.0

Please read the included README.txt file carefully. It is quite long, but you will save the time for reading hundredfold as soon as the backup works and you don’t have to do anything any more.
Unpack everything to some folder. The folder where wget.exe and clean.vbs reside is also the root folder of all backup stuff. Read the README.txt again, edit example_backup.cmd according to your needs and configuration and lean back!

License

I have released my part of this package under the GNU GPLv3. Wget uses the same license, and I’ve included everything that was inside the original wget package. Feel free to use, modify or spread my work as long as you include the original copyright notice and license.

Please be careful when editing the batch file or modifying the script. I cannot take any responsiblity for your lost data if you do something wrong. If you are unsure please ask me.
This little script has been working fine for me for over 18 months and I’m currently backing up 5 different web hosts every day without any problems, and of course without having to do anything manually. Every few weeks I check if everything is fine and delete some old archives, but that’s it!

If you have any questions, comments or improvements you want to share with others, feel free to comment here or send me an email using david (at) vworld.at.

Post a Comment