This file is indexed.

/usr/lib/xcp/bin/xe-restore-metadata is in xcp-xapi 1.3.2-5.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/bin/bash
# Script which restores metadata into a VDI
# Citrix Systems Inc, 2008

if [ ! -e /etc/xcp/inventory ]; then
  echo Must run on a XenServer host.
  exit 1
fi

. /etc/xcp/inventory

XE="/usr/lib/xcp/bin/xe"

master_uuid=$(${XE} pool-list params=master --minimal)
if [ $? -gt 0 ]; then
   echo Error: unable to determine master host uuid
   exit 1
fi

if [ "${master_uuid}" != "${INSTALLATION_UUID}" ]; then
   echo Error: must run this script on the master host in the resource pool.
   exit 1
fi

metadata_version=1
default_restore_mode="all"
debug="/bin/true"

function usage {
    echo "Usage: $0 [-h] [-v] [-y] [-n] [-p] [-x <VDI UUID>] [-u <SR UUID>] [-m all|sr]"
    echo
    echo " -h: Display this help message"
    echo " -x: Specify the VDI UUID to override probing"
    echo " -p: Just scan for a metadata VDI and print out its UUID to stdout"
    echo " -u: UUID of the SR you wish to restore from"
    echo " -n: Perform a dry run and just echo the metadata import commands (default: false)"
    echo " -l: Just list the available backup dates"
    echo " -d: Specify which date to restore from (default: latest)"
    echo " -m: Either 'sr' to restore only the VMs on the SR, or 'all' for VMs (default: ${default_restore_mode})"
    echo " -v: Verbose output"
    echo " -y: Assume non-interactive mode and yes to all questions"
    echo 
    exit 1
}

function test_sr {
  sr_uuid_found=$(${XE} sr-list uuid="$1" --minimal)
  if [ "${sr_uuid_found}" != "$1" ]; then
     echo Invalid SR UUID specified: $1
     usage
  fi
}

dry_run=0
sr_uuid=
yes=0
just_list_dates=0
just_probe=0
chosen_date=""
restore_mode=${default_restore_mode}
while getopts "yhpvx:d:lnu:m:" opt ; do
    case $opt in
    h) usage ;;
    u) sr_uuid=${OPTARG} ;;
    n) dry_run=1 ;;
    l) just_list_dates=1 ;;
    p) just_probe=1 ;;
    v) debug="" ;;
    d) chosen_date=${OPTARG} ;;
    m) restore_mode=${OPTARG} ;;
    x) vdis=${OPTARG} ;;
    y) yes=1 ;;
    *) echo "Invalid option"; usage ;;
    esac
done

case "${restore_mode}" in
all) ;;
sr) ;;
*) echo Unknown restore mode: "${restore_mode}"; usage
esac

# get pool uuid
IFS=,
pool_uuid=$(${XE} pool-list params=uuid --minimal)
if [ -z "${pool_uuid}" ]; then
  echo Unable to determine pool UUID.
  exit 1
fi

# determine if the SR UUID is vaid
if [  -z "${sr_uuid}" ]; then
  # use the default-SR from the pool
  sr_uuid=$(${XE} pool-param-get uuid=${pool_uuid} param-name=default-SR)
fi
test_sr "${sr_uuid}"

sr_name=$(${XE} sr-param-get uuid=${sr_uuid} param-name=name-label)

# get a list of all VDIs if an override has not been provided on the cmd line
if [ -z "${vdis}" ]; then
  vdis=$(${XE} vdi-list params=uuid sr-uuid=${sr_uuid} read-only=false --minimal)
fi

mnt=
function cleanup {
   cd /
   if [ ! -z "${mnt}" ]; then
      umount ${mnt} >/dev/null 2>&1
      rmdir ${mnt}
      mnt=""
   fi

   if [ ! -z "${vbd_uuid}" ]; then
      ${debug} echo -n "Unplugging VBD: " >&2
      ${XE} vbd-unplug uuid=${vbd_uuid} timeout=20
      # poll for the device to go away if we know its name
      if [ "${device}" != "" ]; then
          device_gone=0
          for ((i=0; i<10; i++)); do
              ${debug} echo -n "." >&2
              if [ ! -b ${device} ]; then
                  ${debug} echo " done" >&2
                  device_gone=1
                  break
              fi
              sleep 1
          done
          if [ ${device_gone} -eq 0 ]; then
              ${debug} echo " failed" >&2
              ${debug} echo Please destroy VBD ${vbd_uuid} manually. >&2
          else
              ${XE} vbd-destroy uuid=${vbd_uuid}
              vbd_uuid=""
          fi
      fi
      device=""
   fi
}

if [ -z "${vdis}" ]; then
   echo No VDIs found on SR. >&2
   exit 0
fi

trap cleanup SIGINT ERR

for vdi_uuid in ${vdis}; do
   ${debug} echo -n "Creating VBD: " >&2
   vbd_uuid=$(${XE} vbd-create vm-uuid=${CONTROL_DOMAIN_UUID} vdi-uuid=${vdi_uuid} device=autodetect 2>/dev/null)

   if [ $? -ne 0 -o -z "${vbd_uuid}" ]; then
      ${debug} echo error creating VBD for VDI ${vdi_uuid} >&2
      cleanup
      continue
   fi

   ${debug} echo ${vbd_uuid} >&2

   ${debug} echo -n "Plugging VBD: " >&2
   ${XE} vbd-plug uuid=${vbd_uuid}
   device=/dev/$(${XE} vbd-param-get uuid=${vbd_uuid} param-name=device)

   if [ ! -b ${device} ]; then
     ${debug} echo ${device}: not a block special >&2
     cleanup
     continue
   fi

   ${debug} echo ${device} >&2

   ${debug} echo -n "Probing device: " >&2
   probecmd="/usr/lib/xcp/lib/probe-device-for-file"
   metadata_stamp="/.ctxs-metadata-backup"
   mnt=
   ${probecmd} ${device} ${metadata_stamp}
   if [ $? -eq 0 ]; then
     ${debug} echo found metadata backup >&2
     ${debug} echo -n "Mounting filesystem: " >&2
     mnt=/var/run/pool-backup-${vdi_uuid}
     mkdir -p ${mnt}
     /sbin/fsck -a ${device} >/dev/null 2>&1
     if [ $? -ne 0 ]; then
        echo File system integrity error.  Please correct manually. >&2
        cleanup
        continue
     fi
     mount ${device} ${mnt} >/dev/null 2>&1
     if [ $? -ne 0 ]; then
       ${debug} echo failed >&2
       cleanup
     else
       if [ -e "${mnt}/.ctxs-metadata-backup" ]; then
          ${debug} echo Found backup metadata on VDI: ${vdi_uuid} >&2
          xe vdi-param-set uuid=${vdi_uuid} other-config:ctxs-pool-backup=true
          break
       fi
     fi
   else
     ${debug} echo backup metadata not found >&2
   fi
   cleanup
done

if [ $just_probe -gt 0 ]; then
   echo ${vdi_uuid}
   cleanup
   exit 0
fi

cd ${mnt}
${debug} echo "" >&2

if [ ! -d ${mnt}/metadata ]; then
   echo Metadata backups not found. >&2
   cleanup
   exit 1
fi

cd ${mnt}/metadata

if [ $just_list_dates -gt 0 ]; then
    ls -1r ${mnt}/metadata
    cleanup
    exit 0
fi

if [ -z "${chosen_date}" ]; then
    chosen_metadata_dir=$(ls | sort -n | tail -1)
    if [ -z "${chosen_metadata_dir}" ]; then
       echo No VM metadata backups found in ${mnt}/metadata >&2
       cleanup
       exit 1
    fi
else
    if [ ! -d ${mnt}/metadata/${chosen_date} ]; then
       echo Date directory "${chosen_date}" not found >&2
       cleanup
       exit 1
    fi
    chosen_metadata_dir=${chosen_date}
fi

case ${restore_mode} in
sr)
    full_dir=${mnt}/metadata/${chosen_metadata_dir}/by-sr/${sr_uuid}
    ;;
all)
    full_dir=${mnt}/metadata/${chosen_metadata_dir}/all
    ;;
esac

if [ ! -d ${full_dir} ]; then
    echo No VM metadata exports were found for the selected SR >&2
    cleanup
    exit 1
fi

${debug} echo Selected: ${full_dir}

cd ${full_dir}
${debug} echo "" >&2
${debug} echo Latest VM metadata found is: >&2
${debug} ls >&2

if [ $yes -eq 0 ]; then
   echo "Do you wish to reimport all VM metadata?" 
   echo "Please type in "yes" and <enter> to continue." 
   read response
   if [ "$response" != "yes" ]; then
     echo Aborting metadata restore.
     cleanup
     exit 1
   fi
fi

${debug} echo "" >&2
${debug} echo Restoring VM metadata: >&2

trap - ERR

IFS=" "
error_count=0
good_count=0
shopt -s nullglob
for meta in *.vmmeta; do
   if [ ${dry_run} -gt 0 ]; then
       echo xe vm-import filename=${meta} sr-uuid=${sr_uuid} --metadata --preserve
   else
       "/usr/lib/xcp/bin/xe" vm-import filename="${full_dir}/${meta}" sr-uuid=${sr_uuid} --metadata --preserve
       if [ $? -gt 0 ]; then
          error_count=$(( $error_count + 1 ))
       else
          good_count=$(( $good_count + 1 ))
       fi
   fi
done

smmeta_file=${mnt}/metadata/${chosen_metadata_dir}/SRMETA.xml
cmd="/usr/lib/xcp/lib/restore-sr-metadata.py -u ${sr_uuid} -f ${smmeta_file}"
if [ -e ${smmeta_file} ]; then
    if [ ${dry_run} -gt 0 ]; then
        echo ${cmd}
    else
        ${cmd}
    fi
fi

echo "Restored ${good_count} VM(s), and ${error_count} error(s)"
cleanup
exit ${error_count}