Backup to Amazon S3 - ON CENTOS 6.x

Hello Again !



This code will help you make mysql db backup and then upload it to AMAZON S3 Bucket, Inside your bucket create a folder called database





yum install s3cmd -y - If you can't install s3cmd then please follow the instructions


cd /etc/yum.repos.d

touch s3cmd.repo

nano s3cmd.repo



AND COPY THE CODE BELOW -


```php #

Save this file to /etc/yum.repos.d on your system

and run “yum install s3cmd”


[s3tools]

name=Tools for managing Amazon S3 - Simple Storage Service (RHEL_6)

type=rpm-md

baseurl=Index of /repo/RHEL_6

gpgcheck=1

gpgkey=http://s3tools.org/repo/RHEL_6/repodata/repomd.xml.key

enabled=1

```



Try Above YUM Command again - This time it will install S3CMD, Once installation is completed try running the following command - It will configure your S3CMD using your AMAZON KEY AND Access ID.


s3cmd --configure






#!/bin/bash
S3_BUCKET=YOUR BUCKET NAME HERE
DATE=`date +%d%m%Y_%H%M`
BACKUP_LOC=/home/admin/user_backups/$DATE

mysql_backup(){
mkdir $BACKUP_LOC
mysqldump -uUSERNAME -pPASSWORD DBNAMEHERE > $BACKUP_LOC/databasename_
$DATE.sql

s3cmd ls s3://$S3_BUCKET/database/$DATE > /tmp/log.txt
grep -lr "$DATE" /tmp/log.txt
if [ $? -ne 0 ]
then
mkdir /tmp/$DATE
s3cmd put -r /tmp/$DATE s3://$S3_BUCKET/datbase/

s3cmd sync -r $BACKUP_LOC s3://$S3_BUCKET/database/$DATE/

else
s3cmd sync -r $BACKUP_LOC s3://$S3_BUCKET/database/$DATE/

fi

}
mysql_backup
exit 0