forked from johnfelipe/lamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·356 lines (335 loc) · 14.3 KB
/
backup.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/bin/bash
# Author: yeho <lj2007331 AT gmail.com>
# BLOG: https://linuxeye.com
#
# Notes: OneinStack for CentOS/RedHat 6+ Debian 7+ and Ubuntu 12+
#
# Project home page:
# https://oneinstack.com
# https://github.com/oneinstack/oneinstack
# Check if user is root
[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
oneinstack_dir=$(dirname "`readlink -f $0`")
pushd ${oneinstack_dir}/tools > /dev/null
. ../options.conf
[ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}
DB_Local_BK() {
for D in `echo ${db_name} | tr ',' ' '`
do
./db_bk.sh ${D}
done
}
DB_Remote_BK() {
for D in `echo ${db_name} | tr ',' ' '`
do
./db_bk.sh ${D}
DB_GREP="DB_${D}_`date +%Y%m%d`"
DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
echo "file:::${backup_dir}/${DB_FILE} ${backup_dir} push" >> config_backup.txt
echo "com:::[ -e "${backup_dir}/${DB_FILE}" ] && rm -rf ${backup_dir}/DB_${D}_$(date +%Y%m%d --date="${expired_days} days ago")_*.tgz" >> config_backup.txt
done
}
DB_OSS_BK() {
for D in `echo ${db_name} | tr ',' ' '`
do
./db_bk.sh ${D}
DB_GREP="DB_${D}_`date +%Y%m%d`"
DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
/usr/local/bin/ossutil cp -f ${backup_dir}/${DB_FILE} oss://${oss_bucket}/`date +%F`/${DB_FILE}
if [ $? -eq 0 ]; then
/usr/local/bin/ossutil rm -rf oss://${oss_bucket}/`date +%F --date="${expired_days} days ago"`/
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${backup_dir}/${DB_FILE}
fi
done
}
DB_COS_BK() {
for D in `echo ${db_name} | tr ',' ' '`
do
./db_bk.sh ${D}
DB_GREP="DB_${D}_`date +%Y%m%d`"
DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
${python_install_dir}/bin/coscmd upload ${backup_dir}/${DB_FILE} /`date +%F`/${DB_FILE}
if [ $? -eq 0 ]; then
${python_install_dir}/bin/coscmd delete -r -f `date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${backup_dir}/${DB_FILE}
fi
done
}
DB_UPYUN_BK() {
for D in `echo ${db_name} | tr ',' ' '`
do
./db_bk.sh ${D}
DB_GREP="DB_${D}_`date +%Y%m%d`"
DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
/usr/local/bin/upx put ${backup_dir}/${DB_FILE} /`date +%F`/${DB_FILE}
if [ $? -eq 0 ]; then
/usr/local/bin/upx rm -a `date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${backup_dir}/${DB_FILE}
fi
done
}
DB_QINIU_BK() {
for D in `echo ${db_name} | tr ',' ' '`
do
./db_bk.sh ${D}
DB_GREP="DB_${D}_`date +%Y%m%d`"
DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
/usr/local/bin/qshell rput ${qiniu_bucket} /`date +%F`/${DB_FILE} ${backup_dir}/${DB_FILE}
if [ $? -eq 0 ]; then
/usr/local/bin/qshell listbucket ${qiniu_bucket} /`date +%F --date="${expired_days} days ago"` /tmp/qiniu.txt > /dev/null 2>&1
/usr/local/bin/qshell batchdelete -force ${qiniu_bucket} /tmp/qiniu.txt > /dev/null 2>&1
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${backup_dir}/${DB_FILE}
rm -f /tmp/qiniu.txt
fi
done
}
DB_S3_BK() {
for D in `echo ${db_name} | tr ',' ' '`
do
./db_bk.sh ${D}
DB_GREP="DB_${D}_`date +%Y%m%d`"
DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
${python_install_dir}/bin/s3cmd put ${backup_dir}/${DB_FILE} s3://${s3_bucket}/`date +%F`/${DB_FILE}
if [ $? -eq 0 ]; then
${python_install_dir}/bin/s3cmd rm -r s3://${s3_bucket}/`date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${backup_dir}/${DB_FILE}
fi
done
}
DB_GDRIVE_BK() {
# get the IP information
IPADDR=$(../include/get_ipaddr.py)
IPADDR=${IPADDR:-127.0.0.1}
Parent_root_id=$(/usr/local/bin/gdrive list --no-header -q "trashed = false and name = '${IPADDR}'" | awk '{print $1}' | head -1)
[ -z "${Parent_root_id}" ] && sleep 60 && Parent_root_id=$(/usr/local/bin/gdrive mkdir ${IPADDR} | awk '{print $2}')
sleep 60
Parent_sub_id=$(/usr/local/bin/gdrive list --no-header -q "'${Parent_root_id}' in parents and trashed = false and name = '`date +%F`'" | awk '{print $1}' | head -1)
[ -z "${Parent_sub_id}" ] && sleep 60 && Parent_sub_id=$(/usr/local/bin/gdrive mkdir -p ${Parent_root_id} `date +%F` | awk '{print $2}')
sleep 60
for D in `echo ${db_name} | tr ',' ' '`
do
./db_bk.sh ${D}
DB_GREP="DB_${D}_`date +%Y%m%d`"
DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
/usr/local/bin/gdrive upload -p ${Parent_sub_id} ${backup_dir}/${DB_FILE}
sleep 120
if [ $? -eq 0 ]; then
Parent_expired_id=$(/usr/local/bin/gdrive list --no-header -q "'${Parent_root_id}' in parents and trashed = false and name = '`date +%F --date="${expired_days} days ago"`'" | awk '{print $1}' | head -1)
[ -n "${Parent_expired_id}" ] && sleep 60 && /usr/local/bin/gdrive delete -r ${Parent_expired_id} > /dev/null 2>&1
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${backup_dir}/${DB_FILE}
sleep 60
fi
done
}
DB_DROPBOX_BK() {
for D in `echo ${db_name} | tr ',' ' '`
do
./db_bk.sh ${D}
DB_GREP="DB_${D}_`date +%Y%m%d`"
DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
/usr/local/bin/dbxcli put ${backup_dir}/${DB_FILE} `date +%F`/${DB_FILE}
if [ $? -eq 0 ]; then
/usr/local/bin/dbxcli rm -f `date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${backup_dir}/${DB_FILE}
fi
done
}
WEB_LOCAL_BK() {
for W in `echo ${website_name} | tr ',' ' '`
do
./website_bk.sh $W
done
}
WEB_Remote_BK() {
for W in `echo ${website_name} | tr ',' ' '`
do
if [ `du -sm "${wwwroot_dir}/${WebSite}" | awk '{print $1}'` -lt 2048 ]; then
./website_bk.sh $W
Web_GREP="Web_${W}_`date +%Y%m%d`"
Web_FILE=`ls -lrt ${backup_dir} | grep ${Web_GREP} | tail -1 | awk '{print $NF}'`
echo "file:::${backup_dir}/${Web_FILE} ${backup_dir} push" >> config_backup.txt
echo "com:::[ -e "${backup_dir}/${Web_FILE}" ] && rm -rf ${backup_dir}/Web_${W}_$(date +%Y%m%d --date="${expired_days} days ago")_*.tgz" >> config_backup.txt
else
echo "file:::${wwwroot_dir}/$W ${backup_dir} push" >> config_backup.txt
fi
done
}
WEB_OSS_BK() {
for W in `echo $website_name | tr ',' ' '`
do
[ ! -e "${wwwroot_dir}/${WebSite}" ] && { echo "[${wwwroot_dir}/${WebSite}] not exist"; break; }
PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
if [ ! -e "${PUSH_FILE}" ]; then
pushd ${wwwroot_dir} > /dev/null
tar czf ${PUSH_FILE} ./$W
popd > /dev/null
fi
/usr/local/bin/ossutil cp -f ${PUSH_FILE} oss://${oss_bucket}/`date +%F`/${PUSH_FILE##*/}
if [ $? -eq 0 ]; then
/usr/local/bin/ossutil rm -rf oss://${oss_bucket}/`date +%F --date="${expired_days} days ago"`/
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${PUSH_FILE}
fi
done
}
WEB_COS_BK() {
for W in `echo ${website_name} | tr ',' ' '`
do
[ ! -e "${wwwroot_dir}/${WebSite}" ] && { echo "[${wwwroot_dir}/${WebSite}] not exist"; break; }
PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
if [ ! -e "${PUSH_FILE}" ]; then
pushd ${wwwroot_dir} > /dev/null
tar czf ${PUSH_FILE} ./$W
popd > /dev/null
fi
${python_install_dir}/bin/coscmd upload ${PUSH_FILE} /`date +%F`/${PUSH_FILE##*/}
if [ $? -eq 0 ]; then
${python_install_dir}/bin/coscmd delete -r -f `date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${PUSH_FILE}
fi
done
}
WEB_UPYUN_BK() {
for W in `echo ${website_name} | tr ',' ' '`
do
[ ! -e "${wwwroot_dir}/${WebSite}" ] && { echo "[${wwwroot_dir}/${WebSite}] not exist"; break; }
[ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}
PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
if [ ! -e "${PUSH_FILE}" ]; then
pushd ${wwwroot_dir} > /dev/null
tar czf ${PUSH_FILE} ./$W
popd > /dev/null
fi
/usr/local/bin/upx put ${PUSH_FILE} /`date +%F`/${PUSH_FILE##*/}
if [ $? -eq 0 ]; then
/usr/local/bin/upx rm -a `date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${PUSH_FILE}
fi
done
}
WEB_QINIU_BK() {
for W in `echo ${website_name} | tr ',' ' '`
do
[ ! -e "${wwwroot_dir}/${WebSite}" ] && { echo "[${wwwroot_dir}/${WebSite}] not exist"; break; }
[ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}
PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
if [ ! -e "${PUSH_FILE}" ]; then
pushd ${wwwroot_dir} > /dev/null
tar czf ${PUSH_FILE} ./$W
popd > /dev/null
fi
/usr/local/bin/qshell rput ${qiniu_bucket} /`date +%F`/${PUSH_FILE##*/} ${PUSH_FILE}
if [ $? -eq 0 ]; then
/usr/local/bin/qshell listbucket ${qiniu_bucket} /`date +%F --date="${expired_days} days ago"` /tmp/qiniu.txt > /dev/null 2>&1
/usr/local/bin/qshell batchdelete -force ${qiniu_bucket} /tmp/qiniu.txt > /dev/null 2>&1
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${PUSH_FILE}
rm -f /tmp/qiniu.txt
fi
done
}
WEB_S3_BK() {
for W in `echo ${website_name} | tr ',' ' '`
do
[ ! -e "${wwwroot_dir}/${WebSite}" ] && { echo "[${wwwroot_dir}/${WebSite}] not exist"; break; }
[ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}
PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
if [ ! -e "${PUSH_FILE}" ]; then
pushd ${wwwroot_dir} > /dev/null
tar czf ${PUSH_FILE} ./$W
popd > /dev/null
fi
${python_install_dir}/bin/s3cmd put ${PUSH_FILE} s3://${s3_bucket}/`date +%F`/${PUSH_FILE##*/}
if [ $? -eq 0 ]; then
${python_install_dir}/bin/s3cmd rm -r s3://${s3_bucket}/`date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${PUSH_FILE}
fi
done
}
WEB_GDRIVE_BK() {
# get the IP information
IPADDR=$(../include/get_ipaddr.py)
IPADDR=${IPADDR:-127.0.0.1}
Parent_root_id=$(/usr/local/bin/gdrive list --no-header -q "trashed = false and name = '${IPADDR}'" | awk '{print $1}' | head -1)
[ -z "${Parent_root_id}" ] && sleep 60 && Parent_root_id=$(/usr/local/bin/gdrive mkdir ${IPADDR} | awk '{print $2}')
sleep 60
Parent_sub_id=$(/usr/local/bin/gdrive list --no-header -q "'${Parent_root_id}' in parents and trashed = false and name = '`date +%F`'" | awk '{print $1}' | head -1)
[ -z "${Parent_sub_id}" ] && sleep 60 && Parent_sub_id=$(/usr/local/bin/gdrive mkdir -p ${Parent_root_id} `date +%F` | awk '{print $2}')
sleep 60
for W in `echo ${website_name} | tr ',' ' '`
do
[ ! -e "${wwwroot_dir}/${WebSite}" ] && { echo "[${wwwroot_dir}/${WebSite}] not exist"; break; }
[ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}
PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
if [ ! -e "${PUSH_FILE}" ]; then
pushd ${wwwroot_dir} > /dev/null
tar czf ${PUSH_FILE} ./$W
popd > /dev/null
fi
/usr/local/bin/gdrive upload -p ${Parent_sub_id} ${PUSH_FILE}
sleep 120
if [ $? -eq 0 ]; then
Parent_expired_id=$(/usr/local/bin/gdrive list --no-header -q "'${Parent_root_id}' in parents and trashed = false and name = '`date +%F --date="${expired_days} days ago"`'" | awk '{print $1}' | head -1)
[ -n "${Parent_expired_id}" ] && sleep 60 && /usr/local/bin/gdrive delete -r ${Parent_expired_id} > /dev/null 2>&1
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${PUSH_FILE}
sleep 60
fi
done
}
WEB_DROPBOX_BK() {
for W in `echo ${website_name} | tr ',' ' '`
do
[ ! -e "${wwwroot_dir}/${WebSite}" ] && { echo "[${wwwroot_dir}/${WebSite}] not exist"; break; }
[ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}
PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
if [ ! -e "${PUSH_FILE}" ]; then
pushd ${wwwroot_dir} > /dev/null
tar czf ${PUSH_FILE} ./$W
popd > /dev/null
fi
/usr/local/bin/dbxcli put ${PUSH_FILE} `date +%F`/${PUSH_FILE##*/}
if [ $? -eq 0 ]; then
/usr/local/bin/dbxcli rm -f `date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
[ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${PUSH_FILE}
fi
done
}
for DEST in `echo ${backup_destination} | tr ',' ' '`
do
if [ "${DEST}" == 'local' ]; then
[ -n "`echo ${backup_content} | grep -ow db`" ] && DB_Local_BK
[ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_LOCAL_BK
fi
if [ "${DEST}" == 'remote' ]; then
echo "com:::[ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}" > config_backup.txt
[ -n "`echo ${backup_content} | grep -ow db`" ] && DB_Remote_BK
[ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_Remote_BK
./mabs.sh -c config_backup.txt -T -1 | tee -a mabs.log
fi
if [ "${DEST}" == 'oss' ]; then
[ -n "`echo ${backup_content} | grep -ow db`" ] && DB_OSS_BK
[ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_OSS_BK
fi
if [ "${DEST}" == 'cos' ]; then
[ -n "`echo ${backup_content} | grep -ow db`" ] && DB_COS_BK
[ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_COS_BK
fi
if [ "${DEST}" == 'upyun' ]; then
[ -n "`echo ${backup_content} | grep -ow db`" ] && DB_UPYUN_BK
[ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_UPYUN_BK
fi
if [ "${DEST}" == 'qiniu' ]; then
[ -n "`echo ${backup_content} | grep -ow db`" ] && DB_QINIU_BK
[ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_QINIU_BK
fi
if [ "${DEST}" == 's3' ]; then
[ -n "`echo ${backup_content} | grep -ow db`" ] && DB_S3_BK
[ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_S3_BK
fi
if [ "${DEST}" == 'gdrive' ]; then
[ -n "`echo ${backup_content} | grep -ow db`" ] && DB_GDRIVE_BK
[ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_GDRIVE_BK
fi
if [ "${DEST}" == 'dropbox' ]; then
[ -n "`echo ${backup_content} | grep -ow db`" ] && DB_DROPBOX_BK
[ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_DROPBOX_BK
fi
done