-
Notifications
You must be signed in to change notification settings - Fork 17
/
vzpbackup.sh
executable file
·254 lines (218 loc) · 5.99 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
#
# vzpbackup.sh
#
# A script to backup the containers running on an OpenVZ host.
# The container needs to utilize ploop as disk storage.
# Traditional storage is not supported by this script.
# The backup can be taken while the container is running.
#
# The script is based on the information on the ploop wiki page
# (http://openvz.org/Ploop/Backup) and has been developed based
# on that information.
#
# After reading the command line arguments the script will create a
# snapshot of the ploop device and backup it (via tar) to a
# configurable directory. It will always include the config file
# of the container backed up.
#
# Author: Andreas Faerber, [email protected]
##
## DEFAULTS
##
SUSPEND=no
BACKUP_DIR=/store/vzpbackup
WORK_DIR=/store/vzpbackup
COMPRESS=no
COMPACT=0
##
## VARIABLES
##
TIMESTAMP=`date '+%Y%m%d-%H%M%S'`
VZLIST_CMD=/usr/sbin/vzlist
VZCTL_CMD=/usr/sbin/vzctl
EXCLUDE=""
PREFIX="vzpbackup_"
## Add NODE hostname
PREFIX="$PREFIX`hostname -s`_"
## VARIABLES END
## FUNCTIONS
contains() {
string="$1"
substring="$2"
case "$string" in
*"$substring"*)
return 1
;;
*)
return 0
;;
esac
return 0
}
show_usage() {
echo -e "--------------------------------------------------------------------
Usage: $0
\t[--suspend=<yes/no>]
\t[--backup-dir=<Backup-Directory>]
\t[--work-dir=<Temp-Directory>]
\t[--compress=<no/pz/bz/pbz/tbz/gz/tgz/xz/txz>]
\t[--compact]
\t[--all]
\t<CTID> <CTID>
--------------------------------------------------------------------
Defaults:";
show_param;
}
show_param() {
echo "---";
echo -e "SUSPEND: \t\t$SUSPEND"
echo -e "BACKUP_DIR: \t\t$BACKUP_DIR"
echo -e "WORK_DIR: \t\t$WORK_DIR"
echo -e "COMPRESS: \t\t$COMPRESS"
echo -e "COMPACT: \t\t$COMPACT"
echo -e "CTIDs to backup: \t\t$CTIDS"
echo -e "EXCLUDE CTIDs: \t\t$EXCLUDE"
echo "---";
}
## FUNCTIONS END
## Get global and local config, if there exists
if [ -f "/etc/vz/vzpbackup.conf" ]; then
source "/etc/vz/vzpbackup.conf";
fi
if [ -f "./vzpbackup.conf" ]; then
source "./vzpbackup.conf";
fi
for i in "$@"
do
case $i in
--help)
show_usage;
exit 0;
;;
--suspend=*)
SUSPEND=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
;;
--exclude=*)
EXCLUDE="$EXCLUDE `echo $i | sed 's/[-a-zA-Z0-9]*=//'`"
;;
--backup-dir=*)
BACKUP_DIR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
WORK_DIR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
;;
--work-dir=*)
WORK_DIR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
;;
--compress=*)
COMPRESS=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
;;
--compact)
COMPACT=1
;;
--all)
CTIDS=`$VZLIST_CMD -a -Hoctid`
;;
*)
# Parse CTIDs here
CTIDS=$CTIDS" "$i
;;
esac
done
show_param;
if [ "x$SUSPEND" != "xyes" ]; then
CMDLINE="${CMDLINE} --skip-suspend"
fi
if [ -z "$CTIDS" ]; then
echo ""
echo "No CTs to backup (Either give CTIDs or --all on the commandline)"
exit 0
fi
for i in $CTIDS
do
CTID=$i
contains "$EXCLUDE" $CTID
CONTAINS=$?
if [ $CONTAINS -eq 1 ]; then
echo "Excluding CTID $CTID .."
continue;
fi
# Check if the VE exists
if grep -w "$CTID" <<< `$VZLIST_CMD -a -Hoctid` &> /dev/null; then
if [ $COMPACT == 1 ]; then
echo "Compacting CTID: $CTID"
$VZCTL_CMD compact $CTID > /tmp/vzpbackup_compact_$CTID_$TIMESTAMP.log
echo "Compact log file: /tmp/vzpbackup_compact_$CTID_$TIMESTAMP.log"
fi
echo "Backing up CTID: $CTID"
ID=$(uuidgen)
VE_PRIVATE=$(VEID=$CTID; source /etc/vz/vz.conf; source /etc/vz/conf/$CTID.conf; echo $VE_PRIVATE)
echo $ID > $VE_PRIVATE/vzpbackup_snapshot
# Take CT snapshot with parameters
$VZCTL_CMD snapshot $CTID --id $ID $CMDLINE
# Backup configuration additional configuration files (/etc/vz/conf/$CTID.*)
echo "Copying config files: "
for f in $(ls -1 /etc/vz/conf/$CTID.*)
do
CONF_EXT=${f##*.}
cp $f "$VE_PRIVATE/dump/{$ID}.ve.$CONF_EXT"
echo $f
done
# Copy the backup somewhere safe
# We copy the whole directory which then also includes
# a possible the dump (while being suspended) and container config
cd $VE_PRIVATE
HNAME=`$VZLIST_CMD -Hohostname $CTID`
FILENAME="${PREFIX}${CTID}_${HNAME}_${TIMESTAMP}"
COMPRESS_SUFFIX=""
if [ "$COMPRESS" == "tgz" ]; then
tar -zcvf $WORK_DIR/$FILENAME.tar.gz .
COMPRESS_SUFFIX="gz"
elif [ "$COMPRESS" == "tbz" ]; then
tar -jcvf $WORK_DIR/$FILENAME.tar.bz2 .
COMPRESS_SUFFIX="bz2"
elif [ "$COMPRESS" == "txz" ]; then
tar -Jcvf $WORK_DIR/$FILENAME.tar.xz .
COMPRESS_SUFFIX="xz"
elif [ "$COMPRESS" == "pz" ]; then
tar --use-compress-program=pigz -cvf $WORK_DIR/$FILENAME.tar.gz .
COMPRESS_SUFFIX="gz"
elif [ "$COMPRESS" == "pbz" ]; then
tar --use-compress-program=pbzip2 -cvf $WORK_DIR/$FILENAME.tar.bz2 .
COMPRESS_SUFFIX="bz2"
else
tar -cvf $WORK_DIR/$FILENAME.tar .
if [ "$COMPRESS" == "bz" ]; then
bzip2 $WORK_DIR/$FILENAME.tar
COMPRESS_SUFFIX="bz2"
elif [ "$COMPRESS" == "gz" ]; then
gzip $WORK_DIR/$FILENAME.tar
COMPRESS_SUFFIX="gz"
elif [ "$COMPRESS" == "xz" ]; then
xz --compress $WORK_DIR/$FILENAME.tar
COMPRESS_SUFFIX="xz"
fi
fi
echo "Removing backup config files: "
for f in $(ls -1 $VE_PRIVATE/dump/{$ID}.ve.*)
do
ls -la "$f"
rm "$f"
done
if [ "$COMPRESS_SUFFIX" == "" ]; then
BACKUP_FILE="$FILENAME.tar"
else
BACKUP_FILE="$FILENAME.tar.$COMPRESS_SUFFIX"
fi
# Move file from temp directory to backup directory
if [ "$BACKUP_DIR" != "$WORK_DIR" ]; then
echo "Moving backup file"
mv $WORK_DIR/$BACKUP_FILE $BACKUP_DIR/$BACKUP_FILE
fi
echo "BACKUP FILE: $BACKUP_DIR/$BACKUP_FILE"
ls -la $BACKUP_DIR/$BACKUP_FILE
# Delete (merge) the snapshot
$VZCTL_CMD snapshot-delete $CTID --id $ID
else
echo "WARNING: No CT found for ID $CTID. Skipping..."
fi
done