This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vzpbackup.sh
executable file
·149 lines (135 loc) · 3.74 KB
/
vzpbackup.sh
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
#!/bin/bash
# DEFAULTS
DESTINATION="/vz/backup"
KEEP_COUNT=0
SUSPEND="no"
VERBOSE="no"
FULL_BACKUP="no"
INC_BACKUP="no"
VZCTL_PARAM=""
BACKUP_VES=""
RSYNC_OPTS="$RSYNC_OPTS"
TEMPLATES="yes"
declare -x EXCLUDES
# COMMANDLINE PARSING
shopt -s extglob
for param in "$@"; do
value=${param#*=}
case $param in
-h|--help)
echo "Usage: $0 [--verbose(=yes)] [--destination=<backup-destination>] [--keep-count=<keep count>] [--suspend=<yes|no>] [--templates=<yes|no>] [--exclude=<VEID>] [--full or --inc(cremental)] [--all or VEIDs]"
echo "Defaults:"
echo "- --destination=$DESTINATION"
echo "- --keep-count=$KEEP_COUNT"
echo "- --suspend=$SUSPEND"
echo "- --verbose=$VERBOSE"
exit 0
;;
--verbose|--verbose=yes)
VERBOSE="yes"
;;
--destination=*)
DESTINATION=$value
;;
--keep-count=+([0-9]))
KEEP_COUNT=$value
;;
--suspend=+(yes|no))
SUSPEND=$value
;;
--full)
FULL_BACKUP="yes"
;;
--inc|--incremental)
INC_BACKUP="yes"
;;
--templates=+(yes|no))
TEMPLATES=$value
;;
--exclude=+([0-9]|\,))
for VEID in ${value//\,/ }; do
EXCLUDES[$VEID]=$VEID
done
;;
--all)
for VEID in $( vzlist -a -H -o ctid ); do
BACKUP_VES="$BACKUP_VES $VEID"
done
;;
+([0-9]))
BACKUP_VES="$BACKUP_VES $param"
;;
esac
done
shopt -u extglob
# CHECKS
if [ "$BACKUP_VES" = "" ]; then
echo "Neither --all or VEIDs is/are given.."
exit 1
fi
if [ "$INC_BACKUP" = "no" ] && [ "$FULL_BACKUP" = "no" ]; then
echo "Neither --inc(remental) or --full given.."
exit 1
fi
if ! which vzctl &>/dev/null; then
echo "Couldn't find vzctl in \$PATH. Are you sure it's there?"
exit 1
fi
if [ "$TEMPLATES" = "yes" ]; then
TEMPLATE_DIR=$( source /etc/vz/vz.conf; echo $TEMPLATE )
RSYNC_OPTS="$RSYNC_OPTS --include=$TEMPLATE_DIR/*"
fi
if [ "$INC_BACKUP" = "yes" ]; then
RSYNC_OPTS="$RSYNC_OPTS --exclude=$( VEID=; source /etc/vz/vz.conf; echo "$VE_PRIVATE*/root.hdd/root.hdd" )"
fi
if [ "$SUSPEND" = "no" ]; then
VZCTL_PARAM="$VZCTL_PARAM --skip-suspend"
fi
if [ "$VERBOSE" = "yes" ]; then
RSYNC_OPTS="$RSYNC_OPTS --verbose"
fi
# LOCKFILE
if [ -f /var/run/vzbackup.pid ]
then
OLD_PID=$( cat /var/run/vzbackup.pid )
if [ -d /proc/${OLD_PID} ]
then
echo "There's already a backup running.. Aborting.."
pstree -p $( cat /var/run/vzbackup.pid )
exit 0
else
echo "Mh. There's a lockfile. But no backup is running.. Ignoring it.."
fi
fi
echo $$ > /var/run/vzbackup.pid
trap "rm /var/run/vzbackup.pid" EXIT
# SCRIPT
for VEID in $BACKUP_VES; do
if [ "x${EXCLUDES[$VEID]}" = "x" ]; then
if [ -f "/etc/vz/conf/$VEID.conf" ]; then
VE_PRIVATE=$( source /etc/vz/conf/$VEID.conf; echo $VE_PRIVATE )
if [ "$INC_BACKUP" = "yes" ]; then
vzctl snapshot $VEID --id $( uuidgen ) $VZCTL_PARAM
elif [ "$FULL_BACKUP" = "yes" ]; then
vzctl snapshot-list $VEID -H -o uuid | \
while read UUID; do
vzctl snapshot-delete $VEID --id $UUID
done
vzctl snapshot $VEID --id $( uuidgen ) $VZCTL_PARAM
vzctl compact $VEID
fi
RSYNC_OPTS="$RSYNC_OPTS --include=$VE_PRIVATE"
fi
fi
done
if [ "$FULL_BACKUP" = "yes" ]; then
if (( $KEEP_COUNT > 0 )); then
echo "KEEP_COUNT > 0; keeping backup.."
REF_DATE=$( expr $( date --date="$( vzctl snapshot-list $VEID -H -o date | head -n1 )" +%s ) - 86400 )
RSYNC_OPTS="--backup --backup-dir=$( date --date="@$REF_DATE" +%Y.%m.%d )"
fi
fi
rsync -az $RSYNC_OPTS --exclude="/vz/*/*" /vz $DESTINATION
if [ "$FULL_BACKUP" = "yes" ]; then
ssh $( echo $DESTINATION | cut -d\: -f1 ) "find $( echo $DESTINATION | cut -d\: -f2 )/* -maxdepth 0 -iname '????.??.??' | head -n -$KEEP_COUNT | xargs rm -rf"
fi