-
Notifications
You must be signed in to change notification settings - Fork 3
/
backup_db.sh
executable file
·333 lines (269 loc) · 10 KB
/
backup_db.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/bash
VERBOSE=0
COMPRESS='bzip2'
USER='mysql'
GROUP='mysql'
DIRECTORYATTRIBUTES=0770
FILEATTRIBUTES=640
TIME_REMOVED_DUMP_FILES='1 week ago'
BACKUP_DIR='/var/backups/mysql'
CONFIG_FILE='/etc/mysql/debian.cnf'
DATABASE=''
TABLES=''
INCLUDE_TABLES=''
INCLUDE_DATA_TABLES=''
EXCLUDE_TABLES=''
EXCLUDE_DATA_TABLES=''
BIN_DEPS="mysql mysqldump $COMPRESS"
# === DO NOT EDIT BELOW THIS LINE ===
if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
# === FUNCTIONS ===
source $(dirname "$0")/functions.sh
if [ -f '/etc/debian_version' ]; then
CONFIG_FILE='/etc/mysql/debian.cnf'
else
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
fi
backup()
{
log " START "
query="SHOW databases;"
#
# Inlude tables
#
local default_databases_include=(
)
local default_tables_include=(
)
#
# Exclude tables
#
local default_databases_exclude=(
'information_schema'
'performance_schema'
)
local array_views=()
lockfile "$DST/$DATABASE/lockfile.lock"
mkdir -p $DST/$DATABASE 2>/dev/null 1>&2
chown $USER:$GROUP $DST/$DATABASE
chmod $DIRECTORYATTRIBUTES $DST/$DATABASE
query="SHOW CREATE DATABASE \`$DATABASE\`;"
mysql --defaults-file=$CONFIG_FILE --skip-column-names -B -e "$query" | awk -F"\t" '{ print $2 }' | sed -E 's/^CREATE DATABASE `/CREATE DATABASE IF NOT EXISTS `/' > $DST/$DATABASE/__create.sql
if [ -f $DST/$DATABASE/__create.sql ]; then
log " > Export create"
fi
local mysqlDumpVars="--single-transaction"
if mysqldump --column-statistics=0 --version &>/dev/null; then
mysqlDumpVars="$mysqlDumpVars --column-statistics=0"
fi
query="SHOW FULL TABLES WHERE Table_type = 'VIEW';"
for viewName in $(mysql --defaults-file=$CONFIG_FILE $DATABASE -N -e "$query" | sed -E 's/|//' | awk '{print $1}'); do
mysqldump --defaults-file=$CONFIG_FILE $mysqlDumpVars $DATABASE $viewName | sed -E 's/DEFINER=[^*]*\*/\*/' >> $DST/$DATABASE/__views.sql
array_views+=($viewName)
done
if [ -f $DST/$DATABASE/__views.sql ]; then
log " > Exports views"
fi
mysqldump --defaults-file=$CONFIG_FILE $mysqlDumpVars --routines --skip-events --skip-triggers --no-create-info --no-data --no-create-db --skip-opt $DATABASE | sed -E 's/DEFINER=[^*]*\*/\*/' > $DST/$DATABASE/__routines.sql
if [ -f $DST/$DATABASE/__routines.sql ]; then
log " > Exports Routines"
fi
mysqldump --defaults-file=$CONFIG_FILE $mysqlDumpVars --triggers --skip-events --skip-routines --no-create-info --no-data --no-create-db --skip-opt $DATABASE | sed -E 's/DEFINER=[^*]*\*/\*/' > $DST/$DATABASE/__triggers.sql
if [ -f $DST/$DATABASE/__triggers.sql ]; then
log " > Exports Triggers"
fi
mysqldump --defaults-file=$CONFIG_FILE $mysqlDumpVars --events --skip-routines --skip-triggers --no-create-info --no-data --no-create-db --skip-opt $DATABASE | sed -E 's/DEFINER=[^*]*\*/\*/' > $DST/$DATABASE/__events.sql
if [ -f $DST/$DATABASE/__events.sql ]; then
log " > Exporting Events"
fi
local default_tables_exclude=(
'slow_log'
'general_log'
)
tables_exclude=( ${default_tables_exclude[@]} ${array_views[@]} ${EXCLUDE_TABLES[@]} )
tables_exclude_expression=$(prepaire_skip_expression "${tables_exclude[@]}")
log "Exclude tables: $tables_exclude_expression"
data_tables_exclude=( ${EXCLUDE_DATA_TABLES[@]} )
data_tables_exclude_expression=$(prepaire_skip_expression "${data_tables_exclude[@]}")
log "Exclude data tables: $data_tables_exclude_expression"
tables=( ${TABLES[@]} )
tables_expression=$(prepaire_skip_expression "${tables[@]}")
log "Only tables: $tables_expression"
#
# Get list`s tables
#
query="SHOW TABLES;"
command="mysql --defaults-file=$CONFIG_FILE --skip-column-names -B $DATABASE -e \"$query\""
if [ $tables_exclude_expression ]; then
command=" $command | egrep -v \"$tables_exclude_expression\""
fi
if [ $tables_expression ]; then
command=" $command | egrep \"$tables_expression\""
fi
log "Command: $command"
for TABLE in $(eval $command); do
log " ** Dump $DATABASE.$TABLE"
if [ $(echo $data_tables_exclude_expression| grep $TABLE) ]; then
log "Exclude data from table $TABLE"
mysqldump --defaults-file=$CONFIG_FILE $mysqlDumpVars --no-data --add-drop-table --skip-events --skip-routines --skip-triggers --tab=$DST/$DATABASE/ $DATABASE $TABLE
else
# If fields has geospatial types
checkGeo="mysql --defaults-file=$CONFIG_FILE -B $DATABASE -e \"SHOW COLUMNS FROM $TABLE WHERE Type IN ('point', 'polygon', 'geometry', 'linestring')\""
hasGeo=$(eval $checkGeo)
if [ ! -z "$hasGeo" ]; then
mysqldump --defaults-file=$CONFIG_FILE $mysqlDumpVars --flush-logs --default-character-set=utf8 --add-drop-table --quick --skip-events --skip-routines --skip-triggers --result-file=$DST/$DATABASE/$TABLE.sql $DATABASE $TABLE
else
mysqldump --defaults-file=$CONFIG_FILE $mysqlDumpVars --flush-logs --default-character-set=utf8 --add-drop-table --quick --skip-events --skip-routines --skip-triggers --tab=$DST/$DATABASE/ $DATABASE $TABLE
fi
fi
sed -i -E 's/AUTO_INCREMENT=[0-9]*\b//' $DST/$DATABASE/$TABLE.sql
if [ -f "$DST/$DATABASE/$TABLE.sql" ]; then
chmod $FILEATTRIBUTES $DST/$DATABASE/$TABLE.sql
chown $USER:$GROUP $DST/$DATABASE/$TABLE.sql
log " ** set permision to $DATABASE/$TABLE.sql"
else
log " ** WARNING : $DST/$DATABASE/$TABLE.sql not found"
fi
if [ -f "$DST/$DATABASE/$TABLE.txt" ]; then
if [ $COMPRESS ]; then
log " ** $COMPRESS $DATABASE/$TABLE.txt in background"
if [ $COMPRESS == 'bzip2' ]; then
if [ -f "$DST/$DATABASE/$TABLE.txt.bz2" ]; then
rm $DST/$DATABASE/$TABLE.txt.bz2
fi
($COMPRESS $DST/$DATABASE/$TABLE.txt && chmod $FILEATTRIBUTES $DST/$DATABASE/$TABLE.txt.bz2 && chown $USER:$GROUP $DST/$DATABASE/$TABLE.txt.bz2) &
elif [ $COMPRESS == 'gzip' ]; then
if [ -f "$DST/$DATABASE/$TABLE.txt.gz" ]; then
rm $DST/$DATABASE/$TABLE.txt.gz
fi
($COMPRESS $DST/$DATABASE/$TABLE.txt && chmod $FILEATTRIBUTES $DST/$DATABASE/$TABLE.txt.gz && chown $USER:$GROUP $DST/$DATABASE/$TABLE.txt.gz) &
fi
fi
else
log " ** WARNING : $DST/$DATABASE/$TABLE.txt not found"
fi
done
log " END "
}
usage()
{
cat << EOF
This mysql backup engine.
Usage: $0 <[database-name]> <[options]> or bash $0 <[database-name]> <[options]>
Options:
--tables= Dump only such tables
--exclude-tables= Exclude tables
--exclude-data-tables= Exclude data tables
-c= | --compress= Compress with gzip or bzip2
-v | --verbose Add verbose into output
-l= | --lifetime= Lifetime for dump files
--config= Config file of Debian format
--dir= Backup directory
-h | --help This text
Examples:
backup.sh --verbose --compress=
backup.sh --verbose --compress=gzip
backup.sh --verbose --compress=bzip2
backup.sh --verbose --compress=
backup.sh --verbose --compress= --lifetime="3 day ago"
backup.sh --verbose --config="/etc/mysql/debian.cnf" --lifetime="1 day ago"
backup.sh --verbose --dir="/var/backups/mysql" --config="/etc/mysql/debian.cnf" --lifetime="1 day ago"
backup.sh --verbose --dir="/home/backups/mysql" --lifetime="1 day ago"
backup.sh --verbose --dir="/home/backups/mysql" --exclude-tables="tbl_template" --lifetime="1 day ago"
backup.sh --verbose --dir="/home/backups/mysql" --tables="tbl_template tbl_template1 tbl_template2"
EOF
}
if [ $# = 0 ]; then
usage;
exit;
fi
DATABASE="$1"
for BIN in $BIN_DEPS; do
which $BIN 1>/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: Required command file not be found: $BIN"
exit 1
fi
done
for i in "$@"
do
case $i in
-t=* | --tables=*)
TABLES=( "${i#*=}" )
shift # past argument=value
;;
--exclude-tables=*)
EXCLUDE_TABLES=( "${i#*=}" )
shift # past argument=value
;;
--exclude-data-tables=*)
EXCLUDE_DATA_TABLES=( "${i#*=}" )
shift # past argument=value
;;
--include-tables=*)
INCLUDE_TABLES=( "${i#*=}" )
shift # past argument=value
;;
--include-data-tables=*)
INCLUDE_DATA_TABLES=( "${i#*=}" )
shift # past argument=value
;;
-c=* | --compress=*)
COMPRESS=( "${i#*=}" )
shift # past argument=value
;;
-l=* | --lifetime=*)
TIME_REMOVED_DUMP_FILES=( "${i#*=}" )
shift # past argument=value
;;
--dir=*)
BACKUP_DIR=( "${i#*=}" )
shift # past argument=value
;;
--config=*)
CONFIG_FILE=( "${i#*=}" )
shift # past argument=value
;;
-v | --verbose)
VERBOSE=1
shift # past argument=value
;;
-h | --help)
usage
exit
;;
*)
# unknown option
;;
esac
done
DATE=`date '+%Y.%m.%d'`
DATEOLD=`date --date="$TIME_REMOVED_DUMP_FILES" +%Y.%m.%d`
DST=$BACKUP_DIR/$DATE
DSTOLD=$BACKUP_DIR/$DATEOLD
if check_connection; then
if [ ! -d "$DST" ]; then
mkdir -p $DST;
chmod $DIRECTORYATTRIBUTES $DST;
chown $USER:$GROUP $DST;
fi
if [ -d "$DSTOLD" ]; then
rm -fr $DSTOLD;
fi
# === SETTINGS ===
log "============================================"
log "Dump into: $DST"
log "Config file: $CONFIG_FILE"
log "Verbose: $VERBOSE"
log "Compress: $COMPRESS"
log "Database: $DATABASE"
log "Tables: $TABLES"
log "Exclude tables: $EXCLUDE_TABLES"
log "Life time: $TIME_REMOVED_DUMP_FILES"
log "============================================"
log ""
# === AUTORUN ===
backup
else
log "Failed to establish a connection to the database"
fi