-
Notifications
You must be signed in to change notification settings - Fork 24
/
butgg.sh
480 lines (456 loc) · 14.7 KB
/
butgg.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/bin/sh
# Set variables
GITHUB_LINK="https://raw.githubusercontent.com/mbrother2/backuptogoogle/master"
GO_FILE="go1.12.5.freebsd-amd64"
BUTGG_CONF="${HOME}/.gdrive/butgg.conf"
DF_BACKUP_DIR="${HOME}/backup"
DF_SYNC_FILE="No"
DF_LOG_FILE="${HOME}/.gdrive/butgg.log"
DF_DAY_REMOVE="7"
DF_GDRIVE_ID="None"
DF_EMAIL_USER="None"
DF_EMAIL_PASS="None"
DF_EMAIL_TO="None"
GDRIVE_BIN="${HOME}/bin/gdrive"
GDRIVE_TOKEN="${HOME}/.gdrive/token_v2.json"
CRON_BACKUP="${HOME}/bin/cron_backup.sh"
SETUP_FILE="${HOME}/bin/butgg.sh"
CRON_TEMP="${HOME}/.gdrive/old_cron"
SECOND_OPTION=$2
# Date variables
TODAY=`date +"%d_%m_%Y"`
# Color variables
GREEN='\e[32m'
RED='\e[31m'
YELLOW='\e[33m'
REMOVE='\e[0m'
# Change color of words
change_color(){
case $1 in
green) echo -e "${GREEN}$2${REMOVE}";;
red) echo -e "${RED}$2${REMOVE}";;
yellow) echo -e "${YELLOW}$2${REMOVE}";;
*) echo "$2";;
esac
}
# Check MD5 of downloaded file
check_md5sum(){
curl -o $2 ${GITHUB_LINK}/$1
ORIGIN_MD5=`curl -s ${GITHUB_LINK}/MD5SUM | grep $1 | awk '{print $1}'`
LOCAL_MD5=`md5 $2 | awk '{print $4}'`
if [ "${ORIGIN_MD5}" == "${LOCAL_MD5}" ]
then
show_write_log "Check md5sum for file $1 successful"
else
show_write_log "`change_color red [CHECKS][FAIL]` Can not verify md5 for file $1. Exit!"
exit 1
fi
}
# Check log file
check_log_file(){
if [ ! -f ${BUTGG_CONF} ]
then
LOG_FILE=${DF_LOG_FILE}
else
LOG_FILE=`cat ${BUTGG_CONF} | grep "^LOG_FILE" | cut -d"=" -f2 | sed 's/"//g' | sed "s/'//g"`
if [ "${LOG_FILE}" == "" ]
then
LOG_FILE=${DF_LOG_FILE}
fi
fi
create_dir .gdrive
create_dir bin
}
# Write log
show_write_log(){
echo "`date "+[ %d/%m/%Y %H:%M:%S ]"` $1" | tee -a ${LOG_FILE}
}
# Create necessary directory
create_dir(){
if [ ! -d ${HOME}/$1 ]
then
mkdir -p ${HOME}/$1
if [ ! -d ${HOME}/$1 ]
then
echo "Can not create directory ${HOME}/$1. Exit"
exit 1
else
if [ "$1" == ".gdrive" ]
then
show_write_log "---"
show_write_log "Creating necessary directory..."
fi
show_write_log "Create directory ${HOME}/$1 successful"
fi
else
if [ "$1" == ".gdrive" ]
then
show_write_log "---"
show_write_log "Creating necessary directory..."
fi
show_write_log "Directory ${HOME}/$1 existed. Skip"
fi
echo 1 >> ${HOME}/$1/test.txt
if [ $? -ne 0 ]
then
echo "Can not write to ${HOME}/$1. Exit"
exit 1
else
show_write_log "Check write to ${HOME}/$1 successful"
fi
rm -f ${HOME}/$1/test.txt
}
check_package(){
which $1
if [ $? -ne 0 ]
then
show_write_log "Command $1 not found. Trying to install $1..."
sleep 3
${INSTALL_CM} install -y $1
which $1
if [ $? -ne 0 ]
then
show_write_log "Can not install $1 package. Please install $1 manually."
exit 1
fi
fi
show_write_log "Package $1 is installed"
}
# Write config
write_config(){
if [ "$3" == "" ]
then
VAR=$1
eval "$VAR"="$2"
if [ -f ${BUTGG_CONF} ]
then
sed -i "/^$1/d" ${BUTGG_CONF}
fi
echo "$1=$2" >> ${BUTGG_CONF}
else
VAR=$1
eval "$VAR"="$3"
if [ -f ${BUTGG_CONF} ]
then
sed -i "/^$1/d" ${BUTGG_CONF}
fi
echo "$1=$3" >> ${BUTGG_CONF}
fi
}
# Check network
check_network(){
show_write_log "Cheking network..."
curl -sI raw.githubusercontent.com >/dev/null
if [ $? -eq 0 ]
then
show_write_log "Connect Github successful"
else
show_write_log "`change_color red [CHECKS][FAIL]` Can not connect to Github file, please check your network. Exit"
exit 1
fi
curl -sI dl.google.com >/dev/null
if [ $? -eq 0 ]
then
show_write_log "Connect Google successful"
else
show_write_log "`change_color red [CHECKS][FAIL]` Can not connect to Google file, please check your network. Exit"
exit 1
fi
}
# Detect OS
detect_os(){
show_write_log "Checking OS..."
if [ -f /etc/freebsd-update.conf ]
then
SYS="BSD"
OS="FreeBSD"
INSTALL_CM="pkg"
else
show_write_log "Sorry! We do not support your OS. Exit"
exit 1
fi
show_write_log "OS supported"
show_write_log "Checking necessary package..."
check_package curl
if [ "${FIRST_OPTION}" != "--update" ]
then
check_package git
fi
}
# Download file from Github
download_file(){
show_write_log "Downloading script cron file from github..."
check_md5sum cron_backup.sh "${CRON_BACKUP}"
show_write_log "Downloading setup file from github..."
check_md5sum butgg.sh "${SETUP_FILE}"
chmod 755 ${CRON_BACKUP} ${SETUP_FILE}
}
# Build GDRIVE_BIN
build_gdrive(){
cd $HOME/bin
show_write_log "Downloading go from Google..."
curl -o ${GO_FILE}.tar.gz https://dl.google.com/go/${GO_FILE}.tar.gz
show_write_log "Extracting go lang..."
tar -xf ${GO_FILE}.tar.gz
show_write_log "Cloning gdrive project from Github..."
rm -rf gdrive
git clone https://github.com/gdrive-org/gdrive.git
show_write_log "Build your own gdrive!"
echo ""
echo "Read more https://github.com/mbrother2/backuptogoogle/wiki/Create-own-Google-credential-step-by-step"
read -p " Your Google API client_id: " gg_client_id
read -p " Your Google API client_secret: " gg_client_secret
sed -i ".${TODAY}" "s#^const ClientId =.*#const ClientId = \"${gg_client_id}\"#g" $HOME/bin/gdrive/handlers_drive.go
sed -i ".${TODAY}" "s#^const ClientSecret =.*#const ClientSecret = \"${gg_client_secret}\"#g" $HOME/bin/gdrive/handlers_drive.go
echo ""
show_write_log "Building gdrive..."
cd $HOME/bin/gdrive
$HOME/bin/go/bin/go get github.com/prasmussen/gdrive
$HOME/bin/go/bin/go build -ldflags '-w -s'
if [ $? -ne 0 ]
then
show_write_log "`change_color red [ERROR]` Can not build gdrive. Exit"
exit 1
else
show_write_log "Build gdrive successful. Gdrive bin locate here ${GDRIVE_BIN} "
fi
mv $HOME/bin/gdrive/gdrive $HOME/bin/gdrive.bin
chmod 755 $HOME/bin/gdrive.bin
rm -f $HOME/bin/${GO_FILE}.tar.gz
rm -rf $HOME/bin/go
rm -rf $HOME/bin/gdrive
mv $HOME/bin/gdrive.bin $HOME/bin/gdrive
}
# Setup gdrive credential
setup_credential(){
show_write_log "Setting up gdrive credential..."
if [ "${SECOND_OPTION}" == "credential" ]
then
if [ -f ${GDRIVE_TOKEN} ]
then
rm -f ${GDRIVE_TOKEN}
fi
fi
${GDRIVE_BIN} about
if [ $? -ne 0 ]
then
show_write_log "`change_color yellow [WARNING]` Can not create gdrive credential. Please run \"${GDRIVE_BIN} about\" to create it after"
sleep 3
else
show_write_log "Setup gdrive credential successful"
fi
}
# Set up config file
setup_config(){
show_write_log "Setting up config file..."
echo ""
read -p " Which directory on your server do you want to upload to Google Drive?(default ${DF_BACKUP_DIR}): " BACKUP_DIR
read -p " How many days do you want to keep backup on Google Drive?(default ${DF_DAY_REMOVE}): " DAY_REMOVE
echo ""
echo "Read more https://github.com/mbrother2/backuptogoogle/wiki/What-is-the-option-SYNC_FILE%3F"
read -p " Do you want only sync file(default no)(y/n): " SYNC_FILE
echo ""
echo "Read more: https://github.com/mbrother2/backuptogoogle/wiki/Get-Google-folder-ID)"
read -p " Your Google folder ID(default ${DF_GDRIVE_ID}): " GDRIVE_ID
echo ""
echo "Read more https://github.com/mbrother2/backuptogoogle/wiki/Turn-on-2-Step-Verification-&-create-app's-password-for-Google-email"
read -p " Do you want to send email if upload error(default no)(y/n): " SEND_EMAIL
if [ "${SEND_EMAIL}" == "y" ]
then
read -p " Your Google email user name: " EMAIL_USER
read -p " Your Google email password: " EMAIL_PASS
read -p " Which email will be receive notify?: " EMAIL_TO
fi
if [ "${SYNC_FILE}" == "y" ]
then
SYNC_FILE="Yes"
else
SYNC_FILE="No"
fi
echo ""
echo "LOG_FILE=${LOG_FILE}" > ${BUTGG_CONF}
write_config BACKUP_DIR "${DF_BACKUP_DIR}" "${BACKUP_DIR}"
write_config DAY_REMOVE "${DF_DAY_REMOVE}" "${DAY_REMOVE}"
write_config GDRIVE_ID "${DF_GDRIVE_ID}" "${GDRIVE_ID}"
write_config EMAIL_USER "${DF_EMAIL_USER}" "${EMAIL_USER}"
write_config EMAIL_PASS "${DF_EMAIL_PASS}" "${EMAIL_PASS}"
write_config EMAIL_TO "${DF_EMAIL_TO}" "${EMAIL_TO}"
write_config SYNC_FILE "${DF_SYNC_FILE}" "${SYNC_FILE}"
if [ $? -ne 0 ]
then
show_write_log "`change_color red [ERROR]` Can not write config to file ${BUTGG_CONF}. Please check permission of this file. Exit"
exit 1
else
if [ ! -d ${BACKUP_DIR} ]
then
show_write_log "`change_color yellow [WARNING]` Directory ${BACKUP_DIR} does not exist! Ensure you will be create it after."
sleep 3
fi
show_write_log "Setup config file successful"
fi
}
# Set up cron backup
setup_cron(){
show_write_log "Setting up cron backup..."
crontab -l > ${CRON_TEMP}
CHECK_CRON=`cat ${CRON_TEMP} | grep -c "cron_backup.sh"`
if [ ${CHECK_CRON} -eq 0 ]
then
echo "PATH=$PATH" >> ${CRON_TEMP}
echo "0 0 * * * sh ${CRON_BACKUP} >/dev/null 2>&1" >> ${CRON_TEMP}
crontab ${CRON_TEMP}
if [ $? -ne 0 ]
then
show_write_log "Can not setup cronjob to backup! Please check again"
SHOW_CRON="`change_color yellow [WARNING]` Can not setup cronjob to backup"
else
show_write_log "Setup cronjob to backup successful"
SHOW_CRON="0 0 * * * sh ${CRON_BACKUP} >/dev/null 2>&1"
fi
else
show_write_log "Cron backup existed. Skip"
SHOW_CRON=`cat ${CRON_TEMP} | grep "cron_backup.sh"`
fi
rm -f ${CRON_TEMP}
}
# Show information
show_info(){
echo ""
if [ "${SECOND_OPTION}" == config ]
then
show_write_log "+-----"
show_write_log "| SUCESSFUL! Your information:"
show_write_log "| Backup dir : ${BACKUP_DIR}"
show_write_log "| Keep backup : ${DAY_REMOVE} days"
show_write_log "| Google folder ID: ${GDRIVE_ID}"
show_write_log "| Your email : ${EMAIL_USER}"
show_write_log "| Email password : ${EMAIL_PASS}"
show_write_log "| Email notify : ${EMAIL_TO}"
show_write_log "| Config file : ${BUTGG_CONF}"
show_write_log "+-----"
else
show_write_log "+-----"
show_write_log "| SUCESSFUL! Your information:"
show_write_log "| Backup dir : ${BACKUP_DIR}"
show_write_log "| Config file : ${BUTGG_CONF}"
show_write_log "| Log file : ${LOG_FILE}"
show_write_log "| Keep backup : ${DAY_REMOVE} days"
show_write_log "| Google folder ID: ${GDRIVE_ID}"
show_write_log "| Your email : ${EMAIL_USER}"
show_write_log "| Email password : ${EMAIL_PASS}"
show_write_log "| Email notify : ${EMAIL_TO}"
show_write_log "| butgg.sh file : ${SETUP_FILE}"
show_write_log "| Cron backup file: ${CRON_BACKUP}"
show_write_log "| Gdrive bin file : ${GDRIVE_BIN}"
show_write_log "| Cron backup : ${SHOW_CRON}"
show_write_log "| Google token : ${GDRIVE_TOKEN}"
show_write_log "+-----"
echo ""
echo " If you get trouble when use butgg.sh please report here:"
echo " https://github.com/mbrother2/backuptogoogle/issues"
fi
}
_setup(){
check_log_file
if [ -z "${SECOND_OPTION}" ]
then
detect_os
check_network
download_file
build_gdrive
setup_credential
setup_config
setup_cron
show_info
else
case ${SECOND_OPTION} in
config)
setup_config
show_info
;;
credential)
setup_credential
;;
only-build)
detect_os
check_network
build_gdrive
;;
no-build)
detect_os
check_network
download_file
setup_credential
setup_config
setup_cron
show_info
;;
no-update)
detect_os
check_network
build_gdrive
setup_credential
setup_config
setup_cron
show_info
;;
*)
show_write_log "No such command: ${SECOND_OPTION}. Please use butgg.sh --help"
;;
esac
fi
}
_update(){
check_log_file
detect_os
check_network
download_file
}
_uninstall(){
check_log_file
show_write_log "Removing all butgg.sh scripts..."
rm -f ${GDRIVE_BIN} ${CRON_BACKUP} ${SETUP_FILE}
if [ $? -ne 0 ]
then
show_write_log "Can not remove all butgg.sh scripts. Please check permission of these files"
else
show_write_log "Remove all butgg.sh scripts successful"
fi
read -p " Do you want remove ${HOME}/.gdrive directory?(y/n) " REMOVE_GDRIVE_DIR
if [ "${REMOVE_GDRIVE_DIR}" == "y" ] || [ "${REMOVE_GDRIVE_DIR}" == "Y" ]
then
rm -rf ${HOME}/.gdrive
if [ $? -ne 0 ]
then
show_write_log "Can not remove directory ${HOME}/.gdrive. Please check permission of this directory"
else
echo "Remove directory ${HOME}/.gdrive successful"
fi
else
show_write_log "Skip remove ${HOME}/.gdrive directory"
fi
}
_help(){
echo "butgg.sh - Backup to Google Drive solution"
echo ""
echo "Usage: butgg.sh [options] [command]"
echo ""
echo "Options:"
echo " --help show this help message and exit"
echo " --setup setup or reset all scripts & config file"
echo " config only setup config"
echo " credential only setup credential"
echo " only-build only build gdrive bin"
echo " no-build setup butgg without build gdrive"
echo " no-update setup butgg without update script"
echo " --update update to latest version"
echo " --uninstall remove all butgg scripts and .gdrive directory"
}
# Main functions
case $1 in
--help) _help ;;
--setup) _setup ;;
--update) _update ;;
--uninstall) _uninstall ;;
*) echo "No such option: $1. Please use butgg.sh --help" ;;
esac