forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnsrkvmbackup.sh
More file actions
executable file
·261 lines (229 loc) · 8.78 KB
/
nsrkvmbackup.sh
File metadata and controls
executable file
·261 lines (229 loc) · 8.78 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
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
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
version=0.9.1
OPTIND=1
verb=0
logDir="/nsr/logs/cloudstack/"
snapPrefix="CSBKP_"$RANDOM"_"
clusterClient=""
networkerServer=""
hvVersion=""
libvVersion=""
apiVersion=""
kvmDName=""
kvmDUuid=""
logFile=""
mediaPool=""
retentionTime=""
log () {
[[ "$verb" -eq 1 ]] && builtin echo "$@"
if [[ "$1" == "-ne" || "$1" == "-e" || "$1" == "-n" ]]; then
builtin echo -e "$(date '+%Y-%m-%d %H-%M-%S>')" "${@: 2}" >> "$logFile"
else
builtin echo "$(date '+%Y-%m-%d %H-%M-%S>')" "$@" >> "$logFile"
fi
}
vercomp(){
local a b IFS=. -; set -f
printf -v a %08d $1; printf -v b %08d $3
test $a "$2" $b
}
usage() {
echo "
Usage:[-v] [-h] [-l log_dir] [-dr] [-s networker_server] [-c networker_cluster_client] [-t target_vm] [-u target_uuid] [-p snapprefix] [-P media_pool ] [-R retention_time ]
Options:
-h Help and usage
-v Enable verbose mode
-l log_dir. Specify log directory. Default is /nsr/logs/cloudstack
-s networker_server Specifiy the EMC Networker server we are going to use
-c networker_cluster_client Specify the EMC Networker client CLUSTER to use
-t target_vm KVM domain to backup
-u target_uuid KVM domain to backup
-p Snapshot Prefix for backups
-P mediaPool EMC Networker Media Pool
-R retention_time Backup retention time
Supplements Apache Cloudstack B&R Framework EMC Networker plugin and performs the backup of the Virtual Machines
"
}
sanity_checks() {
log "Performing environment sanity checks..."
log -ne "\t[1] Checking if Networker is installed\t"
if [[ $(systemctl list-unit-files | grep networker) = *networker* ]]; then
log "Success"
else
log "Failure"
log -e "\n\tNetworker Service NOT FOUND. Make sure that Networker client is properly installed"
exit 1
fi
log -ne "\t[2] Checking if Networker is running\t"
if [[ $(systemctl is-active networker) = *active* ]]; then
log "Success"
else
log "Failure"
log -e "\n\tNetworker Service is not running. Investigate Networker logs, startup server and try again"
exit 2
fi
log -ne "\t[3] Checking Networker DNS Resolution\t"
if [[ $(getent hosts "$networkerServer") = *$networkerServer* ]]; then
log "Success"
else
log "Failure"
log -e "\n\tNetworker Server cannot be resolved. Backups will most probably fail. Consider adding the ip/hostname to /etc/host or fix DNS resolution"
exit 3
fi
log -ne "\t[4] Checking QEMU / Libvirt Versions \t"
hvVersion=$(virsh version | grep hypervisor | awk '{print $(NF)}')
libvVersion=$(virsh version | grep libvirt | awk '{print $(NF)}' | tail -n 1)
apiVersion=$(virsh version | grep API | awk '{print $(NF)}')
if vercomp "$hvVersion" \> 2.1.2; then
log -n "Success"
log -ne "\t\t [ Libvirt: $libvVersion apiVersion: $apiVersion ]"
echo
else
log "Failure"
log -e "\n\tYour QEMU version $hvVersion is unsupported. Consider upgrading at least to latest QEMU at branch 2"
exit 4
fi
log -ne "\t[4] Checking Permissions \t\t"
if groups $USER | grep -q '\blibvirt\b'; then
log -n "Success"
log -ne "\t\t User $USER is part of libvirt group"
echo
else
log "Failure - User $USER is not part of libvirt group"
exit 6
fi
log "Environment Sanity Checks successfully passed"
}
echo "
Cloudstack B&R Framework - EMC Networker backup script
Version $version
"
backup_domain() {
name=$1
snapName=$2
log "Preparing snapshots and gathering information for backing up domain $name under snapshot name $snapName"
log "Retention time is $retentionTime"
declare -A TRGSRC
while IFS=',' read -r TARGET SOURCE
do
if [[ $SOURCE != "-" ]]; then
TRGSRC+=(["$TARGET"]="$SOURCE")
fi
done < <(virsh -c qemu:///system domblklist "$name" --details | grep file | grep -v 'cdrom' | grep -v 'floppy' | sed 's/ */,/g' | cut -d',' -f 4-)
diskspec=""
for target in "${!TRGSRC[@]}"; do
log -e "\tDisk for $target is at ${TRGSRC[${target}]}"
diskspec="$diskspec --diskspec $target,snapshot=external"
disks="$disks ${TRGSRC[${target}]} "
done
cmd="$(virsh -c qemu:///system snapshot-create-as --domain "$name" --name "$snapName" --no-metadata --atomic --quiesce --disk-only "$diskspec")"
retVal=$?
log "$cmd"
if [ "$retVal" -ne 0 ]; then
log "Agent not responding, trying to snapshot directly"
cmd="$(virsh -c qemu:///system snapshot-create-as --domain "$name" --name "$snapName" --no-metadata --atomic --disk-only "$diskspec")"
retVal=$?
if [ "$retVal" -ne 0 ]; then
log "Failed to create snapshot for $name"
exit 7
fi
log "Created snapshot(s) for $name"
fi
cmd="$(save -LL -q -e "${retentionTime}" -s "$networkerServer" -c "$clusterClient" -N "$name" -b "$mediaPool" $disks)"
retVal=$?
log "$cmd"
echo "$cmd" | grep -oE 'savetime=[0-9]{10}'
if [ $retVal -ne 0 ]; then
log "Unable to backup $disks for $name"
else
log "Backup $disks for $name completed!!"
fi
#Merge changes and conclude
SNAPSHOTS="$(virsh -c qemu:///system domblklist "$name" --details | grep file | grep -v 'cdrom' | grep -v 'floppy' | awk '{print $4}')"
for target in "${!TRGSRC[@]}"; do
log "Merging Snasphots for $target"
cmd="$(virsh -c qemu:///system blockcommit "$name" "$target" --active --pivot)"
retVal=$?
log "$cmd"
if [ $retVal -ne 0 ]; then
log "Unable to merge disk %target changes for domain $name"
exit 8
fi
done
#Clean snapshots
for snapshot in $SNAPSHOTS; do
log "Deleting Snapshot $snapshot"
cmd=$(rm -f "$snapshot")
retVal=$?
log "$cmd"
if [ $retVal -ne 0 ]; then
log "Unable to delete snapshot $snapshot"
exit 8
fi
log "Deleted Snapshot: $snapshot"
done
}
while getopts "h?vs:l:c:t:u:p:P:R:" opt; do
case "$opt" in
h|\?)
usage
exit 254
;;
c) clusterClient="$OPTARG"
;;
s) networkerServer="$OPTARG"
;;
l) logDir="$OPTARG"
;;
t) kvmDName="$OPTARG"
;;
u) kvmDUuid="$OPTARG"
;;
p) snapPrefix="$OPTARG"
;;
P) mediaPool="$OPTARG"
;;
R) retentionTime="$OPTARG"
;;
v) verb=1
;;
esac
done
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
if [[ -z "$networkerServer" || -z "$kvmDName" || -z "$clusterClient" || -z "$kvmDUuid" ]]; then
usage
exit 255
fi
if [ ! -d "$logDir" ]; then
mkdir -p "$logDir"
fi
logFile="$logDir/BACKUP-$kvmDName-$(date +'%Y_%m_%d_%I_%M_%p').log"
# Perform Initial sanity checks
sanity_checks
log -e "\nLooking for domain $kvmDName with UUID $kvmDUuid"
if [[ "$kvmDName" == $(virsh -c qemu:///system domname "$kvmDUuid" | head -1) && "$kvmDUuid" == $(virsh -c qemu:///system domuuid "$kvmDName" | head -1) ]]; then
log "Domain found...."
else
log "Domain not found on this host. Aborting....."
log "Check for the location of the Instance in the cloudstack management console"
exit 5
fi
backup_domain "$kvmDName" "$snapPrefix$kvmDName"
exit 0