Configuring Hostgator VPS for automated CPanel backups to Amazon S3

Warning! Geekiness ahead

Ok, so just had a wonderful time trying to set up a way to automatically backup my hostgator VPS to Amazon S3.

I did it with the help of some googling, head scratching and tricky bastardness so I thought I’d better document it here to remind me how to do it when I need to do it again.

I got most of the information from the post here and in the comments. Although, I had to piss about a lot to get it finally working. Mainly because of curly quotes in the original page, windoze line breaks and my monkey heritage.

(this is for VPS on Hostgator running Centos 5)

1. Create AmazonS3 bucket

Easy bit here, just create a bucket

2. Install S3 client for Linux

First need to install s3tools repo

cd /etc/yum.repos.d
wget http://s3tools.org/repo/CentOS_5/s3tools.repo

Next need to install

yum install s3cmd

Answer the questions with Y

3. Configure s3cmd

s3cmd --configure

Enter in the access key and secret key from Amazon Security Credentials

4. Enable daily backups from WHM

You can select which accounts by clicking the button marked ‘select’

If it’s already configured, find out the backup directory by typing

grep BACKUPDIR /etc/cpbackup.conf

5. Create the log directories

mkdir /var/log/backuplogs

6. Write a script to automate the backup and save it as /root/dailybackup.sh

You should change the email and bucket name to reflect your own values

#!/bin/bash

##Notification email address
_EMAIL=youremail@yourdomain.com

ERRORLOG=/var/log/backuplogs/backup.err`date +%F`
ACTIVITYLOG=/var/log/backuplogs/activity.log`date +%F`

##Directory which needs to be backed up
SOURCE=/backup/cpbackup/daily/*.gz

##Name of the backup in bucket
DESTINATION=`date +%F`

##Backup degree
DEGREE=3

#Clear the logs if the script is executed second time
:> ${ERRORLOG}
:> ${ACTIVITYLOG}

##Uploading the daily backup to Amazon s3
/usr/bin/s3cmd -r put ${SOURCE} s3://yourbucketname/${DESTINATION}/ 1>>${ACTIVITYLOG} 2>>${ERRORLOG}
ret2=$?

##Sent email alert
msg="BACKUP NOTIFICATION ALERT FROM `hostname`"

if [ $ret2 -eq 0 ];then
msg1="Amazon s3 Backup Uploaded Successfully"
else
msg1="Amazon s3 Backup Failed!!\n Check ${ERRORLOG} for more details"
fi
echo -e "$msg1"|mail -s "$msg" ${_EMAIL}

#######################
##Deleting backup’s older than DEGREE days
## Delete from both server and amazon
#######################
DELETENAME=$(date --date="${DEGREE} days ago" +%F)

/usr/bin/s3cmd -r --force del s3://yourbucketname/${DELETENAME} 1>>${ACTIVITYLOG} 2>>${ERRORLOG}

7. Grant execute privilege to the script

chmod u+x /root/dailybackup.sh

8. Set up a cpanel hook to run the script after the backup has completed

nano /scripts/postcpbackup

enter this as the contents

#!/usr/bin/perl
system("/root/dailybackup.sh");

make it executable

chmod u+x /scripts/postcpbackup

That’s it!

In case of disaster, copy the file from Amazon s3 with

mkdir restore
s3cmd -r get s3://yourbucketname/2011-02-32/filename.gz restore

Notes
I changed the bash script so it only copies *.gz files
I wanted to change the time at which the cpbackup occurs so I went to ‘manage plugins’ in WHM and put ‘install and keep udpated’ tick on ‘cronconfig’ and then went to ‘configure cpanel cron times’ and set the time I wanted cpbackup to run.

About Andy

This is my personal website where I (occasionally) post about things going on in the world of me. I am the creator of CommentLuv and administrator of comluv.com
Blog Tools, linux , , , , , , ,

30 responses to Configuring Hostgator VPS for automated CPanel backups to Amazon S3


  1. Hi Andy,
    This is really very good and informative article. Actually I don’t really understand the usability of Hostgator VPS? Well thanks for sharing.
    Shivam Garg recently posted..Bootstrap CouponMy Profile

  2. kool. been looking for an option like that. It would save a damn lot of time logging on to cpanel and my main problem has been remembering to take a backup. automated does the trick. thanks brother.
    Indifest recently posted..Famous Places to Visit in Mysore | Popular Attractions in MysoreMy Profile

  3. Why would one use Hostgator over say Slicehost or Linode who are the leaders in VPS performance and pricing?
    Benjamin Kerensa recently posted..Erick, We the TechCrunch Readers Dislike TrollingMy Profile

  4. Genie

    I’ve long in search for that an d I finally have it answered. It saves a lot of time and get back up. I completely understand the whole point about it.

  5. Thanks for this useful and informative post Andy.
    You saved me hours in front of cpanel and live chat of hostgator.
    Keep on the good work!
    Nick Sotos recently posted..North Face Denali Jacket Review and CouponsMy Profile

  6. I recently started using a VPS hosting for one of my blogs. I should try setting up a similar automated backups.
    Btw, I am glad that I stumbled upon your personal blog. I am a fan of your commentluv plugin. Good luck with your commentluv premium launch.
    Cheolsu recently posted..Myspace Login with FacebookMy Profile

  7. Jun

    Thanks for sharing this information. In the past, I create back up once a week, with this I won’t have problem backing up and its automatic. Also, it’s step by step making it useful for me who have just basic understanding on these kind of stuff.

  8. Thanks for the great write-up. I don’t have HostGator, but it still worked. Only thing was that I needed to do this for the /root/dailybackup.sh file: http://www.gizmola.com/blog/archives/87-Linux-shell-scripting-bad-interpreter-No-such-file-or-directory.html

    :-)
    Clifford P recently posted..DIY iPhone Videos for Real EstateMy Profile

  9. Clifford
    Twitter:

    Thanks for this. I’m not on HostGator, but all’s been working fine. :)

    How can I delete the backup from the server after 1 day but from S3 after 30 days (or possibly even delete from server immediately after upload completed successfully – would that be 0 days)? Instead of deleting from both sources at the same interval.
    The comments in /root/dailybackup.sh say that the DEGREE variable is used for both server and S3, but the command that you use (/usr/bin/s3cmd -r –force del s3://t…..) looks like it would only delete from S3.

    Thanks!
    Clifford recently posted..Annotate websites and share your edits (Freebies)My Profile

  10. Hey Andy, thanks for this detailed post. It really helped me because I’m a starter blogger. I just started a blog like 2 weeks ago and I need this kind of advices. Keep up the good work ;)
    Salvado recently posted..VLC media player free downloadMy Profile

  11. Joshua Miller

    Maybe this is a silly question, but running this backup daily in no way affects your bandwidth quota does it?

  12. Kevin
    Twitter:

    Since this is for cPanel/WHM is there any reason these directions would not work on any cPanel?

  13. Great code, having a cloud to store backups is making keeping websites secure easier than ever.
    Jamie recently posted..Reborn Babies For Sale CheapMy Profile

  14. Joe

    Thanks, you just solidified my decision to NOT go this route. Going to use backup buddy for my WordPress backups to S3 instead.
    Joe
    Joe recently posted..How To Check All In Anchor Backlinks According to Pot Pie GirlMy Profile

  15. Thanks for the post, really looks like a great idea. I got the folloing erro though during setup:
    yum install s3cmd
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    * base: mirror.raystedman.net
    * extras: mirror.raystedman.net
    * update: mirror.raystedman.net
    Segmentation fault (core dumped)

    What is the matter? I’ve tried looking into this but cant see any specific…

  16. Hi! Andy,
    Thank you for ur step by step tutorial. :)

  17. matt
    Twitter:

    Thanks for this page and guide. Honestly, I us it at least once a month. :)

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>

CommentLuv badge