Amazon s3 backup for web server files and SQL using bash


I needed a solid backup method for my clients websites, where I wanted to improve from the previous method of backing up to a separate host in the US Hostmonster. Hostmonster expires in 80 days, so why renew for $100+/year just to house a few GB of client backups.

I wanted to use the Amazon S3 service for my backups with an amazing reliability and a low monthly cost, so I  went ahead crawling the web for bash scripts that would suit my situation. The one I liked the most was Here, but I have adapted it to suit what I want.

Project is now sponsored at sourceforge at http://sourceforge.net/projects/des3bashwebsite/

My Requirements:

Environment: Redhat Cpanel X | WHM Server (VPS and shared - both)
Language: BASH
Automation: Cron Script 
Files: public_html directory and linked mysql database
Desired Output:  A single blob housing the www files and the sql database on my s3
Other: No archives left on server 
Frequency: Weekly or Monthly

I wanted to improve on the above script by being compatible with CPanel, include the SQL data for the website files, tar’ing both the www files and the sql files into one blob, and making the final filename more descriptive about the site. I want to use this script for a large number of websites that will all backup into a single bucket, so the file name was to be of the type: url-YYYYMMDD.tar.gz as to easily distinguish between backups.

This was to be a single dump of the whole website, and with filesize not an issue this is ideal towards the websites I create for clients.

1. Setup the File Structure

All my sites reside on separate cpanel accounts where the common file structure is:

/home/$USER/ (home)
/home/$USER/backup/    (website backup folder) 
/home/$USER/public_html/  (website files here)

Setup the new base structure as 

/home/$USER/
/home/$USER/s3/
/home/$USER/backup/    
/home/$USER/backup/sql/
/home/$USER/public_html/ 

2. Add Files

copy the de-s3-web-backup.sh file into /home/$user/backup/ and set permissions as 760
copy the s3-bash files into /home/$USER/s3 and set permissions 760 for all

3. Create Key file

Paste your amazon aws secret key into a text file and name it s3.key, and save in /home/$USER/s3/s3.key with permissions 760

4. Create a new s3 Bucket

Create a new amazon s3 for all your new downloads. S3 Firefox Organizer is great for this.

5. Get the Script

Download the script (from below) and upload it to your webserver in the /backup/ folder created above. Make sure the file privileges are 760. If you have a different file scheme, don’t put this in your public_html file 

6. Customise Script

You need to input your server parameters into the script in order for it to run.

AMAZONID is your amazon identification key
BUCKET is your amazon aws s3 bucket
SITE is a quick description of your website: example duivesteyn.net
USER is the name of the user account. In CPanel this is your username
mysqlusername is the MYSQL username for the website you are backing up
mysqlpassword is your MYSQL password for the above username
databases is the list of databases you are backing up example: “main” or “main1 main2″ for 2 databases

7. Add to cron

Schedule the task to run automatically through cron. This is also available in your websites CPanel.
Full Backup recommended weekly, and SQL backup recommended nightly.

0 0 * * * sh /home/USER/de-website-backup.sh 

And thats it. be sure to test it by running it in a terminal first, and remember to checkup on your bucket from time to time.

Download Here (GPL)

Version 1.01

https://sourceforge.net/projects/des3bashwebsite/

 
References: Bnee.com s3-bash

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts
Add Sitemap To robots.txt For Autodiscovery
duivesteyn.com.au nameservers

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

You must be logged in to post a comment. Click here to login.

Reader Comments

Hi there. Great script. I can get the script to run, it works except if I have more than one DB in databases=”dbname”. If I only have one everything is good, full data and everything. If I add a second by putting a space between them, I get the files created but no data in them. Any idea’s?

I’m confused - where do we get the de-s3-web-backup.sh file? Also, how would I customize it to only do a particular directory under html_public and a specific sql database that goes with it?