Back to the main page

Backup a VM in standalone Dom0 (OVS Oracle Virtual Server)


The script can be used for this purpose. It takes snapshot of a VM's disk boot image (no other disks at this moment), then backs up that snapshot to some other location.
The script takes three mandatory arguments: a VM name, image retention in counts and backup path.

Requirement is that a VM disk image is file Dom0:/OVS/${vm}/${vm}.img
The /OVS is ocsf2 and its tool reflink is used to take a snapshot of disk image.

This tool can be run manually or as cron job.

Usage: Conditions: VM disk image must be Dom0:/OVS/<VM>/<VM>.img VM cfg must be Dom0:/OVS/<VM>/<VM>.cfg


# crontab -l # backup ldap02 #Usage: 25 02 * * * ldap02 5 /import/vm-backup/Dom0/

The script logs are in /var/log/


#!/bin/env bash # backup a VM in standalone Dom0, done for bz16402 (run localy on Dom0) # argument 1 = VM # argument 2 = retention in counts # argumet 3 = backup path, likely /import/something # Conditions: # VM disk image must be Dom0:/OVS/${vm}/${vm}.img # VM cfg must be Dom0:/OVS/${vm}/${vm}.cfg # ----------------------------------------------------- PROGNAME=`basename $0` LOGDATE=`date +'%Y%m%d-%H%M'` #LOGDATE=`date +%Y-%m-%d_%H_%M` LOG_PATH="/var/log/`basename ${PROGNAME}`" [ -d /var/log/`basename ${PROGNAME}` ] || mkdir /var/log/`basename ${PROGNAME}` LOGFILE=${LOG_PATH}/${LOGDATE}.log # readonly loggerinfo="logger -t "${PROGNAME}" Info:" readonly loggerwarning="logger -t "${PROGNAME}" Warning:" readonly loggerproblem="logger -t "${PROGNAME}" Problem:" # who_cares=" \ " err() { echo ; echo "Problem: $*" ; echo ${loggerproblem} "$*" echo "$*" | mail -s "Problem from `hostname`:${PROGNAME}" ${who_cares} exit 1 } usage() { clear echo " Usage: ${PROGNAME} Conditions: VM disk image must be Dom0:/OVS/<VM>/<VM>.img VM cfg must be Dom0:/OVS/<VM>/<VM>.cfg " exit 1 } # must be three arguments if [ $# -ne 3 ]; then usage fi # impots and their checks vm=$1 counts=$2 bkpath=$3 chk_vm() { /usr/sbin/xm list ${vm} >/dev/null [[ $? -ne 0 ]] && err "VM ${vm} doesn't exist." } get_snapshot() { echo ; echo "Getting snapshot (using reflink) of /OVS/${vm}/${vm}.img" reflink /OVS/${vm}/${vm}.img /OVS/${vm}/${vm}.img-SNAPSHOT-${LOGDATE} || \ err "Can't get snapshot (using reflink) of /OVS/${vm}/${vm}.img" echo "Snapshot is /OVS/${vm}/${vm}.img-SNAPSHOT-${LOGDATE}" } backup_snapshot() { echo ; echo "Backup snapshot /OVS/${vm}/${vm}.img-SNAPSHOT-${LOGDATE}" echo "Backup is ${bkpath}/${vm}.img-FULLBACKUP-${LOGDATE}" rsync -h --progress --verbose --stats \ /OVS/${vm}/${vm}.img-SNAPSHOT-${LOGDATE} \ ${bkpath}/${vm}.img-FULLBACKUP-${LOGDATE} || \ err "Can't backup snapshot to ${bkpath}/${vm}.img-FULLBACKUP-${LOGDATE}" } rm_snapshot() { echo ; echo "Removing snapshot /OVS/${vm}/${vm}.img-SNAPSHOT-${LOGDATE}" rm -f /OVS/${vm}/${vm}.img-SNAPSHOT-${LOGDATE} || \ err "Can't remove snapshot /OVS/${vm}/${vm}.img-SNAPSHOT-${LOGDATE}" } retire_older_backups() { echo # find current number of images bkp_number=`find ${bkpath} -type f -name "*.img-FULLBACKUP*" | wc -l` || \ err "Can't find current number of backups" if [ ${bkp_number} -gt ${counts} ]; then echo "Removing backups older than latest ${counts}" # how many images to delete num_to_delete=`(echo "scale=0; ${bkp_number}-${counts}" | bc -l)` || \ err "Can't find how many images to delete" # list of images to delete bkp_to_delete=`find ${bkpath} -type f -name "*.img-FULLBACKUP*" | sort -nr | tail -${num_to_delete}` || \ err "Can't generate list of images to be deleted" echo ; echo "Backups to remove: ${bkp_to_delete}" ; echo for image in ${bkp_to_delete} do rm -f ${image} || err "Can't remove ${image}" echo "Removed: ${image}" done else echo "There is no older backup to be removed" fi } ##### MAIN ##### ( chk_vm # check presence of VM in Dom0 [[ ${counts} = [[:digit:]]* ]] || err "Retention must be in counts, as integer." [[ -d ${bkpath} ]] || err "Backup path ${bkpath} is not directory." get_snapshot backup_snapshot rm_snapshot retire_older_backups ) >> ${LOGFILE} 2>&1 exit 0

Example of log file 20191004-1425.log is:

Getting snapshot (using reflink) of /OVS/ldap02/ldap02.img Snapshot is /OVS/ldap02/ldap02.img-SNAPSHOT-20191004-1425 Backup snapshot /OVS/ldap02/ldap02.img-SNAPSHOT-20191004-1425 Backup is /import/vm-backup/Dom0/ldap02.img-FULLBACKUP-20191004-1425 ldap02.img-SNAPSHOT-20191004-1425 26.84G 100% 54.22MB/s 0:07:52 (xfer#1, to-check=0/1) Number of files: 1 Number of files transferred: 1 Total file size: 26.84G bytes Total transferred file size: 26.84G bytes Literal data: 26.84G bytes Matched data: 0 bytes File list size: 52 File list generation time: 0.001 seconds File list transfer time: 0.000 seconds Total bytes sent: 26.85G Total bytes received: 31 sent 26.85G bytes received 31 bytes 56.70M bytes/sec total size is 26.84G speedup is 1.00 Removing snapshot /OVS/ldap02/ldap02.img-SNAPSHOT-20191004-1425 There is no older backup to be removed

Back to the main page