-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmbackuprotate
executable file
·112 lines (75 loc) · 2.8 KB
/
vmbackuprotate
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
#!/bin/bash
#####################################
## Rotate virtual machines backups ##
#####################################
usage ()
{
echo ''
echo 'Usage : vmbackuprotate <config file path>'
echo ' config file path: path to configuration file'
echo ''
}
# If wrong number of arguments, print usage and exit
[ $# -ne 1 ] && { usage; exit 1; }
# Parameters
# ----------
# Default values
# Day of the week for weekly backup (1 is monday, 7 is sunday)
WEEKLY_DAY_OF_WEEK=7
# Keep daily backups for N days
DAILY_BACKUPS_LIFETIME=7
# Keep weekly backups for N weeks
WEEKLY_BACKUPS_LIFETIME=4
# Names of VM to backup
# (if not found in config file, no VM to backup)
VM_NAMES=""
# Remote directories to synchronize
# (if not found in config file, no remote dir to synchronize)
REMOTE_DIRS=""
# Source config file (overrides defaults)
source $1
## Check directories are defined (no default values for those)
if [[ -z $VM_DIR ]]; then { echo >&2 "Missing parameter VM_DIR in config file"; exit 3; }; fi
if [[ -z $BAK_DIR ]]; then { echo >&2 "Missing parameter BAK_DIR in config file"; exit 3; }; fi
# Script
# ------
# Create backup dirs if they don't exist
DAILY_DIR=$BAK_DIR/daily
WEEKLY_DIR=$BAK_DIR/weekly
mkdir -p $DAILY_DIR && mkdir -p $WEEKLY_DIR || { echo >&2 "Error creating backup directories. Aborting."; exit 4; }
# Make backups
for vm in $VM_NAMES
do
echo "Backup $vm..."
# Backup VM
BAK_FILENAME="$vm"_`date +%y%m%d`.qcow2
BAK_FILEPATH=$BAK_DIR/$BAK_FILENAME
vmbackup $VM_DIR $vm $BAK_FILEPATH || { echo >&2 "Backup failed for $vm "; continue; }
# Move backup to daily directory
mv $BAK_FILEPATH $DAILY_DIR
# If today is weekly backup, hard link to "weekly" directory
if [ `date +%u` -eq $WEEKLY_DAY_OF_WEEK ]
then
ln $DAILY_DIR/$BAK_FILENAME $WEEKLY_DIR
fi
done
# Get rid of old backups
# LIFETIME = 0 means "do not delete backups, keep them permanently"
# The script does not run every week at the exact same time of day, so we
# subtract 6 hours to deal with this approximation
if [ $DAILY_BACKUPS_LIFETIME -gt 0 ]; then
find $DAILY_DIR -mmin +$(( ($DAILY_BACKUPS_LIFETIME * 24 - 6 ) * 60 )) -delete
fi
if [ $WEEKLY_BACKUPS_LIFETIME -gt 0 ]; then
find $WEEKLY_DIR -mmin +$(( ($WEEKLY_BACKUPS_LIFETIME * 7 * 24 - 6 ) * 60 )) -delete
fi
# Synchronize remote directories
for remote in $REMOTE_DIRS
do
echo "Synchronize $remote..."
# A 'vmbackup' directory is created in each remote dir
# Hardcoding a subdirectory name is not elegant, but it is a simple measure to avoid wreaking havoc
# in the system if entering / as remote dir by mistake in the config file.
ionice -c2 -n7 nice -n19 rsync -a -H --delete --progress --sparse $BAK_DIR/ $remote/vmbackup || { echo >&2 "Synchronization failed for $remote"; continue; }
done
exit 0