mysqldump 全量備份 + mysqlbinlog 二進制日志增量備份


第一節:mysqldump 全量備份 + mysqlbinlog 二進制日志增量備份

1.1 mysqldump 數據庫增量備份前提

(1)要開啟 mysql log-bin 日志功能,若沒有開啟則,修改配置文件 /etc/my.cnf,添加如下內容:

[mysqld]
datadir=/var/lib/mysql/data
log-bin=mysql-bin
server-id=1

  重啟 mysql

(2)查看數據庫是否開啟了二進制日志打印功能:

mysql> show variables like 'log_%';
+---------------------------------+---------------------+
| Variable_name                   | Value               |
+---------------------------------+---------------------+
| log_bin                         | ON                  |
......

   log_bin 為 ON 則表示該功能已經開啟

(3)存在一個完全備份,生產環境一般凌晨某個時刻進行全備

#例如:
mysqldump -uroot -p --default-character-set=gbk --single-transaction -F -B school |gzip > /backup/school_$(date +%F).sql.gz

  InnoDB 表在備份時,通常啟用選項 --single-transaction 來保證備份的一致性

1.2 mysqldump 全量備份 + mysqlbinlog 二進制日志增量備份過程

模擬 00:00 進行全量備份,誤刪數據庫,恢復:

(1)准備數據庫和表並向表中插入數據

mysql> create database school default character set utf8;    #創建school數據庫
Query OK, 1 row affected (0.00 sec)

mysql> use school    #切換至school數據庫
Database changed
mysql> create table student(
    -> id int(10) not null comment '學號',
    -> name varchar(16) not null comment '姓名',
    -> sex varchar(16) not null comment '性別',
    -> age tinyint(2) not null default '0' comment '學生年齡',
    -> dept varchar(16) default 'null' comment '學生所在系別',
    -> primary key (id))
    -> ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
    -> COLLATE=utf8_general_ci;       #創建student表
Query OK, 0 rows affected (0.03 sec)

mysql> insert into student values     #向表中插入數據
    -> (1,'張三','',24,'計算機'),(2,'李四','',27,'英語'),
    -> (3,'王五','',22,'電子商務'),(4,'趙六','',20,'物流管理');
Query OK, 4 rows affected, 4 warnings (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 4

mysql> select * from student;         #查看student表中的所有字段的值
+----+--------+-----+-----+--------------+
| id | name   | sex | age | dept         |
+----+--------+-----+-----+--------------+
|  1 | 張三 | 男 |  24 | 計算機    |
|  2 | 李四 | 女 |  27 | 英語       |
|  3 | 王五 | 男 |  22 | 電子商務 |
|  4 | 趙六 | 女 |  20 | 物流管理 |
+----+--------+-----+-----+--------------+
4 rows in set (0.00 sec)

(2)模擬凌晨 00:00 全備

[root@centos6 ~]# mkdir /backup    #創建備份文件夾
[root@centos6 ~]# date -s "20200225"
[root@centos6 ~]# mysqldump -uroot -p111111 --default-character-set=utf8 --single-transaction -F -B school -e | gzip > /backup/mysql_backup_`date +%F`.sql.gz

(3)全備之后再向表中寫入數據

[root@centos6 ~]# mysql -uroot -p111111
......
mysql> use school
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> insert into student values
    -> (5,'tom','',29,'化學'),(6,'jack','',19,'法語'),
    -> (7,'mimi','',21,'化妝'),(8,'kaka','',20,'酒店管理');
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+--------+-----+-----+--------------+
| id | name   | sex | age | dept         |
+----+--------+-----+-----+--------------+
|  1 | 張三 | 男 |  24 | 計算機    |
|  2 | 李四 | 女 |  27 | 英語       |
|  3 | 王五 | 男 |  22 | 電子商務 |
|  4 | 趙六 | 女 |  20 | 物流管理 |
|  5 | tom    | 男 |  29 | 化學       |
|  6 | jack   | 女 |  19 | 法語       |
|  7 | mimi   | 女 |  21 | 化妝       |
|  8 | kaka   | 女 |  20 | 酒店管理 |
+----+--------+-----+-----+--------------+
8 rows in set (0.00 sec)

(4)模擬用戶破壞數據

mysql> drop database school;
Query OK, 1 row affected (0.01 sec)

(5)檢查全備后的所有 binlog

[root@centos6 ~]# ll -nat /var/lib/mysql/data/
總用量 21276
-rw-rw----. 1 27 27  5242880 2月  25 00:00 ib_logfile0
-rw-rw----. 1 27 27 10485760 2月  25 00:00 ibdata1
-rw-rw----. 1 27 27      416 2月  25 00:00 mysql-bin.000004
drwxr-xr-x. 4 27 27     4096 2月  25 00:00 .
-rw-rw----. 1 27 27       76 2月  25 00:00 mysql-bin.index
-rw-rw----. 1 27 27     1581 2月  25 00:00 mysql-bin.000003
drwx------. 2 27 27     4096 1月  12 18:34 school
drwxr-xr-x. 3 27 27     4096 1月  12 18:31 ..
-rw-rw----. 1 27 27  5242880 1月  12 18:31 ib_logfile1
-rw-rw----. 1 27 27   765307 1月  12 18:31 mysql-bin.000002
-rw-rw----. 1 27 27    19734 1月  12 18:31 mysql-bin.000001
drwx------. 2 27 27     4096 1月  12 18:31 mysql

(6)立即刷新;再次查看 binlog:根據時間點及前一個 binlog 可以知道發現問題時刻前 binlog 日志

[root@centos6 ~]# mysqladmin -uroot -p111111 flush-logs
[root@centos6 ~]# ll -nat /var/lib/mysql/data/
總用量 21276
drwxr-xr-x. 3 27 27     4096 2月  25 00:05 .
-rw-rw----. 1 27 27       95 2月  25 00:05 mysql-bin.index
-rw-rw----. 1 27 27      106 2月  25 00:05 mysql-bin.000005    #鎖定該binlog
-rw-rw----. 1 27 27      544 2月  25 00:05 mysql-bin.000004
-rw-rw----. 1 27 27  5242880 2月  25 00:03 ib_logfile0
-rw-rw----. 1 27 27 10485760 2月  25 00:03 ibdata1
-rw-rw----. 1 27 27     1581 2月  25 00:00 mysql-bin.000003
drwxr-xr-x. 3 27 27     4096 1月  12 18:31 ..
-rw-rw----. 1 27 27  5242880 1月  12 18:31 ib_logfile1
-rw-rw----. 1 27 27   765307 1月  12 18:31 mysql-bin.000002
-rw-rw----. 1 27 27    19734 1月  12 18:31 mysql-bin.000001
drwx------. 2 27 27     4096 1月  12 18:31 mysql

(7)備份出 binlog 至 /backup 目錄

[root@centos6 ~]# cp /var/lib/mysql/data/mysql-bin.* /backup/

(8)恢復全量備份

[root@centos6 data]# cd /backup/
[root@centos6 backup]# zcat mysql_backup_2020-02-25.sql.gz >mysql_backup_2020-02-25.sql
[root@centos6 backup]# mysql -uroot -p111111 -e "create database school;"
[root@centos6 backup]# mysql -uroot -p111111 school < /backup/mysql_backup_2020-02-25.sql
[root@centos6 backup]# mysql -uroot -p111111
......
mysql> use school
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> select * from student;
+----+--------+-----+-----+--------------+
| id | name   | sex | age | dept         |
+----+--------+-----+-----+--------------+
|  1 | 張三 | 男 |  24 | 計算機    |
|  2 | 李四 | 女 |  27 | 英語       |
|  3 | 王五 | 男 |  22 | 電子商務 |
|  4 | 趙六 | 女 |  20 | 物流管理 |
+----+--------+-----+-----+--------------+
4 rows in set (0.00 sec)    #增量恢復成功
mysql> quit

(9)恢復增量備份

[root@centos6 backup]# cd /var/lib/mysql/data/
[root@centos6 data]# mysqlbinlog -d school mysql-bin.000004 > school-bin.sql
[root@centos6 data]# mysqlbinlog -d school mysql-bin.000005 >> school-bin.sql
[root@centos6 data]# vim school-bin.sql    #打開school-bin.sql刪除drop sql語句
[root@centos6 data]# mysql -uroot -p111111 school < school-bin.sql 
ERROR 1007 (HY000) at line 65: Can't create database 'school'; database exists    #因為school已經恢復至全量備份,所以此處提示school數據庫已經存在,不過不要緊,對存在的數據覆蓋
[root@centos6 data]# mysql -uroot -p111111
......
mysql> use school
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> select * from student;
+----+--------+-----+-----+--------------+
| id | name   | sex | age | dept         |
+----+--------+-----+-----+--------------+
|  1 | 張三 | 男 |  24 | 計算機    |
|  2 | 李四 | 女 |  27 | 英語       |
|  3 | 王五 | 男 |  22 | 電子商務 |
|  4 | 趙六 | 女 |  20 | 物流管理 |
|  5 | tom    | 男 |  29 | 化學       |
|  6 | jack   | 女 |  19 | 法語       |
|  7 | mimi   | 女 |  21 | 化妝       |
|  8 | kaka   | 女 |  20 | 酒店管理 |
+----+--------+-----+-----+--------------+
8 rows in set (0.00 sec)    #增量備份恢復
mysql> quit

1.3 mysqlbinlog 增量恢復方式

(1)基於時間點恢復:

#(1)指定開始時間到結束時間
myslbinlog mysqlbin.000005 --start-datetime='2020-02-25 01:10:46' --stop datetime='2020-02-25 03:10:46' -r time.sql
#(2)指定開始時間到文件結束
myslbinlog mysqlbin.000005 --start-datetime='2020-02-25 01:10:46' -d esen -r time.sql
#(3)從文件開頭到指定結束時間
myslbinlog mysqlbin.000005 --stop-datetime='2020-02-25 03:10:46' -d esen -r time.sql

(2)基於位置點的增量恢復:

#(1)指定開始位置到結束位置
myslbinlog mysqlbin.000005 --start-position=510 --stop-position=1312 -r pos.sql
#(2)指定開始位置到文件結束
myslbinlog mysqlbin.000005 --start-position=510 -r pos.sql
#(3)從文件開始位置到指定結束位置
myslbinlog mysqlbin.000005 --stop-position=1312 -r pos.sql

  或者,在增量備份的時候,可以將二進制文件轉化成 .sql 語句,然后在刪除 “drop” 語句,修改 .sql,再導入即可(上述恢復采用該方法)

1.4 實現自動化備份(數據庫小)

(1)備份計划

  什么時間:2:00

  對那些數據備份

  備份的文件放的位置

(2)備份腳本

[root@centos6 ~]# vim /mysql_back.sql
#!/bin/bash
back_dir=/backup
back_file=`date +%F`_all.sql
user=root
pass=111111

if [ ! -d /backup ];then
 mkdir -p /backup
fi

# 備份並截斷日志
mysqldump -u${user} -p${pass} --lock-all-tables --routines --events --triggers -
-master-data=2 --flush-logs --all-databases > /$back_dir/$back_file

# 只保留最近一周的備份
cd $back_dir
find . -mtime +7 -exec rm -rf {} \;

(3)測試

[root@centos6 ~]# chmod +x /mysql_back.sql 
[root@centos6 ~]# /mysql_back.sql 
[root@centos6 ~]# ls /backup
2020-02-25_all.sql

1.5 復制數據文件

復制數據文件方式,可以使用 cp 或 tar

(1)停止服務

[root@centos6 ~]# service mysqld stop

(2)備份數據文件

[root@centos6 ~]# mkdir /backup
[root@centos6 ~]# tar cvf /backup/all.`date +%F`.tar.gz /var/lib/mysql/*

(3)將備份文件拷貝到目標服務器

scp /backup/all.`date +%F`.tar.gz 192.168.129.142:/tmp/

(4)啟動服務,如有需要則恢復數據庫數據

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM