Skip to content

Instantly share code, notes, and snippets.

@lopnor
Created August 22, 2011 04:24
Show Gist options
  • Save lopnor/1161651 to your computer and use it in GitHub Desktop.
Save lopnor/1161651 to your computer and use it in GitHub Desktop.
#!/bin/sh
# see also: http://d.hatena.ne.jp/rx7/20100606/p1
source /etc/profile.d/aws-apitools-common.sh
if [ -r $HOME/.snapshotrc ]
then
source $HOME/.snapshotrc
fi
if [ -z "$PK" ]
then
echo "PK is not defined" >&2;
exit 1;
fi
if [ -z "$CERT" ]
then
echo "CERT is not defined" >&2;
exit 1;
fi
if [ -z "$GENERATIONS" ]
then
echo "GENERATIONS is not defined" >&2;
exit 1;
fi
region=`ec2-metadata|grep placement|cut -d' ' -f2|sed 's/[a-z]$//'`
instance=`ec2-metadata|grep instance-id|cut -d' ' -f2`
vol=`ec2-describe-instances -K $PK -C $CERT --region $region $instance|grep 'vol-'|awk '{print $3}'`
snapshot=`ec2-create-snapshot -K $PK -C $CERT --region $region -d 'backup by snapshot.sh' $vol|awk '{print $2}'`
echo '[took snapshot]' $snapshot
count=1
existings=`ec2-describe-snapshots -K $PK -C $CERT --region $region |grep $vol|grep 'backup by snapshot.sh'|sort -k5 -r|awk '{print $2}' | grep -v $snapshot`
for target in $existings;
do
if [ $count -lt $GENERATIONS ]
then
echo '[keeping] ' $target
else
echo '[deleting] ' $target
ec2-delete-snapshot -K $PK -C $CERT --region $region $target > /dev/null 2>&1
fi
count=`expr ${count} + 1`
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment