Last active
May 11, 2017 16:32
-
-
Save rambabusaravanan/db58352d622aad566a90adb67bb3aaa0 to your computer and use it in GitHub Desktop.
Backup MySQL Database, Archive, Filesystem Sync to Amazon S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# src details | |
SRCPATH='/path/of/target' | |
SRCFILE='file-or-directory' | |
# s3 details | |
S3BUCKET='bucketname' | |
S3PATH='path/in/bucket' | |
S3REGION='us-east-1' | |
TMPDIR='/tmp/s3' | |
# variables | |
CURRDATE=$(date +%Y-%m-%d) | |
PREVDATE=$(date +%Y-%m-%d --date='4 week ago') | |
### SCRIPT STARTS ### | |
# make the tmp dir | |
mkdir -pv $TMPDIR | |
# compress target and backup to s3 | |
cd $SRCPATH; | |
echo `date`' Compression Start'; | |
tar --xz -cf $TMPDIR/$SRCFILE.$CURRDATE.tar.xz $SRCFILE | |
echo `date`' Compression Complete'; | |
aws s3 cp $TMPDIR/$SRCFILE.$CURRDATE.tar.xz s3://$S3BUCKET/$S3PATH/ --region $S3REGION | |
aws s3 rm s3://$S3BUCKET/$S3PATH/$SRCFILE.$PREVDATE.tar.xz --region $S3REGION | |
# remove files in tmp dir | |
cd | |
rm -rfv $TMPDIR/$SRCFILE.$CURRDATE.tar.xz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# s3 details | |
S3BUCKET='bucketname' | |
S3PATH='path/in/bucket' | |
SRCDIR='/var/www' | |
### SCRIPT STARTS ### | |
cd $SRCDIR | |
aws s3 sync $SRCDIR s3://$S3BUCKET/$S3PATH/ --delete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Notes: | |
# S3 Bucket's region is needed. You can get the bucket's location by running "aws s3api get-bucket-location --bucket BUCKET" | |
# db details | |
DBHOST='localhost' | |
DBPORT='3306' | |
DBUSER='root' | |
DBPASS='password' | |
DBNAME='databasename' | |
# s3 details | |
S3BUCKET='bucketname' | |
S3PATH='path/in/bucket' | |
S3REGION='us-east-1' | |
TMPDIR='/tmp/s3' | |
# variables | |
CURRDATE=$(date +%Y-%m-%d) | |
PREVDATE=$(date +%Y-%m-%d --date='4 week ago') | |
### SCRIPT STARTS ### | |
# make the tmp dir | |
mkdir -pv $TMPDIR | |
cd $TMPDIR | |
# dump db and backup to s3 | |
mysqldump -h$DBHOST -P$DBPORT -u$DBUSER -p$DBPASS --quote-names --create-options --force $DBNAME > $DBNAME.$CURRDATE.sql | |
gzip $DBNAME.$CURRDATE.sql | |
aws s3 cp $TMPDIR/$DBNAME.$CURRDATE.sql.gz s3://$S3BUCKET/$S3PATH/ --region $S3REGION | |
aws s3 rm s3://$S3BUCKET/$S3PATH/$DBNAME.$PREVDATE.sql.gz --region $S3REGION | |
# remove files in tmp dir | |
cd | |
rm -rfv $TMPDIR/$DBNAME.$CURRDATE.sql.gz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment