forked from bupy7/sh-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup
executable file
·76 lines (62 loc) · 1.37 KB
/
backup
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
#!/bin/bash
ROOT_PATH=$(cd $(dirname $0) && pwd);
. $ROOT_PATH/include/core
print 'Backup...' $BOLD$CYAN
TIMESTAMP=$(date +%s)
DATE_EXPIRE=$(expr $TIMESTAMP - $MAX_STORAGE_TIME)
case "$(uname -s)" in
# OSX
Darwin)
TMP_DIR=$(mktemp -d -t backup)
OLD_DIR=$(date -r $DATE_EXPIRE +%F)
;;
# Linux
Linux)
TMP_DIR=$(mktemp -dt)
OLD_DIR=$(date --date="@$DATE_EXPIRE" +%F)
;;
# Other
*)
print 'Your system is not supported.' $BOLD$RED
exit
;;
esac
print '- Prepare files...'
cd $ROOT_DIR
tar -czf $TMP_DIR/root.tgz ./
print '- Prepare database...'
case $DB_TYPE in
# MySQL
mysql)
mysqldump -u$DB_USERNAME -p$DB_PASSWORD $DB_NAME > $TMP_DIR/dump.sql
;;
# PostgreSQL
postgresql)
PGPASSWORD=$DB_PASSWORD
pg_dump -U $DB_USERNAME -d $DB_NAME > $TMP_DIR/dump.sql
;;
# Other
*)
print 'Your database is not supported.' $BOLD$RED
exit
;;
esac
print '- Archiving...'
cd $TMP_DIR
tar -czf backup.tgz root.tgz dump.sql
print '- Uploading to FTP...'
dir_name=$(date +%F)
file_name="${TIMESTAMP}_${RANDOM}"
lftp <<EOF
connect $FTP_HOST
login $FTP_LOGIN $FTP_PASSWORD
set ftp:ssl-allow no
cd $FTP_ROOT
!mkdir $dir_name || true
cd $dir_name
put backup.tgz -o ${file_name}.tgz
rm -rf $OLD_DIR
EOF
print '- Cleaning tmp...'
rm -rf $TMP_DIR
print 'OK' $BOLD$GREEN