2009年6月25日星期四

ZT:使用Xtrabackup备份MySQL数据库

MySQL数据库 的备份,一直是个比较头痛的问题。各种工具虽然不少,但一个真正好用易用的,却又非常难找。Mysqldump做为数据的逻辑备份工具还行,但是无法进行 在线热备,而没有物理备份工具,在数据量比较大的时候,恢复的时间也会长得无法接受。InnoDB倒是有个商业的InnoDB Hotbackup,可以对InnoDB引擎的表实现在线热备。最近发现了一个工具,percona出品的Xtrabackup,是InnoDB Hotbackup的一个开源替代品,可以在线对InnoDB/XtraDB引擎的表进行物理备份,试用了一下,非常的不错,值得向MySQL DBA们推荐。

下面是一个实际备份的例子,采用了gzip将备份流进行压缩,约20GB的数据库,压缩后大小为340MB,当然,压缩后的大小跟数据库中实际使用的空间是相关的。备份时间约6分44秒。

innobackupex是参考了InnoDB Hotbackup的innoback脚本修改而来的,主要是为了方便的同时备份InnoDB和MyISAM引擎的表,并且加入了一些使用的选项。如 –slave-info可以记录备份恢复后,作为slave需要的一些信息,根据这些信息,可以很方便的利用备份来重做slave。

注意,从备份后的tar包解包的时候,需要使用-i参数。
  1. $innobackupex-1.5.1 --user=root --stream=tar /bak/ --slave-info | gzip > /bak/bak_mysql.tar.gz

  2. InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy.
  3. All Rights Reserved.

  4. This software is published under
  5. the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.

  6. IMPORTANT: Please check that the backup run completes successfully.
  7. At the end of a successful backup run innobackup
  8. prints "innobackup completed OK!".

  9. innobackupex: Using mysql Ver 14.12 Distrib 5.0.67, for redhat-linux-gnu (x86_64)
  10. using EditLine wrapper
  11. innobackupex: Using mysql server version 5.0.67-log

  12. innobackupex: Created backup directory /bak
  13. 090625 15:23:00 innobackupex: Starting mysql with options: --unbuffered --user=root
  14. 090625 15:23:00 innobackupex: Connected to database with mysql child process (pid=3431)
  15. 090625 15:23:04 innobackupex: Connection to database server closed

  16. 090625 15:23:04 innobackupex: Starting ibbackup with command:
  17. xtrabackup --backup --suspend-at-end --log-stream --target-dir=./
  18. innobackupex: Waiting for ibbackup (pid=3565) to suspend
  19. innobackupex: Suspend file '/opt/mysqldata/xtrabackup_suspended'

  20. xtrabackup: suspend-at-end is enabled.
  21. xtrabackup: uses posix_fadvise().
  22. xtrabackup: cd to /opt/mysqldata
  23. xtrabackup: Target instance is assumed as followings.
  24. xtrabackup: innodb_data_home_dir = /opt/mysqldata
  25. xtrabackup: innodb_data_file_path = ibdata1:10G;ibdata2:10G
  26. xtrabackup: innodb_log_group_home_dir = ./
  27. xtrabackup: innodb_log_files_in_group = 4
  28. xtrabackup: innodb_log_file_size = 104857600
  29. xtrabackup: use O_DIRECT
  30. xtrabackup: Stream mode.
  31. >> log scanned up to (0 3053406941)

  32. 090625 15:23:06 innobackupex: Continuing after ibbackup has suspended

  33. innobackupex: Starting to backup InnoDB tables and indexes
  34. innobackupex: from original InnoDB data directory '/opt/mysqldata'
  35. innobackupex: Backing up as tar stream 'ibdata1'
  36. >> log scanned up to (0 3053416714)
  37. ...这里省略若干行
  38. >> log scanned up to (0 3054123851)
  39. tar: ibdata1: file changed as we read it
  40. innobackupex: Backing up as tar stream 'ibdata2'
  41. >> log scanned up to (0 3054142116)
  42. ...这里省略若干行
  43. >> log scanned up to (0 3054618483)
  44. innobackupex: Backing up file '/opt/mysqldata/test/sp.ibd'
  45. innobackupex: Backing up file '/opt/mysqldata/test/tmp_dy.ibd'
  46. innobackupex: Backing up files '/opt/mysqldata/testdb/*.ibd' (206 files)
  47. >> log scanned up to (0 3054638401)
  48. >> log scanned up to (0 3054668860)
  49. tar: testdb/group_group_thread_0027.ibd: file changed as we read it
  50. >> log scanned up to (0 3054695015)
  51. >> log scanned up to (0 3054928216)
  52. tar: testdb/group_thread_reply_0007.ibd: file changed as we read it
  53. >> log scanned up to (0 3054952588)
  54. >> log scanned up to (0 3055005439)
  55. tar: testdb/group_user_0001.ibd: file changed as we read it
  56. >> log scanned up to (0 3055028610)
  57. >> log scanned up to (0 3055044650)
  58. tar: testdb/group_user_0006.ibd: file changed as we read it
  59. >> log scanned up to (0 3055060461)
  60. innobackupex: Backing up file '/opt/mysqldata/testdb/comments.ibd'
  61. innobackupex: Backing up file '/opt/mysqldata/testdb/testdb.ibd'
  62. innobackupex: Backing up file '/opt/mysqldata/testdb/testdb_content.ibd'
  63. innobackupex: Backing up file '/opt/mysqldata/testdb/testdb_ids.ibd'
  64. 090625 15:29:17 innobackupex: Starting mysql with options: --unbuffered --user=root
  65. 090625 15:29:17 innobackupex: Connected to database with mysql child process (pid=5458)
  66. >> log scanned up to (0 3055072495)
  67. 090625 15:29:21 innobackupex: Starting to lock all tables...
  68. >> log scanned up to (0 3055087148)
  69. >> log scanned up to (0 3055119993)
  70. 090625 15:29:39 innobackupex: All tables locked and flushed to disk

  71. 090625 15:29:39 innobackupex: Starting to backup .frm, .MRG, .MYD, .MYI,
  72. innobackupex: .TRG, .TRN, and .opt files in
  73. innobackupex: subdirectories of '/opt/mysqldata'
  74. innobackupex: Backing up file '/opt/mysqldata/test/sp.frm'
  75. innobackupex: Backing up file '/opt/mysqldata/test/tmp_dy.frm'
  76. innobackupex: Backing up files '/opt/mysqldata/testdb/*.{frm,MYD,MYI,MRG,TRG,TRN,opt}' (207 files)
  77. innobackupex: Backing up file '/opt/mysqldata/testdb/comments.frm'
  78. innobackupex: Backing up file '/opt/mysqldata/testdb/testdb.frm'
  79. innobackupex: Backing up file '/opt/mysqldata/testdb/testdb_content.frm'
  80. innobackupex: Backing up file '/opt/mysqldata/testdb/testdb_ids.frm'
  81. innobackupex: Backing up file '/opt/mysqldata/testdb/db.opt'
  82. innobackupex: Backing up files '/opt/mysqldata/mysql/*.{frm,MYD,MYI,MRG,TRG,TRN,opt}' (52 files)
  83. 090625 15:29:40 innobackupex: Finished backing up .frm, .MRG, .MYD, .MYI, .TRG, .TRN, and .opt files

  84. innobackupex: Resuming ibbackup

  85. xtrabackup: The latest check point (for incremental): '0:3054881736'
  86. >> log scanned up to (0 3055120013)
  87. xtrabackup: Transaction log of lsn (0 3053102766) to (0 3055120013) was copied.
  88. 090625 15:29:44 innobackupex: All tables unlocked
  89. 090625 15:29:44 innobackupex: Connection to database server closed

  90. innobackupex: Backup created in directory '/bak/'
  91. innobackupex: MySQL binlog position: filename '', position
  92. innobackupex: MySQL slave binlog position: master host '127.0.0.1',
  93. filename 'mysql-bin.000006', position 227207755
  94. 090625 15:29:44 innobackupex: innobackup completed OK!
  95. innobackupex: You must use -i (--ignore-zeros) option for extraction of the tar stream.
复制代码
本文网址:http://www.ningoo.net/html/2009/ ... mysql_database.html

收藏之,以备后查!

没有评论:

发表评论