forked from aXon/ZFS-snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zfs_snap.sh
executable file
·103 lines (93 loc) · 2.45 KB
/
zfs_snap.sh
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
#!/usr/bin/env bash
#
# zfs_snap.sh
# Copyright 2012 Nils Bausch
# License: GNU GPL Version 3 or later
#
# take ZFS snapshots with a time stamp
usage () {
[ "$#" -ne 0 ] && echo "$@" >&2
echo "Usage: $(basename $0) [-h] [-d presetName] [-l labelprefix] [-r <num>]" >&2
exit 2
}
# property used to check if auto updates should be made or not
SNAPSHOT_PROPERTY_NAME="com.sun:auto-snapshot"
SNAPSHOT_PROPERTY_VALUE="true"
[ "$#" -eq 0 ] && usage
PRESET=
LABELPREFIX="Automatic"
LABEL="$(date +"%Y%m%d-%H%M%S")"
RETENTION=10
while [ "$#" -gt 0 ]; do
case "$1" in
-h | --help)
usage
;;
-d | --default)
[ "$#" -ge 2 ] || usage "Missing argument"
shift
PRESET="$1"
;;
-l | --label)
[ "$#" -ge 2 ] || usage "Missing argument"
shift
USER_PREFIX="$1"
;;
-r | --retention)
[ "$#" -ge 2 ] || usage "Missing argument"
shift
USER_RETENTION="$1"
;;
*)
echo "Unknown argument: $1" >&2
usage
;;
esac
shift
done
# go through possible presets if available
if [ -n "$PRESET" ]; then
case "$PRESET" in
hourly)
LABELPREFIX="h"
RETENTION=48
;;
daily)
LABELPREFIX="d"
RETENTION=14
;;
weekly)
LABELPREFIX="w"
RETENTION=4
;;
monthly)
LABELPREFIX="m"
RETENTION=12
;;
yearly)
LABELPREFIX="y"
RETENTION=5
;;
*) echo "Unknown default option" >&2
exit 1
;;
esac
fi
# if these variables are set they will override the preset
[ -z "${USER_PREFIX+x}" ] || LABELPREFIX="$USER_PREFIX"
[ -z "${USER_RETENTION+x}" ] || RETENTION="$USER_RETENTION"
# available datasets for backup
readarray -t datasets < <(zfs list -H -o name)
#TAKE SNAPSHOTS
for dataset in "${datasets[@]}"; do
# get value of auto-snapshot property, either true or false
VALUE="$(zfs get "$SNAPSHOT_PROPERTY_NAME" "$dataset" -H -o value)"
if [ "$VALUE" = "$SNAPSHOT_PROPERTY_VALUE" ]; then
zfs snapshot "$dataset@$LABELPREFIX-$LABEL"
fi
done
#DELETE SNAPSHOTS
for dataset in "${datasets[@]}"; do
# grep for prefix and a dash to not accidentally match anything else
zfs list -t snapshot -H -o name | grep "$dataset@$LABELPREFIX-" | sort -r | tail -n +$((RETENTION+1)) | xargs -n 1 -r zfs destroy -r
done