MySQL Backup--使用mysqldump依次备份所有数据库
某些场景下需要将数据库分开备份,有些场景又需要将所有数据库合在一起备份,特此整理此备份脚本
#!/bin/bash
##===========================================================================##
## use mysqldump to backup all user database, keey days.
##===========================================================================##
## mysql_backup_database option:
## ONE_BACKUP: backup all user database into one zip file
## MORE_BACKUP: backup user database into different zip files.
## DATABASE_NAME: only backup the specified database. ##===========================================================================##
## mysql backup config
mysql_exe="/export/servers/mysql/bin/mysql"
mysqldump_exe="/export/servers/mysql/bin/mysqldump"
mysql_backup_folder="/export/data/mysql/dumps/"
mysql_backup_log="${mysql_backup_folder}mysql_dump_log.txt"
mysql_backup_log_his="${mysql_backup_folder}mysql_dump_log_his.txt"
mysql_backup_host="127.0.0.1"
mysql_backup_port=
mysql_backup_user="root"
mysql_backup_password="rootpwd"
mysql_backup_database="ONE_BACKUP"
mysql_backup_table_name_list=""
mysql_backup_keep_days= ##====================================================##
## get mysql version
##====================================================##
function get_mysql_version()
{
master_version_tmp=`${mysql_exe} \
--host="${mysql_backup_host}" --port=${mysql_backup_port} \
--user="${mysql_backup_user}" --password="${mysql_backup_password}" \
-e "select @@version;"`
if [[ master_version_tmp == 5.5.* ]]
then
mysql_version="mysql55"
elif [[ master_version_tmp == 5.6.* ]]
then
mysql_version="mysql56"
else
mysql_version="mysql57"
fi
} ##===========================================================================##
## remove expired backup file
## keep the backup file of the last N days
function remove_expired_file()
{
echo "$(date "+%Y-%m-%d %H:%M:%S") start to remove expired backup file." >> ${mysql_backup_log}
echo "keep days: ${mysql_backup_keep_days}" >> ${mysql_backup_log}
find "${mysql_backup_folder}" -mtime +${mysql_backup_keep_days} -name "*" -exec rm -rf {} \;
echo "$(date "+%Y-%m-%d %H:%M:%S") start to mysqldump." >> ${mysql_backup_log}
} ##===========================================================================##
## backup single database
function backup_single_database()
{
current_database_name=$
mysql_backup_file_path="${mysql_backup_folder}""${current_database_name}-`date -I`.sql.gz"
echo "$(date "+%Y-%m-%d %H:%M:%S") start to backup database ${current_database_name} to ${mysql_backup_file_path}" >> ${mysql_backup_log} ($mysqldump_exe \
--host="${mysql_backup_host}" \
--port=$mysql_backup_port \
--user="${mysql_backup_user}" \
--password="${mysql_backup_password}" \
--databases "${current_database_name}" \
--set-gtid-purged=OFF \
--single-transaction \
--hex-blob --opt --quick \
--events --routines --triggers \
--default-character-set="utf8" \
--master-data= \
|gzip > "${mysql_backup_file_path}" \
) >>${mysql_backup_log} >>${mysql_backup_log} if [ $? = ]
then
echo "$(date "+%Y-%m-%d %H:%M:%S") backup database ${current_database_name} success." >> ${mysql_backup_log}
else
echo "$(date "+%Y-%m-%d %H:%M:%S") backup database ${current_database_name} failed." >> ${mysql_backup_log}
fi
} function backup_more_databases()
{
for database_name in ${database_name_list};
do
if [ "$database_name" == "" ]
then
echo "database name can be empty"
else
backup_single_database "${database_name}"
fi
done
} function backup_all_databases()
{
mysql_backup_file_path="${mysql_backup_folder}""full-backup-`date -I`.sql.gz"
echo "$(date "+%Y-%m-%d %H:%M:%S") start to backup all databases to ${mysql_backup_file_path}" >> ${mysql_backup_log} ($mysqldump_exe \
--host="${mysql_backup_host}" \
--port=$mysql_backup_port \
--user="${mysql_backup_user}" \
--password="${mysql_backup_password}" \
--all-databases \
--ignore-table='mysql.*' \
--ignore-table='information_schema.*' \
--ignore-table='performance_schema.*' \
--ignore-table='sys.*' \
--set-gtid-purged=OFF \
--single-transaction \
--hex-blob --opt --quick \
--events --routines --triggers \
--default-character-set="utf8" \
--master-data= \
|gzip > "${mysql_backup_file_path}" \
) >>${mysql_backup_log} >>${mysql_backup_log} if [ $? = ]
then
echo "$(date "+%Y-%m-%d %H:%M:%S") backup database ${current_database_name} success." >> ${mysql_backup_log}
else
echo "$(date "+%Y-%m-%d %H:%M:%S") backup database ${current_database_name} failed." >> ${mysql_backup_log}
fi
} function backup_databases()
{
if [ "${mysql_backup_database}" == "ONE_BACKUP" ];
then
backup_all_databases
elif [ "${mysql_backup_database}" == "MORE_BACKUP" ];
then
database_name_list=`${mysql_exe} --host="${mysql_backup_host}" --port=$mysql_backup_port --user="${mysql_backup_user}" --password="${mysql_backup_password}" -Ne "show databases;" |egrep -v "(mysql|sys|test|information_schema|performance_schema|dada_call_center)"`
backup_more_databases
else
database_name_list=${mysql_backup_database}
backup_more_databases
fi
} ##====================================================##
## . dump user script on mysql
## . this script only can be used on mysql 5.7
##====================================================##
function dump_user_script_5_7()
{
script_file_path="${mysql_backup_folder}""user-script-`date -I`.sql"
echo "$(date "+%Y-%m-%d %H:%M:%S") start backup user script." >> ${mysql_backup_log}
(echo "select concat('show create user ''',user,'''@''',host, ''';
','show grants for ''',user,'''@''',host, ''';')
from mysql.user where user <>'root' and user<>'' and host <> '' " | \
${mysql_exe} --host="${mysql_backup_host}" --port=${mysql_backup_port} \
--user="${mysql_backup_user}" --password="${mysql_backup_password}" -N | \
${mysql_exe} --host="${mysql_backup_host}" --port=${mysql_backup_port} \
--user="${mysql_backup_user}" --password="${mysql_backup_password}" -N | \
sed "s/$/;/" >> ${script_file_path}) >>${mysql_backup_log} >>${mysql_backup_log} if [ $? = ]
then
echo "$(date "+%Y-%m-%d %H:%M:%S") backup user script success." >> ${mysql_backup_log}
else
echo "$(date "+%Y-%m-%d %H:%M:%S") backup user script failed." >> ${mysql_backup_log}
fi
} ##====================================================##
## . dump user script on mysql
## . this script only can be used on mysql 5.5
##====================================================##
function dump_user_script_5_5()
{
script_file_path="${mysql_backup_folder}""user-script-`date -I`.sql"
echo "$(date "+%Y-%m-%d %H:%M:%S") start backup user script." >> ${mysql_backup_log} (echo "select concat('show grants for ''',user,'''@''',host, ''';')
from mysql.user where user <>'root' and user<>'' and host <> '' " | \
${mysql_exe} --host="${mysql_backup_host}" --port=${mysql_backup_port} \
--user="${mysql_backup_user}" --password="${mysql_backup_password}" -N | \
${mysql_exe} --host="${mysql_backup_host}" --port=${mysql_backup_port} \
--user="${mysql_backup_user}" --password="${mysql_backup_password}" -N | \
sed "s/$/;/" >> ${script_file_path}) >>${mysql_backup_log} >>${mysql_backup_log} if [ $? = ]
then
echo "$(date "+%Y-%m-%d %H:%M:%S") backup user script success." >> ${mysql_backup_log}
else
echo "$(date "+%Y-%m-%d %H:%M:%S") backup user script failed." >> ${mysql_backup_log}
fi
} ##===========================================================================## function backup_mysql_user()
{
if [[ mysql_version == "mysql55" ]]
then
dump_user_script_5_5
else
dump_user_script_5_7
fi
} ##===========================================================================##
function mysql_backup()
{
echo > ${mysql_backup_log}
get_mysql_version
remove_expired_file
backup_databases
backup_mysql_user
cat ${mysql_backup_log} > ${mysql_backup_log_his}
} mysql_backup
MySQL Backup--使用mysqldump依次备份所有数据库的更多相关文章
- Mysql启停以及恢复备份恢复数据库
1.mysql启停 进入cmd 输入如下命令 net stop mysql(自己起的mysql名称) -------停 net strat mysql ---------------------- ...
- mysql导入导出.sql文件 备份还原数据库
从数据库导出数据库文件: 进入你的MySQL的安装目录的bin目录或者在C盘的根目录都行,我选的是在bin目录下,下面的例子出第一个外将以在C盘的根目录来讲解 我的mysql安装在了C盘,C: ...
- 使用mysqldump命令备份恢复MySQL数据库
1.各种用法说明 A. 最简单的用法: mysqldump -uroot -pPassword [database name] > [dump file] 上述命令将指定数据库备份到某dump文 ...
- mysql学习(4)-mysqldump备份和恢复数据
背景 最近在公司做数据迁移方面的工作,使用mysql数据库在测试环境模拟数据迁移,在迁移测试的过程中需要做数据备份和恢复 mysql数据备份和恢复比较简单,可以选择mysqldump工具,这里简单提一 ...
- MySQL 5.7.10 自动备份、自动清理旧备份集
http://blog.csdn.net/mchdba/article/details/51527081 MySQL版本是5.7.10-log社区版本,需要进行备份,但是备份时间长了后,磁盘不够用,所 ...
- mysql 容灾 灾备 备份
一.数据备份 1.使用mysqldump命令备份 mysqldump命令将数据库中的数据备份成一个文本文件.表的结构和表中的数据将存储在生成的文本文件中. mysqldump命令的工作原理很简单.它先 ...
- MySQL 5.7.10 自动备份、自动清理旧备份集(转)
1,mysqldump备份脚本 备份脚本为,里面有几个需要注意的参数: (1)--master-data=2 :这个参数可以在搭建从库的时候,记录当前备份的复制点信息. (2)--extended-i ...
- 动态备份SQL-SERVER数据库——SQLDMO
转载:http://www.cnblogs.com/liulanglang/archive/2007/12/04/981812.html 上周要写一个SQL-SERVER数据库备份还原的程序,很没有思 ...
- MySQL Backup mysqldump备份流程学习
我们都知道MySQL逻辑备份工具mysqldump可以保证备份数据的一致性,但是它是怎么保持一致性的? 本文不讨论mysqldump具体的选项和用法,一直对mysqldump的工作机制梳理的不太清楚, ...
随机推荐
- 【Spring IoC】IoC介绍(一)
IoC(Inversion of Control)的职责:原先由程序员主动通过new实例化对象这个事情,现在交由Spring负责,即由IoC容器负责. Spring 容器是 Spring 框架的核心. ...
- 【入门篇一】SpringBoot简介(1)
官网:https://spring.io/projects/spring-boot 一.什么是SpringBoot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新S ...
- java第三讲课后动手动脑及代码编写
1. 类就是类型,对象就是这种类型的实例,也就是例子.类是抽象的东西,对象是某种类的实实在在的例子.例如:车是一个类,汽车,自行车就是他的对象. 对象的定义方法? (1)对象声明:类名 对象名: (2 ...
- Task异常捕获的几种方式
在调用Task的Wait()方法或Result属性处会抛出Task中的异常. 但是如果没有返回结果,或者不想调用Wait()方法,该怎么获取异常呢? 可以使用ContinueWith()方法 var ...
- es6模块化规范
在 es6 之前是没有块这个概念的,es6zhong 引入:实际如下: 若 xx1 和 xx2 中有变量名相同,且引入在同一 html 下,需要为引入的 script 标签加上 type=“modul ...
- [LeetCode] 394. Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- vue图片放大、缩小、旋转等
用于图片浏览的Vue组件,支持旋转.缩放.翻转等操作,基于viewer.js. 效果: 安装 使用npm命令安装 npm install v-viewer 使用 引入v-viewer及必需的css样式 ...
- C#面试题(一)
一.背景 最近学习巩固C#的基础,看到<小胖李的面试之旅>的文章,把他的面试题目拿来总结以及学习. 二.面试题 1.wcf想用http协议通信怎么做? 答:在配置文件里面配置,因为wcf默 ...
- java8 HashMap源码 详细研读
HashMap原理 目的: 单纯分析和学习hashmap的实现,不多说与Hashtable.ConcurrentHashMap等的区别. 基于 jdk1.8 在面试中有些水平的公司比较喜欢问HashM ...
- google 镜像
google 镜像 http://scholar.hedasudi.com/ http://ac.scmor.com/