aboutsummaryrefslogtreecommitdiff
path: root/ec2snapshot
blob: ba48688a604dc847f3efa906970f1094c8023448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#! /bin/sh
# This file is part of Eclat 
# Copyright (C) 2012-2014 Sergey Poznyakoff
#
# Eclat is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# Eclat is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Eclat.  If not, see <http://www.gnu.org/licenses/>.

set -e
retcount=
rettime=
eclat_options=
descr=
tags=
snapshot_tag=
snapshot_filter=
dry_run=
verbose=0
quiet=
allfs=
allvol=
fstypes=
fslist=
vollist=
dumpconfig=
excludelist=

verbose() {
    if test $verbose -gt 0; then
	echo "$0: $*"
    fi
}

# runeclat RUNID ARGS FORMAT CMD ARGS...
runeclat() {
    local runid="$1" v vars="$2" fmt
    shift 2
    fmt="$1"'else if (.Response.Errors) {
               error("Error '$runid': ",.Response.Errors.Error.Message,"\n");
               exit(1);
            } else {
               error("Unrecognized response:\n");
               dump(.);
               exit(1);
            }'
    shift
    ret=$(eclat $eclat_options --format-expr "$fmt" $*)
    if [ $? -ne 0 ]; then
	echo >&2 "$0: $runid: eclat failed"
	exit 1
    fi
    set -- $ret
    for v in $vars
    do
	if test $# -eq 0; then
	    break
	fi
	eval $v=\$1
	shift
    done
    REST=$*
}			

getinstid() {
    if test -z "$instid"; then
	instid=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
	if test -z "$instid"; then
	    echo >&2 "$0: cannot determine instance ID"
	    exit 1
        fi
    fi
}

dev2volume0() {
    unset volume
    runeclat "getting volume ID for $1" "volume" \
	  'if (.DescribeInstancesResponse.reservationSet) {
             for (var in .DescribeInstancesResponse.reservationSet.item) {
               for (inst in var.instancesSet.item) {
                 for (dev in inst.blockDeviceMapping.item) {
                   if (dev.deviceName == "'$1'")
                     print(dev.ebs.volumeId,"\n");
                 }
               }
             }
           }' describe-instances $instid
}

dev2volume() {
    getinstid
    dev2volume0 $1
    if [ -z "$volume" ]; then
        case $1 in
        /dev/xv*) dev2volume /dev/s${1##/dev/xv}
        esac
    fi
}

fs2volume() {
    local dev
    dev=$(awk '$2=="'$1'" { print $1; exit }' /etc/mtab)
    if test -z "$dev"; then
        echo >&2 "$0: $1 is not a mountpoint"
        exit 1
    fi
    dev2volume $dev
    if [ -z "$volume" ]; then
        dev2volume ${dev%%[0-9]}
    fi
}

# gettags WHAT ID FLT
gettags() {
    eclat $eclat_options -e 'if (.DescribeTagsResponse)
		 for (var in .DescribeTagsResponse.tagSet.item)
                    if ('"$3"')
		       print(var.key,"=",var.value,"\n");' \
	  describe-tags $1 $2 
}

# puttags WHAT ID [NAME[=VALUE]...]
puttags() {
    local obj=$1 id=$2
    shift 2
    if [ -n "$dry_run" ]; then
	cat -
    else
	eclat $eclat_options create-tags $obj $id -T - $*
    fi
}

# list_snapshots VOLUME
list_snapshots() {
    eclat $eclat_options -e '
     if (.DescribeSnapshotsResponse.snapshotSet) {
       sort(last,".item.startTime","t");
       for (var in last.item) {
        print(var.snapshotId,"\t",timestamp(var.startTime),"\n");
       }
     }' 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:};;
                    /*) 
                        fslist="$VALUE";;
		    *)
		        echo >&2 "$0: $1:$line: unrecognized value"
			exit 1
                esac;;
            exclude)
		excludelist=$VALUE;;
	    description)
		descr=$VALUE;;
            retention-days)
		rettime=$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
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
    -d DAYS        retain snapshots not older than DAYS from now
    -e OPT         additional options for eclat(1).
    -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:

   $progname -d 14 -r 20 vol-feedbeef

will retain 20 most recent snapshots taken during the last 14 days.  This
may be useful if several snapshots are taken daily.

For help regarding eclat configuration, see eclat(1) and eclat.conf(5).

Send bug reports to <gray@gnu.org>
EOF
}

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) rettime=$OPTARG;;
    e) eclat_options="$eclat_options $OPTARG";;
    n) dry_run=echo
       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	

test -n "$rettime" && rettime=$(($rettime * 86400))

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 rettime retcount eclat_options tags snapshot_filter
    do
	eval echo "$var=\$$var"
    done
fi

shift $(($OPTIND - 1))

if [ -n "$allvol" ]; then
    getinstid
    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) {
                     print(dev.ebs.volumeId," ");
                   }
                 }
               }
             }' describe-instances $instid)
elif [ -n "$allfs" ]; then
    set -- $(grep ^/dev /etc/mtab | awk '$3 != "swap" { print $2 }')
elif [ -n "$fstypes" ]; then
    set -- $(mount -t "$fstypes" | awk '{print $3}')
elif [ -n "$fslist" ]; then
    set -- $fslist
fi

excludevol=
excludefs=
for x in $excludelist
do
    case $x in
    /dev/*)     dev2volume $x;;
    /*)         fs2volume $x;;
    *)          volume=$x;;
    esac
    if [ -z "$volume" ]; then
        excludefs=":$x:$excludefs"
    else
        excludevol=":$volume:$excludevol"
    fi
done

if [ $# -eq 0 ]; then
    echo >&2 "$0: no volume ID"
    exit 1
fi

if [ $verbose -gt 1 ]; then
    pfs=
    for arg in $*
    do
        if $(echo "$excludefs" | grep -q ":$arg:"); then
            continue
        fi
        pfs="$pfs $arg"
    done
    echo "Filesystems: $pfs"
    echo "Exclude volumes: $excludevol"
fi

if [ -n "$dumpconfig" ]; then
    exit 0
fi    

for filesystem
do
    if $(echo "$excludefs" | grep -q ":$filesystem:"); then
        continue
    fi
    case $filesystem in
    /dev/*)     dev2volume $filesystem;;
    /*)         fs2volume $filesystem
                if [ -z "$volume" ]; then
	            verbose "ignoring unrecognized filesystem $filesystem"
                    continue
                fi;;
    *)		volume=$filesystem;;
    esac

    if [ -z "$volume" ]; then
	if [ -n "$allfs$fstypes" ]; then
	    verbose "ignoring unrecognized filesystem $filesystem"
	    continue
	else
	    echo >&2 "$0: $filesystem: no corresponding volume"
	    exit 1
	fi
    fi

    if $(echo "$excludevol" | grep -q ":$volume:"); then
        continue
    fi
    if $(echo "$vollist" | grep -q ":$volume:"); then
	continue
    fi
    vollist=":$volume:$vollist"

    remlist=
    if test -n "${retcount}$rettime"; then
	verbose "listing available snapshots of $filesystem ($volume)"
	snapshots=$(list_snapshots $volume)

        if test -n "$rettime"; then
            now=$(date +%s)
            remlist="$remlist"$(echo "$snapshots" | while read id ts
            do
		    test -z "$ts" && break
                    test $(($now - $ts)) -lt $rettime && break
	            echo $id
            done)
	    set -- $(echo "$snapshots" | cut -f 1)
	    count=$(echo "$remlist" | wc -l)
	    test $count -gt 1 && shift $count
	    snapshots=$(echo $* | tr ' ' '\n')
	else
	    snapshots=$(echo "$snapshots" | cut -f 1)
        fi
    
        if test -n "$retcount"; then
            count=$(echo "$snapshots" | wc -l)
	    if test $count -gt $retcount; then
		remlist="$remlist "$(echo "$snapshots" |
		                     head -n $(($count - $retcount)))
            fi
        fi
    fi

    for id in $remlist
    do
        verbose removing $id
        $dry_run eclat $eclat_options delete-snapshot $id
    done    

    verbose "creating a snapshot of $filesystem ($volume)"

    if test -z "$dry_run"; then
        runeclat "getting snapshot ID" "snapshot" \
          'if (.CreateSnapshotResponse) print(last.snapshotId,"\n");' \
           create-snapshot $volume $descr
	if [ -z "$snapshot" ]; then
	    echo >&1 "$0: failed creating snapshot for $filesystem ($volume)"
	    exit 1
	fi
    else
        snapshot=snap-01234567
    fi

    if test -n "$tags"; then
        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 $eclat_options create-tags -s $snapshot $tags
    fi    
       
    test -n "$quiet" || echo "$filesystem $volume $snapshot"
done

Return to:

Send suggestions and report system problems to the System administrator.