Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh -e
- export LIBVIRT_DEFAULT_URI="qemu:///system"
- PROCOWNER="$(id -un):$(id -gn)"
- #TMP_DIR=/srv/kvm_local/backups
- LOCKFILE=/var/lock/$(basename $0)
- if [ -t 1 ]; then
- VERBOSE=1
- else
- VERBOSE=0
- fi
- msg() {
- echo "$(date '+%Y-%m-%d %H:%M:%S')$@"
- }
- #if [ ! -d $TMP_DIR ]; then
- # echo "error: no tmp directory: $TMP_DIR" >&2
- # exit 1
- #fi
- echo checking lockfile $LOCKFILE
- [ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en $LOCKFILE "$0" "$@" || :
- echo locked as pid $$
- DOMLIST=$(virsh list --state-running --without-managed-save --persistent --name)
- #DOMLIST=migration-test
- [ ! -z "$1" ] && DOMLIST="$1"
- echo domain list: $DOMLIST
- DST_ROOT=/srv/kvm_backups
- DST_BASE=$DST_ROOT/backups
- DST_QEMU=$DST_ROOT/qemu
- if [ ! -d $DST_BASE ]; then
- echo "error: no base directory: $DST_BASE" >&2
- exit 1
- fi
- mkdir -p $DST_QEMU
- if [ ! -d $DST_QEMU ]; then
- echo "error: no base qemu directory: $DST_QEMU" >&2
- exit 1
- fi
- for dom in $DOMLIST; do
- echo ""
- msg " = processing $dom..."
- if virsh domblklist $dom >/dev/null; then
- DST_DIR=$DST_BASE/$dom
- SNAPNAME=$(date +%Y%m%d-%H%M%S)
- SNAPYEAR=$(date +%Y)
- mkdir -p $DST_DIR
- if [ ! -d $DST_DIR ]; then
- echo "error: can't create dst dir: $DST_DIR" >&2
- exit 2
- fi
- SKIPFILE=/var/tmp/backup-skip.$dom
- #
- # rm -f -- $SKIPFILE
- #
- # virsh domblklist $dom | egrep '[svh]d[a-z]' | while read BLKID BLKPATH; do
- # BLKOWNER=$(stat --format '%U:%G' $BLKPATH)
- #
- # if [ $BLKOWNER != $PROCOWNER ]; then
- # echo "error: ownership of $BLKPATH ($BLKOWNER) doesn't match $PROCOWNER." >&2
- # echo "error: skipping this domain for now (skipfile: $SKIPFILE)." >&2
- #
- # touch $SKIPFILE
- # fi
- # done
- TSTART=$(date +%s)
- if [ ! -e $SKIPFILE ]; then
- msg " * saving latest domain definition to $DST_QEMU/$dom.xml"
- virsh dumpxml --inactive $dom > $DST_QEMU/$dom.xml
- msg " * backing up domain definition to $DST_DIR/$dom-$SNAPNAME.xml"
- virsh dumpxml --inactive $dom > $DST_DIR/$dom-$SNAPNAME.xml
- if virsh undefine $dom >/dev/null; then
- virsh domblklist $dom | egrep '[svh]d[a-z]' | while read BLKID BLKPATH; do
- if [ -f $BLKPATH ]; then
- msg " * processing volume $BLKID / $BLKPATH ($(du -hs $BLKPATH | cut -f 1))..."
- DSTFPATH=$DST_DIR/$(basename $BLKPATH)-$SNAPNAME
- msg " * performing blockcopy of $BLKPATH to $DSTFPATH..."
- if [ $VERBOSE = 1 ]; then
- virsh blockcopy $dom $BLKID $DSTFPATH --transient-job --wait --verbose --finish || ( echo "error: failed to blockcopy $BLKPATH." >&2 && exit 101 )
- else
- virsh blockcopy $dom $BLKID $DSTFPATH --transient-job --wait --finish >/dev/null || ( echo "error: failed to blockcopy $BLKPATH." >&2 && exit 101 )
- fi
- RC=$?
- msg " * blockcopy RC: $RC"
- if [ "$RC" = "0" -a -f $DSTFPATH ]; then
- BKBLKPATH=$DST_ROOT/$(basename $BLKPATH)
- msg " * linking $DSTFPATH to $BKBLKPATH..."
- [ -f $BKBLKPATH ] && rm -f -- $BKBLKPATH
- ln $DSTFPATH $BKBLKPATH
- else
- echo "error: unable to link $DSTFPATH: block operation failed and/or file not found" >&2
- fi
- #msg " * saving $DSTFPATH to $DST_DIR/$(basename $TMPFPATH).tar"
- #nice tar -C $(dirname $TMPFPATH) -cSf $DST_DIR/$(basename $TMPFPATH).tar $(basename $TMPFPATH) || echo error: failed to archive $TMPFPATH. >&2
- msg " * cleaning up $DST_DIR..."
- #rm -f -- $TMPFPATH
- find $DST_DIR -type f -mtime +7 \( -name '*.qcow2-*' -o -name '*.xml' \) -delete
- elif [ $BLKPATH != "-" ]; then
- echo "error: base block path not found: $BLKPATH, skipping." >&2
- fi
- done
- virsh define $DST_DIR/$dom-$SNAPNAME.xml >/dev/null
- msg " = completed in:" $(date "+%M:%S" -d @$(( $(date +%s) - $TSTART )) )
- msg " + finished."
- else
- echo "could not undefine domain, skipping." >&2
- fi
- fi
- else
- echo "error: unable to query blocklist of domain $dom." >&2
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement