aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2014-01-13 15:17:24 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2014-01-13 15:17:24 +0200
commit6d001e6e54e6597eb6c4dad9c3e8ca5cfcbb685e (patch)
tree1c287aa5d5a6e7310b8b42d7052440f5c0d15bef
parent2054f0da893a4ca551c6670ba8703480aa48143a (diff)
downloadscripts-6d001e6e54e6597eb6c4dad9c3e8ca5cfcbb685e.tar.gz
scripts-6d001e6e54e6597eb6c4dad9c3e8ca5cfcbb685e.tar.bz2
Improve ec2setup
Introduce configuration file and several new options: -c, -T, -x
-rwxr-xr-xec2snapshot96
1 files changed, 89 insertions, 7 deletions
diff --git a/ec2snapshot b/ec2snapshot
index 1a57b08..c07de55 100755
--- a/ec2snapshot
+++ b/ec2snapshot
@@ -21,6 +21,8 @@ retdays=
eclat_options=
descr=
tags=
+snapshot_tag=
+snapshot_filter=
dry_run=
verbose=0
quiet=
@@ -28,6 +30,7 @@ allfs=
allvol=
fstypes=
vollist=
+dumpconfig=
verbose() {
if test $verbose -gt 0; then
@@ -96,7 +99,7 @@ dev2volume() {
# gettags WHAT ID FLT
gettags() {
- eclat -e 'if (.DescribeTagsResponse)
+ eclat $eclat_options -e 'if (.DescribeTagsResponse)
for (var in .DescribeTagsResponse.tagSet.item)
if ('"$3"')
print(var.key,"=",var.value,"\n");' \
@@ -110,7 +113,7 @@ puttags() {
if [ -n "$dry_run" ]; then
cat -
else
- eclat create-tags $obj $id -T - $*
+ eclat $eclat_options create-tags $obj $id -T - $*
fi
}
@@ -122,9 +125,58 @@ list_snapshots() {
for (var in last.item) {
print(var.snapshotId,"\t",timestamp(var.startTime),"\n");
}
- }' describe-snapshots status=completed volume-id=$1
+ }' describe-snapshots status=completed volume-id=$1 $snapshot_filter
}
+# read_config FILE
+read_config() {
+ if [ ! -e $1 ]; then
+ echo >&2 "$0: configuration file $1 does not exist"
+ exit 1
+ elif [ ! -r $1 ]; then
+ echo >&2 "$0: configuration file $1 is not readable"
+ exit 1
+ fi
+
+ line=0
+ while read KW VALUE
+ do
+ line=$((line + 1))
+ test -z "$KW" && continue
+ case $KW in
+ "#"*)
+ continue;;
+ backup)
+ case $VALUE in
+ allvolumes)
+ allvol=1;;
+ allfs)
+ allfs=1;;
+ fstype:*)
+ fstypes=${VALUE##fstype:};;
+ *)
+ echo >&2 "$0: $1:$line: unrecognized value"
+ exit 1
+ esac;;
+ description)
+ descr=$VALUE;;
+ retention-days)
+ retdays=$VALUE;;
+ retention-snapshots)
+ retcount=$VALUE;;
+ tag)
+ tags="$tags \"$VALUE\"";;
+ snapshot-tag)
+ snapshot_tag=$VALUE;;
+ eclat-options)
+ eclat_options="$eclat_options $VALUE";;
+ *)
+ echo >&2 "$0: $1:$line: unrecognized keyword"
+ exit 1
+ esac
+ done < $1
+}
+
usage() {
progname=$(basename $0)
cat <<EOF
@@ -132,7 +184,9 @@ usage: $progname [OPTIONS] [VOLUME...]
$progname maintains snapshots of a specified EC2 volumes, devices or mountpoints
OPTIONS are:
+ -A create snapshots of all attached volumes
-a create snapshots of all mounted file systems
+ -c FILE read configuration from FILE
-f TYPES create snapshots of mounted file systems of given TYPES
(comma-separated list)
-D TEXT use TEXT as a description for the new snapshot
@@ -141,11 +195,14 @@ OPTIONS are:
-n do nothing, print what would have been done; implies -v
-h print this help list
-r NUMBER retain at most NUMBER of most recent snapshots
+ -T TAG=VALUE mark snapshots with this tag and delete only those expired
+ snapshots that are marked with it
-t TAG[=VALUE] define TAG for the created snapshot; VALUE can refer to
the tags from the VOLUME using the \$NAME notation, e.g.
-t 'source=\$Name'
-v inclrease output verbosity
-q do not print snapshot ID before exit
+ -x dump configuration values and exit
If both -d and -r are given, -d is given precedence over -r, i.e. the
command:
@@ -161,12 +218,13 @@ Send bug reports to <gray@gnu.org>
EOF
}
-while getopts "Aaf:D:d:e:nhr:t:vq" OPTION
+while getopts "Aac:f:D:d:e:nhr:t:vqx" OPTION
do
case $OPTION in
A) allvol=1;;
a) allfs=1;;
f) fstypes=$OPTARG;;
+ c) read_config $OPTARG;;
D) descr=$OPTARG;;
d) retdays=$OPTARG;;
e) eclat_options="$eclat_options $OPTARG";;
@@ -174,18 +232,34 @@ do
verbose=$(($verbose + 1));;
h) usage; exit 0;;
r) retcount=$OPTARG;;
+ T) snapshot_tag=$OPTARG;;
t) tags="$tags \"$OPTARG\"";;
v) verbose=$(($verbose + 1));;
q) quiet=1;;
+ x) dumpconfig=1
+ verbose=$(($verbose + 2));;
*) exit 1
esac
done
+if [ -n "$snapshot_tag" ]; then
+ snapshot_filter="tag:$snapshot_tag"
+ tags="$tags \"$snapshot_tag\""
+fi
+
+if [ $verbose -gt 1 ]; then
+ echo "Configuration:"
+ for var in allvol allfs fstypes descr retdays retcount eclat_options tags snapshot_filter
+ do
+ eval echo "$var=\$$var"
+ done
+fi
+
shift $(($OPTIND - 1))
if [ -n "$allvol" ]; then
getinstid
- set -- $(eclat -e 'if (.DescribeInstancesResponse.reservationSet) {
+ set -- $(eclat $eclat_options -e 'if (.DescribeInstancesResponse.reservationSet) {
for (var in .DescribeInstancesResponse.reservationSet.item) {
for (inst in var.instancesSet.item) {
for (dev in inst.blockDeviceMapping.item) {
@@ -205,6 +279,14 @@ if [ $# -eq 0 ]; then
exit 1
fi
+if [ $verbose -gt 1 ]; then
+ echo "Filesystems: $*"
+fi
+
+if [ -n "$dumpconfig" ]; then
+ exit 0
+fi
+
for filesystem
do
case $filesystem in
@@ -288,12 +370,12 @@ do
fi
if test -n "$tags"; then
- eval $(eclat -e 'if (.DescribeTagsResponse)
+ eval $(eclat $eclat_options -e 'if (.DescribeTagsResponse)
for (var in .DescribeTagsResponse.tagSet.item)
print(var.key,"=",var.value,"\n");' \
describe-tags -v $volume |
sed -re 's/"/\\"/g;s/=(.*)/="\1"/')
- eval $dry_run eclat create-tags -s $snapshot $tags
+ eval $dry_run eclat $eclat_options create-tags -s $snapshot $tags
fi
test -n "$quiet" || echo "$filesystem $volume $snapshot"

Return to:

Send suggestions and report system problems to the System administrator.