-
Notifications
You must be signed in to change notification settings - Fork 14
/
xtrabackup.sh
executable file
·151 lines (120 loc) · 4.03 KB
/
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
#!/bin/bash
#
# Raw Backup MySQL data using Percona XtraBackup tool
# Daniel Guzman Burgos <daniel.guzman.burgos@percona.com>
#
clear
set -o pipefail
# Initial values
lockFile="/var/lock/xtrabackup.lock"
errorFile="/var/log/mysql/xtrabackup.err"
logFile="/var/log/mysql/xtrabackup.log"
mysqlUser=root
mysqlPort=3306
backupPath="/root/backups/xtrabackup/$(date +%Y%m%d%H%M%S)/"
# Retention times #
weekly=4
daily=7
######
email="root@localhost"
# Function definitions
function sendAlert () {
if [ -e "$errorFile" ]
then
alertMsg=$(cat $errorFile)
echo -e "${alertMsg}" | mailx -s "[$HOSTNAME] ALERT XtraBackup backup" "${email}"
fi
}
function destructor () {
sendAlert
rm -f "$lockFile" "$errorFile"
}
# Setting TRAP in order to capture SIG and cleanup things
trap destructor EXIT INT TERM
function verifyExecution () {
local exitCode="$1"
local mustDie=${3-:"false"}
if [ $exitCode -ne "0" ]
then
msg="[ERROR] Failed execution. ${2}"
echo "$msg" >> ${errorFile}
if [ "$mustDie" == "true" ]; then
exit 1
else
return 1
fi
fi
return 0
}
function setLockFile () {
if [ -e "$lockFile" ]; then
trap - EXIT INT TERM
verifyExecution "1" "Script already running. $lockFile exists"
sendAlert
rm -f "$errorFile"
exit 2
else
touch "$lockFile"
fi
}
function logInfo (){
echo "[$(date +%y%m%d-%H:%M:%S)] $1" >> $logFile
}
function verifyXtrabackup () {
which xtrabackup &> /dev/null
verifyExecution "$?" "Cannot find xtrabackup tool" true
logInfo "[OK] Found 'xtrabackup' bin"
which innobackupex &> /dev/null
verifyExecution "$?" "Cannot find innobackupex tool" true
logInfo "[OK] Found 'innobackupex' bin"
}
function runXtrabackup () {
verifyXtrabackup
out=$(innobackupex --user=$mysqlUser --slave-info --safe-slave-backup --parallel=4 --lock-wait-threshold=90 --lock-wait-query-type=all --lock-wait-timeout=300 --kill-long-queries-timeout=40 --kill-long-query-type=all --rsync --no-timestamp $backupPath > $logFile 2>&1)
verifyExecution "$?" "Problems executing innobackupex. $out" true
touch $backupPath/unprepared
logInfo "[OK] Innobackupex OK"
echo "Finished dump at: $(date "+%Y-%m-%d %H:%M:%S")" > $backupPath/.metadata
prepareXtrabackup
return
}
function prepareXtrabackup () {
out=$(innobackupex --apply-log $backupPath > $logFile 2>&1)
verifyExecution "$?" "Can't prepare backup $backupPath. $out" true
mv $backupPath/unprepared $backupPath/prepared
logInfo "[Info] $backupPath prepared"
}
function removeOldBackup () {
rootPath=$(dirname $backupPath 2>&1)
verifyExecution "$?" "Couldn't find backup path. $rootPath" true
pushd $rootPath &> /dev/null
daysAgo=$(date -d "$daily days ago" +%s)
weeksAgo=$(date -d "$weekly weeks ago" +%s)
logInfo "[Info] Removing old backups"
for i in $(ls -1); do
day=$(cat $rootPath/$i/.metadata | grep Finished | awk -F": " '{print $2}' | awk '{print $1}' 2>&1)
verifyExecution "$?" "Couldn't find $rootPath/$i/.metadata file. $day"
backupTs=$(date --date="$day" +%s)
# Remove weekly backups older than $weekly
if [ $weeksAgo -gt $backupTs ]; then
out=$(rm -rf $rootPath/$i 2>&1)
verifyExecution "$?" "Error removing $rootPath/${i}. $out"
logInfo " [OK] Removed $rootPath/$i weekly backup"
fi
# Do not remove daily backup if its from Sunday
weekDay=$(date --date="$day" +%u)
if [ $weekDay -eq 7 ]; then
continue;
fi
# Remove daily backups older than $daily
if [ $daysAgo -gt $backupTs ]; then
out=$(rm -rf $rootPath/$i 2>&1)
verifyExecution "$?" "Error removing $rootPath/${i}. $out"
logInfo " [OK] Removed $rootPath/$i daily backup"
fi
done
popd &> /dev/null
}
setLockFile
runXtrabackup
removeOldBackup