首页 > 代码库 > 16、Xtrabackup备份与恢复

16、Xtrabackup备份与恢复

XTRABACKUP
xtrabackup是percona公司开发的第三方备份软件,只备份innodb引擎表的ibd文件(frm不备份),不备份myisam引擎表。备份性能很高,备份期间没有任和锁,ddl、dml、select都不受影响
xtrabackup全备:
step1:这步只是在系统层做了个拷贝,数据库目录只拷贝ibd文件,frm表结构需要恢复时手动拷贝,log文件不备份
  1. [root@likun data]# xtrabackup --backup --datadir=/mysql/data/--target-dir=/root/bak              
[root@likun bak]# ls
db1  ibdata1  xtrabackup_checkpoints  xtrabackup_logfile
 
step2:这步会根据第一步的lsn结果扫描日志文件,将备份期间的新log、undo,补充到备份文件中,使他们合并成为完整的有一致性的
  1. [root@likun bak]# xtrabackup --prepare --use-memory=50M--datadir=/mysql/data/--target-dir=/root/bak      
[root@likun bak]# ls
db1  ibdata1  xtrabackup_binlog_pos_innodb  xtrabackup_checkpoints  xtrabackup_logfile
 

xtrabackup   增量备:  
周一:首先做一个全备,但不要做prepare
insert into test3 values(1);commit;
xtrabackup --backup --datadir=/mysql/data/ --target-dir=/root/bak/1/
周二:基于周一做增量备,--incremental-basedir设置为周一全备的目录,表示基于哪天做增量
insert into test3 values(2);commit;
xtrabackup --backup --datadir=/mysql/data/ --target-dir=/root/bak/2/ --incremental-basedir=/root/bak/1/
周三:基于周二做增量备
insert into test3 values(3);commit;   
xtrabackup --backup --datadir=/mysql/data/ --target-dir=/root/bak/3/ --incremental-basedir=/root/bak/2/
 
恢复:
--apply-log-only   只应用日志,把增量合在一起,而不产生日志
xtrabackup --prepare  --apply-log-only --redo-only --target-dir=/root/bak/1/
xtrabackup --prepare  --apply-log-only --redo-only --target-dir=/root/bak/1/ --incremental-basedir=/root/bak/2/
xtrabackup --prepare  --apply-log-only --redo-only --target-dir=/root/bak/1/ --incremental-basedir=/root/bak/3/
这时目录1,数据才是完整的了   
 
xtrabackup --prepare   --target-dir=/root/bak/1
最终生成完整备份,可以看到目录1下面生成了ib_logfile日志文件
 
 



INNOBACKUPEX
使用innobackupex做完整备份:
先保证my.cnf配置文件中配置了 basedir 和 datadir,如果配置文件在其他位置,指定‘--defaults-file=/etc/my.cnf’
step1:生成完整备份,同时copy innodb和myisam以及frm文件,会锁myisam表
[root@likun bak]# innobackupex --user=root /root/bak
  1. InnoDBBackupUtility v1.5.1-xtrabackup;Copyright2003,2009InnobaseOy
  2. and Percona LLC and/or its affiliates 2009-2013.  AllRightsReserved.
  3. This software is published under
  4. the GNU GENERAL PUBLIC LICENSE Version2,June1991.
  5. Get the latest version of PerconaXtraBackup, documentation, and help resources:
  6. http://www.percona.com/xb/p
  7. 14081014:09:34  innobackupex:Connecting to MySQL server with DSN ‘dbi:mysql:;mysql_read_default_group=xtrabackup‘ as ‘root‘  (using password: NO).
  8. 14081014:09:34  innobackupex:Connected to MySQL server
  9. 14081014:09:34  innobackupex:Executing a version check against the server...
  10. 14081014:09:34  innobackupex:Done.
  11. IMPORTANT:Please check that the backup run completes successfully.
  12.            At the end of a successful backup run innobackupex
  13.            prints "completed OK!".
  14. innobackupex:Using mysql server version 5.5.30-log
  15. innobackupex:Created backup directory /root/bak/2014-08-10_14-09-34
  16. 14081014:09:34  innobackupex:Starting ibbackup with command: xtrabackup  --defaults-group="mysqld"--backup --suspend-at-end --target-dir=/root/bak/2014-08-10_14-09-34--tmpdir=/tmp --extra-lsndir=‘/tmp‘
  17. innobackupex:Waitingfor ibbackup (pid=15364) to suspend
  18. innobackupex:Suspend file ‘/root/bak/2014-08-10_14-09-34/xtrabackup_suspended_2‘
  19. xtrabackup version 2.2.3 based on MySQL server 5.6.17Linux(x86_64)(revision id:)
  20. xtrabackup: uses posix_fadvise().
  21. xtrabackup: cd to /mysql/data
  22. xtrabackup: open files limit requested 0,set to 40960
  23. xtrabackup:using the following InnoDB configuration:
  24. xtrabackup:   innodb_data_home_dir =./
  25. xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextend
  26. xtrabackup:   innodb_log_group_home_dir =./
  27. xtrabackup:   innodb_log_files_in_group =2
  28. xtrabackup:   innodb_log_file_size =5242880
  29. >> log scanned up to (1670110)
  30. [01]Copying./ibdata1 to /root/bak/2014-08-10_14-09-34/ibdata1
  31. >> log scanned up to (1670110)
  32. [01]        ...done
  33. >> log scanned up to (1670110)
  34. [01]Copying./db1/test3.ibd to /root/bak/2014-08-10_14-09-34/db1/test3.ibd
  35. [01]        ...done
  36. [01]Copying./db1/test1.ibd to /root/bak/2014-08-10_14-09-34/db1/test1.ibd
  37. [01]        ...done
  38. [01]Copying./db1/test4.ibd to /root/bak/2014-08-10_14-09-34/db1/test4.ibd
  39. [01]        ...done
  40. [01]Copying./mysql/abc.ibd to /root/bak/2014-08-10_14-09-34/mysql/abc.ibd
  41. [01]        ...done
  42. [01]Copying./db2/test.ibd to /root/bak/2014-08-10_14-09-34/db2/test.ibd
  43. [01]        ...done
  44. >> log scanned up to (1670110)
  45. xtrabackup:Creating suspend file ‘/root/bak/2014-08-10_14-09-34/xtrabackup_suspended_2‘ with pid ‘15364‘
  46. 14081014:09:38  innobackupex:Continuing after ibbackup has suspended
  47. 14081014:09:38  innobackupex:Executing FLUSH TABLES WITH READ LOCK...
  48. 14081014:09:38  innobackupex:All tables locked and flushed to disk
  49. 14081014:09:38  innobackupex:Starting to backup non-InnoDB tables and files
  50. innobackupex: in subdirectories of ‘/mysql/data‘
  51. innobackupex:Backing up file ‘/mysql/data/db1/test4.frm‘
  52. innobackupex:Backing up file ‘/mysql/data/db1/test3.frm‘
  53. >> log scanned up to (1670110)
  54. innobackupex:Backing up file ‘/mysql/data/db1/db.opt‘
  55. innobackupex:Backing up file ‘/mysql/data/db1/test1.frm‘
  56. innobackupex:Backing up files ‘/mysql/data/mysql/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘(73 files)
  57. innobackupex:Backing up files ‘/mysql/data/performance_schema/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘(18 files)
  58. innobackupex:Backing up file ‘/mysql/data/db2/db.opt‘
  59. innobackupex:Backing up file ‘/mysql/data/db2/test.frm‘
  60. 14081014:09:38  innobackupex:Finished backing up non-InnoDB tables and files
  61. 14081014:09:38  innobackupex:Executing FLUSH ENGINE LOGS...
  62. 14081014:09:38  innobackupex:Waitingfor log copying to finish
  63. xtrabackup:The latest check point (for incremental):‘1670110‘
  64. xtrabackup:Stopping log copying thread.
  65. .>> log scanned up to (1670110)
  66. xtrabackup:Creating suspend file ‘/root/bak/2014-08-10_14-09-34/xtrabackup_log_copied‘ with pid ‘15364‘
  67. xtrabackup:Transaction log of lsn (1670110) to (1670110) was copied.
  68. 14081014:09:39  innobackupex:All tables unlocked
  69. innobackupex:Backup created in directory ‘/root/bak/2014-08-10_14-09-34‘
  70. innobackupex:MySQL binlog position: filename ‘mysql-bin.000011‘, position 107
  71. 14081014:09:39  innobackupex:Connection to database server closed
  72. 14081014:09:39  innobackupex: completed OK!
step2:同步日志文件
[root@likun bak]# innobackupex   --use-memory=50M --apply-log /root/bak/2014-08-10_14-09-34/
 
 
恢复完整备份:
service mysql stop
清空data目录
innobackupex会根据配置文件中datadir进行恢复
[root@likun data]# innobackupex --copy-back /root/bak/2014-08-10_14-09-34/
chown mysql. * -R
service mysql start

 
使用innodbbackupex做增量备份
mkdir {1,2,3}
周一:
 insert into test3 values(1);commit;
[root@likun bak]# innobackupex --user=root /root/bak/1/
周二:
 insert into test3 values(2);commit;   
[root@likun bak]# innobackupex --user=root --incremental /root/bak/2/ --incremental-basedir=/root/bak/1/2014-08-10_14-37-26/
周三:
 insert into test3 values(3);commit;      
[root@likun 2]# innobackupex --user=root --incremental /root/bak/3/ --incremental-basedir=/root/bak/2/2014-08-10_14-42-01/
 
恢复增量备份:
准备原始备份:--redo-only同xtrabackup的apply-log-only
[root@likun 2]# innobackupex --apply-log --redo-only /root/bak/1/2014-08-10_14-37-26/ --use-memory=50M --user=root
叠加周二增量:
[root@likun 2]# innobackupex --apply-log --redo-only /root/bak/1/2014-08-10_14-37-26/ --incremental-dir=/root/bak/2/2014-08-10_14-42-01/ --use-memory=50m --user=root
叠加周三增量:   
[root@likun 2]# innobackupex --apply-log --redo-only /root/bak/1/2014-08-10_14-37-26/ --incremental-dir=/root/bak/3/2014-08-10_14-43-40/ --use-memory=50m --user=root
这样最终在目录1生成完整备份文件。最后执行:
[root@likun 2]# innobackupex --apply-log /root/bak/1/2014-08-10_14-37-26/ --use-memory=50m --user=root
 
将完整的增量备份恢复到数据库:
service mysql stop
清空data目录
[root@likun mysql]#  innobackupex --copy-back /root/bak/1/2014-08-10_14-37-26/
chown mysql. * -R
service mysql start
 
OK~~

 

16、Xtrabackup备份与恢复