Skip to content

Instantly share code, notes, and snippets.

@aerostitch
Last active September 2, 2016 18:00
Show Gist options
  • Save aerostitch/063cf8399a477ba29747f7cd1153f392 to your computer and use it in GitHub Desktop.
Save aerostitch/063cf8399a477ba29747f7cd1153f392 to your computer and use it in GitHub Desktop.
#!/bin/bash
# packages { ['jq', 'awscli']: ensure => installed }
# cat ~/.aws/config
# [profile ebs_snapshot]
# aws_access_key_id = XXXXXXXXX
# region = us-east-1
# output = json
# aws_secret_access_key = XXXXXXXXXX
export AWS_DEFAULT_PROFILE=ebs_snapshot
export AWS_CONFIG_FILE=${HOME}/.aws/config
# Puppet, mysql, mysqladmin, aws are all in /usr/bin
PATH=/usr/bin:$PATH
instance_id=$(facter -p ec2_instance_id)
snap_date=$(date +'%Y-%m-%d')
snap_time=$(date +'%H:%M:%S')
# to avoid having Puppet restart mysql
puppet agent --disable "EBS snapshot backup as of ${snap_date} ${snap_time}"
# For mariadb multistream replication
mysql -e 'show all slaves status\G'
mysql -e 'stop all slaves;'
mysqladmin status
mysqladmin shutdown
mysqladmin status
while read device ; do
volume=$(aws ec2 describe-volumes --filters "Name=attachment.instance-id,Values=${instance_id}" "Name=attachment.device,Values=${device}" --query 'Volumes[].VolumeId' --output text)
snapshot_id=$(aws ec2 create-snapshot --volume-id $volume --description "${snap_date} ${snap_time} from automated backup script on $(hostname) - ${instance_id}" |jq -r '.SnapshotId')
aws ec2 create-tags --resources $snapshot_id --tags "Key=hostname,Value=$(hostname)" "Key=device,Value=${device}" "Key=vpc,Value=$(facter -p vpc)" "Key=snap_day,Value=${snap_date}"
done < <(/sbin/pvs -o pv_name,lv_path | awk '$2 ~ /\/dev\/lvm/ {sub(/\/dev\/xvd/,"/dev/sd", $1) ; print $1}')
/etc/init.d/mysql start
sleep 30 # the mysql service takes some time to start
mysqladmin status
mysql -e 'start all slaves;'
mysql -e 'show all slaves status\G'
puppet agent --enable
puppet agent -t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment