-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql-backup.sh
128 lines (113 loc) · 3.07 KB
/
mysql-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
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
#!/bin/bash
# Variables
MYSQLDUMP="/usr/bin/mysqldump"
HOST="$1"
#BKP_USERNAME='' ENVIRONMENT
#BKP_PASSWORD='' ENVIRONMENT
BKP_DIR="/backups"
BKP_TMP="$BKP_DIR/$HOST.sql"
BKP_NAME="$HOST-$(date +%F-%H%M%S)"
LOG_DIR="/var/log/mysqlBackup"
PID_FILE="/var/run/mysqlBackup-$HOST.pid"
NUM_DUMPS="50"
NUM_LOGS="60"
# Functions
log() {
echo "$(date +%F) $(date +%H%M) | $@" >> $LOG_DIR/$(hostname).$(date +%F).log
}
log_begin() {
log "--------------------- Backup Begin ---------------------"
}
log_end() {
log "--------------------- Backup End ----------------------"
}
create_dir() {
if [ ! -d "$1" ]; then
mkdir -p $1
log "$1 directory was created"
fi
}
pidfile_check() {
if [ -e $PID_FILE ]; then
log "mysqlBackup $PID_FILE already running. Leaving."
log_end
exit
fi
}
pidfile_create() {
pidfile_check
echo $$ > $PID_FILE
log "Pidfile was created."
}
pidfile_remove() {
rm -rf $PID_FILE
log "Pidfile was removed."
}
dump_cleanup() {
rm -rf $BKP_TMP
log "Uncompressed dump was cleaned"
}
is_running() {
if [ $(pgrep $1 | wc -l) -ne 0 ]; then
dump_cleanup
log "$1 is running. Leaving."
pidfile_remove
log_end
exit
fi
}
dump() {
log "Running mysqldump"
$MYSQLDUMP -h $HOST -u $BKP_USERNAME -p$BKP_PASSWORD --all-databases > $BKP_TMP
if [ $? -eq 0 ]; then
log "mysqldump completed successfully"
else
dump_cleanup
log "mysqldump failed"
pidfile_remove
log_end
exit
fi
}
dump_compress() {
is_running tar
tar czf $BKP_DIR/$BKP_NAME.tar.gz $BKP_TMP
log "Dump compression completed successfully"
}
dump_cleanup_old() {
if [ $(ls -d1rt $BKP_DIR/*.tar.gz | head -n -$NUM_DUMPS | wc -l) -gt 0 ]; then
log "Old dumps files:"
for i in $(ls -d1rt $BKP_DIR/*.tar.gz | head -n -$NUM_DUMPS); do
log "$i"
done
ls -d1rt $BKP_DIR/*.tar.gz | head -n -$NUM_DUMPS | xargs rm
log "Old dumps was cleaned successfully"
else
log "No old dumps found"
fi
}
log_cleanup_old() {
if [ $(ls -d1rt $LOG_DIR/*.log | head -n -$NUM_LOGS | wc -l) -gt 0 ]; then
log "Old log files:"
for i in $(ls -d1rt $LOG_DIR/*.log | head -n -$NUM_LOGS); do
log "$i"
done
ls -d1rt $LOG_DIR/*.log | head -n -$NUM_LOGS | xargs rm
log "Old logs was cleaned successfully"
else
log "No old logs found"
fi
}
# Main
create_dir $LOG_DIR # Create log directory
log_begin # Begin log transaction
pidfile_create # Creating Pidfile
create_dir $BKP_TMP # Create backup directory
dump_cleanup # Cleanup uncompressed dump (if exists)
dump # Begin mysqldump for all dbs
dump_compress # Compressing dumped databases
dump_cleanup # Cleanup uncompressed dump
#dump_cleanup_old # Cleanup old compressed dump (1 Day Old)
log_cleanup_old # Cleanup old logs
pidfile_remove # Removing Pidfile
log_end # End log transaction