-
Notifications
You must be signed in to change notification settings - Fork 22
/
pve-autosnap
executable file
·136 lines (114 loc) · 4.22 KB
/
pve-autosnap
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
#!/bin/bash
usage ()
{
echo "Usage: pve-snap --mode [--storage STORAGENAME] [--leave NUMBER] [--sleep=NUMBER]"
echo
echo "Example: pve-snap --weekly --storage=ceph --leave=2 --sleep=10"
echo " or: pve-snap --yearly --storage=pve-data"
echo " or: pve-snap -d -s stor -l 4"
echo
echo "Arguments:"
echo " -d, --daily Run this script in mode for daily autosnapshots"
echo " -w, --weekly Run this script in mode for weekly autosnapshots"
echo " -m, --monthly Run this script in mode for monthly autosnapshots"
echo " -y, --yearly Run this script in mode for yearly autosnapshots"
echo " -s, --storage=STORAGENAME Specify the storage name for which will be used auto snapshots"
echo " (not specified will enable for all storages)"
echo " -l, --leave=NUMBER Specify the number of snapshots which should will leave, anything longer will be removed"
echo " -D, --sleep=NUMBER Specify the modifier for sleep that would create a delay after each snapshot operation"
echo " (calculate as: NUMBER gigabytes per minute, min 1 minute)"
echo
exit
}
loadkeys ()
{
OPTS=`getopt -o dwmysl: --long daily,weekly,monthly,yearly,storage,leave,sleep: -n 'parse-options' -- "$@"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
mode=
storage=
leave=
sleepmodofier=
while true; do
case "$1" in
-d | --daily ) mode=daily; shift ;;
-w | --weekly ) mode=weekly; shift ;;
-m | --monthly ) mode=monthly; shift ;;
-y | --yearly ) mode=yearly; shift ;;
-s | --storage ) storage="$2"; shift; shift ;;
-l | --leave ) leave="$2"; shift; shift ;;
-D | --sleep ) sleepmodifier="$2"; shift; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
if [ "$mode" = "" ] ; then echo -e "\nMode is not set\n"; usage; break; fi
}
gensnapname ()
{
if [ $mode == "daily" ] ; then
snapprefix="autodaily"
snapname="$snapprefix$(date +%y%m%d)"
elif [ $mode == "weekly" ] ; then
snapprefix="autoweekly"
snapname="$snapprefix$(date +%y%m%d)"
elif [ $mode == "monthly" ] ; then
snapprefix="automonthly"
snapname="$snapprefix$(date +%y%m%d)"
elif [ $mode == "yearly" ] ; then
snapprefix="autoyearly"
snapname="$snapprefix$(date +%y%m%d)"
fi
}
getvms ()
{
vms=($(/usr/sbin/qm list | awk '{print$1}' | sed 1d))
}
cleansnapshots ()
{
if [[ "$leave" != "" ]] ; then
snapshots=($(grep -Po "(?<=\[).*(?=\])" "/etc/pve/nodes/$(hostname)/qemu-server/$vm.conf" | grep ^"$snapprefix" | tac))
i=0
for snap in "${snapshots[@]}"
do
if [ "$i" -ge "$leave" ] ; then
echo "vm$vm: Removing snapshot $snap"
/usr/sbin/qm delsnapshot $vm $snap
else
echo "vm$vm: Saving snapshot $snap"
fi
let i++
done
fi
}
sleepsnapshot ()
{
if [ "$sleepmodifier" != "" ] ; then
disksizes=($(sed '/^\[.*\]$/q' /etc/pve/nodes/$(hostname)/qemu-server/$vm.conf | grep "$storage:vm-$vm" | sed -r -e 's/.*size=[^0-9]*//g ; s/[^MG0-9]*//g'))
for disksize in "${disksizes[@]}"
do
if [[ $disksize == *"G" ]] ;
then disksize=($(echo $disksize | sed 's/[^0-9]//g'))
else disksize=$[($(echo $disksize | sed 's/[^0-9]//g'))/1024/1024/1024] ; fi
sleeptime=$[$disksize/$sleepmodifier]
if [ $sleeptime = 0 ] ; then sleeptime=1; fi
sleep ${sleeptime}m
done
fi
}
autosnapshot ()
{
for vm in "${vms[@]}"
do
storagecheck=($(sed '/^\[.*\]$/q' /etc/pve/nodes/$(hostname)/qemu-server/$vm.conf | grep -ci "$storage:[vm-]*$vm"))
if [[ "$storagecheck" > "0" ]] ; then
echo "vm$vm: Creating snapshot $snapname"
/usr/sbin/qm snapshot $vm $snapname
sleepsnapshot
cleansnapshots
fi
done
}
loadkeys $@
gensnapname
getvms
autosnapshot