Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 272 lines (238 sloc) 7.44 kb
f77e728 Initial import of hg.uk.xensource.com/carbon/trunk/api.hg c/s 9691:9a…
Xen hg user authored
1 #!/bin/bash
2 # Script which backups metadata into a VDI
3 # Citrix Systems Inc, 2008
4
c495ebd @alexz CA-37151: If a SIGTERM/SIGINT is received during metadata backup we s…
alexz authored
5 trap "cleanup" TERM INT
6
13b8348 @jonludlam Move everything over to using Fhs.ml / @ substitution
jonludlam authored
7 if [ ! -e @INVENTORY@ ]; then
f77e728 Initial import of hg.uk.xensource.com/carbon/trunk/api.hg c/s 9691:9a…
Xen hg user authored
8 echo Must run on a XenServer host.
9 exit 1
10 fi
11
13b8348 @jonludlam Move everything over to using Fhs.ml / @ substitution
jonludlam authored
12 . @INVENTORY@
f77e728 Initial import of hg.uk.xensource.com/carbon/trunk/api.hg c/s 9691:9a…
Xen hg user authored
13
13b8348 @jonludlam Move everything over to using Fhs.ml / @ substitution
jonludlam authored
14 XE="@BINDIR@/xe"
f77e728 Initial import of hg.uk.xensource.com/carbon/trunk/api.hg c/s 9691:9a…
Xen hg user authored
15
16 master_uuid=$(${XE} pool-list params=master --minimal)
17 if [ $? -gt 0 ]; then
18 echo Error: unable to determine master host uuid
19 exit 1
20 fi
21
22 if [ "${master_uuid}" != "${INSTALLATION_UUID}" ]; then
23 echo Error: must run this script on the master host in the resource pool.
24 exit 1
25 fi
26
27 history_kept=25
28 metadata_version=1
29 debug=/bin/true
30
31 function usage {
32 echo "Usage: $0 [-h] [-i] [-u <SR UUID>]"
33 echo
34 echo " -h: Display this help message"
35 echo " -c: Create a backup VDI if one cannot be found (default: false)"
36 echo " -i: Initialize the backup VDI before performing the backup"
37 echo " -u: UUID of the SR you wish to backup to"
38 echo " -d: Dont perform a backup, but leave the disk mounted in a shell"
39 echo " -k: Number of older backups to preserve (default: ${history_kept})"
40 echo " -n: Just try to find a backup VDI and stop the script after that"
41 echo " -v: Verbose output"
42 echo
43 echo
44 echo "Use the -d option to examine the backup VDI, and restore it if necessary."
45 echo "It is a good idea to copy the backup onto the control domain before"
46 echo "restoring it via the xe pool-database-restore CLI command."
47 exit 1
48 }
49
50 function test_sr {
51 sr_uuid_found=$(${XE} sr-list uuid="$1" --minimal)
52 if [ "${sr_uuid_found}" != "$1" ]; then
53 echo Invalid SR UUID specified: $1
54 usage
55 fi
56 }
57
58 leave_mounted=0
59 init_fs=0
60 create_vdi=0
61 just_find_vdi=0
c495ebd @alexz CA-37151: If a SIGTERM/SIGINT is received during metadata backup we s…
alexz authored
62 fs_uninitialised=0
f77e728 Initial import of hg.uk.xensource.com/carbon/trunk/api.hg c/s 9691:9a…
Xen hg user authored
63 while getopts "hvink:u:dc" opt ; do
64 case $opt in
65 h) usage ;;
c495ebd @alexz CA-37151: If a SIGTERM/SIGINT is received during metadata backup we s…
alexz authored
66 c) create_vdi=1 ; fs_uninitialised=1 ;;
67 i) init_fs=1 ; fs_uninitialised=1 ;;
f77e728 Initial import of hg.uk.xensource.com/carbon/trunk/api.hg c/s 9691:9a…
Xen hg user authored
68 k) history_kept=${OPTARG} ;;
69 u) sr_uuid=${OPTARG} ;;
70 d) leave_mounted=1 ;;
71 n) just_find_vdi=1 ;;
72 v) debug="" ;;
73 *) echo "Invalid option"; usage ;;
74 esac
75 done
76
77 # get pool uuid
78 IFS=,
79 pool_uuid=$(${XE} pool-list params=uuid --minimal)
80 if [ -z "${pool_uuid}" ]; then
81 echo Unable to determine pool UUID.
82 exit 1
83 fi
84
85 # determine if the SR UUID is vaid
86 if [ -z "${sr_uuid}" ]; then
87 # use the default-SR from the pool
88 sr_uuid=$(${XE} pool-param-get uuid=${pool_uuid} param-name=default-SR)
89 fi
90 test_sr "${sr_uuid}"
91
92 sr_name=$(${XE} sr-param-get uuid=${sr_uuid} param-name=name-label)
93 # see if a backup VDI already exists on the selected SR
94 vdi_uuid=$(${XE} vdi-list other-config:ctxs-pool-backup=true sr-uuid=${sr_uuid} params=uuid --minimal)
95
96 mnt=
97 function cleanup {
c495ebd @alexz CA-37151: If a SIGTERM/SIGINT is received during metadata backup we s…
alexz authored
98 trap "" TERM INT
f77e728 Initial import of hg.uk.xensource.com/carbon/trunk/api.hg c/s 9691:9a…
Xen hg user authored
99 cd /
100 if [ ! -z "${mnt}" ]; then
101 umount ${mnt} >/dev/null 2>&1
102 rmdir ${mnt}
103 fi
104
105 if [ ! -z "${vbd_uuid}" ]; then
106 ${debug} echo -n "Unplugging VBD: "
107 ${XE} vbd-unplug uuid=${vbd_uuid} timeout=20
108 # poll for the device to go away if we know its name
109 if [ "${device}" != "" ]; then
110 device_gone=0
111 for ((i=0; i<10; i++)); do
112 ${debug} echo -n "."
113 if [ ! -b ${device} ]; then
114 ${debug} echo " done"
115 device_gone=1
116 break
117 fi
118 sleep 1
119 done
120 if [ ${device_gone} -eq 0 ]; then
121 ${debug} echo " failed"
122 echo Please destroy VBD ${vbd_uuid} manually.
123 else
124 ${XE} vbd-destroy uuid=${vbd_uuid}
125 fi
126 fi
127 fi
c495ebd @alexz CA-37151: If a SIGTERM/SIGINT is received during metadata backup we s…
alexz authored
128 if [ ${fs_uninitialised} -eq 1 -a -n "${vdi_uuid}" ] ; then
129 ${XE} vdi-destroy uuid=${vdi_uuid}
130 fi
f77e728 Initial import of hg.uk.xensource.com/carbon/trunk/api.hg c/s 9691:9a…
Xen hg user authored
131 }
132
133 echo Using SR: ${sr_name}
134 if [ -z "${vdi_uuid}" ]; then
135 if [ "${create_vdi}" -gt 0 ]; then
136 echo -n "Creating new backup VDI: "
137 vdi_uuid=$(${XE} vdi-create virtual-size=250MiB sr-uuid=${sr_uuid} type=user name-label="Pool Metadata Backup")
138 init_fs=1
139 if [ $? -ne 0 ]; then
140 echo failed
141 exit 1
142 fi
143 else
144 echo "Backup VDI not found, aborting. You can initialise one using the '-c' flag."
145 exit 3
146 fi
147 echo ${vdi_uuid}
148 ${XE} vdi-param-set uuid=${vdi_uuid} other-config:ctxs-pool-backup=true
149 else
150 ${debug} echo "Using existing backup VDI: ${vdi_uuid}"
09b462d CA-5300: Bug in xe-backup-metadata preventing scheduled metadata back…
Mike McClurg authored
151 fs_uninitialised=0
f77e728 Initial import of hg.uk.xensource.com/carbon/trunk/api.hg c/s 9691:9a…
Xen hg user authored
152 fi
153
154 if [ ${just_find_vdi} -gt 0 ]; then
155 exit 0
156 fi
157
158 ${debug} echo -n "Creating VBD: "
159 vbd_uuid=$(${XE} vbd-create vm-uuid=${CONTROL_DOMAIN_UUID} vdi-uuid=${vdi_uuid} device=autodetect)
160 ${debug} echo ${vbd_uuid}
161
162
163 if [ $? -ne 0 -o -z "${vbd_uuid}" ]; then
164 echo error creating VBD
165 cleanup
166 exit 1
167 fi
168
169 ${debug} echo -n "Plugging VBD: "
170 ${XE} vbd-plug uuid=${vbd_uuid}
171 device=/dev/$(${XE} vbd-param-get uuid=${vbd_uuid} param-name=device)
172
173 if [ ! -b ${device} ]; then
174 ${debug} echo ${device}: not a block special
175 cleanup
176 exit 1
177 fi
178
179 ${debug} echo ${device}
180
181 if [ $init_fs -eq 1 ]; then
182 ${debug} echo -n "Creating filesystem: "
183 mkfs.ext3 -j -F ${device} > /dev/null 2>&1
184 ${debug} echo "done"
c495ebd @alexz CA-37151: If a SIGTERM/SIGINT is received during metadata backup we s…
alexz authored
185 fs_uninitialised=0
f77e728 Initial import of hg.uk.xensource.com/carbon/trunk/api.hg c/s 9691:9a…
Xen hg user authored
186 fi
187
188 ${debug} echo -n "Mounting filesystem: "
189 mnt=/var/run/pool-backup-${vdi_uuid}
190 mkdir -p ${mnt}
191
192 /sbin/fsck -a ${device} >/dev/null 2>&1
193 if [ $? -ne 0 ]; then
194 ${debug} fsck failed. Please correct manually
195 cleanup
196 exit 1
197 fi
198
199 mount ${device} ${mnt} > /dev/null 2>&1
200 if [ $? -ne 0 ]; then
201 ${debug} echo failed
202 cleanup
203 exit 1
204 fi
205 ${debug} echo ${mnt}
206
207 if [ ${leave_mounted} -eq 0 ]; then
208 lrconf=${mnt}/conf/${vdi_uuid}
209 if [ ! -f ${lrconf} ]; then
210 ${debug} echo -n "Initialising rotation: "
211 mkdir -p ${mnt}/conf/
212 echo "${mnt}/${pool_uuid}.db {" >> ${lrconf}
213 echo " rotate ${history_kept}" >> ${lrconf}
214 echo " missingok" >> ${lrconf}
215 echo "}" >> ${lrconf}
216 echo done
217 echo ${metadata_version} >> ${mnt}/.ctxs-metadata-backup
218 fi
219
220 # invoke logrotate to rotate over old pool db backups
221 echo -n "Rotating old backups: "
222 logrotate -f ${lrconf}
223 num_found=$(find ${mnt} -name \*.db\.* | wc -l)
224 echo found ${num_found}
225
226 # perform the pool database dump
227 echo -n "Backing up pool database: "
228 ${XE} pool-dump-database file-name=${mnt}/${pool_uuid}.db
229 echo done
230
231 # backup the VM metadata for each VM in the pool into a dated directory
232 datetime=$(date +%F-%H-%M-%S)
233 metadir=${mnt}/metadata/${datetime}
234 mkdir -p ${metadir}
235 echo -n "Cleaning old VM metadata: "
236 IFS=" "
237 todelete=$(cd ${mnt}/metadata && ls -1 |sort -n | head -n -${history_kept} | xargs echo)
238 for dir in ${todelete}; do
239 rm -rf ${mnt}/metadata/${dir}
240 done
241 echo done
242 IFS=","
243 echo -n "Backing up SR metadata: "
244 mkdir -p ${metadir}
13b8348 @jonludlam Move everything over to using Fhs.ml / @ substitution
jonludlam authored
245 "@LIBEXECDIR@/backup-sr-metadata.py" -f ${metadir}/SRMETA.xml
f77e728 Initial import of hg.uk.xensource.com/carbon/trunk/api.hg c/s 9691:9a…
Xen hg user authored
246 echo "done"
247
248 echo -n "Backing up VM metadata: "
249 ${debug} echo ""
250 mkdir -p ${metadir}/all
251 for vmuuid in $(${XE} vm-list params=uuid is-control-domain=false --minimal); do
252 ${debug} echo -n .
253 ${XE} vm-export --metadata uuid=${vmuuid} filename=${metadir}/all/${vmuuid}.vmmeta >/dev/null 2>&1
254 done
255 echo "done"
256 echo -n "Backing up Template metadata: "
257 ${debug} echo ""
13b8348 @jonludlam Move everything over to using Fhs.ml / @ substitution
jonludlam authored
258 template_uuids=$("@LIBEXECDIR@/print-custom-templates")
f77e728 Initial import of hg.uk.xensource.com/carbon/trunk/api.hg c/s 9691:9a…
Xen hg user authored
259 if [ $? -eq 0 ]; then
260 for tmpl_uuid in ${template_uuids}; do
261 ${XE} template-export --metadata template-uuid=${tmpl_uuid} filename=${metadir}/all/${tmpl_uuid}.vmmeta >/dev/null 2>&1
262 done
263 fi
264 echo "done"
13b8348 @jonludlam Move everything over to using Fhs.ml / @ substitution
jonludlam authored
265 "@LIBEXECDIR@/link-vms-by-sr.py" -d ${metadir}
f77e728 Initial import of hg.uk.xensource.com/carbon/trunk/api.hg c/s 9691:9a…
Xen hg user authored
266 else
267 cd ${mnt}
268 env PS1="Mounted backup VDI on: ${mnt}\nPress ^D to exit shell and safely detach it.\n\n[\u@\h \W]\$ " bash
269 fi
270
271 cleanup
Something went wrong with that request. Please try again.