-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump-mysql.sh
executable file
·199 lines (169 loc) · 4.69 KB
/
dump-mysql.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env bash
#
# Create dumps of MySQL databases.
# Mitigate potential path issues depending on where you're running the script from
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
LIB_DIR="$(dirname "$SCRIPT_DIR")/lib"
# shellcheck source=lib/log.sh
. "$LIB_DIR"/log.sh
# shellcheck source=lib/package.sh
. "$LIB_DIR"/package.sh
# shellcheck source=lib/paths.sh
. "$LIB_DIR"/paths.sh
# shellcheck source=lib/array.sh
. "$LIB_DIR/array.sh"
# -------------------------
# GLOBAL defaults
# -------------------------
DESTINATION="${HOME}/.bashlib"
DB_HOST=""
DB_PORT=""
DB_USER=""
DB_PASSWORD=""
DB_NAMES=()
#######################################
# Print the usage output for the script.
# Globals:
# None
# Arguments:
# None
# Outputs:
# Writes usage to stdout
#######################################
function dump_mysql::usage() {
local script_name
script_name=$(basename "${0}")
echo "$script_name"
echo
echo "Create dumps of MySQL databases."
echo
echo "Usage: ./scripts/$script_name <DB_URL> [DESTINATION]"
echo
echo "help - Print this usage information"
echo "deps - Show the required dependencies to run this script"
echo
echo "Examples:"
echo " ./scripts/$script_name (with configured Globals)"
echo " ./scripts/$script_name mysql://db_user:[email protected]:3306/db_name"
echo " ./scripts/$script_name mysql://db_user:[email protected]:3306/db_name ./Backups"
}
#######################################
# Check if the required dependencies for
# the scripts are installed.
# Arguments:
# None
# Returns:
# 0 if all dependencies were found, 1 otherwise.
#######################################
function dump_mysql::deps() {
local deps=('mysql' 'mysqldump' 'trurl' 'pv')
for dep in "${deps[@]}"; do
package::is_executable "${dep}"
rc=$?
if [ $rc -ne 0 ]; then
log::red "Could not find package '${dep}' in system PATH. Please install '${dep}' to proceed!"
return 1
fi
log::green "Found package '${dep}' in system PATH."
done
log::green "Found all dependant packages: '${deps[*]}' in system PATH. Ready to proceed!"
return 0
}
#######################################
# Run MySQL dump.
# Globals:
# SOURCE
# DESTINATION
# Arguments:
# A source file path from which to backup.
# A name for the backup (to name the tarball).
# A destination file path to backup to.
# Returns:
# 0 if the source path does not exist.
# Otherwise the parent return value of 'tar'.
#######################################
function dump_mysql::run() {
local db_url, db_host, db_port, db_user, db_password, db_name, destination_path
db_url=${1}
db_host=${DB_HOST:-"$(trurl "$db_url" --get '{host}')"}
db_port=${DB_PORT:-"$(trurl "$db_url" --get '{port}')"}
db_user=${DB_USER:-"$(trurl "$db_url" --get '{user}')"}
db_password=${DB_PASSWORD:-"$(trurl "$db_url" --get '{password}')"}
path=$(trurl "$db_url" --get '{path}')
db_name=${path#/}
destination_path=${2:-"$DESTINATION"}
paths::ensure_existence "$destination_path"
curdate=$(date '+%d-%m-%Y+%T')
file="$destination_path/dump_$db_name-$curdate.sql"
array::is_empty "${DB_NAMES[@]}"
rc=$?
if [ $rc -eq 0 ]; then
dump_mysql::exec "$db_host" "$db_port" "$db_user" "$db_password" "$db_name" "$file"
else
for db in "${DB_NAMES[@]}"; do
dump_mysql::exec "$db_host" "$db_port" "$db_user" "$db_password" "$db" "$file"
done
fi
}
function dump_mysql::exec() {
local db_host, db_port, db_user, db_password, db_name, filename
db_host=${1}
db_port=${2}
db_user=${3}
db_password=${4}
db_name=${5}
filename=${6}
db_size=$(
mysql \
-h "$db_host" \
-P "$db_port" \
-u "$db_user" \
"-p$db_password" \
--silent \
--skip-column-names \
-e "SELECT ROUND(SUM(data_length) * 1.09) AS \"size_bytes\" \
FROM information_schema.TABLES \
WHERE table_schema='$db_name';"
)
size=$(numfmt --to=iec-i --suffix=B "$db_size")
log_time::yellow "Dumping database '$db_name' (≈$size) into $file ..."
mysqldump \
-h "$db_host" \
-P "$db_port" \
-u "$db_user" \
"-p$db_password" \
--compact \
--dump-date \
--hex-blob \
--databases \
--no-tablespaces \
--order-by-primary \
--quick \
"$db_name" |
pv --size "$db_size" \
>"$filename"
log_time::green "Finished backup of MySQL database: $db_name"
}
# --------------------------------
# MAIN
# --------------------------------
function main() {
local cmd=${1}
case "${cmd}" in
help)
dump_mysql::usage
;;
deps)
dump_mysql::deps
return $?
;;
*)
dump_mysql::run "$@"
return $?
;;
esac
}
# ------------
# 'main' call
# ------------
main "$@"