Backup Anything to Rackspace CloudFiles

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

A week or so ago I posted about how to do MySQL backups to rackspace cloud files.  Another important process to backing up your sites and data, besides the database, are the actual files that make it up.

I use the php cloudfiles master library for this script.  It is availible 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 = 'YOUR_SERVER_NAME'; //Name of the Current Server
$curr_date_time = date("Y-m-d--H:i:s"); //DON' CHANGE - Date
$curr_day = date("Yd"); //DON' CHANGE - Date
$sites = array("ENTERDATABASES HERE IN ARRAY FORMAT"); //Array of Databases to be Backed up
$site_root = "/var/www/";
$temp_dir = "/bak/mysql/"; //temp directory
$site_temp_dir = "/bak/site/"; //temp directory
$rscfUsername = 'YOUR RACKSPACE USERNAME'; // the username for your rackspace account
$rscfApiKey = 'YOUR RACKSPACE API KEY'; // the api key for your account
$rscfContainer = 'YOUR RACKSPACE CONTAINER'; //rackspace containr
$to_email = ''; //to email for errors
$subject = "Backup Failed"; //subject for errors
$headers = "From:" . "\r\n"; //header for emails
$body = "Backup failed for " . $server_name; //body for emails
foreach($sites as $site) {


 exec("tar -czpf {$site_temp_dir}{$site}{$curr_date_time}_site.tar.gz {$site_root}{$site}");
 // check if the file is created
 if(file_exists("{$site_temp_dir}{$site}{$curr_date_time}_site.tar.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 = $site.$curr_date_time.'.tar.gz';
 $store = $container->create_object($objectName);
 $store->load_from_filename("{$site_temp_dir}{$site}{$curr_date_time}_site.tar.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));




Download Backup Scripts

If you liked this post, say thanks by sharing it:
Tagged with: , ,
Posted in php, 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>