mysql 利用binlog增量備份,還原執行個體

來源:互聯網
上載者:User

標籤:

mysql 利用binlog增量備份,還原執行個體

張映 發表於 2010-09-29

分類目錄: mysql

標籤:binlog, mysql, mysqldump, 增量備份

一,什麼是增量備份

增量備份,就是將新增加的資料進行備份。假如你一個資料庫,有10G的資料,每天會增加10M的資料,資料庫每天都要備份一次,這麼多資料是不是都要備份呢?還是只要備份增加的資料呢,很顯然,我只要備份增加的資料。這樣減少伺服器的負擔。


二,啟用binlog

vi my.cnf

log-bin=/var/lib/mysql/mysql-bin.log,如果是這樣的話log-bin=mysql-bin.log預設在datadir目錄下面

[[email protected] 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這樣的檔案裡面到底是什麼東西

[[email protected] mysql]# mysqlbinlog   /var/lib/mysql/mysql-bin.000002 > /tmp/add.sql

查看複製列印?
  1. [[email protected] mysql]# cat /tmp/add.sql   // 下面是根據mysql-bin產生的檔案(部分內容)  
  2. /*!40019 SET @@session.max_insert_delayed_threads=0*/;  
  3. /*!50003 SET @O[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/;  
  4. DELIMITER /*!*/;  
  5. # at 4  
  6. #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  
  7. # Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.  
  8. ROLLBACK/*!*/;  
  9. BINLOG ‘ 
  10. 6D2jTA8BAAAAZgAAAGoAAAABAAQANS4xLjUwLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
  11. AAAAAAAAAAAAAAAAAADoPaNMEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC 
  12. ‘/*!*/;  
  13. # at 106  
  14. #100929 21:29:35 server id 1  end_log_pos 134     Intvar  
  15. SET INSERT_ID=16/*!*/;  
  16. # at 134  
  17. #100929 21:29:35 server id 1  end_log_pos 237     Query    thread_id=1    exec_time=0    error_code=0  
  18. use test/*!*/;           //這裡是test資料庫  
  19. SET TIMESTAMP=1285766975/*!*/;  
  20. SET @@session.pseudo_thread_id=1/*!*/;  
  21. SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1/*!*/;  
  22. SET @@session.sql_mode=0/*!*/;  
  23. SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;  
  24. /*!\C utf8 *//*!*/;  
  25. SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;  
  26. SET @@session.lc_time_names=0/*!*/;  
  27. SET @@session.collation_database=DEFAULT/*!*/;  
  28. insert into aa(name)values(‘cccccccccc‘)  
  29. /*!*/;  
  30. # at 237  
  31. #100929 21:32:21 server id 1  end_log_pos 265     Intvar  
  32. SET INSERT_ID=12/*!*/;  
  33. # at 265  
  34. #100929 21:32:21 server id 1  end_log_pos 370     Query    thread_id=1    exec_time=0    error_code=0  
  35. SET TIMESTAMP=1285767141/*!*/;  
  36. insert into user(name)values(‘cccccccccc‘)  
  37. /*!*/;  
  38. # at 370  
  39. #100929 21:35:25 server id 1  end_log_pos 440     Query    thread_id=1    exec_time=0    error_code=0  
  40. SET TIMESTAMP=1285767325/*!*/;  
  41. BEGIN  
  42. /*!*/;  
  43. # at 440  
  44. #100929 21:35:25 server id 1  end_log_pos 468     Intvar  
  45. SET INSERT_ID=45/*!*/;  
  46. # at 468  
  47. #100929 21:35:25 server id 1  end_log_pos 573     Query    thread_id=1    exec_time=0    error_code=0  
  48. use blog/*!*/;             //這裡是blog資料庫  
  49. SET TIMESTAMP=1285767325/*!*/;  
  50. insert into city(CityName)values(‘asdf‘)  
  51. /*!*/;  
  52. # at 573  
  53. #100929 21:35:25 server id 1  end_log_pos 600     Xid = 205  
  54. COMMIT/*!*/;  
  55. DELIMITER ;  
  56. # End of log file  
  57. ROLLBACK /* added by mysqlbinlog */;  
  58. /*!50003 SET [email protected]_COMPLETION_TYPE*/;  

下面還有一個重要索引檔案就是mysql-bin.index

  1. [[email protected] mysql]# cat mysql-bin.index  
  2. ./mysql-bin.000001  
  3. ./mysql-bin.000002  
  4. ./mysql-bin.000003  
  5. ./mysql-bin.000004  
  6. ./mysql-bin.000005  
  7. ./mysql-bin.000006  

四,增量備份和增量還原

1,增量備份

既然我們知道了,mysql裡面新增加的資料在mysql-bin這樣的檔案裡面,我們只要把mysql-bin這樣的檔案進行備份就可以了。

cp /var/lib/mysql/mysql-bin* /data/mysql_newbak/

2,增量還原,講幾個常用的,比較有用的

a),根據時間來還原 --start-date,--stop-date

[[email protected] 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

根據條件看一下資料

查看複製列印?
  1. [[email protected] mysql]# /usr/local/mysql/bin/mysqlbinlog  --start-date="2010-09-29 18:00:00"  
  2. --stop-date="2010-09-29 23:00:00"  /var/lib/mysql/mysql-bin.000002  
  3. //下面是部分內容,其實也就是一些對資料進行操作的sql語句  
  4. # at 237  
  5. #100929 21:32:21 server id 1  end_log_pos 265     Intvar  
  6. SET INSERT_ID=12/*!*/;  
  7. # at 265  
  8. #100929 21:32:21 server id 1  end_log_pos 370     Query    thread_id=1    exec_time=0    error_code=0  
  9. SET TIMESTAMP=1285767141/*!*/;  
  10. insert into user(name)values(‘cccccccccc‘)  
  11. /*!*/;  
  12. # at 370  
  13. #100929 21:35:25 server id 1  end_log_pos 440     Query    thread_id=1    exec_time=0    error_code=0  
  14. SET TIMESTAMP=1285767325/*!*/;  
  15. BEGIN  
  16. /*!*/;  
  17. # at 440  
  18. #100929 21:35:25 server id 1  end_log_pos 468     Intvar  
  19. SET INSERT_ID=45/*!*/;  
  20. # at 468  
  21. #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

[[email protected] mysql]# /usr/local/mysql/bin/mysqlbinlog --start-position=370 --stop-position=440  /var/lib/mysql/mysql-bin.000002 |mysql -u root -p

//查看插入的內容,根a)中是一樣的

[[email protected] 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搞混了。哈哈。

[[email protected] mysql]# /usr/local/mysql/bin/mysqlbinlog -d test  /var/lib/mysql/mysql-bin.000002

查看內容,請參考a)

d),根據資料庫所在IP來分-h

[[email protected] mysql]# /usr/local/mysql/bin/mysqlbinlog -h 192.1681.102  /var/lib/mysql/mysql-bin.000002

查看內容,請參考a)

e),根據資料庫所佔用的連接埠來分-P

有的時候,我們的mysql用的不一定是3306連接埠,注意是大寫的P

[[email protected] mysql]# /usr/local/mysql/bin/mysqlbinlog -p 13306  /var/lib/mysql/mysql-bin.000002

查看內容,請參考a)

f),根據資料庫serverid來還原--server-id

在資料庫的設定檔中,都有一個serverid並且同一叢集中serverid是不能相同的。

[[email protected] mysql]# /usr/local/mysql/bin/mysqlbinlog --server-id=1  /var/lib/mysql/mysql-bin.000002

查看內容,請參考a)

注意:上面的幾個例子,我都是一個一個說的,其實可以排列組合的。例如

[[email protected] 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這個資料庫的資料

 

http://blog.51yip.com/mysql/1042.html

mysql 利用binlog增量備份,還原執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.