-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprod_backup.sh
64 lines (52 loc) · 1.76 KB
/
prod_backup.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
#!/bin/bash
set -e #stops execution if a variable is not set
set -u #stop execution if something goes wrong
usage() {
echo "usage: $(basename $0) [option]"
echo "option=full: do a full backup of all databases."
echo "option=incremental: do a incremental backup"
echo "option=help: show this help"
}
full_backup() {
date
sudo innobackupex $ARGS ./ | ssh [email protected] "cat - > $BACKUP_DIR/full_backup_$( date '+%Y-%m-%d' ).xbstream"
date
}
incremental_backup()
{
date
echo "Connect to PERCONA_SCHEMA.xtrabackup_history and get the last incremental lsn"
last_full_time=$(mysql -u$USER -p$PWD -se "SELECT start_time FROM PERCONA_SCHEMA.xtrabackup_history where incremental='N' order by start_time desc limit 1;")
if [ -z "$last_full_time" ]
then
echo "ERROR: no full backup has been done before. aborting"
exit -1
fi
inc_num=$(mysql -u$USER -p$PWD -se "select count(*)+1 from PERCONA_SCHEMA.xtrabackup_history where incremental='Y' and start_time >= '$last_full_time' ;")
last_lsn=$(mysql -u$USER -p$PWD -se "select innodb_to_lsn from PERCONA_SCHEMA.xtrabackup_history order by start_time desc limit 1;")
sudo innobackupex $ARGS --incremental --incremental-lsn=$last_lsn ./ | ssh [email protected] "cat - > $BACKUP_DIR/incremental_backup_'$inc_num'_$( date '+%Y-%m-%d' ).xbstream"
date
}
#START HERE
BACKUP_DIR=/var/db_backups/prod_backups
USER="db-user"
PWD='db-pwd'
ARGS=" --user=$USER --password=$PWD --no-lock --history --stream=xbstream "
if [ $# -eq 0 ]
then
usage
exit 1
fi
case $1 in
"full")
full_backup
;;
"incremental")
incremental_backup
;;
"help")
usage
#break
;;
*) echo "invalid option";;
esac