-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_vms
executable file
·132 lines (105 loc) · 3.58 KB
/
backup_vms
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
#!/bin/bash
#live backup of lvm backed vms using borg
#
##list archives
#borg list ${backup_mountpoint}/${backupdir}
#
##restore vm
#nice ionice -c3 borg extract --stdout ${backup_mountpoint}/${backupdir}::LVM-DATE |nice ionice -c3 dd bs=16M of=${lvpath}/LVM iflag=fullblock status=progress
#
#ssh:
#sync;dd iflag=fullblock oflag=direct if=/dev/vmvg1/hostname bs=8M |nice pigz -1 -c - |ssh -o compression=no remote_ip_address "nice pigz -d | dd iflag=fullblock oflag=direct bs=4M of=/dev/vmvg1/hostname status=progress"
#
#borg extract --stdout ${backup_mountpoint}/${backupdir}::LVM-DATE |dd bs=8M of=${lvpath}/LVM oflag=direct iflag=fullblock
date
if [ $(id -u) -ne 0 ];then
echo "must be root"
exit 1
fi
error=0
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
export BORG_PASSPHRASE=""
export BORG_KEY_FILE=""
backup_mountpoint="/mnt/ssnas1"
lvpath="/dev/vmvg1"
backupdir="vms_$(hostname)"
blacklist="swap
clone
_testing
clean_install"
#check if script is already running
if [[ `pgrep -f "bash.*$0"` != "$$" ]]; then
echo "already running"
exit 0
fi
#make sure ls isn't polluted
unalias ls >/dev/null 2>&1
#lvs of vms to backup
lvs=$(ls ${lvpath}|grep -v swap)
#list of storage for running vms
running_vms=$(virsh list|awk '{print $2}'|grep --color=never -v -e Name -v -e "^$"|xargs -I% cat "/etc/libvirt/qemu/%.xml"|grep "${lvpath}")
#check that backup mountpoint is mounted
mountpoint -q ${backup_mountpoint}||mount ${backup_mountpoint} >/dev/null 2>&1
mountpoint -q ${backup_mountpoint} >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "ssnas1 not mounted, exiting."
exit 1
fi
#create backup directory if it does not exist
if [ ! -d ${backup_mountpoint}/${backupdir} ];then
borg init --encryption=authenticated-blake2 ${backup_mountpoint}/${backupdir} >/dev/null 2>&1
fi
#backup lvm configuration
echo "backing up lv information"
lvdisplay > /tmp/lvdisplay.txt
result=$(borg create ${backup_mountpoint}/${backupdir}::lvdisplay-`hostname`-`date +%Y-%m-%d-%H` /tmp/lvdisplay.txt 2>&1)
#do actual backups
for lv in $lvs;do
skip=0
#skip blacklisted vms
if [ ! -z "$blacklist" ];then
for blacklisted in $blacklist;do
if [[ $lv == *$blacklisted* ]]; then
echo -e "blacklisted: $lv - skipping\n"
skip=1
fi
done
fi
#skip non-running vms
if [ $(echo "$running_vms"|grep --color=never "$lv" -c) -eq 0 ];then
skip=1
fi
if [ $skip -ne 1 ];then
echo "backing up $lv at `date`"
lvcreate -L40G -s -n backup$lv $lvpath/$lv >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "ERROR: creation of snapshot failed (backup$lv $lvpath/$lv), skipping vm."
error=2
lvremove $lvpath/backup$lv -f >/dev/null 2>&1
continue
#exit 2
fi
sync
result=$(nice ionice -c3 borg create --compression zstd --chunker-params 19,23,21,4095 --read-special -v --stats ${backup_mountpoint}/${backupdir}::${lv}-`date +%Y-%m-%d-%H` $lvpath/backup$lv 2>&1)
if [ $? -ne 0 ];then
echo "ERROR: backup of $lv failed, continuing."
error=3
echo "$result"
lvremove $lvpath/backup$lv -f >/dev/null 2>&1
#exit 3
fi
lvremove $lvpath/backup$lv -f >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "ERROR: removal of snaphot failed, continuing."
error=4
#exit 4
fi
echo -e "backup of $lv complete\n"
fi
done
echo "removing old archives..."
#echo "just running with --dry-run, remove this when we actually want to remove old backups"
#borg prune --dry-run --stats -v --keep-weekly=8 ${backup_mountpoint}/${backupdir}
nice ionice -c3 borg prune --save-space --stats -v --keep-hourly=24 --keep-daily=7 --keep-weekly=2 ${backup_mountpoint}/${backupdir}
exit $error