-
Notifications
You must be signed in to change notification settings - Fork 3
/
restore_db.sh
262 lines (211 loc) · 7.02 KB
/
restore_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
#!/bin/bash
# === CONFIG ===
VERBOSE=0
LOAD_DATA_LOCAL_INFILE=0
CONVERT_INNODB=0
CONFIG_CHUNK=100000
BIN_DEPS="ls grep awk sort uniq bunzip2 bzip2 mysql"
RESTORE_INTO=''
# === 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
restore_table()
{
local TABLE=$1
log "RESTORE: Import data into $DATABASE/$TABLE"
if [ -f "$DATABASE_DIR/$TABLE.txt.bz2" ]; then
log "RESTORE: < $TABLE"
if [ -f "$DATABASE_DIR/$TABLE.txt" ]; then
log "RESTORE: Delete source: $TABLE.txt"
rm $DATABASE_DIR/$TABLE.txt
fi
bunzip2 -k $DATABASE_DIR/$TABLE.txt.bz2
fi
if [ -s "$DATABASE_DIR/$TABLE.txt" ]; then
OPTIONS='--unbuffered --wait --reconnect --skip-column-names'
OPERATOR='LOAD DATA LOW_PRIORITY INFILE'
if [ $LOAD_DATA_LOCAL_INFILE -eq 1 ]; then
OPERATOR='LOAD DATA LOW_PRIORITY LOCAL INFILE'
OPTIONS='--unbuffered --local-infile --wait --reconnect --skip-column-names'
fi
local error=''
error=$(mysql --defaults-file=$CONFIG_FILE $DATABASE $OPTIONS --execute="
SET GLOBAL net_buffer_length=1000000; -- Set network buffer length to a large byte number
SET GLOBAL max_allowed_packet=1000000000; -- Set maximum allowed packet size to a large byte number
SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO';
SET SESSION wait_timeout=3600;
SET foreign_key_checks = 0;
SET unique_checks = 0;
SET sql_log_bin = 0;
SET autocommit = 0;
LOCK TABLES $TABLE WRITE;
$OPERATOR '$DATABASE_DIR/$TABLE.txt' IGNORE INTO TABLE $TABLE CHARACTER SET UTF8;
UNLOCK TABLES;
COMMIT;
SET autocommit=1;
SET foreign_key_checks = 1;
SET unique_checks = 1;
SET sql_log_bin = 1;
" 2>&1)
if [[ -z "$error" ]]; then
log "RESTORE: + $TABLE"
else
log "RESTORE: - $TABLE"
log "RESTORE: Rise error: $error"
fi
fi
if [ $DATABASES_TABLE_CHECK ]; then
if [ -f "$DATABASE_DIR/$TABLE.ibd" ]; then
if [ ! $(innochecksum $DATABASE_DIR/$TABLE.ibd) ]; then
log "$TABLE [OK]"
else
log "$TABLE [ERR]"
fi
fi
fi
}
restore()
{
DATABASE_DIR=$@
log "RESTORE: Check path $DATABASE_DIR"
log "RESTORE: ** START **"
DATABASE=${DATABASE_DIR##*/}
if [ $DATABASE ]; then
log "RESTORE: Found restore files $DATABASE_DIR"
if [ -f $DATABASE_DIR/__create.sql ]; then
if [ ! -z "$RESTORE_INTO" ]; then
sed -i -E 's/`'$DATABASE'`/`'$RESTORE_INTO'`/' $DATABASE_DIR/__create.sql
DATABASE="$RESTORE_INTO"
fi
log "RESTORE: Create database $DATABASE if not exists"
sed -i -E 's/^CREATE DATABASE `/CREATE DATABASE IF NOT EXISTS `/' $DATABASE_DIR/__create.sql
mysql --defaults-file=$CONFIG_FILE < $DATABASE_DIR/__create.sql
fi
tables=$(ls -1 $DATABASE_DIR | grep --invert-match '^__' | grep .sql | awk -F. '{print $1}' | sort | uniq)
log "RESTORE: Create tables in $DATABASE"
for TABLE in $tables; do
log "RESTORE: Create table: $DATABASE/$TABLE"
if [ $CONVERT_INNODB -eq 1 ]; then
sed -i -E 's/ENGINE=MyISAM/ENGINE=InnoDB/' $DATABASE_DIR/$TABLE.sql
fi
error=$(mysql --defaults-file=$CONFIG_FILE $DATABASE -e "
SET foreign_key_checks = 0;
DROP TABLE IF EXISTS $TABLE;
SOURCE $DATABASE_DIR/$TABLE.sql;
SET foreign_key_checks = 1;
" 2>&1)
if [[ ! -z "$error" ]]; then
log "Rise error: $error"
fi
done
log "RESTORE: Import data into $DATABASE"
for TABLE in $tables; do
restore_table "$TABLE"
done
if [ -f "$DATABASE_DIR/__routines.sql" ]; then
log "RESTORE: Import routines into $DATABASE"
mysql --defaults-file=$CONFIG_FILE $DATABASE < $DATABASE_DIR/__routines.sql
fi
if [ -f "$DATABASE_DIR/__views.sql" ]; then
log "RESTORE: Import views into $DATABASE"
mysql --defaults-file=$CONFIG_FILE $DATABASE < $DATABASE_DIR/__views.sql
fi
if [ -f "$DATABASE_DIR/__triggers.sql" ]; then
log "RESTORE: Import triggers into $DATABASE"
mysql --defaults-file=$CONFIG_FILE $DATABASE < $DATABASE_DIR/__triggers.sql
fi
if [ -f "$DATABASE_DIR/__events.sql" ]; then
log "RESTORE: Import events into $DATABASE"
mysql --defaults-file=$CONFIG_FILE $DATABASE < $DATABASE_DIR/__events.sql
fi
log "RESTORE: Flush privileges;"
mysql --defaults-file=$CONFIG_FILE -e "flush privileges;"
log "RESTORE: ** END **"
else
log "RESTORE: Database not found"
fi
}
usage()
{
cat << EOF
usage: $0 options
This script restore databases.
OPTIONS:
--chunk= Put NUMBER lines per output file
--config= Path to configfil
--convert-innodb=1 Convert database into InnoDb
--restore_into= Special name for create database with new name
--verbose
-h | --help Usage
Examples:
restore_db.sh --verbose
EOF
}
# === CHECKS ===
DATABASE_DIR=$(pwd)
if [ -f '/etc/debian_version' ]; then
CONFIG_FILE='/etc/mysql/debian.cnf'
else
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
fi
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
--config=*)
CONFIG_FILE=( "${i#*=}" )
shift # past argument=value
;;
--chunk=*)
CONFIG_CHUNK=( "${i#*=}" )
shift # past argument=value
;;
--convert-innodb)
CONVERT_INNODB=1
shift # past argument=value
;;
--restore_into=*)
RESTORE_INTO=( "${i#*=}" )
shift # past argument=value
;;
-l | --local)
LOAD_DATA_LOCAL_INFILE=1
shift # past argument=value
;;
-v | --verbose)
VERBOSE=1
shift # past argument=value
;;
-h | --help)
usage
exit
;;
*)
# unknown option
;;
esac
done
if check_connection; then
# === SETTINGS ===
log "RESTORE: ============================================"
log "RESTORE: Restore from: $DATABASE_DIR"
log "RESTORE: Restore into database: $RESTORE_INTO"
log "RESTORE: Config file: $CONFIG_FILE"
log "RESTORE: Load from local y/n (default n): $LOAD_DATA_LOCAL_INFILE"
log "RESTORE: Convert into InnoDB y/n (default n): $CONVERT_INNODB"
log "RESTORE: Verbose: $VERBOSE"
log "RESTORE: ============================================"
log "RESTORE: "
lockfile "$DATABASE_DIR/lockfile.lock"
# === AUTORUN ===
restore $DATABASE_DIR
else
log "Failed to establish a connection to the database"
fi