-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.sh
executable file
·47 lines (38 loc) · 1.04 KB
/
init.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
#!/bin/bash
set -e
filename_prefix="dump-$(date +'%m-%d-%Y_%H-%M').sql" #will output: dump-11-20-2019_16-38.sql
parseEnv() {
# DB properties
HOST=${DB_DUMP_HOST:-localhost}
PORT=${DB_DUMP_PORT:-3360}
USER=${DB_DUMP_USER:-mysql}
PASS=${DB_DUMP_PASS:-mysql}
NAME=${DB_DUMP_NAME:-mysql}
# dump file properties
FILE_NAME=${DB_DUMP_FILE_NAME:-$filename_prefix}
DUMP_PATH=/dumps/${FILE_NAME}
# s3 properties, bucket and endpoint
BUCKET=${AWS_BUCKET_NAME:-dumps}
ENDPOINT=${AWS_END_POINT:-https://s3.amazonaws.com}
# construct /YEAR/MONTH/DAY/HOUR path
FOLDER_NAME=$(date +%F | tr '-' '//')/$(date +%H)
FILE_PATH=${FOLDER_NAME}/${FILE_NAME}
}
dumpDB() {
MYSQL_PWD=${PASS} mysqldump \
--single-transaction \
--quick \
--lock-tables=false \
-h ${HOST} \
-u ${USER} \
-P ${PORT} \
${NAME} > ${DUMP_PATH}
}
moveDump() {
aws s3 mv ${DUMP_PATH} s3://${BUCKET}/${FILE_PATH} --endpoint=${ENDPOINT}
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
parseEnv || exit 1
dumpDB || exit 1
moveDump || exit 1
fi