diff options
Diffstat (limited to 'kvm_tools')
| -rw-r--r-- | kvm_tools/config_andromeda | 6 | ||||
| -rw-r--r-- | kvm_tools/config_cigar | 6 | ||||
| -rw-r--r-- | kvm_tools/config_example | 6 | ||||
| -rw-r--r-- | kvm_tools/config_example_restore | 7 | ||||
| -rw-r--r-- | kvm_tools/config_milky | 6 | ||||
| -rwxr-xr-x | kvm_tools/kvm_live_backup.sh | 165 | ||||
| -rwxr-xr-x | kvm_tools/kvm_live_restore.sh | 96 | ||||
| -rw-r--r-- | kvm_tools/kvm_live_snap.png | bin | 0 -> 138980 bytes |
8 files changed, 292 insertions, 0 deletions
diff --git a/kvm_tools/config_andromeda b/kvm_tools/config_andromeda new file mode 100644 index 0000000..e97ca76 --- /dev/null +++ b/kvm_tools/config_andromeda @@ -0,0 +1,6 @@ +USER=root +DISK=sda +DOMAIN=andromeda +TARGET=/home/miguel/BACKUP/ANDROMEDA +SOURCE=/home/miguel/KVM/ANDROMEDA +LIMIT_RATE=20m diff --git a/kvm_tools/config_cigar b/kvm_tools/config_cigar new file mode 100644 index 0000000..101d8c2 --- /dev/null +++ b/kvm_tools/config_cigar @@ -0,0 +1,6 @@ +USER=root +DISK=sda +DOMAIN=cigar +TARGET=/home/nick/BACKUP_CIGAR +SOURCE=/home/miguel/KVM/CIGAR +LIMIT_RATE=20m diff --git a/kvm_tools/config_example b/kvm_tools/config_example new file mode 100644 index 0000000..f28f425 --- /dev/null +++ b/kvm_tools/config_example @@ -0,0 +1,6 @@ +USER=root +DISK=sda +DOMAIN=milky +TARGET=/home/miguel/KVM/02_BACKUP/MILKY +SOURCE=/home/miguel/KVM/MILKY +LIMIT_RATE=200m diff --git a/kvm_tools/config_example_restore b/kvm_tools/config_example_restore new file mode 100644 index 0000000..5fb8c3d --- /dev/null +++ b/kvm_tools/config_example_restore @@ -0,0 +1,7 @@ +SOURCE=/mnt/BACKUP/BACKUP/ANDROMEDA +PREFIX=andromeda_20180222_185847 +TARGET=/mnt/BACKUP/KVM/andro +NAME=andromeda +USER=root +STRIP_TAR=4 +LIMIT_RATE=200m diff --git a/kvm_tools/config_milky b/kvm_tools/config_milky new file mode 100644 index 0000000..2ad4b76 --- /dev/null +++ b/kvm_tools/config_milky @@ -0,0 +1,6 @@ +USER=root +DISK=sda +DOMAIN=milky +TARGET=/home/miguel/BACKUP/MILKY +SOURCE=/home/miguel/KVM/MILKY +LIMIT_RATE=20m diff --git a/kvm_tools/kvm_live_backup.sh b/kvm_tools/kvm_live_backup.sh new file mode 100755 index 0000000..ddd3df7 --- /dev/null +++ b/kvm_tools/kvm_live_backup.sh @@ -0,0 +1,165 @@ +#!/bin/bash + +##################################################################### +# +# A FOOLISH KVM BACKUP SCRIPT +# hacked together by Miguel<m.i@gmx.at> around mid February 2018 +# +# DO NOT FORGET TO SYNC & FSTRIM YOUR DISK BEFORE RUNNING THIS SHIT +# untar also needs -S !!! +# +# TODO: ADD TIMING INFO ? +# +# TODO: do not make so many assumptions about file names and locations :( +# This script should/could be much more universal! +# Do some BASIC error checking +# An RESTORE script would be very helpfull as well +# +# TODO!: check if we run inside tmux +# ask if fstrim performed +# check/ask for sudo +# config external backup space +# show config and ask if ok +# override --all-yes with param +# use dash instead bash +# dry run +# +##################################################################### + +VERSION="0.1" + +# we exit as soon as a single command fails! +set -e + +echo "" +echo " 1) Are you running inside a tmux/screen?" +echo " 2) Did you remember to perform sync & fstrim?" +echo "" +echo " Please type 'YES' if you want to continue." +echo "" + +read proceed +if [[ "$proceed" != "YES" ]]; then + exit +fi + +# script expects exaclty one param (see config_example) +if [ "$#" -ne 1 ]; then + echo "Usage: $0 [config_file]" + exit +fi + +# color helpers (set on terminal only) +black= +red= +green= +yellow= +blue= +magenta= +cyan= +white= +reset= +if test -t 1; then +black=`tput setaf 0` +red=`tput setaf 1` +green=`tput setaf 2` +yellow=`tput setaf 3` +blue=`tput setaf 4` +magenta=`tput setaf 5` +cyan=`tput setaf 6` +white=`tput setaf 7` +reset=`tput sgr0` +fi + +# pretty printing funcs / vars + +cnt=0 +all=11 + +function print_ok { + echo [${green}OK$reset] +} +function print_cnt { + printf " [${cyan}%2s${reset}/${all}${reset}] " ${1} +} +function print_txt { + ((++cnt)) + print_cnt $cnt + printf "%-30s " "${1}..." +} + +# let's do it +echo +echo "### ${magenta}FOOL LIVE KVM BACKUP ${VERSION}${reset} ###" +echo +echo " Using config file: ${yellow}${1}${reset}" +echo +source $1 + +# some vars +TIMESTAMP=`date +"%Y%m%d_%H%M%S"` +LOG=${DOMAIN}_${TIMESTAMP}.log +DOMAIN_T=${DOMAIN}_${TIMESTAMP} + +# tell what we gonna do +echo " Today we backup: ${yellow}${DISK}$reset on ${yellow}${DOMAIN}${reset} as ${yellow}${USER}${reset}" +echo " SOURCE_DIR: ${yellow}${SOURCE}$reset" +echo " TARGET_DIR: ${yellow}${TARGET}$reset" +echo " LIMIT_RATE for tar: ${yellow}${LIMIT_RATE}${reset}/second" +echo " Logging to: ${yellow}${LOG}$reset" +echo +echo " Keep your fingers crossed!" +echo + +# chande into target dir +cd $TARGET + +print_txt "LOG CURRENT DOMBLK LIST" +sudo -u $USER virsh domblklist $DOMAIN > $LOG +print_ok + +print_txt "DUMPING XML" +sudo -u $USER virsh dumpxml $DOMAIN > $DOMAIN_T.xml +sudo -u $USER virsh dumpxml $DOMAIN | sed "s:$SOURCE/${DOMAIN}.img:$SOURCE/${DOMAIN}.SNAP:" > ${DOMAIN_T}_snapped.xml +print_ok + +print_txt "SAVING VM (RAM)" +sudo -u $USER virsh save $DOMAIN $DOMAIN_T.ram >> $LOG +print_ok + +print_txt "SNAPSHOTTING DISK" +sudo -u $USER virsh snapshot-create-as $DOMAIN SNAP --disk-only --atomic --no-metadata --quiesce >> $LOG +print_ok + +print_txt "RESTORING VM (RAM)" +sudo -u $USER virsh restore $DOMAIN_T.ram --xml ${DOMAIN_T}_snapped.xml >> $LOG +print_ok + +print_txt "LOG CURRENT DOMBLKLIST" +sudo -u $USER virsh domblklist $DOMAIN >> $LOG +print_ok + +print_txt "SAVING SNAPSHOT" +sudo -u $USER tar -C ${SOURCE} -cSf - $DOMAIN.img 2>>$LOG | pv -L $LIMIT_RATE 2>/dev/null 1> $DOMAIN_T.img.tar +print_ok + +print_txt "BLOCK-COMMITTING DELTAS" +sudo -u $USER virsh blockcommit $DOMAIN $DISK --pivot >> $LOG +print_ok + +print_txt "REMOVING DELTAS" +sudo -u $USER rm ${SOURCE}/$DOMAIN.SNAP >> $LOG +print_ok + +print_txt "LOG CURRENT DOMBLKLIST" +sudo -u $USER virsh domblklist $DOMAIN >> $LOG +print_ok + +print_txt "LOG QCOW2 IMAGE INFO" +sudo -u $USER qemu-img info ${SOURCE}/$DOMAIN.img >> $LOG +print_ok + +echo +echo "### ${magenta}FINITO${reset} ###" +echo + diff --git a/kvm_tools/kvm_live_restore.sh b/kvm_tools/kvm_live_restore.sh new file mode 100755 index 0000000..b2d6fcb --- /dev/null +++ b/kvm_tools/kvm_live_restore.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +#TODO: move helpers to another file/lib +#TODO: Add sudo everywhere.. +#TODO: remove --strip-components as soon as we get new backups with tar -C +#TODO: check if a virbr1 is available + +VERSION="0.1" + +sudo brctl addbr virbr1 + +set -e + +if [ "$#" -ne 1 ]; then + echo "Usage: $0 [config_file]" + exit +fi + +# color helpers (set on terminal only) +black= +red= +green= +yellow= +blue= +magenta= +cyan= +white= +reset= +if test -t 1; then +black=`tput setaf 0` +red=`tput setaf 1` +green=`tput setaf 2` +yellow=`tput setaf 3` +blue=`tput setaf 4` +magenta=`tput setaf 5` +cyan=`tput setaf 6` +white=`tput setaf 7` +reset=`tput sgr0` +fi + +# pretty printing funcs / vars + +cnt=0 +all=5 + +function print_ok { + echo [${green}OK$reset] +} +function print_cnt { + printf " [${cyan}%2s${reset}/${all}${reset}] " ${1} +} +function print_txt { + ((++cnt)) + print_cnt $cnt + printf "%-30s " "${1}..." +} + +echo +echo "### ${magenta}FOOL LIVE KVM RESTORE ${VERSION}${reset} ###" +echo +echo " Using config file: ${yellow}${1}${reset}" +echo +source $1 + +UUID=$(uuid) +LOG=$TARGET/log.log + +echo +print_txt "SED xml DEFINITION" + +#sed "s:<uuid>.*</uuid>:<uuid>$UUID</uuid>:" \ +# diff $SOURCE/$PREFIX.xml $TARGET/$NAME.xml + +sudo -u $USER cat $SOURCE/$PREFIX.xml | +sed "s:<name>.*</name>:<name>$NAME</name>:" | +sed "s:<source *file=.*/>:<source file='$TARGET/$NAME.img'/>:" \ + > $TARGET/$NAME.xml +print_ok + + +print_txt "UNTAR QCOW2 IMAGE" +cat $SOURCE/$PREFIX.img.tar | pv -L $LIMIT_RATE 2>/dev/null | tar -C $TARGET --strip-components=$STRIP_TAR -xSf - +print_ok + +print_txt "RENAME QCOW2 IMAGE" +sudo -u $USER mv $TARGET/*.img $TARGET/$NAME.img +print_ok + +print_txt "DEFINE NEW KVM" +sudo -u $USER sudo virsh define $TARGET/$NAME.xml > $LOG +print_ok + +print_txt "RESTORE RUNNING KVM" +sudo -u $USER sudo virsh restore $SOURCE/$PREFIX.ram --xml $TARGET/$NAME.xml > $LOG +print_ok + diff --git a/kvm_tools/kvm_live_snap.png b/kvm_tools/kvm_live_snap.png Binary files differnew file mode 100644 index 0000000..22f8cf6 --- /dev/null +++ b/kvm_tools/kvm_live_snap.png |
