-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
88 lines (68 loc) · 1.41 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
#!/bin/bash -eu
log() {
local LEVEL=$1
shift
local MESSAGE=$*
local DATE=$(date +%Y-%m-%d\ %H:%M:%S)
echo "${DATE} ${LEVEL} ${MESSAGE}"
}
log_error() {
log_info Checking environment...
if [ $LOG -le 3 ]; then
log ERROR $*
fi
}
log_info() {
if [ $LOG -le 2 ]; then
log INFO $*
fi
}
log_debug() {
if [ $LOG -le 1 ]; then
log DEBUG $*
fi
}
check_env() {
set +eu
local error=false
if [ -z "${LOG_LEVEL}" -o "${LOG_LEVEL}" == "ERROR" ]; then
LOG=3
elif [ "${LOG_LEVEL}" == "INFO" ]; then
LOG=2
elif [ "${LOG_LEVEL}" == "DEBUG" ]; then
LOG=1
else
log_error "Unknown log level ${LOG_LEVEL}"
error=true
fi
if [ -z "${HOST}" ]; then
HOST="db"
fi
USER="${USER:-root}"
if [ -z "${PASSWORD}" ]; then
log_error "PASSWORD is mandatory"
error=true
fi
if [ -z "${DATABASE}" ]; then
log_error "DATABASE is mandatory"
error=true
fi
NAME="${NAME:-$DATABASE}"
BACKUP_DIR="/backups"
if ${error}; then
exit 1
fi
set -eu
log_info Environment checked
}
backup_sql() {
local DATE=$(date +%Y%m%d-%H%M%S)
local DUMP_NAME="${NAME}_${DATE}.sql.bz2"
local FILE_NAME="/backups/${FULLNAME:-$DUMP_NAME}"
log_info Backuping mysql into ${FILE_NAME} ...
mysqldump -q -h ${HOST} --single-transaction --user=${USER} --password=${PASSWORD} ${DATABASE} | pbzip2 > ${FILE_NAME}
log_info Backup done
}
PARAMS=$*
check_env
backup_sql