某些场景下需要将数据库分开备份,有些场景又需要将所有数据库合在一起备份,特此整理此备份脚本

#!/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依次备份所有数据库的更多相关文章

  1. Mysql启停以及恢复备份恢复数据库

    1.mysql启停 进入cmd 输入如下命令 net stop mysql(自己起的mysql名称) -------停 net strat mysql   ---------------------- ...

  2. mysql导入导出.sql文件 备份还原数据库

    从数据库导出数据库文件:   进入你的MySQL的安装目录的bin目录或者在C盘的根目录都行,我选的是在bin目录下,下面的例子出第一个外将以在C盘的根目录来讲解   我的mysql安装在了C盘,C: ...

  3. 使用mysqldump命令备份恢复MySQL数据库

    1.各种用法说明 A. 最简单的用法: mysqldump -uroot -pPassword [database name] > [dump file] 上述命令将指定数据库备份到某dump文 ...

  4. mysql学习(4)-mysqldump备份和恢复数据

    背景 最近在公司做数据迁移方面的工作,使用mysql数据库在测试环境模拟数据迁移,在迁移测试的过程中需要做数据备份和恢复 mysql数据备份和恢复比较简单,可以选择mysqldump工具,这里简单提一 ...

  5. MySQL 5.7.10 自动备份、自动清理旧备份集

    http://blog.csdn.net/mchdba/article/details/51527081 MySQL版本是5.7.10-log社区版本,需要进行备份,但是备份时间长了后,磁盘不够用,所 ...

  6. mysql 容灾 灾备 备份

    一.数据备份 1.使用mysqldump命令备份 mysqldump命令将数据库中的数据备份成一个文本文件.表的结构和表中的数据将存储在生成的文本文件中. mysqldump命令的工作原理很简单.它先 ...

  7. MySQL 5.7.10 自动备份、自动清理旧备份集(转)

    1,mysqldump备份脚本 备份脚本为,里面有几个需要注意的参数: (1)--master-data=2 :这个参数可以在搭建从库的时候,记录当前备份的复制点信息. (2)--extended-i ...

  8. 动态备份SQL-SERVER数据库——SQLDMO

    转载:http://www.cnblogs.com/liulanglang/archive/2007/12/04/981812.html 上周要写一个SQL-SERVER数据库备份还原的程序,很没有思 ...

  9. MySQL Backup mysqldump备份流程学习

    我们都知道MySQL逻辑备份工具mysqldump可以保证备份数据的一致性,但是它是怎么保持一致性的? 本文不讨论mysqldump具体的选项和用法,一直对mysqldump的工作机制梳理的不太清楚, ...

随机推荐

  1. oracle中having与where的区别

    1.where 不能放在group by 的后面 2.HAVING 是跟GROUP BY 连在一起用的,放在GROUP BY 后面,此时的作用相当于WHERE 3.WHERE 后面的条件中不能有聚集函 ...

  2. css网格布局

    先来一段基本布局 <!doctype html> <html> <head> <meta charset="utf-8"> < ...

  3. appium--连续滑动

    TouchAction 在之前说过了滑动swip,那种是两点之间的滑动,比如上滑,左滑等.但实际工作中会遇到一些复杂的场景,如九宫格的滑动等待,这时候就要使用TouchAction,TouchActi ...

  4. cd1101d 树形dp

    cd1101d 简单dp 链接 codeforces 思路 所有数的质因数存下来,最多6个. 然后\(f[i][j][0/1]\)表示i子树内链gcd为j的i是否为链头. 暴力转移就行了 代码 #in ...

  5. B1023 组个最小数 (20 分)

    一.技术总结 这一题主要的处理点是数字0别在第一个位置输出,这里提供两种解决思路. 一个是直接在一个for循环里面只要输出了一个位置后直接判断,是否了0的数字可以输出. 还有一个就是直接for循环一次 ...

  6. [LeetCode] 633. Sum of Square Numbers 平方数之和

    Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...

  7. 如何让position fixed不再基于浏览器窗口定位

    position:fixed默认是相对浏览器定位的. 就是将某个元素固定在浏览器的某个确定的位置,不随滚动条的移动而变化: MDN对position: fixed有一个注释: 当元素祖先的 trans ...

  8. 国家虚拟仿真实验教学项目共享平台(实验空间)PHP SDK

    使用XJWT标准,此标准基于JSON Web Token (JWT)开发.XJWT包含三个参数:header, payload, signature,因此生成token就要先获得这三个参数. clas ...

  9. python面试题第一份

    阅读目录 1 Python的函数参数传递 2 Python中的元类(metaclass) 3 @staticmethod和@classmethod 4 类变量和实例变量 5 Python自省 6 字典 ...

  10. RestTemplate使用教程

    原文地址:https://www.cnblogs.com/f-anything/p/10084215.html 一.概述 spring框架提供的RestTemplate类可用于在应用中调用rest服务 ...