mysql 备份指定前缀的数据库信息
mysql_dump.py 备份指定前缀的数据库信息,备份的文件支持gzip压缩!生成的文件名字指定了的DB名称加上导出数据的时间!
(1)运行脚本效果如下:
请输入连接数据库的账号:root
请输入连接数据库的密码:
请输入连接数据库的地址(默认:127.0.0.1):127.0.0.1
请输入导出数据库的前缀:test
请输入连接数据库的端口号(默认:3306):3306
请输入备份数据库的路径(默认:/home/,记得添加最后的斜线):/home/dbback/
备份【test】数据成功!!
备份【test001】数据成功!!
备份【test02】数据成功!!
备份【test07】数据成功!!
#!/usr/bin/bash
#@author shiwei.du
#@email dusw0214@126.com
#@desc Exports all databases that begin with the specified
#@created Thu Mar 10 18:34:50 CST 2016
#@github https://github.com/dsw0214/mysql
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
热门日志
分类
- Django(4)
- ssdb(1)
- Mac(7)
- C(1)
- memcache(1)
- Python(32)
- Vim(8)
- sed(2)
- ansible(3)
- awk(4)
- shell(3)
- about(1)
- git(9)
- bat(4)
- svn(0)
- docker(1)
- Tornado(1)
- go(2)
- 架构(18)
- Vue(1)
- game(2)
- Html(6)
- Java(8)
- Mysql(37)
- Ajax(2)
- Jsp(1)
- Struts(8)
- Linux(72)
- JavaScript(39)
- Staruml(0)
- Mouth(1)
- Php(102)
- Windows(8)
- Message(48)
- Lua(10)
- Compute(1)
- Redis(7)
- Nginx(12)
- Jquery(1)
- Apache(1)
- cocos2d-x(8)