Created
April 15, 2016 12:44
-
-
Save ymcdull/b54ef56879d61235bb60dd098445bbea to your computer and use it in GitHub Desktop.
ebs snapshot
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
#!/bin/bash | |
# ebs-snapshot.sh. Performs and validates EBS snapshots. | |
# snapshot date format 20111118-2305 | |
snapshot_date=`date +s%Y%m%d-%H%M` | |
# Set name of script | |
progname=${0##*/} ## Get the name of the script without its path | |
# Print script usage information. -b can be a single EBS volume ID or a comma separated list of volume IDs | |
print_usage() { printf "USAGE: ./%s -b Volume1,volume2" "$progname."} | |
# Exit with error if there are no command-line arguments given | |
if [ -z "$*" ]; then print_usage ; exit 1; fi | |
# Print error message and exit with error code. | |
# Usage: die [errno [message]] | |
die() { | |
error=${1:-1} ## exits with 1 if error number not given | |
shift | |
[ -n "$*" ] && | |
printf "%s%s: %s\n" "$scriptname" ${version:+" ($version)"} "$*" >&2 | |
exit "$error" | |
} | |
# Main snapshot function, takes 1 argument, the volume ID, and outputs the corresponding snapshot ID into /tmp/last_snapshot | |
ebs_snapshot() { | |
# ${variable%\\n} is the shell equivalent of CHOMP. | |
ec2-create-snapshot -d backup-"$1"-"${snapshot_date%\\n}" |awk '{print $2}' > /tmp/"$1"-last || die "Unable to take snapshot of volume $1" | |
} | |
# Check the snapshot status. | |
snapshot_status() { | |
ec2-describe-snapshots -C <%= aws-cert-file %> -K <%= aws-key-file %> $1 | |
} | |
# Define the required and optional command-line options for processing by getopts | |
optstring=b: | |
# Process command-line arguments | |
while getopts $optstring opt | |
do | |
case $opt in | |
b) ebs_volumes=$OPTARG ;; | |
*) print_usage ; exit 1 | |
esac | |
done | |
# Main program loop. Performs a snapshot on each volume passed to it. | |
for i in `echo $ebs_volumes |sed 's/\,/ /g'` do | |
ebs_snapshot $i | |
completed=false | |
until [ $completed = "true" ]; do | |
snapshot_status `cat /tmp/"$i"-last` |grep completed | |
if [ $? = 0 ]; | |
completed="true" | |
elif [ $? = 1 ]; | |
completed = "false" | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment