MySQL模擬備份與恢復
阿新 • • 發佈:2021-09-22
一、MySQL-5.6 環境
#背景 1.備份策略:每天23:00,計劃任務呼叫mysqldump執行全備指令碼 2.故障時間點:上午10點開發人員誤刪除一個核心業務表,如何恢復 #恢復思路 1.掛維護頁 2.找測試庫測試 3.恢復週二全備 4.擷取週二全備直到週三上午十點誤刪除之前binlog,並恢復 5.測試業務功能正常 6.恢復業務: 方案一:故障庫導回到原生產 方案二:直接用測試庫承當生產,先跑著。 #建立資料庫進行模擬 mysql> create database backup character set utf8; mysql> use backup; mysql> create table xuesheng; mysql> create table xuesheng(id int,name varchar(10)); mysql> insert into xuesheng values('1','張山'),('2','李四');
二、核心業務庫被刪,恢復
-A 等於 --all-databases,匯出所有庫 -B 指定庫備份,只能指定庫的名字 -F 備份資料的同時重新整理binlog -d 僅表結構 -t 僅資料 -E 備份事件 -R --routines 備份儲存過程和函式資料 --triggers 備份觸發器資料 --master-data=2 備份時進行打點,記錄備份時的binlog位置點(必須開啟binlog才能使用) --single-transaction 快照備份 1.模擬週二全備 #(我這裡沒有設定密碼,完整的需要加 -u使用者 -p) [root@db01 ~]# mysqldump -A -R -E --master-data=2 --single-transaction --triggers >/tmp/backup_`date +%F`.sql 2.模擬週三白天變化 mysql> use backup; mysql> insert into xuesheng values('5','王五'); 3.刪除庫 mysql> drop database backup; Query OK, 1 row affected (0.00 sec) 4.找到起始位置點 [root@db01 ~]# vim /tmp/backup_2021-04-27.sql #在22行 -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=1320808; 5.找到結束位置點 [root@db01 ~]# mysqlbinlog --base64-output=decode-rows -vvv --start-position=1320808 /usr/local/mysql/data/mysql-bin.000003 > 1.txt [root@db01 ~]# cat 1.txt ------------------------------ # at 1321041 #210427 19:05:05 server id 1 end_log_pos 1321139 CRC32 0x12ef3e7b Query thread_id=8 exec_time=0 error_code=0 SET TIMESTAMP=1619521505/*!*/; drop database backup -------------------------------- 5.擷取binlog [root@db01 ~]# mysqlbinlog --start-position=1320808 --stop-position=1321041 /usr/local/mysql/data/mysql-bin.000003 > /tmp/huifu.sql 6.恢復週二全備和 mysql> set sql_log_bin=0; mysql> source /tmp/backup_2021-04-27.sql; mysql> source /tmp/huifu.sql;