MySQL自动化运维之用mysqldump和mysqlbinlog实现某一数据库的每周全备和每天差异备份,并添加到执行计划【热备】
案例:
线上有一数据库,需要每周全备一次,每天差备一次[安全起见还是差备吧,不要增备,不要吝啬磁盘哦,而且差备恢复还很快]
1、每周对数据库hellodb做完全备份
crontab任务计划:
10 01 * * 1 /bin/bash /work/dump-complete-hello.sh ===>每周周一凌晨1点10分执行全备脚本/work/dump-complete-hello.sh
全备脚本/work/dump-complete-hello.sh内容如下:
#!/bin/bash
# 全备文件存放位置
weekbackup=/complete/hello-`date +%F`.sql
# 用mysqldump执行全备
# --database后跟要备的数据库
# --master-data记录CHANGE MASTER TO语句,2表示把这一行注释
# --flush-logs锁定表之后执行flush logs命令,切换binlog文件
# --single-transaction:单个事物,由于数据库hellodb里面的表都是innodb存储引擎,支持事物,可以保证备份时数据处于一致状态
/usr/local/mysql/bin/mysqldump --database hellodb --master-data=2 --flush-logs --single-transaction > $weekbackup
# 后边的语句是为了创建一个存放全备文件存放位置的语句,增量备份时需要用到
cat > weekbackup.sh << EOF
#!/bin/bash
EOF
echo "wb=$weekbackup" >> weekbackup.sh
2、每日对数据库hellodb做差异备份:
crontab 任务计划:
20 02 * * * /bin/bash /work/dump-incre.sh ==>每天凌晨2点20分执行差备脚本/work/dump-incre.sh
差备脚本/work/dump-incre.sh内容如下:
#!/bin/bash
# source 一下/work/weekbackup.sh,该脚本是由最近一次全备脚本产生的,提供全备文件存放位置
. /work/weekbackup.sh
# 获取当前数据库使用的二进制文件
binlog=`/usr/local/mysql/bin/mysql -e 'show master status' | grep 'bin' | awk '{print $1}'`
# 从最近一次全备文件中获取全备终止time
time=grep 'completed' $wb | awk '{printf "%s %s\n",$5,$6}' # 通过mysqlbinlog对数据库hellodb进行差异备份
# --start-position 指明增量备份的起始position,其值为全备的终止position
# /var/log/mysql/binarylog/$binlog 为当前数据库正在使用的二进制日志文件
/usr/local/mysql/bin/mysqlbinlog --start-datetime="$time" /var/log/mysql/binarylog/$binlog > /increment/incre-`date +%F%H%M%S`.sql
3、恢复测试:
全备恢复:
[root@localhost data]# mysql < /complete/hello-2015-01-13.sql
[root@localhost data]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.36-MariaDB-log MariaDB Server
Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| newdb |
| performance_schema |
| tempdb |
| test |
+--------------------+
7 rows in set (0.00 sec)
MariaDB [(none)]> use hellodb;
Database changed
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
差备恢复:
[root@localhost data]# mysql < /increment/incre-2015-01-13.sql
[root@localhost data]# mysqll
-bash: mysqll: command not found
[root@localhost data]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.36-MariaDB-log MariaDB Server
Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use hellodb;
Database changed
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| tb1 |
| teachers |
| toc |
+-------------------+
8 rows in set (0.00 sec)
MariaDB [hellodb]> select * from tb1;
+------+
| name |
+------+
| wjs |
+------+
1 row in set (0.01 sec)
从上面的结果可知全备和差异备份都可恢复,那就可以投入正常使用了,可以交差了 ,哈哈
MySQL自动化运维之用mysqldump和mysqlbinlog实现某一数据库的每周全备和每天差异备份,并添加到执行计划【热备】的更多相关文章
- 部署MySQL自动化运维工具inception+archer
***************************************************************************部署MySQL自动化运维工具inception+a ...
- 有赞MySQL自动化运维之路—ZanDB
有赞MySQL自动化运维之路—ZanDB 一.前言 在互联网时代,业务规模常常出现爆发式的增长.快速的实例交付,数据库优化以及备份管理等任务都对DBA产生了更高的要求,单纯的凭借记忆力去管理那几十 ...
- Inception介绍(MySQL自动化运维工具)
Inception介绍 GitHub:https://github.com/mysql-inception/inception 文档:https://mysql-inception.github.io ...
- 有赞 MySQL 自动化运维之路 — ZanDB
转自:https://tech.youzan.com/youzan-mysql-auto-ops-road/ 一.前言 在互联网时代,业务规模常常出现爆发式的增长.快速的实例交付,数据库优化以及备份管 ...
- mysql操作及自动化运维
备份恢复工具:percona-xtrabackup-2.0.0-417.rhel6.x86_64.rpm mysql主从配置命令: 主: 1.编辑主MYSQL 服务器的MySQL配置文件my.cnf, ...
- 自动化运维之Saltstack
第三十八课 自动化运维之Saltstack 目录 一.自动化运维介绍 二. saltstack安装 三. 启动saltstack服务 四. saltstack配置认证 五. saltstack远程执行 ...
- 自动化运维—tomcat服务起停(mysql+shell+django+bootstrap+jquery)
项目简介: 项目介绍:自动化运维是未来的趋势,最近学了不少东西,正好通过这个小项目把这些学的东西串起来,练练手. 基础架构: 服务器端:web框架-Django 前端:html css jQuery ...
- CheungSSH国产自动化运维工具开源Web界面
CheungSSH web2.0 发布文档 CheungSSH 简介 CheungSSH是一款国人自主研发的Linux运维自动化管理服务器软件,秉着为企业降低运营成本,解放管理员双手和自动化生产的理念 ...
- 自动化运维工具ansible部署以及使用
测试环境master 192.168.16.74webserver1 192.168.16.70webserver2 192.168.16.72安装ansiblerpm -Uvh http://ftp ...
随机推荐
- 一种面向对象的TCP/IP中间件
这是一个使用C++封装的TCP/IP协议栈(仅传输层),属于本人所设计的中间件的一员,具有硬件无关,应用无关特性,使用非常方便,一看代码便知: #include "net.h" / ...
- python学习4
1.python中的函数的参数,这个参数的设置比起C比较特殊的地方就是参数可以预保留的.这个意思就是可以保留下来不填写,然后需要的时候再传入. 这个调用之后结果如下,另外可以看出python比起C来一 ...
- Django基础之wsgi
Django 一 什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演 ...
- 如何配置Linux系统的网络IP地址
一台安装了Linux系统的电脑如果想要联网,首先要做的就是进行网络配置.今天小编就以CentOS6.4系统为例为大家介绍整个网络配置的过程,虽然只是以CentOS6.4系统为例,但是其它的Linux系 ...
- [LeetCode] Encode and Decode Strings 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
- [LeetCode] Factor Combinations 因子组合
Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...
- [LeetCode] Department Top Three Salaries 系里前三高薪水
The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...
- @RenderSection,@RenderPage,@RenderBody介绍
在MVC的模板页中会用到上面三个东西,那么今天就简单归纳下各有什么作用 1.@RenderSection 用法 对CSS或JS部分模块的预留定义 例如模板页定义了@RenderSection(&quo ...
- EncodingHelper
/// <summary> /// Url解码 /// </summary> /// <param name="str">原始字符串</p ...
- Android零碎知识
1.当启动一个APP时按下后退键会调用onBackPressed方法. 如果想要屏蔽后退键只需要重写onBackPressed方法如下即可: @Override public void onBackP ...