Created
August 29, 2013 02:47
-
-
Save jwoffindin/6373731 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
#!/usr/bin/env zsh | |
# Given a list of instance-id in an AWS region, it makes a snapshot of the root | |
# volume (assumed to be /dev/sda!) and copies it to the target region. | |
# | |
# The root volume is remounted 'ro' on each instance - so this script must | |
# be run from somewhere that can SSH to the server (possibly may require | |
# special firewall rules to be applied). | |
# | |
# Output is written to ${OUTPUT_FILE} | |
set -e | |
CERT=$HOME/.ssh/aws/orel-migration-test.pem | |
SSH_USER="root" # Set this to login account associated with CERT | |
SOURCE_REGION="us-east-1" | |
TARGET_REGION="ap-southeast-2" | |
OUTPUT_FILE="source-instance-snapshots.csv" | |
for INSTANCE_ID in "$@" ; do | |
echo "Processing ${INSTANCE_ID}" | |
INFO=$(ec2-describe-instances ${INSTANCE_ID} --region ${SOURCE_REGION}) | |
INSTANCE_IP=$(echo ${INFO} | awk '/^INSTANCE/ { print $14 }') | |
if [[ -z $INSTANCE_IP ]] ; then | |
echo "unable to get IP (got ${INSTANCE_IP})" | |
exit | |
fi | |
VOLUME_ID=$(echo $INFO | awk '$1 == "BLOCKDEVICE" && $2 == "/dev/sda" { print $3 }') | |
if [ -z "$VOLUME_ID" ] ; then | |
echo "unable to get volume id" | |
exit | |
fi | |
echo " remounting / read-only" | |
ssh -i ${CERT} -tt ${SSH_USER}@${INSTANCE_IP} sudo mount -f -o ro,remount / | |
echo " creating snapshot" | |
SNAPSHOT_ID=$(ec2-create-snapshot ${VOLUME_ID} --region ${SOURCE_REGION} | cut -f 2) | |
echo " remounting rw" | |
ssh -i ${CERT} -tt ${SSH_USER}@${INSTANCE_IP} sudo mount -f -o rw,remount / | |
echo " triggering copy to target region" | |
COPIED_SNAPSHOT_ID=$(ec2-copy-snapshot -s ${SNAPSHOT_ID} --source-region ${SOURCE_REGION} --region ${TARGET_REGION} -d "Migrated snapshot ${INSTANCE_ID} ${VOLUME_ID}" | awk '/SNAPSHOT/ { print $2 }') | |
echo " deleting source snapshot" | |
ec2-delete-snapshot ${SNAPSHOT_ID} --region ${SOURCE_REGION} | |
echo " completed ${INSTANCE_ID}" | |
echo ${SOURCE_REGION},${INSTANCE_ID},${INSTANCE_IP},${VOLUME_ID},${COPIED_SNAPSHOT_ID} >> $OUTPUT_FILE | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment