-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmysqldump.sh
executable file
·148 lines (119 loc) · 3.94 KB
/
mysqldump.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
#!/bin/bash
#
# Logic Backup MySQL data using MySQL mysqldump tool
# Daniel Guzman Burgos <daniel.guzman.burgos@percona.com>
#
clear
set -o pipefail
# Initial values
lockFile="/var/lock/mysqldump.lock"
errorFile="/var/log/mysql/mysqldump.err"
logFile="/var/log/mysql/mysqldump.log"
mysqlUser=root
mysqlPort=3306
remoteHost=localhost
backupPath="/root/backups/$(date +%Y%m%d)/"
# 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 MySQLdump backups" "${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 verifyMysqldump () {
which mysqldump &> /dev/null
verifyExecution "$?" "Cannot find mysqldump tool" true
logInfo "[OK] Found 'mysqldump' bin"
}
function runMysqldump () {
verifyMysqldump
out=$(mkdir -p $backupPath)
verifyExecution "$?" "Can't create backup dir $backupPath. $out" true
logInfo "[Info] $backupPath exists"
local schemas=$(mysql -u${mysqlUser} -h${remoteHost} --port=${mysqlPort} -N -e"select schema_name from information_schema.schemata where schema_name not in ('information_schema', 'performance_schema')")
if [ ! -z "$schemas" ]; then
for i in $schemas; do
out=$(mysqldump -u${mysqlUser} -h${remoteHost} --port=${mysqlPort} --single-transaction --master-data=2 --events --routines $i | gzip > $backupPath/${i}.sql.gz 2>&1)
verifyExecution "$?" "Problems dumping db $i. $out"
logInfo "[OK] Dumping $i"
done
echo "Finished dump at: $(date "+%Y-%m-%d %H:%M:%S")" > $backupPath/.metadata
return
fi
verifyExecution "1" "While dumping the DBs, this happened: $schemas"
}
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
runMysqldump
removeOldBackup