-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_backup_snapshot.sh
executable file
·84 lines (71 loc) · 1.74 KB
/
rename_backup_snapshot.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
#!/bin/bash
. net.assortedminorfixes.util_fns.sh
function usage
{
echo "Usage: $0 [-h] [-s snapshot_name] [-p prefix] [-r retention] pool"
echo "-h: This help"
echo "-n: no-act mode, prints what would be done but makes no changes."
echo " Renames a snapshot to match zfsnap format based on its create time and adds a prefix and retention period."
echo " Default snapshot_name is 'backup', prefix is 'tape-' and retention is '1m'."
}
POOL=""
SNAPSHOT_NAME="backup"
PREFIX="tape"
RETENTION="1m"
VERBOSE=yes
OPTSPEC=":hs:p:r:nvq"
while getopts "${OPTSPEC}" option
do
case $option in
h )
usage
exit 0
;;
s )
SNAPSHOT_NAME=${OPTARG}
;;
p )
PREFIX=${OPTARG}
;;
r )
RETENTION=${OPTARG}
;;
n )
NOOP="NO-OP: "
;;
v )
VERBOSE=yes
;;
q )
VERBOSE=
;;
\? )
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
: )
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
*)
if [ "$OPTERR" != 1 ] || [ "${OPTSPEC:0:1}" = ":" ]; then
echo "Non-option argument: '-${OPTARG}'" >&2
fi
;;
esac
done
shift $((OPTIND -1))
POOL="$1"
if [ -z "$POOL" ]
then
usage
exit 1
fi
DATE_FORMAT="${PREFIX}-%Y-%m-%d_%H.%M.%S--${RETENTION}"
BKUP_CTIME=$(zfs get -H -o value creation ${POOL}@${SNAPSHOT_NAME} 2>/dev/null)
[ $? -ne 0 ] && echo "No zfs snapshot by the name ${POOL}@${SNAPSHOT_NAME}" && exit 1
NEW_SNAPSHOT_NAME=$(echo "${BKUP_CTIME}" | date -f - +${DATE_FORMAT})
# Make sure the snapshot is unmounted, or the rename messes things up.
exe umount -f "${POOL}@${SNAPSHOT_NAME}" 2>/dev/null
# Actually rename the snapshot.
exe zfs rename "${POOL}@${SNAPSHOT_NAME}" "${POOL}@${NEW_SNAPSHOT_NAME}"