Backup MySQL to Rackspace Cloudfiles

If you liked this post, say thanks by sharing it:

Something that everyone needs and not everyone has is a good backup strategy for your content.  From personal data to web server data, its all essential that we back it up and often times we don’t think about it.

I have created an automated script in php to automatically backup your mysql database to rackspace cloud files.  This runs as a simple cron job on my servers, but could easily run on a service like or another place.  I have started using innodb as my database engine of choice from mysql recently.  This is great because it does row level locking instead of table locking.  It is essential that this be used over myisam in high write scenarios.  Enough about mysql, the purpose of this is to describe my backup script for a mysql webserver.

I use the php cloudfiles master library for this script.  It is available from racker labs, but can also be downloaded here.

The below script can be set up as a cron job to run however often you choose.  The command to run is:

/usr/bin/php 'path/to/backup.php'

Here is the php script:

$server_name = 'SERVER_NAME'; //Name of the Current Server
$databases = array("DB_NAME"); //Array of Databases to be Backed up
$username = "MYSQL_USERNAME"; //MySQL Username
$password = "MYSQL_PASSWORD"; //MySQL Password
$rscfUsername = 'RACKSPACE_USERNAME'; // the username for your rackspace account
$rscfApiKey = 'RACKSPACE_APIKEY'; // the api key for your account
$rscfContainer = 'RACKSPACE_CONTAINER'; //rackspace containr
$to_email = ''; //to email for errors
$curr_date_time = date("Y-m-d--H:i:s"); //DON' CHANGE - Date
$temp_dir = "/bak/mysql/"; //temp directory
$subject = "Backup Failed"; //subject for errors
$headers = "From:" . "\r\n"; //header for emails
$body = "Backup failed for " . $server_name; //body for emails
foreach($databases as $db) {

 exec("mysqldump -u {$username} -p{$password} --single-transaction {$db} | gzip > {$temp_dir}{$db}{$curr_date_time}.sql.gz");
 // check if the file is created
 if(file_exists("{$temp_dir}{$db}{$curr_date_time}.sql.gz")) {

 $auth = new CF_Authentication($rscfUsername, $rscfApiKey);
 $conn = new CF_Connection($auth);

 // I allready created a container with this name otherwise user create_container
 $container = $conn->get_container($rscfContainer);

 // make a unique name with date in it so you know when it was created
 $objectName = $db.$curr_date_time.'.sql.gz';
 $store = $container->create_object($objectName);
 $store->load_from_filename("{$temp_dir}{$db}{$curr_date_time}.sql.gz"); // send to rackspace

 //unlink("{$temp_dir}{$db}{$curr_date_time}.sql.gz"); // delete the file, not needed anymore

 else {
 // if no file send an email so we know something is wrong
 mail('', 'No file', 'Backup Failed, '.$server_name);
 } catch (Exception $e) {
 // in case of an exception, might be rackspace is gone wild.
 mail('', 'Exception', print_r($e, true));

The entire package can be downloaded below.


Download MySQL Backup for PHP

If you liked this post, say thanks by sharing it:
Tagged with: , , ,
Posted in Rackspace

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>