-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.sh
More file actions
executable file
·309 lines (275 loc) · 9.64 KB
/
db.sh
File metadata and controls
executable file
·309 lines (275 loc) · 9.64 KB
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
#!/bin/bash
export PATH=$PATH:$(dirname $0)/aux
source config.sh
source log.sh
## Table where the applied deltas info will be stored
create_deltas_table="CREATE TABLE $deltas_table
(id CHAR(20),
comment VARCHAR(256),
md5sum CHAR(32));"
usage() {
cat << EOU
Usage: $0 [-h] [-d] [-c conf_file] command
Script to handle the backup and restoration of MySQL's database.
Options:
-h
Show this help.
-d
Enable debug mode
-c conf_file
Use the configuration file conf_file instead of the default
$conf_file
Commands:
backup [user|sys|all]
Do a backup of the user generated, system or all tables (will use
all by default)
restore [-d] [-l [user|sys|all]|file]
Restore the last backup (the -l option) or the given backup sql
file. If -l specified the last 'all' backup will be used unless
user or system specified.
The -d flag drops the database before restoring it.
update [delta1 [...]]
Applies the given delta scripts and registers them in the
database. A delta file must be a SQL script with the name
'IDNUMBER-Comment.sql' where IDNUMBER is a number identifying
the delta.
EOU
exit ${1:-1}
}
## Helper funtion that checks if one element is in a list
is_in() {
local what="$1"
local where=(${@:2})
for elem in ${where[@]}; do
[[ "$what" == "$elem" ]] && return 0
done
return 1
}
get_all_tables() {
local host="${1:-$dbhost}"
local user="${2:-$dbuser}"
local pass="${3:-$dbpass}"
local db="${4:-$database}"
mysql ${user:+-u$user} ${pass:+-p$pass} ${host:+-h $host} \
-N -e "show tables;" $db
}
get_system_tables() {
local tables=($(get_all_tables))
for table in ${tables[@]}; do
if ! is_in $table ${user_tables}; then
echo $table
fi
done
}
get_last_backup() {
type="${1:?Missing backup type, sys or user}"
local lastbackup="$(ls -t $backups_dir/db/*$type*dbbackup.sql.* | head -n1)"
[[ -f "$lastbackup" ]] \
&& echo $lastbackup \
|| return 1
}
get_applied_deltas() {
local host="${1:-$dbhost}"
local user="${2:-$dbuser}"
local pass="${3:-$dbpass}"
local db="${4:-$database}"
mysql ${user:+-u$user} ${pass:+-p$pass} ${host:+-h $host} \
-N -e "select id from $deltas_table;" $db
}
init_deltas_table() {
local host="${1:-$dbhost}"
local user="${2:-$dbuser}"
local pass="${3:-$dbpass}"
local db="${4:-$database}"
tables=($(get_all_tables $host $user $pass $db))
if ! is_in $deltas_table ${tables[@]}; then
mysql ${user:+-u$user} ${pass:+-p$pass} ${host:+-h $host} \
-N -e "$create_deltas_table" $db \
&& log "Delta table $deltas_table initialized on database $db." \
|| { log::ERROR "Failed to initialise deltas table $deltas_table on database $db.";
return 1; }
fi
return 0
}
apply_delta() {
local delta="${1:?No delta file supplied}"
local host="${2:-$dbhost}"
local user="${3:-$dbuser}"
local pass="${4:-$dbpass}"
local db="${5:-$database}"
local mysql="mysql ${user:+-u$user} ${pass:+-p$pass} ${host:+-h $host}"
$mysql $db < $delta \
|| return 1
local id="${delta%%-*}"
local comment="${delta#*-}"
local md5="$(md5sum $delta | head -c 32)"
$mysql -e "insert into $deltas_table values ('$id', '${comment%.sql}', '$md5');" $db \
|| return 1
}
get_delta_md5() {
id="${1:?No id supplied.}"
local host="${2:-$dbhost}"
local user="${3:-$dbuser}"
local pass="${4:-$dbpass}"
local db="${5:-$database}"
local mysql="mysql ${user:+-u$user} ${pass:+-p$pass} ${host:+-h $host}"
$mysql -N -e "Select md5sum from $deltas_table where id='$id';" $db
}
do_backup() {
local mysqldump="mysqldump ${dbuser:+-u$dbuser} ${dbpass:+-p$dbpass} ${dbhost:+-h $dbhost}"
local db="$database"
case ${1:-all} in
all) do_backup sys && do_backup user && return 0;;
sys*) local tables=($(get_system_tables)); local append='sys_' ;;
user) local tables=(${user_tables}); local append='user_' ;;
*) log::WARN "How did you get here??"; usage; exit ;;
esac
[[ -d $backups_dir/db ]] \
|| mkdir -p $backups_dir/db \
|| { log::ERROR "Error creating the backup dir $backups_dir/db."; exit 1;}
local timestamp="$( date +%Y%m%d%H%M%S)"
local outfile="$backups_dir/db/${append}dbbackup.sql.$timestamp"
log "Creating backup at $outfile..."
$mysqldump $db ${tables[@]} > "$outfile" \
&& log::OK "OK" \
|| { log::ERROR " ERROR"; return 1;}
}
do_restore() {
local db="$database"
local mysql="mysql ${dbuser:+-u$dbuser} ${dbpass:+-p$dbpass} ${dbhost:+-h $dbhost}"
local drop_on_partial="False"
[[ "$1" == "-d" ]] && drop_on_partial="True" && shift
[[ "$1" == "" ]] && log::ERROR "No backup file specified." && usage
case $1 in
-l)
case $2 in
user)
local file=$(get_last_backup user) \
|| { log::ERROR "Could not find last user tables backup."; exit 1; }
;;
sys*)
local file=$(get_last_backup sys) \
|| { log::ERROR "Could not find last system tables backup."; exit 1; }
;;
*)
get_last_backup sys > /dev/null \
&& get_last_backup user > /dev/null \
|| { log::ERROR "Cannot locate all the backup files...";
return 1;}
if is_in "$db" $($mysql -e "show databases;"); then
log "Dropping old database $db: "
$mysql -e "drop database $db;" \
&& log::OK "OK" \
|| { log::ERROR "ERROR"; return 1;}
fi
do_restore -l user \
&& do_restore -l sys \
&& return 0
;;
esac
;;
*)
[[ -f ${1:-/dummy/file} ]] \
|| { log::ERROR "File $1 not found."; exit 1; }
local file=$1
;;
esac
if [[ "$drop_on_partial" == "True" ]] \
&& is_in "$db" $($mysql -e "show databases;"); then
log "Dropping old database $db: "
$mysql -e "drop database $db;" \
&& log::OK "OK" \
|| { log::ERROR "ERROR"; return 1;}
fi
$mysql -e "create database if not exists $db;" \
|| { log::ERROR "Error creating database $db."; return 1; }
log "Restoring $file backup..."
$mysql $db < $file \
&& log::OK "OK" \
|| log::ERROR " ERROR"
}
do_update() {
local abort='True'
case $1 in
'--noabort') shift; local abort='False';;
esac
log "Doing a full backup... just in case"
do_backup || { log::ERROR "Failed to do the backup... exitting"; return 1; }
init_deltas_table || return 1
for file in $@; do
[[ -f $file ]] \
|| { log::ERROR "Delta file $file does not exist... aborting"; return 1; }
done
local applied_deltas=($(get_applied_deltas))
log "Applying deltas"
for delta in $@; do
local comment="${delta#*-}"
local id="${delta%%-*}"
if is_in $id ${applied_deltas[@]}; then
new_md5="$(md5sum $delta | head -c 32)"
old_md5="$(get_delta_md5 $id)"
if [[ "$new_md5" != "$old_md5" ]]; then
log::WARN "\tDelta $delta already applied, BUT MD5 DOES NOT MATCH, PLEASE CHECK"
log::WARN "\t\tNew md5: $new_md5"
log::WARN "\t\tOld md5: $old_md5"
else
log "\tDelta $delta already applied, skipping..."
fi
continue
fi
log "\tApplying delta $delta: "
if apply_delta $delta; then
log::OK "\t\tOK"
elif [[ "$abort" == "True" ]]; then
log::ERROR " Error aplying delta $delta, aborting and restoring backup"
do_restore -l
return 1
else
log::ERROR "Error applying delta $delta, but option --noabort set, continuing..."
fi
done
}
## MAIN
[[ "$1" == "-h" ]] && usage 0
[[ "$1" == "-d" ]] && shift && set -x
[[ "$1" == "-c" ]] \
&& {
conf_file="$2"
[[ -f "$conf_file" ]] \
|| {
log::ERROR "Config file $conf_file not found"
exit 1
}
shift 2
}
conf::load ${conf_file:?No conf_file specified} \
|| { log::ERROR "Error parsing config file"; exit 1; }
conf::require database deltas_table backups_dir \
|| { log::ERROR "Missing parametes in the config file."; exit 1; }
log "
Loaded config:
Database connection:
dbhost: ${dbhost:-localhost}
database: ${database}
dbpass: ${dbpass:-None}
dbuser: ${dbuser:-None}
Delta script specifics:
deltas_table: ${deltas_table}
user_tables: ${user_tables:-None}
Backups:
backups_dir: ${backups_dir}
"
case ${1} in
backup)
do_backup "${@:2}"
;;
restore)
do_restore "${@:2}"
;;
update)
do_update "${@:2}"
;;
*)
usage
;;
esac