C3rd
[Rackspace] Backup web server files to Cloud Files using PHP API
Posted: 20 Apr 2011, 22:00pm - WednesdayMy latest task if to enhance our backup system that we currently have which all our backups are stored in the same server. Today, I managed to create an API integration to Rackspace Cloud Files and to transfer the generated backups of the web server to Cloud Files. Below is the guide for those who are having trouble using the API. HowTo:
- Download the Cloud Files API - https://github.com/rackspace/php-cloudfiles/tree
- Follow the steps in extracting the API files to your server, http://cloudfiles.rackspacecloud.com/index.php/PHP_API_Installation
- And you may start creating php script, read http://docs.rackspacecloud.com/files/api/cf-devguide-latest.pdf
PHP Fatal error: Uncaught exception 'BadContentTypeException' with message 'Required Content-Type not set ...Solution:
- Install the PHP PECL FileInfo
# yum -y install php-devel php-pecl php-pear // if "sh: make: command not found" error OR "ERROR: `make' failed" occur, groupinstall the Development Tools. # yum -y groupinstall "Development Tools" // install fileinfo # pecl install fileinfo # service httpd restartif still occurs, last resort is upgrade your php to php 5.3
- Upgrade your PHP to 5.3
# yum -y erase php php-commonthen install the PHP v5.3
# yum -y install php53 php53-devel php-mysql php-gd php-pecl php-pear php-common #service httpd restartand that's it.. your php script should be running... for my scenario:
[root@server backups]# ls backup.logs mysql_03262011_0155.tar.gz mysql_04042011_0155.tar.gz mysql_04132011_0155.tar.gz WebFiles_04-06-2011.tar.gz backup.mail mysql_03272011_0155.tar.gz mysql_04052011_0155.tar.gz mysql_04142011_0155.tar.gz WebFiles_04-13-2011.tar.gz backupSQL.script mysql_03282011_0155.tar.gz mysql_04062011_0155.tar.gz mysql_04152011_0155.tar.gz WebFiles_04-20-2011.tar.gz backupSQL.script.11082010 mysql_03292011_0155.tar.gz mysql_04072011_0155.tar.gz mysql_04162011_0155.tar.gz cronjobs.txt backup.web mysql_03302011_0155.tar.gz mysql_04082011_0155.tar.gz mysql_04172011_0155.tar.gz init.cdn.script backupWEB.script mysql_03312011_0155.tar.gz mysql_04092011_0155.tar.gz mysql_04182011_0155.tar.gz monthly.2011 cdn.backup.php mysql_04012011_0155.tar.gz mysql_04102011_0155.tar.gz mysql_04192011_0155.tar.gz scripts mysql_03242011_0155.tar.gz mysql_04022011_0155.tar.gz mysql_04112011_0155.tar.gz mysql_04202011_0155.tar.gz mysql_03252011_0155.tar.gz mysql_04032011_0155.tar.gz mysql_04122011_0155.tar.gz WebFiles_03-30-2011.tar.gzThese files are my backup files, so I created a PHP script named cdn.backup.php, with initializer named init.cdn.script to upload the backup files to Cloud files. If I execute my script, this is the output:
[root@server backups]# ./init.cdn.script . . . . . . . . . . . . . Uploadeding mysql_04202011_0155.tar.gz... SQL backup completed! . . . . . . . . . . . . . . . . . . . . Uploadeding WebFiles_04-20-2011.tar.gz... Web files backup completed!That's it... My files has been uploaded to cloud files. --- Source code:
<?php if(function_exists("date_default_timezone_set") and function_exists("date_default_timezone_get")) @date_default_timezone_set(@date_default_timezone_get()); require('cloudfiles.php'); date_default_timezone_set('America/New_York'); $cfgAccount = array('user'=>'demo','keys'=>'fe01ce2a7fbac8fafaed7c982a04e229'); $auth = new CF_Authentication($cfgAccount['user'], $cfgAccount['keys']); $auth->authenticate(); if ($auth->authenticated()) { $conn = new CF_Connection($auth); //$conn->ssl_use_cabundle(); $container_name = "backups"; // execute this if no container //$backup_obj = $conn->create_container($container_name); //echo $backup_obj; if (function_exists('systemxx')) { system("ls -t",$lines); foreach($lines as &$line) { echo $line . "\n\n"; } } else { $backupSQLSent = 0; $backupFILESent = 0; $limit = date("j") - 7; for ($i = date("j"); $i > $limit; $i--) { $d = dir("/backups/"); //echo "Handle: " . $d->handle . "\n"; //echo "Path: " . $d->path . "\n"; while (false !== ($entry = $d->read())) { // mysql_04122011.tar.gz if ((preg_match("/mysql_".date("mdY")."/", $entry)) && ($backupSQLSent == 0)) { $backup_obj = $conn->get_container($container_name); echo 'Uploadeding '.$entry."...\n"; $tarballs = $backup_obj->create_object("cdn_".mktime()."_".$entry); $filename = "/backups/".$entry; // upload file in a hard way $fsize = (float) sprintf("%u", filesize($filename)); $fp = fopen($filename, "r"); $tarballs->write($fp, $fsize); // upload file in a convenience way //$tarballs->load_from_filename($filename); echo "SQL backup completed!\n"; $backupSQLSent = 1; } // WebFiles_04-20-2011.tar.gz if ((preg_match("/WebFiles_".date("m-d-Y")."/",$entry)) && ($backupFILESent == 0)) { $backup_obj = $conn->get_container($container_name); echo 'Uploadeding '.$entry."...\n"; $tarballs = $backup_obj->create_object("cdn_".mktime()."_".$entry); $filename = "/backups/".$entry; // upload file in a hard way $fsize = (float) sprintf("%u", filesize($filename)); $fp = fopen($filename, "r"); $tarballs->write($fp, $fsize); // upload file in a convenience way //$tarballs->load_from_filename($filename); echo "Web files backup completed!\n"; $backupFILESent = 1; } if (($backupFILESent == 1) && ($backupSQLSent == 1)) { break; } echo ". "; } $d->close(); // if backup sent, end loop if (($backupFILESent == 1) && ($backupSQLSent == 1)) { break; } echo "\n"; } } } else { echo 'Unable to connect to Cloud Files!'; } ?>--- Downloads: