forked from MiteshShah/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrsnap-mysql.sh
executable file
·129 lines (97 loc) · 3.78 KB
/
rsnap-mysql.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
#!/bin/bash
# -------------------------------------------------------------------------
# Modified By: Mitesh Shah
# Copyright (c) 2007 Vivek Gite <vivek@nixcraft.com>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
### Handle Kernal Signals ####
HandleSignals()
{
echo -e \\t \\t "Rsnapshot-MySQL Script (Failed) Caught Termination Signal" &>> $RsnapLOGS/rsnap-mysql.log
#Unset Trap So We Don't Get Infinate Loop
trap - INT TERM QUIT ABRT KILL
#Flush File System Buffers
#More Details: info coreutils 'sync invocation'
sync
#Exit The Script
exit 0
}
trap "HandleSignals" INT TERM QUIT ABRT KILL
### Set Bins Path ###
RM=/bin/rm
GZIP=/bin/gzip
GREP=/bin/grep
MKDIR=/bin/mkdir
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump
MYSQLADMIN=/usr/bin/mysqladmin
### SETUP MySQL LOGIN ###
if [ -f ~/.my.cnf ]
then
MySQLUSER=$(cat ~/.my.cnf | grep user | cut -d'=' -f2)
MySQLPASS=$(cat ~/.my.cnf | grep pass | cut -d'=' -f2)
else
read -p "Enter The MySQL Username: " MySQLUSER
# Turn Off Echo For Passwords
stty -echo
read -p "Enter The MySQL Password: " MySQLPASS
stty echo
echo
fi
HOSTIP="127.0.0.1"
### Enable Log = 1 ###
LOGS=1
### Default Time Format ###
TIME_FORMAT='%d%b%Y%H%M%S'
### Setup Dump And Log Directory ###
RsnapROOT=/var/rsnap-mysql
RsnapLOGS=/var/log/rsnap-mysql
#####################################
### ----[ No Editing below ]------###
#####################################
### Die On Demand With Message ###
die(){
echo "$@"
exit 999
}
### Make Sure Bins Exists ###
verify_bins(){
[ ! -x $GZIP ] && die "File $GZIP does not exists. Make sure correct path is set in $0."
[ ! -x $MYSQL ] && die "File $MYSQL does not exists. Make sure correct path is set in $0."
[ ! -x $MYSQLDUMP ] && die "File $MYSQLDUMP does not exists. Make sure correct path is set in $0."
[ ! -x $RM ] && die "File $RM does not exists. Make sure correct path is set in $0."
[ ! -x $MKDIR ] && die "File $MKDIR does not exists. Make sure correct path is set in $0."
[ ! -x $MYSQLADMIN ] && die "File $MYSQLADMIN does not exists. Make sure correct path is set in $0."
[ ! -x $GREP ] && die "File $GREP does not exists. Make sure correct path is set in $0."
}
### Make Sure We Can Connect To The Server ###
verify_mysql_connection(){
$MYSQLADMIN -u $MySQLUSER -h $HOSTIP -p$MySQLPASS ping | $GREP 'alive' > /dev/null
[ $? -eq 0 ] || die "Error: Cannot connect to MySQL Server. Make sure username and password are set correctly in $0"
}
### Make A Backup ###
backup_mysql_rsnapshot(){
local DBS="$($MYSQL -u $MySQLUSER -h $HOSTIP -p$MySQLPASS -Bse 'show databases')"
local db="";
[ ! -d $RsnapLOGS ] && $MKDIR -p $RsnapLOGS
[ ! -d $RsnapROOT ] && $MKDIR -p $RsnapROOT
$RM -f $RsnapROOT/* > /dev/null 2>&1
[ $LOGS -eq 1 ] && echo "" &>> $RsnapLOGS/rsnap-mysql.log
[ $LOGS -eq 1 ] && echo "*** Dumping MySQL Database At $(date) ***" &>> $RsnapLOGS/rsnap-mysql.log
[ $LOGS -eq 1 ] && echo "Database >> " &>> $RsnapLOGS/rsnap-mysql.log
for db in $DBS
do
local TIME=$(date +"$TIME_FORMAT")
local FILE="$RsnapROOT/$db.$TIME.gz"
[ $LOGS -eq 1 ] && echo -e \\t "$db" &>> $RsnapLOGS/rsnap-mysql.log
$MYSQLDUMP --single-transaction -u $MySQLUSER -h $HOSTIP -p$MySQLPASS $db | $GZIP -9 > $FILE || echo -e \\t \\t "MySQLDump Failed $db"
done
[ $LOGS -eq 1 ] && echo "*** Backup Finished At $(date) [ files wrote to $RsnapROOT] ***" &>> $RsnapLOGS/rsnap-mysql.log
}
### Main ####
verify_bins
verify_mysql_connection
backup_mysql_rsnapshot