-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpsbackup
executable file
·305 lines (249 loc) · 8.73 KB
/
vpsbackup
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/bash
# 01.12.2018
# touch /standalone for non int vps's
if [ ! -d /root/cpaneldirect -a ! -d /usr/local/solusvm ]; then
echo "No cpaneldirect folder or solusvm"
exit;
fi
if [ "$1" = "" ]; then
echo 'Usage ./vpsbackup ID OPTIONAL|all|noswift|monthly|load';
exit;
elif [ "$1" = "all" ]; then
if [ -f /standalone ]; then
if [ "$2" = "noswift" ]; then
for ID in `virsh list | grep run | awk '{print $2}'`; do nice -n 19 ionice -c3 /admin/swift/vpsbackup $ID noswift; sleep 1m; done
else
for ID in `virsh list | grep run | awk '{print $2}'`; do nice -n 19 ionice -c3 /admin/swift/vpsbackup $ID; sleep 1m; done
fi
exit;
elif [ -f /win ]; then
for ID in `virsh list | grep run | awk '{print $2}' | cut -c8-`; do nice -n 19 ionice -c3 /admin/swift/vpsbackup $ID; sleep 1m; done
exit;
elif [ -f /lin ]; then
for ID in `virsh list | grep run | awk '{print $2}' | cut -c6-`; do nice -n 19 ionice -c3 /admin/swift/vpsbackup $ID; sleep 1m; done
exit;
elif [ -x /usr/sbin/vzlist ]; then
for ID in `vzlist | grep run | awk '{print $1}'`; do nice -n 19 ionice -c3 /admin/swift/vpsbackup $ID; sleep 1m; done
exit;
else
echo "Did not find vps type in all backup at line $LINENO";
exit;
fi
elif [ "$1" = "monthly" ]; then
# monthly skip 1 slice vps
if [ -f /root/cpaneldirect/vps.slicemap ]; then
for ID in `cat /root/cpaneldirect/vps.slicemap | grep -v :1$ | grep : | cut -d: -f1`; do nice -n 19 ionice -c3 /admin/swift/vpsbackup $ID load; sleep $(( RANDOM % (600 - 5 + 1 ) + 5 )); done
exit;
else
echo 'Error: did not find /root/cpaneldirect/vps.slicemap';
exit;
fi
else
ID=$1;
fi
# will be expanded in the future
if [ "$2" = "load" ]; then
echo 'Calling with load checks';
pscheck=`ps auxw | grep [/]admin/swift/cpanelbackup`;
if [ ! "$pscheck" = "" ]; then
echo 'backup process is running, sleepling 1000s';
sleep 1000s;
fi
loadcheck=`cat /proc/loadavg | awk '{print $1}' | cut -d. -f1`;
if [ "$loadcheck" -gt "10" ]; then
echo 'Load over 10 sleeping for 1000s';
sleep 1000s;
fi
fi
date=`date +%Y-%m-%d`
if [ "$2" = "noswift" ]; then
echo 'Using call with no swift - only designed for KVM';
sleep 2s;
noswift=1;
if [ ! -d /backup ]; then
echo '/backup does not exist';
exit;
fi
fi
if [ ! -e /root/tmp ]; then
mkdir -p /root/tmp
fi
LOGFILE=/root/tmp/swift.log
export RUN_BY_CRON=1;
echo | tee -a $LOGFILE
echo " Starting run at $date" | tee -a $LOGFILE
if [ -e /usr/bin/virsh -a ! -e /usr/bin/prlctl ]; then
echo 'KVM' 2>&1 | tee -a $LOGFILE
if [ -f /win ]; then
TYPE=windows;
elif [ -f /cloud ]; then
TYPE=qs;
elif [ -f /standalone ]; then
TYPE='';
else
TYPE=linux;
fi
#SOLUSVM backup
if [ -d /usr/local/solusvm ]; then
TYPE='';
echo 'Found SolusVM';
SOLUSVM=`vgdisplay | grep "VG Name" | awk '{print $3}' | head -n 1`;
echo "VG name is $SOLUSVM";
if [ "$SOLUSVM" = "" ]; then
echo "VG path is blank at $LINENO";
exit;
fi
VZPATH="/dev/${SOLUSVM}";
if [ -d /dev/${SOLUSVM} ]; then
echo "Using /dev/${SOLUSVM}";
else
echo "/dev/${SOLUSVM} is not a directory";
exit;
fi
VPS=`echo ${ID} | cut -d_ -f1`
VMID=${ID};
else
VZPATH=/dev/vz;
VPS="${TYPE}${ID}"
VMID=$VPS;
fi
CONTAINER="vps${ID}"
NAME="${TYPE}${ID}-${date}"
echo "Found: $VPS $CONTAINER $NAME on $date" 2>&1 | tee -a $LOGFILE
if [ ! -x /usr/bin/virsh ]; then
echo "no virsh" 2>&1 | tee -a $LOGFILE
exit;
fi
if [ ! -L ${VZPATH}/${VMID} ]; then
echo "${VZPATH}/$VMID not a symlink" 2>&1 | tee -a $LOGFILE
exit;
fi
check=`virsh list --all | grep $VPS | awk '{print $2}'`;
if [ ! "$check" = "$VPS" ]; then
echo "virsh did not find this vps $VPS" 2>&1 | tee -a $LOGFILE
exit;
fi
myuuID=$(uuidgen)
if [ "$myuuID" = "" ]; then
echo "Error: did not get a uuid for snapshot" 2>&1 | tee -a $LOGFILE
exit;
fi
function cleanupkvm()
{
if [ "$VPS" = "" ]; then
echo "Clean up has no VPS id" 2>&1 | tee -a $LOGFILE
exit;
fi
if [ "$myuuID" = "" ]; then
echo "Clean up has no $myuuID" 2>&1 | tee -a $LOGFILE
exit;
fi
if [ ! "$noswift" = "1" ]; then
echo "Deleting after 60 days by sending deleteafter $CONTAINER $NAME 60" 2>&1 | tee -a $LOGFILE
sleep 10s;
/admin/swift/deleteafter $CONTAINER $NAME 60 2>&1 | tee -a $LOGFILE
fi
echo y | lvremove ${VZPATH}/$myuuID 2>&1 | tee -a $LOGFILE
echo 'Settling for 15 seconds';
sleep 15s;
}
if [ ! "$noswift" = "1" ]; then
/admin/swift/ismkdir $CONTAINER
fi
echo "creating $VPS snapshot called $myuuID" 2>&1 | tee -a $LOGFILE
trap "{ cleanupkvm; exit; }" EXIT
lvcreate -L2048M -s -n $myuuID ${VZPATH}/$VMID 2>&1 | tee -a $LOGFILE
if [ ! "$noswift" = "1" ]; then
/admin/swift/ddlvm $myuuID $CONTAINER $NAME 2>&1 | tee -a $LOGFILE
else
echo 'Non swift DD' | tee -a $LOGFILE
dd bs=4096 conv=noerror if=${VZPATH}/$myuuID | gzip -1c > /backup/$NAME.gz | tee -a $LOGFILE
fi
#cleanupkvm;
trap - TERM
elif [ -f /usr/bin/prlctl ]; then
echo 'Openvz 7' 2>&1 | tee -a $LOGFILE
CTID=${ID};
check=`prlctl list --all | grep "CT vps${CTID}$"`;
if [ "$check" = "" ]; then
prlctl list --all
echo
echo "No running vps for ID ${CTID}" 2>&1 | tee -a $LOGFILE
exit;
fi
MYUUID=`prlctl list -o uuid --no-header vps${CTID} | cut -d{ -f2 | cut -d} -f1`;
if [ ! -d /vz/private/${MYUUID} ]; then
echo "/vz/private/${MYUUID} does not exist for CTID ${CTID}" 2>&1 | tee -a $LOGFILE
exit;
fi
function cleanupvz7()
{
if [ "$CTID" = "" ]; then
echo "Clean up has no CTID" 2>&1 | tee -a $LOGFILE
exit;
fi
mysnap=`prlctl snapshot-list vps${CTID} --tree | cut -d{ -f2 | cut -d} -f1`;
if [ "$mysnap" = "" ]; then
echo 'Got back a blank snapshot, not deleting';
else
prlctl snapshot-delete vps${CTID} --id $mysnap 2>&1 | tee -a $LOGFILE
echo "Deleting after 60 days sending deleteafter vps${CTID} ${CTID}-backup-$date 60" 2>&1 | tee -a $LOGFILE
sleep 10s;
/admin/swift/deleteafter vps${CTID} autobackup-$date 60 2>&1 | tee -a $LOGFILE
echo 'Settling for 15 seconds';
sleep 15s;
fi
}
trap "{ cleanupvz7; exit; }" EXIT
prlctl snapshot vps${CTID} 2>&1 | tee -a $LOGFILE
/admin/swift/ismkdir vps${CTID} 2>&1 | tee -a $LOGFILE
# removed root.hdd from here as we need to ensure we get all the data
/admin/swift/fly vps${CTID} /vz/private/${MYUUID}/ frombackup ${CTID}-backup-$date $myuuID 2>&1 | tee -a $LOGFILE
#cleanupvz;
trap - TERM
elif [ -f /usr/sbin/vzctl ]; then
echo 'OpenVZ' 2>&1 | tee -a $LOGFILE
CTID=$ID;
if [ ! -e /etc/vz/conf/$CTID.conf ]; then
echo 'vz/conf file not found' 2>&1 | tee -a $LOGFILE
exit;
fi
if [ ! `vzlist -H $CTID -o layout` = "ploop" ]; then
echo "$CTID is not ploop" 2>&1 | tee -a $LOGFILE
echo 'Will run slower on the fly backup with out a snapshot' 2>&1 | tee -a $LOGFILE
sleep 5s;
if [ -d /vz/private/${CTID} ]; then
/admin/swift/fly vps${CTID} /vz/private/${CTID} frombackup simfs-${CTID}-backup-$date | tee -a $LOGFILE
else
echo "/vz/private/${CTID} does not exist" | tee -a $LOGFILE
exit;
fi
fi
function cleanupvz()
{
if [ "$CTID" = "" ]; then
echo "Clean up has no CTID" 2>&1 | tee -a $LOGFILE
exit;
fi
if [ "$myuuID" = "" ]; then
echo "Clean up has no $myuuID" 2>&1 | tee -a $LOGFILE
exit;
fi
vzctl snapshot-delete $CTID --id $myuuID 2>&1 | tee -a $LOGFILE
echo "Deleting after 60 days sending deleteafter vps${CTID} ${CTID}-backup-$date 60" 2>&1 | tee -a $LOGFILE
sleep 10s;
/admin/swift/deleteafter vps${CTID} autobackup-$date 60 2>&1 | tee -a $LOGFILE
echo 'Settling for 15 seconds';
sleep 15s;
}
myuuID=$(uuidgen)
VE_PRIVATE=$(vzlist -H -o private $CTID)
trap "{ cleanupvz; exit; }" EXIT
vzctl snapshot $CTID --id $myuuID --skip-suspend --skip-config 2>&1 | tee -a $LOGFILE
/admin/swift/ismkdir vps${CTID} 2>&1 | tee -a $LOGFILE
/admin/swift/fly vps${CTID} $VE_PRIVATE/root.hdd/ frombackup ${CTID}-backup-$date $myuuID 2>&1 | tee -a $LOGFILE
#cleanupvz;
trap - TERM
else
echo 'No VM software found' 2>&1 | tee -a $LOGFILE
fi