Skip to content

Instantly share code, notes, and snippets.

@srkiNZ84
Last active August 30, 2017 20:14
Show Gist options
  • Save srkiNZ84/7380acc9260b54ba99b7 to your computer and use it in GitHub Desktop.
Save srkiNZ84/7380acc9260b54ba99b7 to your computer and use it in GitHub Desktop.
Shell script to backup MySQL database to AWS Glacier, using GPG for encryption
#!/bin/bash
DBUSER=[db user here]
DBPW=[db pass here]
DBNAME=[db name here]
GPGID=[gpg email here]
GLACIER_VAULT=[aws glacier vault here]
WORKDIR=/tmp
DATETIME=`date +%F-%H%M%S-%z`
BACKUPNAME=$DATETIME-backup.sql
cd $WORKDIR
/usr/bin/mysqldump -u $DBUSER -p$DBPW $DBNAME > $BACKUPNAME
# NOTE: The encryption bit assumes that:
# * we've imported the recipients key and marked it as "trusted"
# * we have access to the corresponding private key in order to decrypt the file (i.e. it's our key)
# Assuming the key id is known and the key is published to a public key server, importing can be done with:
# gpg2 --keyserver [keyserver name] --recv-keys [key id]
# e.g. gpg2 --keyserver pgp.mit.edu --recv-keys 00B3AF77
# Marking the key trusted can be accomplished with:
# * $ gpg2 --edit-key {recipient email address}
# * > trust
# * > 5 (select 5 if you ultimately trust the key)
# * > save
gpg2 --recipient $GPGID --encrypt $BACKUPNAME
# NOTE: The upload to Glacier assumes that we've run "aws configure" to setup ACCESS keys etc...
#aws s3 cp $MD5SUM-$BACKUPNAME.gpg s3://$S3BUCKET_NAME/
aws glacier upload-archive --account-id - --body $BACKUPNAME --vault-name $GLACIER_VAULT --archive-description "$BACKUPNAME"
# Cleanup temporary files
rm -f $BACKUPNAME $BACKUPNAME.gpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment