Mysql最常用的三種備份工具分別是mysqldump、Xtrabackup(innobackupex工具)、lvm-snapshot快照。
前面分別介紹了:
Mysql備份系列(1)--備份方案總結(jié)性梳理
Mysql備份系列(2)--mysqldump備份(全量+增量)方案操作記錄
Mysql備份系列(3)--innobackupex備份mysql大數(shù)據(jù)(全量+增量)操作記錄
lvm-snapshot:基于LVM快照的備份
1.關(guān)于快照:
1)事務(wù)日志跟數(shù)據(jù)文件必須在同一個卷上;
2)剛剛創(chuàng)立的快照卷,里面沒有任何數(shù)據(jù),所有數(shù)據(jù)均來源于原卷
3)一旦原卷數(shù)據(jù)發(fā)生修改,修改的數(shù)據(jù)將復(fù)制到快照卷中,此時訪問數(shù)據(jù)一部分來自于快照卷,一部分來自于原卷
4)當(dāng)快照使用過程中,如果修改的數(shù)據(jù)量大于快照卷容量,則會導(dǎo)致快照卷崩潰。
5)快照卷本身不是備份,只是提供一個時間一致性的訪問目錄。
2.基于快照備份幾乎為熱備:
1)創(chuàng)建快照卷之前,要請求MySQL的全局鎖;在快照創(chuàng)建完成之后釋放鎖;
2)如果是Inoodb引擎, 當(dāng)flush tables 后會有一部分保存在事務(wù)日志中,卻不在文件中。 因此恢復(fù)時候,需要事務(wù)日志和數(shù)據(jù)文件
但釋放鎖以后,事務(wù)日志的內(nèi)容會同步數(shù)據(jù)文件中,因此備份內(nèi)容并不絕對是鎖釋放時刻的內(nèi)容,由于有些為完成的事務(wù)已經(jīng)完成,但在備份數(shù)據(jù)中因為沒完成而回滾。 因此需要借助二進制日志往后走一段
3.基于快照備份注意事項:
1)事務(wù)日志跟數(shù)據(jù)文件必須在同一個卷上;
2)創(chuàng)建快照卷之前,要請求MySQL的全局鎖;在快照創(chuàng)建完成之后釋放鎖;
3)請求全局鎖完成之后,做一次日志滾動;做二進制日志文件及位置標(biāo)記(手動進行);
4.為什么基于MySQL快照的備份很好?
原因如下幾點:
1)幾乎是熱備 在大多數(shù)情況下,可以在應(yīng)用程序仍在運行的時候執(zhí)行備份。無需關(guān)機,只需設(shè)置為只讀或者類似只讀的限制。
2)支持所有基于本地磁盤的存儲引擎 它支持MyISAM, Innodb, BDB,還支持 Solid, PrimeXT 和 Falcon。
3)快速備份 只需拷貝二進制格式的文件,在速度方面無以匹敵。
4)低開銷 只是文件拷貝,因此對服務(wù)器的開銷很細微。
5)容易保持完整性 想要壓縮備份文件嗎?把它們備份到磁帶上,F(xiàn)TP或者網(wǎng)絡(luò)備份軟件 -- 十分簡單,因為只需要拷貝文件即可。
6)快速恢復(fù) 恢復(fù)的時間和標(biāo)準(zhǔn)的MySQL崩潰恢復(fù)或數(shù)據(jù)拷貝回去那么快,甚至可能更快,將來會更快。
7)免費 無需額外的商業(yè)軟件,只需Innodb熱備工具來執(zhí)行備份。
快照備份mysql的缺點:
1)需要兼容快照 -- 這是明顯的。
2)需要超級用戶(root) 在某些組織,DBA和系統(tǒng)管理員來自不同部門不同的人,因此權(quán)限各不一樣。
3)停工時間無法預(yù)計,這個方法通常指熱備,但是誰也無法預(yù)料到底是不是熱備 -- FLUSH TABLES WITH READ LOCK 可能會需要執(zhí)行很長時間才能完成。
4)多卷上的數(shù)據(jù)問題 如果你把日志放在獨立的設(shè)備上或者你的數(shù)據(jù)庫分布在多個卷上,這就比較麻煩了,因為無法得到全部數(shù)據(jù)庫的一致性快照。不過有些系統(tǒng)可能能自動做到多卷快照。
下面即是使用lvm-snapshot快照方式備份mysql的操作記錄,僅依據(jù)本人實驗中使用而述.
操作記錄:
如下環(huán)境,本機是在openstack上開的云主機,在openstack上創(chuàng)建一個30G的云硬盤掛載到本機,然后制作lvm邏輯卷。
一、準(zhǔn)備LVM卷,并將mysql數(shù)據(jù)恢復(fù)(或者說遷移)到LVM卷上:
1) 創(chuàng)建一個分區(qū)或保存到另一塊硬盤上面
2) 創(chuàng)建PV、VG、LVM
3) 格式化 LV0
4) 掛載LV到臨時目錄
5) 確認(rèn)服務(wù)處于stop狀態(tài)
6) 將數(shù)據(jù)遷移到LV0
7) 重新掛載LV0到mysql數(shù)據(jù)庫的主目錄/data/mysql/data
8) 審核權(quán)限并啟動服務(wù)
[root@test-huanqiu ~]# fdisk -l
.........
Disk /dev/vdc: 32.2 GB, 32212254720 bytes
16 heads, 63 sectors/track, 62415 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
[root@test-huanqiu ~]# fdisk /dev/vdc //依次輸入p->n->p->1->回車->回車->w
.........
Command (m for help): p
Disk /dev/vdc: 32.2 GB, 32212254720 bytes
16 heads, 63 sectors/track, 62415 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x343250e4
Device Boot Start End Blocks Id System
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-62415, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-62415, default 62415):
Using default value 62415
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@test-huanqiu ~]# fdisk /dev/vdc
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): p
Disk /dev/vdc: 32.2 GB, 32212254720 bytes
16 heads, 63 sectors/track, 62415 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x343250e4
Device Boot Start End Blocks Id System
/dev/vdc1 1 62415 31457128+ 5 Extended
Command (m for help):
[root@test-huanqiu ~]# pvcreate /dev/vdc1
Device /dev/vdc1 not found (or ignored by filtering).
[root@test-huanqiu ~]# vgcreate vg0 /dev/vdc1
Volume group "vg0" successfully created
[root@test-huanqiu ~]# lvcreate -L +3G -n lv0 vg0
Logical volume "lv0" created.
[root@test-huanqiu ~]# mkfs.ext4 /dev/vg0/lv0
[root@test-huanqiu ~]# mkdir /var/lv0/
[root@test-huanqiu ~]# mount /dev/vg0/lv0 /var/lv0/
[root@test-huanqiu ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
8.1G 6.0G 1.7G 79% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/vda1 190M 37M 143M 21% /boot
/dev/mapper/vg0-lv0 2.9G 4.5M 2.8G 1% /var/lv0
[root@test-huanqiu ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
LogVol00 VolGroup00 -wi-ao---- 8.28g
LogVol01 VolGroup00 -wi-ao---- 1.50g
lv0 vg0 -wi-a----- 3.00g
----------------------------------------------------------------------------------------------------
如果要想刪除這個lvs,操作如下:
[root@test-huanqiu ~]# umount /data/mysql/data/ //先卸載掉這個lvs的掛載關(guān)系
[root@test-huanqiu ~]# lvremove /dev/vg0/lv0
[root@test-huanqiu ~]# vgremove vg0
[root@test-huanqiu ~]# pvremove /dev/vdc1
[root@test-huanqiu ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
LogVol00 VolGroup00 -wi-ao---- 8.28g
LogVol01 VolGroup00 -wi-ao---- 1.50g
----------------------------------------------------------------------------------------------------
mysql的數(shù)據(jù)目錄是/data/mysql/data,密碼是123456
[root@test-huanqiu ~]# ps -ef|grep mysql
mysql 2066 1286 0 07:33 ? 00:00:06 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql/ --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql//lib/plugin --user=mysql --log-error=/data/mysql/data/mysql-error.log --pid-file=/data/mysql/data/mysql.pid --socket=/usr/local/mysql/var/mysql.sock --port=3306
root 2523 2471 0 07:55 pts/1 00:00:00 grep mysql
[root@test-huanqiu ~]# /etc/init.d/mysql stop
Shutting down MySQL.... SUCCESS!
[root@test-huanqiu ~]# cd /data/mysql/data/
[root@test-huanqiu data]# tar -cf - . | tar xf - -C /var/lv0/
[root@test-huanqiu data]# umount /var/lv0/
[root@test-huanqiu data]# mount /dev/vg0/lv0 /data/mysql/data
[root@test-huanqiu data]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
8.1G 6.0G 1.7G 79% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/vda1 190M 37M 143M 21% /boot
/dev/mapper/vg0-lv0 2.9G 164M 2.6G 6% /data/mysql/data
刪除掛載后產(chǎn)生的lost+found目錄
[root@test-huanqiu data]# rm -rf lost+found
[root@test-huanqiu data]# ll -d /data/mysql/data
[root@test-huanqiu data]# ll -Z /data/mysql/data
[root@test-huanqiu data]# ll -Zd /data/mysql/data
需要注意的是:
當(dāng)SElinux功能開啟情況下,mysql數(shù)據(jù)庫重啟會失敗,所以必須執(zhí)行下面命令,恢復(fù)SElinux安全上下文.
[root@test-huanqiu data]# restorecon -R /data/mysql/data/
[root@test-huanqiu data]# /etc/init.d/mysql start
Starting MySQL... SUCCESS!
二、備份: (生產(chǎn)環(huán)境下一般都是整個數(shù)據(jù)庫備份)
1)鎖表
2)查看position號并記錄,便于后期恢復(fù)
3)創(chuàng)建snapshot快照
4)解表
5)掛載snapshot
6)拷貝snapshot數(shù)據(jù),進行備份。備份整個數(shù)據(jù)庫之前,要關(guān)閉mysql服務(wù)(保護ibdata1文件)
7)移除快照
設(shè)置此變量為1,讓每個事件盡可能同步到二進制日志文件里,以消耗IO來盡可能確保數(shù)據(jù)一致性。
mysql> SET GLOBAL sync_binlog=1;
查看二進制日志和position,以備后續(xù)進行binlog日志恢復(fù)增量數(shù)據(jù)(記住這個position節(jié)點記錄,對后面的增量數(shù)據(jù)備份很重要)
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 1434 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
創(chuàng)建存放binlog日志的position節(jié)點記錄的目錄
所有的position節(jié)點記錄都放在這同一個binlog.pos文件下(后面就使用>>符號追加到這個文件下)
[root@test-huanqiu ~]# mkdir /backup/mysql/binlog
[root@test-huanqiu ~]# mysql -p123456 -e "SHOW MASTER STATUS;" > /backup/mysql/binlog/binlog.pos
[root@test-huanqiu snap1]# cat /backup/mysql/binlog/binlog.pos
File Position Binlog_Do_DB Binlog_Ignore_DB Executed_Gtid_Set
mysql-bin.000004 1434
刷新日志,產(chǎn)生新的binlog日志,保證日志信息不會再寫入到上面的mysql-bin.000004日志內(nèi)。
mysql> FLUSH LOGS;
全局讀鎖,讀鎖請求到后不要關(guān)閉此mysql交互界面
mysql> FLUSH TABLES WITH READ LOCK;
在innodb表中,即使是請求到了讀鎖,但InnoDB在后臺依然可能會有事務(wù)在進行讀寫操作,
可用"mysql> SHOW ENGINE INNODB STATUS;"查看后臺進程的狀態(tài),等沒有寫請求后再做備份。
創(chuàng)建快照,以只讀的方式(--permission r)創(chuàng)建一個3GB大小的快照卷snap1
-s:相當(dāng)于--snapshot
[root@test-huanqiu ~]# mkdir /var/snap1
[root@test-huanqiu ~]# lvcreate -s -L 2G -n snap1 /dev/vg0/lv0 --permission r
Logical volume "snap1" created.
查看快照卷的詳情(快照卷也是LV):
[root@test-huanqiu ~]# lvdisplay
解除鎖定
回到鎖定表的mysql交互式界面,解鎖:
mysql> UNLOCK TABLES;
此參數(shù)可以根據(jù)服務(wù)器磁盤IO的負載來調(diào)整
mysql> SET GLOBAL sync_binlog=0;
[root@test-huanqiu ~]# mount /dev/vg0/snap1 /var/snap1 //掛載快照卷
[root@test-huanqiu snap1]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
8.1G 5.8G 1.9G 76% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/vda1 190M 37M 143M 21% /boot
/dev/mapper/vg0-lv0 2.9G 115M 2.7G 5% /data/mysql/data
/dev/mapper/vg0-snap1
2.9G 115M 2.7G 5% /var/snap1
[root@test-huanqiu ~]# cd /var/snap1/ && ll /var/snap1
[root@test-huanqiu snap1]# mkdir -p /backup/mysql/data/ //創(chuàng)建備份目錄
total 0
對本機的數(shù)據(jù)庫進行備份,備份整個數(shù)據(jù)庫。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | mysql> show databases; + --------------------+ | Database | + --------------------+ | information_schema | | mysql | | performance_schema | | test | + --------------------+ 4 rows in set (0.01 sec) mysql> create database beijing; Query OK, 1 row affected (0.00 sec) mysql> use beijing; Database changed mysql> create table people(id int (5), name varchar (20)); Query OK, 0 rows affected (0.03 sec) mysql> insert into people values ( "1" , "wangshibo" ); Query OK, 1 row affected (0.00 sec) mysql> insert into people values ( "2" , "guohuihui" ); Query OK, 1 row affected (0.01 sec) mysql> insert into people values ( "3" , "wuxiang" ); Query OK, 1 row affected (0.01 sec) mysql> select * from people; + ------+-----------+ | id | name | + ------+-----------+ | 1 | wangshibo | | 2 | guohuihui | | 3 | wuxiang | + ------+-----------+ 3 rows in set (0.00 sec) mysql> show databases; + --------------------+ | Database | + --------------------+ | information_schema | | beijing | | mysql | | performance_schema | | test | + --------------------+ 5 rows in set (0.01 sec) |
--------------------------------------------------------------------------------------------------------------------------
需要注意的是:
innodb表,一般會打開獨立表空間模式(innodb_file_per_table)。
由于InnoDB默認(rèn)會將所有的數(shù)據(jù)庫InnoDB引擎的表數(shù)據(jù)存儲在一個共享空間中:ibdata1文件。
增刪數(shù)據(jù)庫的時候,ibdata1文件不會自動收縮,這對單個或部分?jǐn)?shù)據(jù)庫的備份也將成為問題(如果不是整個數(shù)據(jù)庫備份的情況下,ibdata1文件就不能備份,否則會影響全部數(shù)據(jù)庫的數(shù)據(jù))。
所以若是對單個數(shù)據(jù)庫或部分?jǐn)?shù)據(jù)庫進行快照備份:
1)若是直接誤刪除mysql數(shù)據(jù)目錄下備份庫目錄,可以直接將快照備份數(shù)據(jù)解壓就能恢復(fù)
2)若是使用drop或delete誤刪除的數(shù)據(jù),那么在使用快照備份數(shù)據(jù)恢復(fù)時,就會出問題!因為單庫備份時ibdata1文件不能單獨備份,恢復(fù)時會導(dǎo)致這個文件損壞!
所以正確的做法是:
要對整個數(shù)據(jù)庫進行備份,并且一定要在mysql服務(wù)關(guān)閉的情況下(這樣是為了保護ibdata1文件)。
因為mysql是采用緩沖方式來將數(shù)據(jù)寫入到ibdata1文件中的,這正是fflush()函數(shù)存在的理由。當(dāng)mysql在運行時,對ibdata1進行拷貝肯定會導(dǎo)致ibdata1文件中的數(shù)據(jù)出錯,這樣在數(shù)據(jù)恢復(fù)時,也就肯定會出現(xiàn)“ERROR 1146 (42S02): Table '****' doesn't exist“的報錯!
在對啟用innodb引擎的mysql數(shù)據(jù)庫進行遷移的時候也是同理:
在對innodb數(shù)據(jù)庫進行數(shù)據(jù)遷移的時候,即將msyql(innodb引擎)服務(wù)從一臺服務(wù)器遷移到另一臺服務(wù)器時,在對數(shù)據(jù)庫目錄進行整體拷貝的時候(當(dāng)然就包括了對ibdata1文件拷貝),一定要在關(guān)閉對方mysql服務(wù)的情況下進行拷貝!
ibdata1用來儲存文件的數(shù)據(jù),而庫名的文件夾里面的那些表文件只是結(jié)構(gòu)而已,由于新版的mysql默認(rèn)試innodb,所以ibdata1文件默認(rèn)就存在了,少了這個文件有的數(shù)據(jù)表就會出錯。要知道:數(shù)據(jù)庫目錄下的.frm文件是數(shù)據(jù)庫中很多的表的結(jié)構(gòu)描述文件;而ibdata1文件才是數(shù)據(jù)庫的真實數(shù)據(jù)存放文件。
-------------------------------------------innodb_file_per_table參數(shù)說明------------------------------------------
線上環(huán)境的話,一般都建議打開這個獨立表空間模式。
因為ibdata1文件會不斷的增大,不會減少,無法向OS回收空間,容易導(dǎo)致線上出現(xiàn)過大的共享表空間文件,致使當(dāng)前空間爆滿。
并且ibdata1文件大到一定程序會影響insert、update的速度;并且
另外如果刪表頻繁的話,共享表空間產(chǎn)生的碎片會比較多。打開獨立表空間,方便進行innodb表的碎片整理
使用MyISAM表引擎的數(shù)據(jù)庫會分別創(chuàng)建三個文件:表結(jié)構(gòu)、表索引、表數(shù)據(jù)空間。
可以將某個數(shù)據(jù)庫目錄直接遷移到其他數(shù)據(jù)庫也可以正常工作。
然而當(dāng)使用InnoDB的時候,一切都變了。
InnoDB默認(rèn)會將所有的數(shù)據(jù)庫InnoDB引擎的表數(shù)據(jù)存儲在一個共享空間中:ibdata1文件。
增刪數(shù)據(jù)庫的時候,ibdata1文件不會自動收縮,單個數(shù)據(jù)庫的備份也將成為問題。
通常只能將數(shù)據(jù)使用mysqldump 導(dǎo)出,然后再導(dǎo)入解決這個問題。
在MySQL的配置文件[mysqld]部分,增加innodb_file_per_table參數(shù)。
可以修改InnoDB為獨立表空間模式,每個數(shù)據(jù)庫的每個表都會生成一個數(shù)據(jù)空間。
它的優(yōu)點:
1)每個表都有自已獨立的表空間。
2)每個表的數(shù)據(jù)和索引都會存在自已的表空間中。
3)可以實現(xiàn)單表在不同的數(shù)據(jù)庫中移動。
4)空間可以回收(除drop table操作處,表空不能自已回收)
Drop table操作自動回收表空間,如果對于統(tǒng)計分析或是日值表,刪除大量數(shù)據(jù)后可以通過:alter table TableName engine=innodb;回縮不用的空間。
對于使innodb-plugin的Innodb使用turncate table也會使空間收縮。
對于使用獨立表空間的表,不管怎么刪除,表空間的碎片不會太嚴(yán)重的影響性能,而且還有機會處理。
它的缺點:
單表增加過大,如超過100個G。
結(jié)論:
共享表空間在Insert操作上少有優(yōu)勢。其它都沒獨立表空間表現(xiàn)好。當(dāng)啟用獨立表空間時,請合理調(diào)整一下:innodb_open_files。
InnoDB Hot Backup(冷備)的表空間cp不會面對很多無用的copy了。而且利用innodb hot backup及表空間的管理命令可以實。
1)innodb_file_per_table設(shè)置.設(shè)置為1,表示打開了獨立的表空間模式。 如果設(shè)置為0,表示關(guān)閉獨立表空間模式,開啟方法如下:
在my.cnf中[mysqld]下設(shè)置
innodb_file_per_table=1
2)查看是否開啟:
mysql> show variables like "%per_table%";
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_file_per_table | ON |
+-----------------------+-------+
1 row in set (0.00 sec)
3)關(guān)閉獨享表空間
innodb_file_per_table=0關(guān)閉獨立的表空間
mysql> show variables like ‘%per_table%’;
-------------------------------------------innodb_file_per_table參數(shù)說明------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
備份前,一定要關(guān)閉mysql數(shù)據(jù)庫!因為里面會涉及到ibdata1文件備份,不關(guān)閉mysql的話,ibdata1文件備份后會損壞,從而導(dǎo)致恢復(fù)數(shù)據(jù)失?。?/span>
[root@test-huanqiu snap1]# /etc/init.d/mysql stop
Shutting down MySQL.... SUCCESS!
[root@test-huanqiu data]# lsof -i:3306
[root@test-huanqiu data]#
現(xiàn)在備份整個數(shù)據(jù)庫
[root@test-huanqiu snap1]# tar -zvcf /backup/mysql/data/`date +%Y-%m-%d`dbbackup.tar.gz ./
[root@test-huanqiu snap1]# ll /backup/mysql/data/
total 384
-rw-r--r--. 1 root root 392328 Dec 5 22:15 2016-12-05dbbackup.tar.gz
釋放快照卷,每次備份之后,應(yīng)該刪除快照,減少IO操作
先卸載,再刪除
[root@test-huanqiu ~]# umount /var/snap1/
[root@test-huanqiu ~]# df -h //確認(rèn)上面的掛載關(guān)系已經(jīng)沒了
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
8.1G 5.8G 1.9G 76% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/vda1 190M 37M 143M 21% /boot
/dev/mapper/vg0-lv0 2.9G 115M 2.7G 5% /data/mysql/data
[root@test-huanqiu ~]# lvremove /dev/vg0/snap1
Do you really want to remove active logical volume snap1? [y/n]: y
Logical volume "snap1" successfully removed
數(shù)據(jù)被快照備份后,可以啟動數(shù)據(jù)庫
[root@test-huanqiu ~]# /etc/init.d/mysql start
Starting MySQL.. SUCCESS!
[root@test-huanqiu ~]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 15943 mysql 16u IPv4 93348 0t0 TCP *:mysql (LISTEN)
[root@test-huanqiu ~]#
現(xiàn)在再進行新的數(shù)據(jù)寫入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | mysql> use beijing; Database changed mysql> insert into people values ( "4" , "liumengnan" ); Query OK, 1 row affected (0.02 sec) mysql> insert into people values ( "5" , "zhangjuanjuan" ); Query OK, 1 row affected (0.00 sec) mysql> select * from people; + ------+---------------+ | id | name | + ------+---------------+ | 1 | wangshibo | | 2 | guohuihui | | 3 | wuxiang | | 4 | liumengnan | | 5 | zhangjuanjuan | + ------+---------------+ 5 rows in set (0.00 sec) mysql> create table heihei( name varchar (20),age varchar (20)); Query OK, 0 rows affected (0.02 sec) mysql> insert into heihei values ( "jiujiujiu" , "nan" ); Query OK, 1 row affected (0.00 sec) mysql> select * from heihei; + -----------+------+ | name | age | + -----------+------+ | jiujiujiu | nan | + -----------+------+ 1 row in set (0.00 sec) mysql> create database shanghai; Query OK, 1 row affected (0.01 sec) mysql> show databases; + --------------------+ | Database | + --------------------+ | information_schema | | beijing | | mysql | | performance_schema | | shanghai | | test | + --------------------+ 6 rows in set (0.00 sec) |
假設(shè)一不小心誤操作刪除beijing和shanghai庫
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | mysql> drop database beijing; Query OK, 2 rows affected (0.03 sec) mysql> drop database shanghai; Query OK, 0 rows affected (0.00 sec) mysql> show databases; + --------------------+ | Database | + --------------------+ | information_schema | | mysql | | performance_schema | | test | + --------------------+ 4 rows in set (0.00 sec) |
莫慌!接下來就說下數(shù)據(jù)恢復(fù)操作~~
三、恢復(fù)流程如下:
0)由于涉及到增量數(shù)據(jù)備份,所以提前將最近一次的binlog日志從mysql數(shù)據(jù)目錄復(fù)制到別的路徑下
1)在mysql數(shù)據(jù)庫中執(zhí)行flush logs命令,產(chǎn)生新的binlog日志,讓日志信息寫入到新的這個binlog日志中
1)關(guān)閉數(shù)據(jù)庫,一定要關(guān)閉
2)刪除數(shù)據(jù)目錄下的文件
3)快照數(shù)據(jù)拷貝回來,position節(jié)點記錄回放
4)增量數(shù)據(jù)就利用mysqlbinlog命令將上面提前拷貝的binlog日志文件導(dǎo)出為sql文件,并剔除其中的drop語句,然后進行恢復(fù)。
5)重啟數(shù)據(jù)
先將最新一次的binlog日志備份到別處,用作增量數(shù)據(jù)備份。
比如mysql-bin.000006是最新一次的binlog日志
[root@test-huanqiu data]# cp mysql-bin.000006 /backup/mysql/data/
產(chǎn)生新的binlog日志,確保日志寫入到這個新的binlog日志內(nèi),而不再寫入到上面?zhèn)浞莸腷inlog日志里。
mysql> flush logs;
[root@test-huanqiu data]# ll mysql-bin.000007
-rw-rw----. 1 mysql mysql 120 Dec 5 23:19 mysql-bin.000007
[root@test-huanqiu data]# /etc/init.d/mysql stop
Shutting down MySQL.... SUCCESS!
[root@test-huanqiu data]# lsof -i:3306
[root@test-huanqiu data]# pwd
/data/mysql/data
[root@test-huanqiu data]# rm -rf ./*
[root@test-huanqiu data]# tar -zvxf /backup/mysql/data/2016-12-05dbbackup.tar.gz ./
[root@test-huanqiu data]# /etc/init.d/mysql start
Starting MySQL SUCCESS!
[root@test-huanqiu data]# cat /backup/mysql/binlog/binlog.pos
File Position Binlog_Do_DB Binlog_Ignore_DB Executed_Gtid_Set
mysql-bin.000004 1434
[root@test-huanqiu data]# mysqlbinlog --start-position=1434 /data/mysql/data/mysql-bin.000004 | mysql -p123456
登陸數(shù)據(jù)庫查看,發(fā)現(xiàn)這只是恢復(fù)到快照備份階段的數(shù)據(jù):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | mysql> show databases; + --------------------+ | Database | + --------------------+ | information_schema | | beijing | | mysql | | performance_schema | | test | + --------------------+ 5 rows in set (0.00 sec) mysql> select * from beijing.people; + ------+-----------+ | id | name | + ------+-----------+ | 1 | wangshibo | | 2 | guohuihui | | 3 | wuxiang | + ------+-----------+ 3 rows in set (0.00 sec) mysql> |
快照備份之后寫入的數(shù)據(jù)要利用mysqlbinlog命令將上面拷貝的mysql-bin000006文件導(dǎo)出為sql文件,并剔除其中的drop語句,然后進行恢復(fù)。
[root@test-huanqiu ~]# cd /backup/mysql/data/
[root@test-huanqiu data]# ll
total 388
-rw-r--r--. 1 root root 392328 Dec 5 22:15 2016-12-05dbbackup.tar.gz
-rw-r-----. 1 root root 1274 Dec 5 23:19 mysql-bin.000006
[root@test-huanqiu data]# mysqlbinlog mysql-bin.000006 >000006bin.sql
剔除其中的drop語句
[root@test-huanqiu data]# vim 000006bin.sql //手動刪除sql語句中的drop語句
然后在mysql中使用source命令恢復(fù)數(shù)據(jù)
mysql> source /backup/mysql/data/000006bin.sql;
再次查看下,發(fā)現(xiàn)增量部分的數(shù)據(jù)也已經(jīng)恢復(fù)回來了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | mysql> show databases; + --------------------+ | Database | + --------------------+ | information_schema | | beijing | | mysql | | performance_schema | | shanghai | | test | + --------------------+ 6 rows in set (0.00 sec) mysql> use beijing; Database changed mysql> show tables; + -------------------+ | Tables_in_beijing | + -------------------+ | heihei | | people | + -------------------+ 2 rows in set (0.00 sec) mysql> select * from people; + ------+---------------+ | id | name | + ------+---------------+ | 1 | wangshibo | | 2 | guohuihui | | 3 | wuxiang | | 4 | liumengnan | | 5 | zhangjuanjuan | + ------+---------------+ 5 rows in set (0.00 sec) mysql> select * from heihei; + -----------+------+ | name | age | + -----------+------+ | jiujiujiu | nan | + -----------+------+ 1 row in set (0.00 sec) |
-----------------------------------------------------------------------------------------------------------------
思路:
1)全庫的快照備份只需要在開始時備份一份即可,這相當(dāng)于全量備份。
2)后續(xù)只需要每天備份一次最新的binlog日志(備份后立即flush logs產(chǎn)生新的binlog日志),這相當(dāng)于增量備份了。
3)利用快照備份恢復(fù)全量數(shù)據(jù),利用備份的binlog日志進行增量數(shù)據(jù)恢復(fù)
4)crontab計劃任務(wù),每天定時備份最近一次的binlog日志即可。
----------------------------------------------------------------------------------------------------------------