-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_dump.sh
82 lines (78 loc) · 2.13 KB
/
mysql_dump.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
#!/usr/bin/bash
#@author shiwei.du
#@email [email protected]
#@desc Exports all databases that begin with the specified
#@created Thu Mar 10 18:34:50 CST 2016
host='127.0.0.1'
port=3306
backdir='/home/'
backname=`date +%Y-%m-%d-%s`
color_list=("\e[0;31m" "\e[0;32m" "\e[0;33m")
while [[ true ]]; do
echo -e -n ${color_list[1]}
read -p "请输入连接数据库的账号:" user
echo -e -n "\e[0m"
if [ -z $user ];then
echo -e ${color_list[0]}"请输入连接数据库的账号\e[0m";
break
fi
if [ $user == '' ];then
echo -e ${color_list[0]}"请输入连接数据库的账号\e[0m";
break
fi
echo -e -n ${color_list[1]}
read -p "请输入连接数据库的密码:" -s passwd
echo -e "\e[0m"
if [ -z $passwd ];then
echo
echo -e ${color_list[0]}"请输入连接数据库的密码\e[0m";
break
fi
if [ $passwd == '' ];then
echo
echo -e ${color_list[0]}"请输入连接数据库的密码\e[0m";
break
fi
echo -e -n ${color_list[1]}
read -p "请输入连接数据库的地址(默认:$host):" hosts
echo -e -n "\e[0m"
if [ ! -z $hosts ]; then
host=$hosts
fi
echo -e -n ${color_list[1]}
read -p "请输入导出数据库的前缀:" prefix
echo -e -n "\e[0m"
if [ -z $prefix ]; then
echo
echo -e ${color_list[0]}"请输入导出数据库的前缀\e[0m";
break
fi
if [ $prefix == '' ];then
echo
echo -e ${color_list[0]}"请输入导出数据库的前缀\e[0m";
break
fi
echo -e -n ${color_list[1]}
read -p "请输入连接数据库的端口号(默认:$port):" posts
echo -e -n "\e[0m"
if [ ! -z $posts ]; then
port=$posts
fi
echo -e -n ${color_list[1]}
read -p "请输入备份数据库的路径(默认:$backdir,记得添加最后的斜线):" backdirs
echo -e -n "\e[0m"
if [ ! -z $backdirs ]; then
backdir=$backdirs
fi
dblists=`mysql -u$user -p$passwd -h$host -e "show databases;" | grep ^$prefix`
for db in $dblists
do
mysqldump -u$user -p$passwd -h$host $db | gzip > $backdir$db-$backname.sql.gz
if [[ $? == 0 ]]; then
echo -e "${color_list[1]}备份【$db】数据成功!!\e[0m";
else
echo -e "${color_list[0]}备份【$db】数据失败!!\e[0m";
fi
done
exit
done