Skip to content

Instantly share code, notes, and snippets.

@awaddell
Last active February 15, 2016 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awaddell/b1bc477d265e705a4e65 to your computer and use it in GitHub Desktop.
Save awaddell/b1bc477d265e705a4e65 to your computer and use it in GitHub Desktop.
Backup RDS automatic snapshots with manual ones that don't get deleted along with the instance
#!/bin/bash
: "${mysqlhost:?Need to set mysqlhost}"
# example JMESPath input to --query
# 'DBSnapshots[?starts_with(DBSnapshotIdentifier,`rds:mysql-master-2016-02-14`)==`true`]'
mydate=`date -u +%Y-%m-%d`
region=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/\(.*\)[a-z]/\1/'`
querypre='DBSnapshots[?starts_with(DBSnapshotIdentifier,'
querypost=')==`true`].DBSnapshotIdentifier'
query=$querypre\`rds:$mysqlhost-$mydate\`$querypost
# echo "using query: $query"
autosnapshot=`aws --region $region rds describe-db-snapshots \
--snapshot-type "automated" \
--db-instance-identifier $mysqlhost \
--query $query \
--output text`
if [ -v autosnapshot ]; then
echo "found $autosnapshot"
autosnapshotcopy=`echo "$autosnapshot" | sed 's/rds://' | sed 's/$/-copy/'`
echo "copying to $autosnapshotcopy"
aws --region $region rds copy-db-snapshot \
--source-db-snapshot-identifier $autosnapshot \
--target-db-snapshot-identifier $autosnapshotcopy
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment