一,什麼是增量備份
增量備份,就是將新增加的數據進行備份。假如你一個數據庫,有10G的數據,每天會增加10M的數據,數據庫每天都要備份一次,這麼多數據是不是都要備份呢?還是隻要備份增加的數據呢,很顯然,我只要備份增加的數據。這樣減少服務器的負擔。
二,啓用binlog
vi my.cnf
log-bin=/var/lib/mysql/mysql-bin.log,如果是這樣的話log-bin=mysql-bin.log默認在datadir目錄下面
[root@BlackGhost mysql]# ls |grep mysql-bin
mysql-bin.000001
mysql-bin.000002
mysql-bin.000003
mysql-bin.000004
mysql-bin.000005
mysql-bin.000006
mysql-bin.index
啓動後會產生mysql-bin這樣的文件,每啓動一次,就會增加一個或者多個。
mysql-bin.000002這樣文件存放的是數據庫每天增加的數據,所有數據庫的數據增量都在這裏面。
三,查看mysql-bin.000002這樣的文件裏面到底是什麼東西
[root@BlackGhost mysql]# mysqlbinlog /var/lib/mysql/mysql-bin.000002 > /tmp/add.sql
- [root@BlackGhost mysql]# cat /tmp/add.sql // 下面是根據mysql-bin生成的文件(部分內容)
- /*!40019 SET @@session.max_insert_delayed_threads=0*/;
- /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
- DELIMITER /*!*/;
- # at 4
- #100929 21:23:52 server id 1 end_log_pos 106 Start: binlog v 4, server v 5.1.50-log created 100929 21:23:52 at startup
- # Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.
- ROLLBACK/*!*/;
- BINLOG '
- 6D2jTA8BAAAAZgAAAGoAAAABAAQANS4xLjUwLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
- AAAAAAAAAAAAAAAAAADoPaNMEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC
- '/*!*/;
- # at 106
- #100929 21:29:35 server id 1 end_log_pos 134 Intvar
- SET INSERT_ID=16/*!*/;
- # at 134
- #100929 21:29:35 server id 1 end_log_pos 237 Query thread_id=1 exec_time=0 error_code=0
- use test/*!*/; //這裏是test數據庫
- SET TIMESTAMP=1285766975/*!*/;
- SET @@session.pseudo_thread_id=1/*!*/;
- SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1/*!*/;
- SET @@session.sql_mode=0/*!*/;
- SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
- /*!\C utf8 *//*!*/;
- SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
- SET @@session.lc_time_names=0/*!*/;
- SET @@session.collation_database=DEFAULT/*!*/;
- insert into aa(name)values('cccccccccc')
- /*!*/;
- # at 237
- #100929 21:32:21 server id 1 end_log_pos 265 Intvar
- SET INSERT_ID=12/*!*/;
- # at 265
- #100929 21:32:21 server id 1 end_log_pos 370 Query thread_id=1 exec_time=0 error_code=0
- SET TIMESTAMP=1285767141/*!*/;
- insert into user(name)values('cccccccccc')
- /*!*/;
- # at 370
- #100929 21:35:25 server id 1 end_log_pos 440 Query thread_id=1 exec_time=0 error_code=0
- SET TIMESTAMP=1285767325/*!*/;
- BEGIN
- /*!*/;
- # at 440
- #100929 21:35:25 server id 1 end_log_pos 468 Intvar
- SET INSERT_ID=45/*!*/;
- # at 468
- #100929 21:35:25 server id 1 end_log_pos 573 Query thread_id=1 exec_time=0 error_code=0
- use blog/*!*/; //這裏是blog數據庫
- SET TIMESTAMP=1285767325/*!*/;
- insert into city(CityName)values('asdf')
- /*!*/;
- # at 573
- #100929 21:35:25 server id 1 end_log_pos 600 Xid = 205
- COMMIT/*!*/;
- DELIMITER ;
- # End of log file
- ROLLBACK /* added by mysqlbinlog */;
- /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
下面還有一個重要索引文件就是mysql-bin.index
- [root@BlackGhost mysql]# cat mysql-bin.index
- ./mysql-bin.000001
- ./mysql-bin.000002
- ./mysql-bin.000003
- ./mysql-bin.000004
- ./mysql-bin.000005
- ./mysql-bin.000006
四,增量備份和增量還原
1,增量備份
既然我們知道了,mysql裏面新增加的數據在mysql-bin這樣的文件裏面,我們只要把mysql-bin這樣的文件進行備份就可以了。
cp /var/lib/mysql/mysql-bin* /data/mysql_newbak/
2,增量還原,講幾個常用的,比較有用的
a),根據時間來還原 --start-date,--stop-date
[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog --start-date="2010-09-29 18:00:00" --stop-date="2010-09-29 23:00:00" /var/lib/mysql/mysql-bin.000002 |mysql -u root -p
根據條件看一下數據
- [root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog --start-date="2010-09-29 18:00:00"
- --stop-date="2010-09-29 23:00:00" /var/lib/mysql/mysql-bin.000002
- //下面是部分內容,其實也就是一些對數據進行操作的sql語句
- # at 237
- #100929 21:32:21 server id 1 end_log_pos 265 Intvar
- SET INSERT_ID=12/*!*/;
- # at 265
- #100929 21:32:21 server id 1 end_log_pos 370 Query thread_id=1 exec_time=0 error_code=0
- SET TIMESTAMP=1285767141/*!*/;
- insert into user(name)values('cccccccccc')
- /*!*/;
- # at 370
- #100929 21:35:25 server id 1 end_log_pos 440 Query thread_id=1 exec_time=0 error_code=0
- SET TIMESTAMP=1285767325/*!*/;
- BEGIN
- /*!*/;
- # at 440
- #100929 21:35:25 server id 1 end_log_pos 468 Intvar
- SET INSERT_ID=45/*!*/;
- # at 468
- #100929 21:35:25 server id 1 end_log_pos 573 Query thread_id=1 exec_time=0 error_code=0
b),根據起始位置來還原,--start-position,--stop-position
[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog --start-position=370 --stop-position=440 /var/lib/mysql/mysql-bin.000002 |mysql -u root -p
//查看插入的內容,根a)中是一樣的
[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog --start-position=370 --stop-position=440 /var/lib/mysql/mysql-bin.000002
--start-position=370 --stop-position=440 這裏面數字從哪兒來的呢?
# at 370
#100929 21:35:25 server id 1 end_log_pos 440 Query
thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1285767325/*!*/;
上面的紅色加粗的就是,一個是start-position,一個是stop-position
c),根據數據庫名來進行還原 -d
在這裏是小寫的d,請不要把它和mysqldump中的-D搞混了。哈哈。
[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog -d test /var/lib/mysql/mysql-bin.000002
查看內容,請參考a)
d),根據數據庫所在IP來分-h
[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog -h 192.1681.102 /var/lib/mysql/mysql-bin.000002
查看內容,請參考a)
e),根據數據庫所佔用的端口來分-P
有的時候,我們的mysql用的不一定是3306端口,注意是大寫的P
[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog -p 13306 /var/lib/mysql/mysql-bin.000002
查看內容,請參考a)
f),根據數據庫serverid來還原--server-id
在數據庫的配置文件中,都有一個serverid並且同一集羣中serverid是不能相同的。
[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog --server-id=1 /var/lib/mysql/mysql-bin.000002
查看內容,請參考a)
注意:上面的幾個例子,我都是一個一個說的,其實可以排列組合的。例如
[root@BlackGhost mysql]# /usr/local/mysql/bin/mysqlbinlog --start-position="2010-09-29 18:00:00" -d test -h 127.0.0.1 /var/lib/mysql/mysql-bin.000002 |mysql -u root -p
五,後續
增量備份的時候,有一點讓人不爽,就是mysql-bin這樣的文件,每啓動一次mysql就會增加一些,如果你不去管他的話,時間長了,他會把你的磁盤佔滿。
./mysqldump --flush-logs -u root myblog > /tmp/myblog.sql
備份myblog數據庫,清除增量備份裏面的有關myblog的數據
./mysqldump --flush-logs -u root --all-databases > /tmp/alldatabase.sql
備份所有數據庫,清除增量備份
mysql-bin.index的起索引作用,因爲增量的數據不一定在一個mysql-bin000這樣的文件裏面,這個時候,我們就要根據mysql-bin.index來找mysql-bin這樣的增量文件了。
如果mysql裏面做了這樣的配置binlog-do-db=test1,增量備份裏面只會有test1這個數據庫的數據
mysqlbinlog確實是個強大的mysql數據庫輔助工具。 1.首先將數據存到my.sql C:\wamp\bin\mysql\mysql5.5.8\bin\mysqlbinlog.exe --stop-date="2011-10-30 23:30:00" --result-file="c:\my.sql" ../data/mysql-bin.000025 | mysql -uroot 2.然後開始還原數據 C:\wamp\bin\mysql\mysql5.5.8\bin\mysql.exe source c:\my.sql