In order to backup Cloud Sites to Cloud Files using this example, you will need to create two cron jobs. One cron job will backup the cloud site and database and the second cron job will upload the backup to Cloud Files. This is a very simple example and should not be used for extremely large sites and databases to backup to Cloud Files.
Step 1
For the first cron, create a file called backup.sh. This file should be placed in the root of your domain. You will want to place the following code inside of it ::
#!/bin/sh #Set information specific to your site webroot="YOUR WEBROOT" db_host="YOUR DB HOST" db_user="YOUR DB USERNAME" db_password="YOUR DB PASSWORD" db_name="YOUR DB NAME" #Set the date and name for the backup files date=`date '+%F-%H-%M'` backupname="backup.$date.tar.gz" #Dump the mysql database mysqldump -h $db_host -u $db_user --password="$db_password" $db_name > $webroot/db_backup.sql #Backup Site tar -czpvf $webroot/sitebackup.tar.gz $webroot/web/content/ #Compress DB and Site backup into one file tar --exclude 'sitebackup' --remove-files -czpvf $webroot/$backupname $webroot/sitebackup.tar.gz $webroot/db_backup.sql #Upload your files to cloud files. #First argument is the location of the backup file, second argument is the name to be used when uploaded php $webroot/cloudfiles_backup.php $webroot/$backupname $backupname #After your backup has been uploaded, remove the tar ball from the filesystem. rm $webroot/$backupname
In the above script, you will need to replace the portions in ALL CAPS with your actual information as follows ::
YOUR WEBROOT - This is the absolute path to your files. You can find this path by clicking on Hosting, Cloud Sites, Features, and scrolling down. This will be the "Linux Path" listed there. An example would be: /mnt/target02/123456/www.domain.com (Note, the /web/content is not included in that path)
YOUR DB HOST - Database Host an example would be mysql5-9.wc1 or mysql50-78.wc1.dfw1.stabletransit.com
YOUR DB PASSWORD - the password on this database (note the single quotes and that there is no space between the -p and the single quote)
YOUR DB USER - the database username (e.g.: 12345_username)
YOUR DB NAME - the name of the database you are backing up (e.g.: 12345_database_name). Note that there is no parameter for this option unlike the previous examples (-h -p and -u respectively).
Place this file in your web root and create a cron job. Note, for step by step instructions on how to create a cron, Please visit our section on How_do_I_enable/disable_a_cron_job?
Put the name of the file, which, in our example is backup.sh in the "Command To Run Field". Select Perl as the Command Language and then select the interval in which you wish to run your backup script.
Step 2
For this step, you will need to have access to Cloud Files. If you are not sure or have not setup Cloud Files on your account, please visit How do I Access Cloud Files for more information on setting up your Cloud Files account. Also, please note that you may incur any charges associated with using Cloud Files. For more information regarding Cloud Files costs, please visit :: http://www.rackspacecloud.com/cloud_hosting_products/files/pricing/
Now, you will need to setup the obtain and setup the PHP API. To download the PHP API documentation and files, you can click here. If you click the "Downloads" button you can then download the files in a zip archive.
If you download the archive, unpack it. Then go to your site root and create a directory named "cloudfiles". Upload the files that were in the PHP API archive into the new "cloudfiles" directory. Once it's uploaded the contents of the /YOUR_WEB_ROOT/cloudfiles/ directory should look similar to this picture ::

Next you will need to create a simple PHP script to connect to Cloud Files and upload the backup you are creating. We'll call it "cloudfiles_backup.php", and put it in the same location as the shell script (your web root directory). This file will be called by the shell script we wrote earlier, so if you change that name make sure to find its reference in the shell script and change it there too.
Here are the contents of the script ::
<?php
// include the API - note we must use the absolute server path because this script will be executed through php technology and not http
require("/YOUR WEBROOT/cloudfiles/cloudfiles.php");
// cloud info
$username = "YOUR USERNAME"; // username
$key = "YOUR API KEY"; // api key
$containername = "YOUR CONTAINER NAME"; // container name
// backup file name from command-line argument
$backup = $argv[1];
// Name to use for file once uploaded
$uploadname = $argv[2];
// Connect to Rackspace
$auth = new CF_Authentication($username, $key);
$auth->authenticate();
$conn = new CF_Connection($auth);
// Get the container we want to use
$container = $conn->get_container($containername);
// upload file to Rackspace
$object = $container->create_object($uploadname);
$object->load_from_filename($backup);
?>Like the first script, in the above script you will need to replace the portions in ALL CAPS with your proper values. This includes:
YOUR WEBROOT - As it was before, this is your Linux path, as specified under the "Features" tab for your site.
YOUR USERNAME - Your Rackspace Cloud Files user name (the one you use to log into the control panel).
YOUR API KEY - Your Cloud Files API key. You can get the API key in the control panel in the "Your Account" section in the sidebar, under "API Access".
YOUR CONTAINER NAME - The name of the container you're using on Cloud Files to hold the backup file.
Step 3
The final step is to automate all of this by setting up the actual cron job within your control panel.
The task we create will execute the backup.sh script we created during step 1. The following screen shows a sample of the setup process in the control panel for this task. Please note, the date, time and frequency can be modified to best suite your needs.

Now you have created a method to backup your Cloud Site to your Cloud Files account. Using our cron system will allow you to regulate how often you would like to create and upload your backups.
Resources and Disclaimer
Cron FAQ's: What is a cron job: /knowledge_center/index.php/What_is_a_cron_job%3F
How do I enable cron: /knowledge_center/index.php/How_do_I_enable/disable_a_cron_job%3F
How do I schedule a cron job: /knowledge_center/index.php/How_do_I_schedule_a_cron_job%3F
How do I import a large Mysql Database
/knowledge_center/index.php/How_do_I_import_a_large_Mysql_Database
Cloud Files Knowledge Base
/knowledge_center/index.php/Main_Page
The Rackspace Cloud is not able to provide code support for your Cloud Sites and web applications. This article is designed to offer a very simple script that will backup your Cloud Site to your Cloud Files account. You are welcome to modify any part of these scripts to meet your own needs.

Comments
Save a week of website
Re: Backup rotation
Also curious to know
Re: Incremental backups
WHO can modify the script to DELETE old backups at CloudFiles
Re: script access
Re: Incremental Backups
Any solution for Incremental Backups that has been tried and tested for Cloud Sites?
Re: Incremental Backups
Hi Kevin, at this time, there are a few recommendations if needing incremental backups. For Windows and Mac, you could use Expandrive (http://www.expandrive.com/) to mount your filesystem locally. From here you could use the applications backup offerings. For Linux, you could mount your filesystem using SSHFS, then use rsync or dump to make backups.
If I place the backup script
If I place the backup script in my web root directory, how do I keep outsiders from downloading it and accessing sensitive information? Maybe I'm missing something, but it seems like we're depending on security by obscurity here.
Nevermind, I misunderstood,
Nevermind, I misunderstood, all seems good
I'd like to thank matt
I'd like to thank matt.wheeler for creating this article. i just used it to create a backup. Works great. I am wondering Matt, if you could append this code to add a 3rd cron job that deletes backups older than 30 days. I think there is a huge desire to have something like that.
Re: monthly rotation
I can't think of an easy way to check the date on a backup stored in Cloud Files, but you could use a quick-and-dirty trick with naming. In the first script, change the line that defines "date" so it looks like:
That will change the date appended to the filename from today's date to just the day of the month. A backup on March 14 would end with just "14", as in "backup.14.tar.gz". That way, when April 14 rolls around, the script would wind up overwriting the March 14 backup with the one from April 14 (since it would use the same filename - "backup.14.tar.gz").
It means that you'd have some "backup.31.tar.gz" files sitting around waiting for months with 31 days in them, but in general it means you'll never have more than 31 backup files in your Cloud Files container.
Pardon the noob question, but
Pardon the noob question, but that's what I am in this subject.
I followed these instructions carefully and explicitly. The one instruction that makes wonder if I accomplished the task successfully is one thing not explicitly stated.
The instructions do not explicitly state where the php file "cloudfiles_backup.php" should be stored.
Re: php file location
Not a bad point about being clearer on the location. You should put the PHP script in your web root folder - the same place as the shell script.
I have same question, who has
I have same question, who has authorization to delete backup files. I feels that backup file should not be delete by anyone and it has to keep for future references.
mole removal cream
Add new comment