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)

 

从上面的结果可知全备和差异备份都可恢复,那就可以投入正常使用了,可以交差了 ,哈哈

你可能感兴趣的:(数据库,style,cccccc)