Quantcast
Viewing all articles
Browse latest Browse all 31

Backing Up a VPS cPanel Server to Amazon S3 Bucket

Some webhosts offer remote backup storage at a cost, some do random weekly backups and don't like to talk about it - but will do a restore on request at a cost. With cheap available cloud service at hand why not shift that back up data totally offsite up to the cloud. Here is a tutorial to script a linux cPanel VPS backup to an Amazon S3 bucket.

Create the Bucket

First thing you need to do is set up your Amazon S3 service and create a bucket to dump stuff in. Get your secret key ID and Access ID logged down for later use.

Install Amazon S3 CLI Tools

Next thing to do is to install Amazon S3 command line tools on to your cPanel Server, you will need access to the command line and need to add the repository that the tools are in - this guide is based on the Cent OS system.

Go to the repository directory

cd /etc/yum.repos.d

Get the S3 Command Line Tools Repo

wget http://s3tools.org/repo/CentOS_5/s3tools.repo

This will add the s3tools.repo

Install the S3 Tools

yum install s3cmd

Image may be NSFW.
Clik here to view.
installing s3 tools to cpanel

Configure the Tools

s3cmd --configure

This is where you need your access key ID and secret key and then a encryption password which you need to set.  You will need to make a call on whether you use an encypted transfer or not, pay off being the encrypted transfer will take longer.

You are also asked to verify the path to gpg program it prompts the default which in most cases is correct

/usr/bin/gpg

After you have added in your details run the test to verify the connection and save the settings. If you need to edit the settings at a later date just run the s3cmd --configure command again. The configuraton file is saved in roots home.

/root/.s3cfg

Using the s3cmd tool

List your S3 buckets

s3cmd ls

Make a new bucket

s3cmd mb s3://bucketname

To see all the options and parameters

s3cmd --help

Enable WHM/cPanel Backups

If you haven't already got it going, enable the backups.

Image may be NSFW.
Clik here to view.
cpanel-whm-configure-backup

This will save your backups on your cPanel server by default at

/backup/cpbackup/

And then in daily/weekly and monthly directories, depends on what freqency was enabled as above in the 'Backup Rentention' options.

Getting the BackUp Script in Order

First up is to make a space for the log files so you can see the successes and failures

mkdir /var/log/backuplogs

Save the script as

/root/dailybackup.sh

In the script pasted below, the backup rotation degree is set as 2(“DEGREE=2″ ,). This means that, 2 days old backup will be deleted automatically. You can increase this backup retention period by adjusting the “DEGREE” variable.

This script is set to copy the backups which cPanel generates for the daily backup from the directory below (the script is not mine it belongs to the Jackal all kudos to him on that).

/backup/cpbackup/daily

The script:

 

#!/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=`date +%F`

##Backup degree

DEGREE=2

#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://Backup_daily/${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://Backup_daily/${DELETENAME} 1>>${ACTIVITYLOG} 2>>${ERRORLOG}
 

What you need to change in the script:

  • email address
  • source (if not /backup/cpbackup/daily
  • S3 bucket name - which is in the script in 2 places

Set the correct permissions on the script

chmod u+x /root/dailybackup.sh

To run it daily, copy it to cron

cp -p /root/dailybackup.sh /etc/cron.daily/

To run it after the regular cPanel back up, create a file

/scripts/postcpbackup

With the content

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

The final thing to do is to create a symbolic link of the s3cmd configuration to the root level

ln -s /root/.s3cfg /.s3cfg

That should do it check your bucket after and you should see your backups - if not check the error log and see what went wrong:

/var/log/backuplogs/

Tags:


Viewing all articles
Browse latest Browse all 31

Trending Articles