NOTE: This article is written for our first generation Cloud Control Panel. You can access this interface from the next generation Cloud Control Panel by clicking your username in the upper-right of the control panel and selecting "First Gen Control Panel".
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.
Step 2
For this step, you will need to have access to Cloud Files. If you are not sure or have not set up 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 obtain and set up the PHP API. To download the PHP API documentation and files, click here. If you click the "Downloads" button you can then download the files in a zip archive.
Once 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 that the date, time and frequency can be modified to best suite your needs.

In the above example we chose "Perl" as the scripting language (the "Perl" option is used to run shell scripts as well) and scheduled the task to run daily at 6:45 AM Central time.
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.
© 2011-2013 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

25 Comments
Save a week of website
This script looks good! How can I modify it so it only keeps a week of backup? ie. it creates a backup everyday, and after the 7th backup is done, the oldest one is deleted?
Thanks for your help.
Re: Backup rotation
Also curious to know
Re: Incremental backups
WHO can modify the script to DELETE old backups at CloudFiles
Thanks in advance.
Re: script access
Re: Incremental Backups
Re: Incremental Backups
If I place the backup script
Nevermind, I misunderstood,
I'd like to thank matt
Re: monthly rotation
date=`date '+%d'`
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
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
Stupid question perhaps. Now
Script to remove old backups
Forfiles -p "E:\Scheduled Backups\" -s -m *.* -d -7 -c "cmd /c del /q @path"
Any chance Rackspace could come up with a cloudfiles equivalent?
Thanks!
Re: older files
That will cause the filename to use the name of the day of the week instead of the full date. If you have a backup with "Sunday" in the name, the next Sunday backup that runs will overwrite the old "Sunday" file.
1 week of daily backups and 1 month of weekly backups
I've added the modified .sh & .php scripts to PasteBin for you to check out and/or use (this is as-yet untested so please let me know if there's anything that can be improved or needs to be fixed).
- backup.sh: http://pastebin.com/7khY1YTJ
- cloudfiles_backup.php: http://pastebin.com/XNZxSbcF
Everything else remains the same as far as the setup process is concerned... just modified those 2 files.
Also, you can easily remove the Weekly Backup portion of the PHP script & it becomes a 7 day daily backup script if you only want that (since all of the weekly backup functionality is done w/ the php script).
Follow up
Here's the updated process:
1. Have the bash script trigger normally (with the php & backup removal code removed).
2. Have the php script trigger 30 min. later as a separate cron job (with code added to make it use the correct filename [since it isn't pulling it from the bash script anymore], and code for removing the backup file from Cloud Sites when the transfer is complete).
Systax Error
I am new to PHP and my site at cloud is on .net. I am trying to implement the above backup but getting the following errors.
Scalar found where operator expected at /backup.sh line 13, near "$backupname $backupname"
(Missing operator before $backupname?)
Can't modify constant item in scalar assignment at /backup.sh line 2, near ""/";"
syntax error at /backup.sh line 7, near "/;"
syntax error at /backup.sh line 13, near "$backupname $backupname"
This is my backup.sh
webroot="My WEBROOT"; #Not showing here for security reasons
#Set the date and name for the backup files
date=`date '+%F-%H-%M'`;
backupname="backup.$date.tar.gz";
#Backup Site
tar -czpvf $webroot/sitebackup.tar.gz $webroot/web/content/;
#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;
script not working, pls help
I'm not sure what i'm doing wrong as i see from the above comments that it's working for folks. Would appreciate any guidance.
log says:
/mnt/stor10-wc2-dfw1/550301/www.mydomain.com/backup.sh: line 12: /mnt/stor10-wc2-dfw1/550301/www.mydomain.com
/db_backup.sql
: No such file or directory
tar: Removing leading `/' from member names
tar: /mnt/stor10-wc2-dfw1/550301/www.mydomain.com\r/web/content/\r: Cannot stat: No such file or directory
tar: /mnt/stor10-wc2-dfw1/550301/www.mydomain.com\r/sitebackup.tar.gz: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error exit delayed from previous errors
In order to debug issue, i commented out all lines after "mysqldump". I suspect it might have something to do with single vs double quotes.
Would really appreciate any help to get this up and running.
Thanks
Tariq
re: debugging
You might also make sure the script hasn't had linewrap applied to it - some editors start a new line when you get to the end of a line by default, so look for a "line wrap" setting and turn that off before saving the script. Longer lines like the "tar" steps need to be all on one line instead of wrapped to another line in the script that gets uploaded to the site.
Hi Jered,
I followed your instructions.....tried using notepad++ as well as plain old notepad. In both cases, it's giving the same error. The blank line were still giving errors so i got rid of them completely. Furthermore, in order to debug, I deleted all lines except follows:
===========CONTENT OF BACKUP.SH FILE=============
#!/bin/sh
#Set information specific to your site
webroot="mywebroot"
db_host="mydbhost"
db_user="myuserid"
db_password="mypassword"
db_name="mydbname"
#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
(Note - When you paste the above, each line comes out on it's own separate line, here it shows wrapped and on multiple lines)
Now i'm getting the following error in the log file:
===========CONTENT OF LOG FILE=============
Command to be executed is: /usr/bin/perl /mnt/stor10-wc2-dfw1/550301/www.mywebsite.org/backup.sh
Task ID of this command is: 97772
Where should log be written: /mnt/stor10-wc2-dfw1/550301/www.mywebsite.org/logs
Start time: Wed Oct 31 23:17:12 CDT 2012
Email address: myemail@gmail.com
/mnt/stor10-wc2-dfw1/550301/www.mywebsite.org/backup.sh: line 12: /mnt/stor10-wc2-dfw1/550301/www.mywebsite.org
/db_backup.sql
: No such file or directory
RE: Hi Jered,
I believe our support staff worked with you offline. Please let us know if you have any other problems.
Best, Rae
WordPress backup from Cloud Sites to Cloud Files
See https://gist.github.com/4513611
It's more WP friendly as it connects to the WP core to get your DB auth info, and to get WP paths (such as the uploads dir), so that nothing is hardcoded.
Add new comment