-
Notifications
You must be signed in to change notification settings - Fork 1
/
db-sample-data
executable file
·87 lines (68 loc) · 2.38 KB
/
db-sample-data
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
#!/usr/bin/env bash
#---------------------------------------------------------------------
usage ()
{
cat <<EOT
${0##*/}
Dumps frequently updated data from production to a web-accessible
file for downloading and importing into a local development
database. Prompts with the download URL and waits for a response
from the user to delete the file. Pass the names of the DB tables
you wish to download as arguments. Without arguments, will dump
the entire \$default database.
Usage:
bin/${0##*/} [table] [table2] [table3]
Environment:
DB_SAMPLE_DATA_URL - If this environment variable is set, it will
be used when displaying the download URL for
the temporary zip file.
EOT
exit ${1:-0} # Exit with code 0 unless an arg is passed to the method.
}
if [ "$1" = '-h' ]; then
usage
fi
if [ -z "$1" ]; then
TABLES=''
else
TABLES="--tables "$*""
fi
# Configuration vars. Set these appropriately for your app and environment.
PUBLIC_URL=${DB_SAMPLE_DATA_URL:-}
WEBROOT_PATH="webroot/"
# Internal var setup.
eval $( bin/db-credentials ) # Yuck, but nothing else works!
OPTIONS="--skip-add-drop-table --no-create-info"
TMP_PATH="tmp"
DESTINATION_NAME="${DB_DATABASE}_sample_data_$(date +%Y-%m-%d)"
# Generate the dump command.
if [ -n "$DB_PORT" ]; then
PORT_CLAUSE="--port=$DB_PORT"
fi
CMD="mysqldump --host=${DB_HOST} ${PORT_CLAUSE} --user=${DB_USERNAME} ${OPTIONS} ${DB_DATABASE} $TABLES"
# Run the dump command.
# Handle all the different ways to provide (or not provide) a password.
pushd "${TMP_PATH}" >/dev/null
if [[ $DB_PASSWORD =~ " |'" ]]; then
$CMD -p"${DB_PASSWORD}" > "${DESTINATION_NAME}.sql"
elif [ -n "${DB_PASSWORD}" ]; then
$CMD -p$DB_PASSWORD > "${DESTINATION_NAME}.sql"
else
$CMD > "${DESTINATION_NAME}.sql"
fi
popd >/dev/null
# Zip the file and remove the original .sql.
zip -rq9 "${WEBROOT_PATH}${DESTINATION_NAME}.zip" "${TMP_PATH}/${DESTINATION_NAME}.sql"
rm -f "${TMP_PATH}/${DESTINATION_NAME}.sql"
# Prompt to download the file, then delete it.
echo "## Download URL: ${PUBLIC_URL}/${DESTINATION_NAME}.zip"
read -p "?? Delete ${PUBLIC_URL}/${DESTINATION_NAME}.zip? [Y/n]: " ARG_DELETEFILE
case $ARG_DELETEFILE in
[Yy]*|*)
rm -f "${WEBROOT_PATH}${DESTINATION_NAME}.zip"
echo "## File deleted."
;;
[Nn]*)
echo "## File REMAINS! Please clean it up when you are done to prevent data leaks."
;;
esac