Automated sites backup script

Today I have been restoring automatic backup system for the company I work at. The idea is to take backups of our clients' web sites (both web/html files and database snapshots). So I have decided why not to share? This script was dutifully working for me for almost a year and though it might be not the best way to do stuff, it proven itself pretty robust and problem-free.

Before pasting the script code here, here's how it works.

The script logs into remote server and takes backup of remote html files and database files. One important thing is to notice that database files backup is takes "as is", as I have found out not in the most pleasant way that taking backups of databases via mysqldump command can lead to problems in case tables in the SQL database use different encodings - so binary dump is much safer.

One more thing to notice is that you need to generate an ssh key-pair in order to be able to log into a remote server without entering a password (that's why there is no mention of passwords in the script's settings). You can read about logging into SSH without using passwords here.

Since I use rsync to fetch files from server, only files which were changes between backup sessions are being transferred - which makes backups much faster in case of successive backups.

Usage: php backup.php <backup_type>

you can specify type of backup, so it will be placed into a separate directory. Default is "nightly". Files will be backed up into directories inside the $backupDir you specified, in form of: "<sitename>_<backup_type>_<web or db>".

Once the script is deployed and configured, you might want to specify cron jobs which would make nightly, weekly and monthly backups for you. This way you will have at least 3 versions to choose from in case something shitty happens :)

And of course I would like to notice that the script goes "as-is" without any warranties that it won't destroy your data and nullify you hard disk (although if configured sanely, it would only save you from problems of loosing data).

Have fun with the script, modify it if you want. Hopefully it'll save you some nerve cells if ever a problem occurs on your remote servers.

The script follows:

<?php
/**
* automated backup script
* @copyright (c)2006 Mike Kornienko (mike -at- macdiggs dot c.o.m.)
* @note you need to create ssh key pair in order for everything to work.
**/

/* setup here */

$backupDir = "/home/backup/backups/";

$settings = array(
"server1.com" => array(
"server" => "server1.com",
"username" => "root",
"pathToWeb" => "/var/www/html/",
"pathToDB" => "/var/lib/mysql/"
),
"server2.com" => array(
"server" => "server2.com",
"username" => "root",
"pathToWeb" => "/var/www/html/",
"pathToDB" => "/var/lib/mysql/"
)
);

/***************** !!!don't touch anything below!!! *************/
if ($argc>1) {
$arg = $argv[1];
} else {
$arg = "nightly";
}
doBackup($arg);

function doBackup($type)
{
global $settings;
global $backupDir;

echo "starting backup : $type\n";

foreach ($settings as $key=>$value) {
echo "*** performing backup: $key ***\n";
$archive_name = $key."_".$type;
$theHostName = $value[server];
$theUserName = $value[username];
$thePathWeb = $value[pathToWeb];
$thePathDB = $value[pathToDB];

//performing backup of web folder
$backupPath = $backupDir.$archive_name."_web";
$command = "rsync -azr –delete -e ssh {$theUserName}@{$theHostName}:{$thePathWeb} {$backupPath}";
echo "backup web: $command\n";
exec($command);

$backupPath = $backupDir.$archive_name."_db";
$command = "rsync -azr –delete -e ssh {$theUserName}@{$theHostName}:{$thePathDB} {$backupPath}";
echo "backup db: $command\n";
exec($command);
}

echo "backup complete\n";
}

?>

Leave a Reply