Knowledgebase

WHM Backup To Amazon S3

In this article i will explain how to take cpanel backup to amazon s3,with sync so only updated files are sent to S3. 
The step by step procedure is explained below

Step1) Activate an account in s3. You will get an access key and secret_key after the activation.

You can create a new s3 account by following the url,

http://aws-portal.amazon.com/gp/aws/developer/subscription/index.html?productCode=AmazonS3


Step2) Install s3 client for linux.

root@server:~# apt-get install s3cmd

On redhat or centos based machines(using rpm packages), you can install “s3cmd” as follows,

cd /etc/yum.repos.d
wget http://s3tools.org/repo/CentOS_5/s3tools.repo
yum install s3cmd
Alternatively, you can download it from the url pasted below:

http://s3tools.org/s3cmd



Step3) Configure s3 client using the command

root@server:~# s3cmd --configure

It will ask for the access key and secret key that we got during our account activation. This process reports failure, if we provide the wrong key values. Once this step is completed,  the configuration will be stored inside the file “/root/.s3cfg”.

During configuration you will be asked whether to enable encryption or not. Enabling encryption will improve the security of transfer but will make the upload a little bit slower.



Step4) We need to create a bucket in s3 for storing the backup.

eg: creating a bucket named “Backup_daily”,

root@server:~# s3cmd mb s3://Backup_daily

For additional options refer the url,
http://www.saltycrane.com/blog/2010/02/s3cmd-notes/


Step5) Enable daily backup from WHM. Refer the url pasted below for reference,

http://docs.cpanel.net/twiki/bin/view/AllDocumentation/WHMDocs/ConfigBackup
If backup is already configured, then we can know the location of the backup using the command,

root@server:~#grep BACKUPDIR /etc/cpbackup.conf
BACKUPDIR /backup
root@server:~#
Inside “/backup” there will be another directory named “cpbackup”, which will be holding the daily,weekly and monthly backup’s. In my case,

root@server:~# ls /backup/cpbackup/
./  ../  daily/  monthly/  weekly/
root@server:~#


Step6) Create log directories,

root@server:~# mkdir /var/log/backuplogs
root@server:~#


Step7) Write a script to automate the backup, and save it as “/root/dailybackup.sh” .

root@server:~# cd /root
root@server:~# nano dailybackup.sh

paste the following code


#!/bin/bash
##Notification email address
_EMAIL=your_email@domain.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
##Name of the backup in bucket
DESTINATION=`YOURBUCKETNAME`

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

##Uploading the daily backup to Amazon s3
s3cmd sync --delete-removed ${SOURCE} s3://${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}



press ctrl+x and save the file
Grant execute privilege for the script

root@server:~# chmod u+x /root/dailybackup.sh


Step8) Grant execute privilege for the script and schedule it to run everyday,

root@server:~# chmod u+x /root/dailybackup.sh
root@server:~# cp -p /root/dailybackup.sh /etc/cron.daily/
root@server:~#
NOTE:

Or if you wish to start the amazon s3 backup script right after the cpanel backup process, then create a cpanel post backup hook named “/scripts/postcpbackup” with the following contents,

nano /scripts/postcpbackup

Add the following code

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

press ctrl+x and save the file
Grant execute privilege for the script

root@server:~# chmod u+x /scripts/postcpbackup


Final step is to log into WHM and goto Configure Backup page,  there will be 2 new options





Note:
The post backup hook will start the amazon s3 backup script right after every cpanel backup completion.



In case of disasters we can download the backup from the bucket using the same s3cmd tool.

root@server:~# s3cmd -r get s3://YOURBUCKETNAME/USERNAME  /backup/cpbackup/daily



Want to know how much disk space you are using for your backups at Amazon S3, type the following command

root@server [~]# s3cmd du s3://YOURBUCKETNAME



  • 42 Users Found This Useful

Was this answer helpful?