July 8, 2016

Backup and Restore MySql backup to/from AWS S3

Steps followed from a Centos 6 machine:

1. yum install s3cmd
2. Created a configuration file .s3cfg under user's home directory.
Defined access and secret keys in the file.
access_key=<access key>
secret_key=<secret key>

3. Created a backup script file:
#!/bin/sh
mysqlpass="your password"
db_schema_name="your db schema"
s3bucket="s3://your bucket name"
user_home="/home/auser"
script_home="/home/auser/mysqlbackups"
backup_file="some.backup.sql.gz"

mysqldump -u root -p$mysqlpass $db_schema_name > $script_home/temp.sql
gzip $script_home/temp.sql
rm -rf $script_home/$backup_file
mv $script_home/temp.sql.gz $script_home/$backup_file
/usr/bin/s3cmd -c $user_home/.s3cfg put $script_home/$backup_file $s3bucket/$backup_file

4. Created a restore script file:
#!/bin/sh
mysqlpass="your password"
db_schema_name="your db schema"
s3bucket="s3://your bucket name"
user_home="/home/auser"
script_home="/home/auser/mysqlbackups"
backup_file="some.backup.sql"

cd $script_home
/usr/bin/s3cmd -c $user_home/.s3cfg get $s3bucket/$backup_file.gz
gzip -d $backup_file.gz
mysql -u root -p$mysqlpass $db_schema_name < $backup_file
rm -rf $backup_file

1 comment:

  1. simply superb,mind blowing, i will share your blog to my friends also
    AWS Online Training

    ReplyDelete