-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_xtrabackup.sh
597 lines (536 loc) · 22.6 KB
/
mysql_xtrabackup.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#!/bin/bash
# bgbackup - A backup shell script for MariaDB, MySQL and Percona
#
# Author: collected from internet
# Modifier: Kuntal Basu
# Functions
# Handle control-c
function sigint {
echo "SIGINT detected. Exiting"
if [ "$galera" = yes ] ; then
log_info "Disabling WSREP desync on exit"
mysql --login-path=xtrabackup -e "SET GLOBAL wsrep_desync=OFF;"
fi
# 130 is the standard exit code for SIGINT
exit 130
}
# Mail function
function mail_log {
mail -s "$mailsubpre $HOSTNAME Backup $log_status $butype $mdate" "$maillist" < "$logfile"
}
# Function to check log for okay
function log_check {
if grep -Eq 'completed OK!$' "$logfile" ; then
log_status=SUCCEEDED
else
log_status=FAILED
fi
}
# Logging function
function log_info() {
if [ "$verbose" == "no" ] ; then
printf "%s --> %s\n" "$(date +%Y-%m-%d-%T)" "$*" >>"$logfile"
else
printf "%s --> %s\n" "$(date +%Y-%m-%d-%T)" "$*" | tee -a "$logfile"
fi
if [ "$syslog" = yes ] ; then
logger -p local0.notice -t bgbackup "$*"
fi
}
# Function to create innobackupex command
function innocreate {
mhost=$(hostname)
innocommand="$innobackupex --history "
dirdate=$(date +%Y-%m-%d_%H-%M-%S)
alreadyfullcmd=$mysqlcommand" \"SELECT COUNT(*) FROM $backuphistschema.backup_history WHERE DATE(end_time) = CURDATE() AND butype = 'Full' AND status = 'SUCCEEDED' AND hostname = '$mhost' AND deleted_at = 0 \" "
alreadyfull=$(eval "$alreadyfullcmd")
anyfullcmd=$mysqlcommand" \"SELECT COUNT(*) FROM $backuphistschema.backup_history WHERE butype = 'Full' AND status = 'SUCCEEDED' AND hostname = '$mhost' AND deleted_at = 0 \" "
anyfull=$(eval "$anyfullcmd")
if [ "$bktype" = "directory" ] || [ "$bktype" = "prepared-archive" ]; then
if ( ( [ "$(date +%A)" = "$fullbackday" ] || [ "$fullbackday" = "Everyday" ]) && [ "$alreadyfull" -eq 0 ] ) || [ "$anyfull" -eq 0 ] ; then
butype=Full
dirname="$backupdir/$(hostname --short)/full-$dirdate"
innocommand=$innocommand" $dirname --no-timestamp"
else
if [ "$differential" = yes ] ; then
butype=Differential
diffbasecmd=$mysqlcommand" \"SELECT bulocation FROM $backuphistschema.backup_history WHERE status = 'SUCCEEDED' AND hostname = '$mhost' AND butype = 'Full' AND deleted_at = 0 ORDER BY start_time DESC LIMIT 1\" "
diffbase=$(eval "$diffbasecmd")
dirname="$backupdir/$(hostname --short)/diff-$dirdate"
innocommand=$innocommand" $dirname --no-timestamp --incremental --incremental-basedir=$diffbase"
else
butype=Incremental
incbasecmd=$mysqlcommand" \"SELECT bulocation FROM $backuphistschema.backup_history WHERE status = 'SUCCEEDED' AND hostname = '$mhost' AND deleted_at = 0 ORDER BY start_time DESC LIMIT 1\" "
incbase=$(eval "$incbasecmd")
dirname="$backupdir/$(hostname --short)/incr-$dirdate"
innocommand=$innocommand" $dirname --no-timestamp --incremental --incremental-basedir=$incbase"
fi
fi
elif [ "$bktype" = "archive" ] ; then
if ( ( [ "$(date +%A)" = "$fullbackday" ] || [ "$fullbackday" = "Everyday" ]) && [ "$alreadyfull" -eq 0 ] ) || [ "$anyfull" -eq 0 ] ; then
# if [ "$(date +%A)" = "$fullbackday" ] || [ "$fullbackday" = "Everyday" ] ; then
butype=Full
innocommand=$innocommand" /tmp --stream=$arctype --no-timestamp"
arcname="$backupdir/$(hostname --short)/full-$dirdate.$arctype.gz"
else
butype=Incremental
#incbasecmd=$mysqlcommand" \"SELECT bulocation FROM $backuphistschema.backup_history WHERE status = 'SUCCEEDED' AND hostname = '$mhost' AND deleted_at = 0 ORDER BY start_time DESC LIMIT 1\" "
incbasecmd=$mysqlcommand" \"SELECT to_lsn FROM $backuphistschema.backup_history WHERE status = 'SUCCEEDED' AND hostname = '$mhost' AND deleted_at = 0 ORDER BY start_time DESC LIMIT 1\" "
incbase=$(eval "$incbasecmd")
#innocommand=$innocommand" /tmp --stream=$arctype --no-timestamp --incremental --incremental-basedir=$incbase"
innocommand=$innocommand" /tmp --stream=$arctype --no-timestamp --incremental --incremental-lsn=$incbase"
arcname="$backupdir/$(hostname --short)/inc-$dirdate.$arctype.gz"
fi
fi
if [ -n "$databases" ] && [ "$bktype" = "prepared-archive" ]; then innocommand=$innocommand" --databases=$databases"; fi
[ ! -z "$backupuser" ] && innocommand=$innocommand" --user=$backupuser"
[ ! -z "$backuppass" ] && innocommand=$innocommand" --password=$backuppass"
[ ! -z "$socket" ] && innocommand=$innocommand" --socket=$socket"
[ ! -z "$host" ] && innocommand=$innocommand" --host=$host"
[ ! -z "$hostport" ] && innocommand=$innocommand" --port=$hostport"
if [ "$galera" = yes ] ; then innocommand=$innocommand" --galera-info" ; fi
if [ "$slave" = yes ] ; then innocommand=$innocommand" --slave-info" ; fi
if [ "$parallel" = yes ] ; then innocommand=$innocommand" --parallel=$threads" ; fi
if [ "$compress" = yes ] ; then innocommand=$innocommand" --compress --compress-threads=$threads" ; fi
if [ "$encrypt" = yes ] ; then innocommand=$innocommand" --encrypt=AES256 --encrypt-key-file=$cryptkey" ; fi
if [ "$nolock" = yes ] ; then innocommand=$innocommand" --no-lock" ; fi
if [ "$nolock" = yes ] && [ "$slave" = yes ] ; then innocommand=$innocommand" --safe-slave-backup" ; fi
}
# Function to decrypt xtrabackup_checkpoints
function checkpointsdecrypt {
xbcrypt -d --encrypt-key-file="$cryptkey" --encrypt-algo=AES256 < "$dirname"/xtrabackup_checkpoints.xbcrypt > "$dirname"/xtrabackup_checkpoints
}
# Function to do the backup
function backer_upper {
innocreate
if [ "$monyog" = yes ] ; then
log_info "Disabling MONyog alerts"
monyog disable
sleep 30
fi
if [ "$galera" = yes ] ; then
log_info "Enabling WSREP desync."
mysql --login-path=xtrabackup -e "SET GLOBAL wsrep_desync=ON;"
fi
log_info "Beginning ${butype} Backup"
log_info "Executing xtrabackup command: $(echo "$innocommand" | sed -e 's/password=.* /password=XXX /g')"
if [ "$bktype" = "directory" ] || [ "$bktype" = "prepared-archive" ]; then
$innocommand 2>> "$logfile"
log_check
#if [ "$encrypt" = yes ] && [ "$log_status" = "SUCCEEDED" ] ; then
#checkpointsdecrypt
#fi
fi
if [ "$bktype" = "archive" ] ; then
$innocommand 2>> "$logfile" | $computil -c > "$arcname"
log_check
fi
if [ "$galera" = yes ] ; then
log_info "Disabling WSREP desync."
queue=1
until [ "$queue" -eq 0 ]; do
queue=$(mysql --login-path=xtrabackup -ss -e "show global status like 'wsrep_local_recv_queue';" | awk '{ print $2 }')
sleep 10
done
mysql --login-path=xtrabackup -e "SET GLOBAL wsrep_desync=OFF;"
fi
if [ "$monyog" = yes ] ; then
log_info "Enabling MONyog alerts"
monyog enable
sleep 30
fi
if [ "$log_status" = "SUCCEEDED" ] && [ "$bktype" == "prepared-archive" ] ; then
backup_prepare
fi
log_info "$butype backup $log_status"
log_info "CAUTION: ALWAYS VERIFY YOUR BACKUPS."
}
# Function to prepare backup
function backup_prepare {
prepcommand="$innobackupex $dirname --apply-log"
if [ -n "$databases" ]; then prepcommand=$prepcommand" --export"; fi
log_info "Preparing backup."
$prepcommand 2>> "$logfile"
log_check
log_info "Backup prepare complete."
log_info "Archiving backup."
tar cf "$dirname.tar.gz" -C "$dirname" -I "$computil" . && rm -rf "$dirname"
log_info "Archiving complete."
}
# Function to build mysql command
function mysqlcreate {
mysql=$(command -v mysql)
mysqlcommand="$mysql"
mysqlcommand=$mysqlcommand" --login-path=xtrabackup"
# mysqlcommand=$mysqlcommand" -p$backuphistpass"
#mysqlcommand=$mysqlcommand" -h $backuphisthost"
# [ -n "$backuphistport" ] && mysqlcommand=$mysqlcommand" -P $backuphistport"
mysqlcommand=$mysqlcommand" -Bse "
}
# Function to build mysqldump command
function mysqldumpcreate {
mysqldump=$(command -v mysqldump)
mysqldumpcommand="$mysqldump"
mysqldumpcommand=$mysqldumpcommand" --login-path=xtrabackup"
#mysqldumpcommand=$mysqldumpcommand" -p$backuphistpass"
#mysqldumpcommand=$mysqldumpcommand" -h $backuphisthost"
#[ -n "$backuphistport" ] && mysqldumpcommand=$mysqldumpcommand" -P $backuphistport"
mysqldumpcommand=$mysqldumpcommand" $backuphistschema"
mysqldumpcommand=$mysqldumpcommand" backup_history"
}
# Function to create backup_history table if not exists
function create_history_table {
createtable=$(cat <<EOF
CREATE TABLE IF NOT EXISTS $backuphistschema.backup_history (
uuid varchar(40) NOT NULL,
hostname varchar(100) DEFAULT NULL,
start_time timestamp NULL DEFAULT NULL,
end_time timestamp NULL DEFAULT NULL,
bulocation varchar(255) DEFAULT NULL,
logfile varchar(255) DEFAULT NULL,
to_lsn bigint(20) DEFAULT NULL,
status varchar(25) DEFAULT NULL,
butype varchar(20) DEFAULT NULL,
bktype varchar(20) DEFAULT NULL,
arctype varchar(20) DEFAULT NULL,
compressed varchar(5) DEFAULT NULL,
encrypted varchar(5) DEFAULT NULL,
cryptkey varchar(255) DEFAULT NULL,
galera varchar(5) DEFAULT NULL,
slave varchar(5) DEFAULT NULL,
threads tinyint(2) DEFAULT NULL,
xtrabackup_version varchar(120) DEFAULT NULL,
server_version varchar(50) DEFAULT NULL,
backup_size varchar(20) DEFAULT NULL,
deleted_at timestamp NULL DEFAULT NULL,
PRIMARY KEY (uuid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
EOF
)
$mysqlcommand "$createtable" >> "$logfile"
log_info "backup history table created"
}
# Function to check if Percona backup history records exist and need migrated
function check_migrate {
perconacnt=$($mysqlcommand "SELECT COUNT(a.uuid) FROM PERCONA_SCHEMA.xtrabackup_history a LEFT JOIN $backuphistschema.backup_history b ON a.uuid = b.uuid WHERE b.uuid IS NULL;")
if [ "$perconacnt" -gt 0 ];
then
log_info "$perconacnt Percona backup history records not migrated. Migrating."
migrate
fi
}
# Function to migrate percona backup history records
function migrate {
migratesql=$(cat <<EOF
INSERT INTO $backuphistschema.backup_history (
uuid,
hostname,
start_time,
end_time,
bulocation,
status,
butype,
compressed,
encrypted,
xtrabackup_version
)
SELECT
uuid,
name,
start_time,
end_time,
SUBSTRING_INDEX(SUBSTRING_INDEX(tool_command, ' ', 1), ' ', -1) as backupdirV,
CASE
WHEN partial = 'N' THEN 'SUCCEEDED'
ELSE 'FAILED'
END AS statusV,
CASE
WHEN incremental = 'Y' THEN 'Incremental'
WHEN incremental = 'N' THEN 'Full'
ELSE null
END AS butypeV,
compressed,
encrypted,
concat('MIGRATED FROM PERCONA_SCHEMA - ',tool_version)
FROM PERCONA_SCHEMA.xtrabackup_history
EOF
)
$mysqlcommand "$migratesql" >> "$logfile"
$mysqlcommand "SELECT uuid, bulocation FROM $backuphistschema.backup_history WHERE deleted_at IS NULL" |
while read -r uuid bulocation; do
if test -d "$bulocation"
then
$mysqlcommand "UPDATE $backuphistschema.backup_history SET deleted_at = '0000-00-00 00:00:00' WHERE uuid = '$uuid';coomit; "
else
$mysqlcommand "UPDATE $backuphistschema.backup_history SET deleted_at = NOW() WHERE uuid = '$uuid';commit; "
fi
done
lefttomigratecnt=$($mysqlcommand "SELECT COUNT(*) FROM $backuphistschema.backup_history WHERE deleted_at IS NULL")
if [ "$lefttomigratecnt" -gt 0 ];
then
log_info "Something went wrong, some migrated records not updated correctly."
exit 1
else
log_info "$perconacnt Percona backup history records migrated."
fi
}
# Function to write backup history to database
function backup_history {
versioncommand=$mysqlcommand" \"SELECT @@version\" "
server_version=$(eval "$versioncommand")
xtrabackup_version=$(cat "$logfile" | grep "/usr/bin/innobackupex version")
to_lsn=$(cat "$logfile" |grep "xtrabackup: The latest check point (for incremental):"|sed 's/xtrabackup: The latest check point (for incremental): '\''//g'|sed 's/'\''//g')
if [ "$bktype" = "directory" ] || [ "$bktype" = "prepared-archive" ]; then
backup_size=$(du -sm "$dirname" | awk '{ print $1 }')"M"
bulocation="$dirname"
elif [ "$bktype" = "archive" ] ; then
backup_size=$(du -sm "$arcname" | awk '{ print $1 }')"M"
bulocation="$arcname"
fi
historyinsert=$(cat <<EOF
INSERT INTO $backuphistschema.backup_history (uuid, hostname, start_time, end_time, bulocation, logfile, to_lsn, status, butype, bktype, arctype, compressed, encrypted, cryptkey, galera, slave, threads, xtrabackup_version, server_version, backup_size, deleted_at)
VALUES (UUID(), "$mhost", "$starttime", "$endtime", "$bulocation", "$logfile", "$to_lsn", "$log_status", "$butype", "$bktype", "$arctype", "$compress", "$encrypt", "$cryptkey", "$galera", "$slave", "$threads", "$xtrabackup_version", "$server_version", "$backup_size", 0);commit;
EOF
)
$mysqlcommand "$historyinsert"
#verify insert
verifyinsert=$($mysqlcommand "select count(*) from $backuphistschema.backup_history where hostname='$mhost' and end_time='$endtime'")
if [ "$verifyinsert" -eq 1 ]; then
log_info "Backup history database record inserted successfully."
else
echo "Backup history database record NOT inserted successfully!"
log_info "Backup history database record NOT inserted successfully!"
log_status=FAILED
mail_log
exit 1
fi
}
# Function to cleanup backups.
function backup_cleanup {
if [ $log_status = "SUCCEEDED" ]; then
limitoffset=$((keepnum-1))
delcountcmd=$mysqlcommand" \"SELECT COUNT(*) FROM $backuphistschema.backup_history WHERE end_time < (SELECT end_time FROM $backuphistschema.backup_history WHERE butype = 'Full' ORDER BY end_time DESC LIMIT $limitoffset,1) AND hostname = '$mhost' AND status = 'SUCCEEDED' AND deleted_at = 0\" "
delcount=$(eval "$delcountcmd")
if [ "$delcount" -gt 0 ]; then
deletecmd=$mysqlcommand" \"SELECT bulocation FROM $backuphistschema.backup_history WHERE end_time < (SELECT end_time FROM $backuphistschema.backup_history WHERE butype = 'Full' ORDER BY end_time DESC LIMIT $limitoffset,1) AND hostname = '$mhost' AND status = 'SUCCEEDED' AND deleted_at = 0\" "
eval "$deletecmd" | while read -r todelete; do
log_info "Deleted backup $todelete"
markdeletedcmd=$mysqlcommand" \"UPDATE $backuphistschema.backup_history SET deleted_at = NOW() WHERE bulocation = '$todelete' AND hostname = '$mhost' AND status = 'SUCCEEDED';commit; \" "
rm -Rf "$todelete"
eval "$markdeletedcmd"
done
else
log_info "No backups to delete at this time."
fi
else
log_info "Backup failed. No backups deleted at this time."
fi
}
# Function to dump $backuphistschema schema
function mdbutil_backup {
if [ $log_status = "SUCCEEDED" ]; then
mysqldumpcreate
mdbutildumpfile="$backupdir"/"$(hostname --short)"/"$backuphistschema".backup_history-"$dirdate".sql
$mysqldumpcommand > "$mdbutildumpfile"
log_info "Backup history table dumped to $mdbutildumpfile"
fi
}
# Function to cleanup mdbutil backups
function mdbutil_backup_cleanup {
if [ $log_status = "SUCCEEDED" ]; then
delbkuptbllist=$(ls -tp "$backupdir"/"$(hostname --short)" | grep "$backuphistschema".backup_history | tail -n +$((keepbkuptblnum+=1)))
for bkuptbltodelete in $delbkuptbllist; do
rm -f "$backupdir"/"$(hostname --short)"/"$bkuptbltodelete"
log_info "Deleted backup history backup $bkuptbltodelete"
done
else
log_info "Backup failed. No backup history backups deleted at this time."
fi
}
# Function to check config parameters
function config_check {
# if [[ "$bktype" = "archive" || "$bktype" = "prepared-archive" ]] && [ "$compress" = "yes" ] ; then
# log_info "Archive backup type selected, disabling built-in compression."
# compress="no"
# fi
if [[ "$computil" != "gzip" && "$computil" != "pigz" ]] && [ "$bktype" = "archive" ]; then
verbose="yes"
log_info "Fatal: $computil compression method is unsupported."
log_status=FAILED
mail_log
exit 1
fi
}
# Function to delete logfiles older than the full log retention days
function logfile_cleanup {
#log_info "deleting the below logfile"
find $logpath -mtime +2 -name 'bgba*'
find $logpath -mtime +2 -name 'bgba*' -exec rm -f {} \;
}
#function to send the backup to s3 using s3cmd
function sendbackupToS3 {
checks3cmd=`$s3cmd ls|grep $s3bucket|wc -l`
if [ "$checks3cmd" -eq 1 ]; then
"$s3cmd" put "$arcname" "$s3bucket" 2>> "$logfile"
else
log_info "Fatal: $$s3bucket not found."
log_status=FAILED
mail_log
exit 1
fi
}
# Debug variables function
function debugme {
echo "host: " "$host"
echo "hostport: " "$hostport"
echo "backupuser: " "$backupuser"
echo "backuppass: " "$backuppass"
echo "bktype: " "$bktype"
echo "arctype: " "$arctype"
echo "monyog: " "$monyog"
echo "monyogserver: " "$monyogserver"
echo "monyoguser: " "$monyoguser"
echo "monyogpass: " "$monyogpass"
echo "monyoghost: " "$monyoghost"
echo "monyogport: " "$monyogport"
echo "fullbackday: " "$fullbackday"
echo "keepday: " "$keepday"
echo "backupdir: " "$backupdir"
echo "logpath: " "$logpath"
echo "threads: " "$threads"
echo "parallel: " "$parallel"
echo "encrypt: " "$encrypt"
echo "cryptkey: " "$cryptkey"
echo "nolock: " "$nolock"
echo "compress: " "$compress"
echo "galera: " "$galera"
echo "slave: " "$slave"
echo "maillist: " "$maillist"
echo "mailsubpre: " "$mailsubpre"
echo "mdate: " "$mdate"
echo "logfile: " "$logfile"
echo "queue: " "$queue"
echo "butype: " "$butype"
echo "log_status: " "$log_status"
echo "budirdate: " "$budirdate"
echo "innocommand: " "$innocommand"
echo "prepcommand: " "$prepcommand"
echo "dirname: " "$dirname"
echo "mhost: " "$mhost"
echo "budir: " "$budir"
echo "run_after_success: " "$run_after_success"
echo "run_after_fail: " "$run_after_fail"
}
############################################
# Begin script
# we trap control-c
trap sigint INT
# find and source the config file
etccnf=$( find /encrypted_data/mysql/conf -name bgbackup.cnf )
scriptdir=$( cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
if [ -e "$etccnf" ]; then
source "$etccnf"
elif [ -e "$scriptdir"/bgbackup.cnf ]; then
source "$scriptdir"/bgbackup.cnf
else
echo "Error: bgbackup.cnf configuration file not found"
echo "The configuration file must exist somewhere in /encrypted_data/mysql/conf or"
echo "in the same directory where the script is located"
exit 1
fi
if [ ! -d "$logpath" ]; then
echo "Error: Log dir $logpath not found"
exit 1
fi
if [ ! -w "$logpath" ]; then
echo "Error: Log dir $logpath not writeable"
exit 1
fi
# Set some specific variables
starttime=$(date +"%Y-%m-%d %H:%M:%S")
mdate=$(date +%m/%d/%y) # Date for mail subject. Not in function so set at script start time, not when backup is finished.
logfile=$logpath/bgbackup_$(date +%Y-%m-%d-%T).log # logfile
# verify the backup directory exists
if [ ! -d "$backupdir" ]
then
log_info "Error: $backupdir directory not found"
log_info "The configured directory for backups does not exist. Please create this first."
log_status=FAILED
mail_log
exit 1
fi
# verify user running script has permissions needed to write to backup directory
if [ ! -w "$backupdir" ]; then
log_info "Error: $backupdir directory is not writable."
log_info "Verify the user running this script has write access to the configured backup directory."
log_status=FAILED
mail_log
exit 1
fi
#check for s3cmd
if [ -f $s3cmd ]; then
s3cmd=$s3cmd
else
log_info "s3cmd does not appear to be installed. Please install and try again."
log_status=FAILED
mail_log
exit 1
fi
# Check for xtrabackup
if [ -f $innobackupex ]; then
innobackupex=$innobackupex
else
log_info "xtrabackup/innobackupex does not appear to be installed. Please install and try again."
log_status=FAILED
mail_log
exit 1
fi
mysqlcreate
# Check that mysql client can connect
$mysqlcommand "SELECT 1 FROM DUAL" 1>/dev/null
if [ "$?" -eq 1 ]; then
log_info "Error: mysql client is unable to connect with the information you have provided. Please check your configuration and try again."
log_status=FAILED
mail_log
exit 1
fi
# Check that the database exists before continuing further
$mysqlcommand "USE $backuphistschema"
if [ "$?" -eq 1 ]; then
log_info "Error: The schema containing the history '$backuphistschema' does not exist. Please check your configuration and try again."
log_status=FAILED
mail_log
exit 1
fi
check_table=$($mysqlcommand "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$backuphistschema' AND table_name='backup_history' ")
if [ "$check_table" -eq 0 ]; then
create_history_table # Create history table if it doesn't exist
fi
if [ "$checkmigrate" = yes ] ; then
check_migrate # Check if Percona backup history records exist and migrate if needed
fi
config_check # Check vital configuration parameters
backer_upper # Execute the backup.
#checking iof the s3 backup is enabled
if [ "$backuploc" = "s3" ] ; then
sendbackupToS3 # Sending the backup to s3
fi
backup_cleanup # Cleanup old backups.
endtime=$(date +"%Y-%m-%d %H:%M:%S")
backup_history
mdbutil_backup
mdbutil_backup_cleanup
logfile_cleanup
if ( [ "$log_status" = "FAILED" ] && [ "$mailon" = "failure" ] ) || [ "$mailon" = "all" ] ; then
mail_log # Mail results to maillist.
fi
# run commands after backup, eventually
if [ "$log_status" = "SUCCEEDED" ] && [ ! -z "$run_after_success" ] ; then
$run_after_success # run the command if backup was successful
elif [ "$log_status" = "FAILED" ] && [ ! -z "$run_after_fail" ] ; then
$run_after_fail # run the command if backup had failed
fi
if [ "$debug" = yes ] ; then
debugme
fi
exit