-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvzdump_hook.sh
More file actions
executable file
·218 lines (188 loc) · 5.51 KB
/
vzdump_hook.sh
File metadata and controls
executable file
·218 lines (188 loc) · 5.51 KB
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
#!/bin/bash
#
# Proxmox VZDump hook script for managing disk throttles for backups.
#
# Author: RobHost
# License: MIT
# Version: 0.2, reldate 2016-08-31
# Repo: https://github.com/robhost/proxmox-scripts
#
# This script is intended to be used as a hook script for the Proxmox
# VZDump utility. It applies throttling for backup or removes all disk
# throttles before the dump starts and restores the original config
# after the dump has finished.
#
# In order to use it, use the configuration directive "script" of the
# vzdump utility. This can be done for scheduled backups by putting
# "script: /path/to/this/script" in /etc/vzdump.conf. Don't forget to
# set executable permission for the script file.
#
# If you don't put a vzdump_hook.conf next to the script, all throttles
# will be removed. If you want to raise the throttle for the backup,
# put the desired config options into the conf file, for instance:
#
# iops_rd=1000
# iops_rd_max=10000
#
#
# This script has been tested and used on Proxmox 4.3 and 4.2.
#
# Changelog:
# 0.3, reldate 2017-02-10 - RobHost
# - add support for backup throttle
# 0.2, reldate 2016-08-31 - RobHost
# - add noop output
# 0.1, reldate 2016-08-30 - RobHost
# - hello world
declare -a BACKUP_THROTTLE
CONFFIGPATH="$(dirname "$(realpath "$BASH_SOURCE")")/vzdump_hook.conf"
if [[ -r $CONFFIGPATH ]]
then
BACKUP_THROTTLE=($(< "$CONFFIGPATH"))
fi
# Find config directives of throttled storage for the VM with the given
# vmid. Echoes a line with spaces removed for each found directive.
_get_throttled_storage() {
local vmid="$1"
while read -r cid opts
do
[[ "$cid" =~ ^(ide|sata|scsi|virtio)[0-9]+ ]] || continue
[[ "$opts" =~ (iops|mbps) ]] || continue
echo "${cid}$opts"
done < <(qm config "$vmid" -current)
}
# Remove throttleing options in the given storage config string. This
# can be a multiline string with a storage config directive per line
# with spaces removed, as retuned by _get_throttled_storage.
# Echoes in the same format with throttling options removed.
_apply_backup_throttle() {
local storage="$1"
for storage in $storage
do
local -a storage_a=(${storage/:/ })
local sid=${storage_a[0]}
local -a sopts_a=(${storage_a[1]//,/ })
for i in ${!sopts_a[@]}
do
[[ "${sopts_a[$i]}" =~ ^(iops|mbps) ]] || continue
unset sopts_a[$i]
done
for j in ${BACKUP_THROTTLE[@]}
do
[[ ! ${sopts_a[@]} =~ \ ${j%=*}= ]] || continue
sopts_a+=("$j")
done
local sopts="${sopts_a[*]}"
echo "$sid:${sopts// /,}"
done
}
# Run qm set for the given vmid. All further arguments are handled as
# configuration directives in the format <directive>:<opts> as given by
# _get_throttled_storage and _remove_throttle. VM locks are skipped.
# Echoes the output of qm set.
_update_config() {
local vmid=$1
local conf_a=("${@:2}")
local opts=""
for conf in "${conf_a[@]}"
do
opts+="-${conf/:/ } "
done
qm set "$vmid" -skiplock 1 $opts
}
# Remove and restore storage throttles for a VM. First argument is the
# action to do (remove or restore). Secoand argument is the vmid.
# Returns 1 if invalid action.
storage_throttle() {
local action=$1
local vmid=$2
local script_name=$(basename $0)
# Store config in a persistent location, so it is not lost is case of
# an unexpected reboot of the host.
local storageconfpath="/var/tmp/${script_name}/storageconf_${vmid}"
case "$action" in
remove)
local storage=$(_get_throttled_storage "$vmid")
if [[ -z "$storage" ]]
then
echo "no throttled disks found to unthrottle"
return 0
fi
mkdir -p "$(dirname "$storageconfpath")"
echo "$storage" > "$storageconfpath"
storage=$(_apply_backup_throttle "$storage")
_update_config "$vmid" $storage
;;
restore)
if [[ ! -e "$storageconfpath" ]]
then
echo "no stored throttled disk configs found"
return 0
fi
local storage="$(< "$storageconfpath")"
_update_config "$vmid" $storage
rm -f "$storageconfpath"
;;
*)
echo "invalid action '$action'"
return 1
;;
esac
}
# Process arguments and environment variables as received from and set
# by vzdump. Output of commands is not redirected.
vzdump_hook() {
local phase="$1" # (job|backup)-(start|end|abort)/log-end/pre-(stop|restart)/post-restart
local dumpdir="$DUMPDIR"
local storeid="$STOREID"
case "$phase" in
# set variables for the phases
job-start|job-end|job-abort)
;;&
backup-start|backup-end|backup-abort|log-end|pre-stop|pre-restart|post-restart)
local mode="$2" # stop/suspend/snapshot
local vmid="$3"
local vmtype="$VMTYPE" # openvz/qemu
local hostname="$HOSTNAME"
;;&
backup-end)
local tarfile="$TARFILE"
;;&
log-end)
local logfile="$LOGFILE"
;;&
# do work
job-start)
;;
job-end)
;;
job-abort)
;;
backup-start)
storage_throttle remove "$vmid"
;;
backup-end)
storage_throttle restore "$vmid"
;;
backup-abort)
storage_throttle restore "$vmid"
;;
log-end)
;;
pre-stop)
;;
pre-restart)
;;
post-restart)
;;
*)
echo "unknown phase '$phase'"
return 1
;;
esac
}
# If this script is executed, run main function with the given
# command line arguments. Otherwise do nothing. This makes it possible
# to source this script for testing purposes or use of the functions
# from other hook scripts.
[[ "$BASH_SOURCE" != "$0" ]] || vzdump_hook "$@"