-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_backup.sh
executable file
·104 lines (84 loc) · 2.1 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
#!/bin/bash
SHELL=/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin
osname=`uname`
date_today=$(date)
script=${0}
scriptpath=$(dirname "$script")
logpath=$scriptpath'/backup.log'
function HELP {
echo "This is help"
exit 1
}
# Getting required parameters
while getopts h:u:P:p:d:H opt; do
case $opt in
h)
host=$OPTARG
;;
u)
username=$OPTARG
;;
P)
port=$OPTARG
;;
p)
password=$OPTARG
;;
d)
databases=$OPTARG
;;
H)
HELP
;;
\?) #unrecognized option - show help
echo -e \\n"Option -${BOLD}$OPTARG${NORM} not allowed. "
HELP
esac
done
if [ -z "$username" ]; then
echo -n 'Enter Administrative username : '
read username
if [ -z "$username" ]; then
exit 0
fi
fi
# Getting password
if [ -z "$password" ]; then
echo -n 'Enter MySQL Password : '
read -s password
echo
fi
while [ -z "$databases" ]; do
echo 'Specify Database comma separated (-d) : '
read databases
done
# Convert comma separated string to array
IFS=', ' read -a databases <<< "$databases"
echo '============ '+$date_today+'=========' >> $logpath
max=${#databases[@]}
echo 'number of DB to backup '$max
for ((i=0; i<max; i++))
do
started_date=$(date +'%m-%d-%y')'_'$(date +'%T')
filename=$scriptpath'/'${databases[$i]}'_'$started_date'.sql'
command='/usr/local/bin/mysqldump '
if [ ! -z "$host" ]; then
command=$command' -h'$host
fi
command=$command' -u'$username
if [ ! -z "$password" ]; then
command=$command' -p'$password
fi
command=$command' '${databases[$i]}
$command > $filename # put dump to file
retval=$?
echo $retval
if [ $retval -eq 0 ]; then
end_date=$(date +'%m-%d-%y')'_'$(date +'%T')
echo "${databases[$i]} backup success : $end_date" >> $logpath
else
end_date=$(date +'%m-%d-%y')'_'$(date +'%T')
echo "${databases[$i]} backup error $retval : $end_date" >> $logpath
fi
done