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,

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
yum install s3cmd
Alternatively, you can download it from the url pasted below:

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,

Step5) Enable daily backup from WHM. Refer the url pasted below for reference,
If backup is already configured, then we can know the location of the backup using the command,

root@server:~#grep BACKUPDIR /etc/cpbackup.conf
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/

Step6) Create log directories,

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

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

root@server:~# cd /root
root@server:~# nano

paste the following code

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

##Directory which needs to be backed up
##Name of the backup in bucket

#Clear the logs if the script is executed second time

##Uploading the daily backup to Amazon s3
s3cmd sync --delete-removed ${SOURCE} s3://${DESTINATION}/ 1>>${ACTIVITYLOG} 2>>${ERRORLOG}

##Sent email alert
if [ $ret2 -eq 0 ];then
msg1="Amazon s3 Backup Uploaded Successfully"
msg1="Amazon s3 Backup Failed!!\n Check ${ERRORLOG} for more details"
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/

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

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

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


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

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?