-
Notifications
You must be signed in to change notification settings - Fork 3
/
exportvm.sh
executable file
·168 lines (145 loc) · 4.29 KB
/
exportvm.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
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
#!/bin/sh
#set -x
set -e
usage() {
echo "Usage: [1;32m$(basename $0)[0m [-a one_auth] [-c ceph_conf] [-d export_dir] [-f] [-i rbd_id] [-p rbd_pool] [-u rpc_url] [-v] [vmname]"
echo
echo "Defaults:"
echo " one_auth: /var/lib/bareos/.one_auth"
echo " ceph_conf: /etc/ceph/ceph.conf"
echo " export_dir: /var/lib/bareos/"
echo " -f: file extraction from snapshot (experimental, requires libguestfs)"
echo " rbd_id: libvirt"
echo " rbd_pool: one-pool"
echo " rpc_url: 127.0.0.1"
exit 1
}
if [ $# -lt 1 ]; then
usage
fi
# One env
ONEVM="/usr/bin/onevm"
ONEIMAGE="/usr/bin/oneimage"
# RBD env
RBD="/usr/bin/rbd"
# Script env
C="0"
ABORT="0"
LIMIT="1080"
WAIT="5"
DOW=$(date +"%u")
DATE=$(date +"%F-%T")
while getopts a:c:d:fi:p:u:v o; do
case "$o" in
a) ONE_AUTH="$OPTARG"
;;
c) CEPH_CONF="$OPTARG"
;;
d) EXPORT_PATH="$OPTARG"
;;
f) EXTRACT="YES"
;;
i) RBD_ID="$OPTARG"
;;
p) RBD_POOL="$OPTARG"
;;
u) URL="$OPTARG"
;;
v) LOG="/tmp/exportvm.${DATE}.log"
;;
\?)
usage
;;
esac
done
shift $(($OPTIND - 1))
HOST="${1:-onevm}"
URL="${URL:-127.0.0.1}"
ONE_AUTH="${ONE_AUTH:-/var/lib/bareos/.one_auth}"
ONE_XMLRPC="http://${URL}:2633/RPC2"
RBD_ID="${RBD_ID:-libvirt}"
RBD_POOL="${RBD_POOL:-one-pool}"
EXPORT_PATH="${EXPORT_PATH:-/var/lib/bareos}"
EXTRACT="${EXTRACT:-NO}"
BACKUP="${HOST}_snap_disk"
LOG="${LOG:-/dev/null}"
CEPH_CONF="${CEPH_CONF:-/etc/ceph/ceph.conf}"
export ONE_AUTH ONE_XMLRPC
GEN_DISK_IDS=$(onevm show ${HOST} | sed -n -e '/VM DISKS/,/VM NICS/ p' |grep ${HOST} | awk '{print $1}' | tr "\n" " ")
# If the vm does not exist do not continue
if [ $(${ONEVM} list -f NAME=${HOST} -lNAME |grep -v "NAME"|wc -l) -eq 0 ]; then
echo "VM ${HOST} not found" >> ${LOG} 2>&1
exit 1
fi
# Check the vm is running and proceed
if [ $(${ONEVM} show ${HOST} | grep LCM_STATE | cut -d ":" -f 2) = "RUNNING" ]; then
for DISK_ID in ${GEN_DISK_IDS}; do
SAVEIMG=${BACKUP}_${DISK_ID}
EI=${EXPORT_PATH}/${SAVEIMG}
EF=${EI}_fs
# If the images exists, something is wrong
if [ $(${ONEIMAGE} list -f NAME=${SAVEIMG} -lNAME |grep -v "NAME"|wc -l) -ne 0 ]; then
echo "Backup image ${HOST} already exists" >> ${LOG} 2>&1
exit 1
fi
# Take snapshot for backup
${ONEVM} disk-saveas ${HOST} ${DISK_ID} ${SAVEIMG} >> ${LOG} 2>&1
# Legacy command <= V4.12
#${ONEVM} disk-snapshot --live ${HOST} ${DISK_ID} ${SAVEIMG} >> ${LOG} 2>&1
# Wait LIMIT * WAIT seconds max for the image to be ready
until [ $(${ONEIMAGE} show ${SAVEIMG} | grep STATE | cut -d ":" -f 2) = "rdy" ]; do
if [ ${ABORT} -eq 0 ]; then
C=$(($C+1))
sleep $WAIT
if [ ${C} -eq ${LIMIT} ]; then
ABORT=1
fi
else
echo "Snapshot of ${HOST} exceeded time limit of $((${LIMIT}*${WAIT})) seconds" >> ${LOG} 2>&1
exit 1
fi
done
# Output the path for export
RBD_IMAGE=$(${ONEIMAGE} show ${SAVEIMG} | grep SOURCE | cut -d ":" -f 2)
# Export rdb device to disk for backup
if [ -f ${EI} ]; then
echo "File ${EI} already exists" >> ${LOG} 2>&1
exit 1
else
${RBD} -c $CEPH_CONF --no-progress --id $RBD_ID -p $RBD_POOL export ${RBD_IMAGE} ${EI}
fi
# Delete snapshot once exported to disk
if [ $? -eq 0 ]; then
${ONEIMAGE} delete ${SAVEIMG}
else
echo "Export error for ${EI}" >> ${LOG} 2>&1
exit 1
fi
# Output fileset for backup (for file extraction also output the image once a week)
if [ ${EXTRACT} = "NO" ]; then
echo ${EI}
elif [ ${EXTRACT} = "YES" ]; then
# output the image on a friday
if [ "${DOW}" -eq 5 ]; then
# output snapshot on sat or sun to gives the full fs more backup time
#if [ "${DOW}" -eq 6 -o "${DOW}" -eq 7 ]; then
echo ${EI}
fi
mkdir -p ${EF}
if [ "${DISK_ID}" -eq 0 ]; then
guestfish --ro -i copy-out -a ${EI} / ${EF} >> ${LOG} 2>&1 | head
else
FS=$(virt-list-filesystems ${EI} | head -1)
guestfish --ro -a ${EI} -m ${FS} tar-out / - | tar -xf - -C ${EF}
fi
echo ${EF}
else
echo "Extract error for ${EF}" >> ${LOG} 2>&1
exit 1
fi
done
else
# Bork
echo "${HOST} not found or not running" >> ${LOG} 2>&1
exit 1
fi