-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_VM_proto
More file actions
executable file
·73 lines (55 loc) · 2.27 KB
/
backup_VM_proto
File metadata and controls
executable file
·73 lines (55 loc) · 2.27 KB
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
#!/bin/sh
#backup_VM_proto: prototype backup script for libvirt VM, with versioning
# Copyright (C) 2016 matteo nunziati
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#a space separated list of VM names as shown by 'virsh list'
VMS=""
#a comma separated list with dest mail to send the backup log to (NO SPACES)
MAILTO=""
#how many backups would you like to keep? must be >= 1
KEEP="10"
#this is the sender email address configured in sendmail
FROM=""
#usually you do not want to change these!
LOG="/var/log/virt-backup.log"
BKDIR="/var/lib/libvirt/backup"
COMPRESS="pbzip2"
> $LOG
for VM in $VMS; do
date >> $LOG
echo config keeps latest $KEEP backups >> $LOG
echo "backing up VM $VM..." >> $LOG
/root/virt-backup.pl --action=dump --backupdir=$BKDIR --vm=$VM --compress=$COMPRESS --debug 1>> $LOG 2>&1
TAR_NAME="$BKDIR/$VM.$(date +%F-%H-%M-%S).tar"
echo creating tar archive $TAR_NAME >> $LOG
tar cvf $TAR_NAME $BKDIR/$VM 1>> $LOG 2>&1
#ok the following is not so efficent but how cares...
echo " " >> $LOG
echo "seeking for exceeding backups..." >> $LOG
BACKUPS=$(ls -1 $BKDIR/$VM.*.tar | wc -l)
echo "found $BACKUPS (required was $KEEP):" >> $LOG
ls -1 $BKDIR/$VM.*.tar 1>> $LOG 2>&1
echo " " >> $LOG
#hypotesis is that we have no more than one exceeding backup to kill
if [ $BACKUPS -gt $KEEP ]; then
ONE_OUT=$(ls -1 $BKDIR/$VM.*.tar | head -1)
echo going to kill $ONE_OUT >> $LOG
rm -f $ONE_OUT 1>> $LOG 2>&1
fi
echo " " >> $LOG
echo "clean up folders" >> $LOG
rm -rvf $BKDIR/$VM $BKDIR/$VM.meta 1>> $LOG 2>&1
date >> $LOG
echo "...done" >> $LOG
echo "-------" >> $LOG
done
cat $LOG | mail -s "VM backup report" -r "VM-BACKUP-ROBOT<$FROM>" $MAILTO