-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_export.sh
executable file
·32 lines (25 loc) · 1009 Bytes
/
mysql_export.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
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
WORKSPACE_DIR="${SCRIPT_DIR%/*}"
DUMPS_DIR="${WORKSPACE_DIR}/data/dumps/mysql"
ENV_PATH="${WORKSPACE_DIR}/.env"
DC="${SCRIPT_DIR}/dc.sh"
# shellcheck source=scripts/parse_env.sh
. "${SCRIPT_DIR}/scripts/parse_env.sh"
# shellcheck source=scripts/statuses.sh
. "${SCRIPT_DIR}/scripts/statuses.sh"
if [ -z "$1" ]; then
read -r -p "Enter database name: " DB_NAME
else
read -er -p "Enter database name: " -i "$1" DB_NAME
fi
DB_USER=$(parse_env "DB_USER" "${ENV_PATH}")
DB_PASSWORD=$(parse_env "DB_PASSWORD" "${ENV_PATH}")
TIME=$(date +"%Y_%m_%d_%H%M%S")
DUMP_FILENAME="${DB_NAME}_${TIME}.sql"
DUMP_PATH="${DUMPS_DIR}/${DUMP_FILENAME}"
# shellcheck disable=SC2015
cd "$WORKSPACE_DIR" \
&& ${DC} exec -T mysql sh -c "exec mysqldump -u${DB_USER} -p${DB_PASSWORD} ${DB_NAME}" > "${DUMP_PATH}" \
&& message_success "MySQL dump of database \`$DB_NAME\` created $DUMP_PATH" \
|| message_failure "Error creating the database \`$DB_NAME\` MySQL dump"