標籤:
案例:
線上有一資料庫,需要每周全備一次,每天差備一次[安全起見還是差備吧,不要增備,不要吝嗇磁碟哦,而且差備恢複還很快]
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/bashEOFecho "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}‘`# 從最近一次全備檔案中擷取全備終止timetime=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、恢複測試:
全備恢複:
[[email protected] data]# mysql < /complete/hello-2015-01-13.sql [[email protected] 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)
差備恢複:
[[email protected] data]# mysql < /increment/incre-2015-01-13.sql [[email protected] data]# mysqll -bash: mysqll: command not found [[email protected] 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實現某一資料庫的每周全備和每天差異備份,並添加到執行計畫【熱備】