#!/bin/bash #----------------------------------------------------------------------------- # ec2-rsync.sh # Creates an Amazon Machine Instance (AMI) and attaches my Elastic Block # Storage (EBS) volume to it. Then runs rsync to upload the specified # directories to the volume for backup. # NOT IMPLEMENTED YET: When complete the volume is then # copied to an S3 Snaphot which is more robust than a normal EBS volume. # # The volume is then detached and the AMI shutdown. #----------------------------------------------------------------------------- # aws uses curl for all communication with the Amazon servers. It must be able # to find a CA certificate bundle to authenticate the server. export CURL_CA_BUNDLE=/etc/ssl/certs/cacert.pem # This is the name of the image I will be using. It is an ubuntu 8.10 # intrepid base image. #export EC2_IMAGE=ami-2810385c export EC2_IMAGE= # This is my single EBS volume identifier. I only have one at the moment. # Not sure how to manage more than one in the future? export EC2_VOLUME= # Create a running instance, parsing the output to find the instance # identifier and host name. export EC2_INSTANCE=`aws run-instances --wait $EC2_IMAGE \ -z eu-west-1a -k gsg-keypair | \ sed -e 's/ *//g' | tr '\t' '\n' | grep -m 1 '^i-'` echo "Instance started with ID $EC2_INSTANCE" export EC2_HOST=`aws describe-instances $EC2_INSTANCE | \ sed -e 's/ *//g' | tr '|' '\n' | grep amazonaws.com` echo "Host name is $EC2_HOST" # this is the sed script required to extract the RSA fingerprint from the # console output RSA_FP=`aws get-console-output $EC2_INSTANCE | \ grep '/etc/ssh/ssh_host_rsa_key.pub (RSA)' | \ sed 's/^ec2: 2048 //' | \ sed 's| /etc/ssh/ssh_host_rsa_key\.pub (RSA)||'` while [ -z "$RSA_FP" ] do echo "No console output yet, retrying..." sleep 30 RSA_FP=`aws get-console-output $EC2_INSTANCE | \ grep '/etc/ssh/ssh_host_rsa_key.pub (RSA)' | \ sed 's/^ec2: 2048 //' | \ sed 's| /etc/ssh/ssh_host_rsa_key\.pub (RSA)||'` done export RSA_FP echo "The RSA fingerprint of the new server is $RSA_FP" # attach the EBS volume under /dev/sdh. Sometimes this isn't # instant so sleep for 30s to give it time aws attach-volume $EC2_VOLUME -i $EC2_INSTANCE -d /dev/sdh sleep 30 # ssh in to create a mount point for the volume ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \ -i ~/.ec2/id_rsa-gsg-keypair root@$EC2_HOST \ "mkdir /mnt/store && mount /dev/sdh /mnt/store" # rsync the specified directory. Consider parameterising this/these locations # into a config file. rsync -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /home/gordon/.ec2/id_rsa-gsg-keypair" \ -avz --delete-after \ /shares/internal/photos/ \ root@$EC2_HOST:/mnt/store/photos/ > rsync.log ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \ -i ~/.ec2/id_rsa-gsg-keypair root@$EC2_HOST \ "umount /mnt/store" # detach the volume and shutdown the instance aws detach-volume $EC2_VOLUME -i $EC2_INSTANCE aws terminate-instances $EC2_INSTANCE echo "Terminated instance with ID $EC2_INSTANCE" # create a snapshot of the volume.