Last active
April 21, 2018 03:08
-
-
Save craigderington/64a5e0841253e3e78b8ac66b6d565267 to your computer and use it in GitHub Desktop.
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/bash | |
# Pretty Welcome Message | |
echo -e " *************************************************** " | |
echo -e " *************************************************** " | |
echo -e " Amazon S3 Restore MySQL Databases " | |
echo -e " 4-18-2017 - Craig Derington " | |
echo -e " GitHub @craigderington " | |
echo -e " " | |
echo -e " *************************************************** " | |
echo -e " *************************************************** " | |
# Basic s3cmd vars | |
mysqlpass="" | |
bucket="s3://<s3_bucket_name>" | |
# Output S3 Info for User Feedback | |
echo -e "Retrieving AWS S3 Bucket for: $bucket" | |
# List the contents of our S3 bucket | |
last_backup=`s3cmd ls -l $bucket | sort -r | sed -n -e '2{p;q;}'` | |
# Last backup Found | |
echo "Last Backup Found: " | |
echo $last_backup | |
# Strip the S3 Endpoint from our last_backup variable | |
s3path=${last_backup//DIR} | |
echo "S3 Path: " | |
echo $s3path | |
# Clean the S3 path and strip the bucket name from the front of the path to leave only the S3 folder | |
s3folder=${s3path//'$bucket'} | |
s3folder=${s3folder%/} | |
s3folder="$(echo -e "${s3folder}" | sed -e 's/^[[:space:]]*//')" | |
echo "S3 Folder: " | |
echo $s3folder | |
# Create a temporary directory to store downloaded backup files | |
tmp_dir=$(mktemp -d) | |
cd $tmp_dir | |
echo "Temporary Directory Created to Store S3 Downloads: " | |
echo $tmp_dir | |
# Download all the snapshots | |
echo -e "Downloading Files..." | |
s3cmd ls "$bucket"/"$s3folder"/ | grep -o "s3://.*\.sql\.gz" | xargs -n 1 -P 10 -I {} s3cmd get {} . | |
# Unzip all the files | |
ls | grep "sql.gz" | xargs -n 1 -P 10 -I {} gunzip {} . | |
# Run each file through mysql | |
find . -name '*.sql' | awk '{ print "source",$0 }' | mysql -u root -p$mysqlpass --batch | |
# Purge downloaded backup files | |
echo "Purging downloaded back up files, please wait..." | |
rm -rf $tmp_dir | |
echo "Finished deleting backup archives." | |
# Echo Success or Failure | |
echo "Finished Restoring MySQL databases. You should login to your MySQL Server to verify the validity of your backup." | |
echo "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment