summaryrefslogtreecommitdiff
path: root/kvm_tools/kvm_live_backup.sh
blob: ddd3df78f5656a4a0bd30341d622c0d814bb38c3 (plain)
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
#!/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