mysql備份恢復
mysqldump備份
企業故障恢復案例:
正在運行的網站系統 mysql數據庫 數據量25G,日業務量10-15M
備份策略:
天天晚上23點經過計劃任務調用mysqldump執行全備腳本
故障時間點:
週四上午 開發誤刪除了一個表,如何恢復?
思路:
1. 停業務避免數據二次傷害
2. 找一個臨時庫,恢復週三23.00 全備
3. 截取 週三 23.00 ---> 週四10點 誤刪除之間的binlog,恢復到臨時數據庫
4. 測試可用性和完整性
5.
5.1 方法1: 直接用臨時卡頂替原有生產庫,前端應用割接到新庫
5.2 方法2: 將誤刪除的表導出,導入到原生產庫
6. 開啓業務
故障演練:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 2070 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)
#建立一個新庫
mysql> create table backup.full select * from world.city;
Query OK, 4079 rows affected (0.04 sec)
Records: 4079 Duplicates: 0 Warnings: 0 #這個直接取了 實驗文件 wolrd 的數據到新庫。
如今有數據了。
mysql> create table backup.full_1 select * from mysql.user;
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
驗證數據是否有了:
mysql> use backup
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+------------------+
| Tables_in_backup |
+------------------+
| full |
| full_1 |
+------------------+
2 rows in set (0.00 sec) #能夠看到有兩個數據表了。 如今作一次全備。
## 週三全備:
[root@localhost ~]# mkdir /backup -p
[root@localhost ~]# cd /backup
[root@localhost ~]# mysqldump -uroot -p123 -A -R --triggers --master-data=2 --single-transaction|gzip >/backup/full_$(date +%F).sql.gz
[root@localhost ~]# ll -h /backup/
total 328K
-rw-r--r-- 1 root root 328K Oct 12 21:25 full_2020-10-12.sql.gz
## 模擬23:00到週四數據丟失的變化【屬於正常修改】
如今在建立一點數據
mysql> create table backup.thur select * from world.country;
Query OK, 239 rows affected (0.01 sec)
Records: 239 Duplicates: 0 Warnings: 0
經過查詢能夠看到:
select * from backup.full;
4079 rows in set (0.01 sec) 有439行數據。
模擬刪除修改數據:
mysql> use backup;
mysql> update full set countrycode='CHN'; #模擬不當心把全部地區都改成了CHN
mysql> commit;
Query OK, 0 rows affected (0.00 sec) #並執行了提交
mysql> delete from full where id>200; #大部分數據都刪除了 只保留了200行
Query OK, 3879 rows affected (0.02 sec)
mysql> commit; #並執行了提交
Query OK, 0 rows affected (0.00 sec)
### 模擬故障,刪除了一個表 【屬於異常修改】
mysql> show tables;
+------------------+
| Tables_in_backup |
+------------------+
| full |
| full_1 |
| thur |
+------------------+
3 rows in set (0.00 sec)
mysql> drop table thur; #<------------ 刪除表
Query OK, 0 rows affected (0.00 sec)
mysql> show tables; #現有表
+------------------+
| Tables_in_backup |
+------------------+
| full |
| full_1 |
+------------------+
2 rows in set (0.00 sec)
-----------------------------------------------
-----------------------------------------------
恢復過程:
1. 關閉原有數據庫
2. 啓動一個新數據庫
3. 解壓備份數據
[root@localhost backup]# gzip -d full_2020-10-12.sql.gz
[root@localhost backup]# ll
total 1104
-rw-r--r-- 1 root root 1126759 Oct 12 21:25 full_2020-10-12.sql
4. 截取二進制日誌:
去備份的sql中找
[root@localhost backup]# vim full_2020-10-12.sql
在這裏明顯能夠看到備份的那個binlog日誌和起始位置:
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000008', MASTER_LOG_POS=120; #備份文件中的起始位置
mysqlbinlog --start-position=120 --stop-position=[待查找] /data/mysql/mysql-bin.000008
5. 獲取binlog日誌最後一次正常操做的binlog。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000008 | 399383 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
找出刪除時間點的 position號:
| mysql-bin.000008 | 399262 | Query | 1 | 399383 | use `backup`; DROP TABLE ...
找到開始位置 399262 [這是刪除這個表以前的位置。]
將命令的結尾改爲下面這樣,而後執行:
[root@localhost backup]# mysqlbinlog --start-position=120 --stop-position=399262 /data/mysql/mysql-bin.000008 >/backup/inc.sql
6. 截取數據完畢後,開始恢復數據:
先去一臺臨時mysql服務器恢復數據,檢查
root@localhost backup]# scp -r /backup/inc.sql root@10.0.0.61:/root
另外一臺數據庫操做以下:
set sql_log_bin=0;
source /root/full_2020-10-12.sql
source /root/inc.sql
檢查數據:
MariaDB [world]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| backup |
| binlog |
| mysql |
| performance_schema |
| test |
| world |
+--------------------+
7 rows in set (0.00 sec)
MariaDB [backup]> show tables; #船艦的表都在
+------------------+
| Tables_in_backup |
+------------------+
| full |
| full_1 |
| thur |
+------------------+
3 rows in set (0.00 sec)
恢復完畢。