三、对数据库的增量备份与恢复
为了进行增量备份,先对数据库添加一些数据
 
  
  1. mysql> insert into test values(11);  

  2. Query OK, 1 row affected (0.10 sec)  

  3. mysql> insert into test values(12);  

  4. Query OK, 1 row affected (0.05 sec)  

  5. mysql> insert into test values(13);  

  6. Query OK, 1 row affected (0.00 sec)  

  7. mysql> insert into test values(14);  

  8. Query OK, 1 row affected (0.00 sec)  

  9. mysql> insert into test values(15);  

  10. Query OK, 1 row affected (0.00 sec)  

  11. mysql> flush privileges;  

  12. Query OK, 0 rows affected (0.01 sec)  

  13. mysql> select * from test;  

  14. +------+  

  15. | id   |  

  16. +------+  

  17. |    1 |  

  18. |    2 |  

  19. |    3 |  

  20. |    4 |  

  21. |    5 |  

  22. |   11 |  

  23. |   12 |  

  24. |   13 |  

  25. |   14 |  

  26. |   15 |  

  27. +------+  

  28. 10 rows in set (0.00 sec)  

然后进行增量的备份


 
  
  1. root@client2:/var/lib/mysql# innobackupex --user=root--password=123456--database=test --incremental --incremental-basedir=/tmp/restore/ /tmp/data  

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

  3. and Percona Inc 2009-2012.  All Rights Reserved.  

  4. This software is published under  

  5. the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.  

  6. 130307 21:13:38  innobackupex: Starting mysql with options:  --password=xxxxxxxx--user='root' --unbuffered --  

  7. 130307 21:13:38  innobackupex: Connected to database with mysql child process (pid=12864)  

  8. 130307 21:13:44  innobackupex: Connection to database server closed  

  9. IMPORTANT: Please check that the backup run completes successfully.  

  10.           At the end of a successful backup run innobackupex  

  11.           prints "completed OK!".  

  12. innobackupex: Using mysql  Ver 14.14 Distrib 5.5.28, for debian-linux-gnu (x86_64) using readline 6.2  

  13. innobackupex: Using mysql server version Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.  

  14. innobackupex: Created backup directory /tmp/data/2013-03-07_21-13-44  

  15. 130307 21:13:44  innobackupex: Starting mysql with options:  --password=xxxxxxxx--user='root' --unbuffered --  

  16. 130307 21:13:44  innobackupex: Connected to database with mysql child process (pid=12891)  

  17. 130307 21:13:46  innobackupex: Connection to database server closed  

  18. 130307 21:13:46  innobackupex: Starting ibbackup with command: xtrabackup_55 --backup --suspend-at-end --target-dir=/tmp/data/2013-03-07_21-13-44 --incremental-basedir='/tmp/restore/'

  19. innobackupex: Waiting for ibbackup (pid=12898) to suspend  

  20. innobackupex: Suspend file '/tmp/data/2013-03-07_21-13-44/xtrabackup_suspended'  

  21. xtrabackup_55 version 1.6.7 for Percona Server 5.5.16 Linux (x86_64) (revision id: undefined)  

  22. incremental backup from 59605543 is enabled.  

  23. xtrabackup: uses posix_fadvise().  

  24. xtrabackup: cd to /var/lib/mysql  

  25. xtrabackup: Target instance is assumed as followings.  

  26. xtrabackup:   innodb_data_home_dir = ./  

  27. xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextend  

  28. xtrabackup:   innodb_log_group_home_dir = ./  

  29. xtrabackup:   innodb_log_files_in_group = 2

  30. xtrabackup:   innodb_log_file_size = 5242880

  31. 130307 21:13:46 InnoDB: Using Linux native AIO  

  32. >> log scanned up to (59606124)  

  33. [01] Copying ./ibdata1  

  34.     to /tmp/data/2013-03-07_21-13-44/ibdata1.delta  

  35. [01]        ...done  

  36. 130307 21:13:50  innobackupex: Continuing after ibbackup has suspended  

  37. 130307 21:13:50  innobackupex: Starting mysql with options:  --password=xxxxxxxx--user='root' --unbuffered --  

  38. 130307 21:13:50  innobackupex: Connected to database with mysql child process (pid=12913)  

  39. >> log scanned up to (59606124)  

  40. 130307 21:13:52  innobackupex: Starting to lock all tables...  

  41. >> log scanned up to (59606124)  

  42. >> log scanned up to (59606124)  

  43. 130307 21:14:03  innobackupex: All tables locked and flushed to disk  

  44. 130307 21:14:03  innobackupex: Starting to backup .frm, .MRG, .MYD, .MYI,  

  45. innobackupex: .TRG, .TRN, .ARM, .ARZ, .CSM, .CSV and .opt files in  

  46. innobackupex: subdirectories of '/var/lib/mysql'  

  47. innobackupex: Backing up file '/var/lib/mysql/test/test.frm'  

  48. innobackupex: Backing up file '/var/lib/mysql/test/db.opt'  

  49. 130307 21:14:03  innobackupex: Finished backing up .frm, .MRG, .MYD, .MYI, .TRG, .TRN, .ARM, .ARZ, .CSV, .CSM and .opt files  

  50. innobackupex: Resuming ibbackup  

  51. xtrabackup: The latest check point (for incremental): '59606124'  

  52. >> log scanned up to (59606124)  

  53. xtrabackup: Stopping log copying thread.  

  54. xtrabackup: Transaction log of lsn (59606124) to (59606124) was copied.  

  55. 130307 21:14:05  innobackupex: All tables unlocked  

  56. 130307 21:14:05  innobackupex: Connection to database server closed  

  57. innobackupex: Backup created in directory '/tmp/data/2013-03-07_21-13-44'  

  58. innobackupex: MySQL binlog position: filename 'mysql-bin.000023', position 107  

  59. 130307 21:14:05  innobackupex: completed OK!  

其中,--incremental指明是增量备份,--incremental-basedir指定上次完整备份或者增量备份文件的位置。这里的增量备份其实只针对的是InnoDB,对于MyISAM来说,还是完整备份。
在进行增量备份的恢复之前,先关闭数据库,然后删除数据库test
 
  
  1. root@client2:/var/lib/mysql# service mysql stop  

  2. mysql stop/waiting  

  3. root@client2:/var/lib/mysql# rm -rf test  

  4. root@client2:/var/lib/mysql# ll  

  5. total 77856  

  6. drwx------  7 mysql mysql     4096 Mar  7 21:17 ./  

  7. drwxr-xr-x 38 root  root      4096 Mar  7 19:52 ../  

  8. -rw-r--r--  1 root  root         0 Jan  5 14:22 debian-5.5.flag  

  9. drwx------  2 mysql mysql     4096 Feb 11 17:39 django/  

  10. -rw-rw----  1 mysql mysql 69206016 Mar  7 21:17 ibdata1  

  11. -rw-rw----  1 mysql mysql  5242880 Mar  7 21:17 ib_logfile0  

  12. -rw-rw----  1 mysql mysql  5242880 Mar  7 21:11 ib_logfile1  

  13. drwx------  2 mysql mysql     4096 Jan  5 22:55 monitor/  

  14. drwx------  2 mysql root      4096 Jan  5 14:22 mysql/  

  15. -rw-rw----  1 root  root         6 Jan  5 14:22 mysql_upgrade_info  

  16. drwx------  2 mysql mysql     4096 Jan  5 14:22 performance_schema/  

  17. drwxr-xr-x  2 mysql mysql     4096 Mar  7 19:58 xtrbackup/  

增量备份的恢复
 
  
  1. root@client2:/var/lib/mysql# innobackupex -user=root--password=123456--defaults-file=/etc/mysql/my.cnf --apply-log /tmp/restore/ --incremental-dir=/tmp/data/2013-03-07_21-13-44/  

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

  3. and Percona Inc 2009-2012.  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 apply-log run completes successfully.  

  7.           At the end of a successful apply-log run innobackupex  

  8.           prints "completed OK!".  

  9. 130307 21:18:20  innobackupex: Starting ibbackup with command: xtrabackup_55  --defaults-file="/etc/mysql/my.cnf" --prepare --target-dir=/tmp/restore --incremental-dir=/tmp/data/2013-03-07_21-13-44/  

  10. xtrabackup_55 version 1.6.7 for Percona Server 5.5.16 Linux (x86_64) (revision id: undefined)  

  11. incremental backup from 59605543 is enabled.  

  12. xtrabackup: cd to /tmp/restore  

  13. xtrabackup: This target seems to be already prepared.  

  14. xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(59606124)  

  15. xtrabackup: page size for /tmp/data/2013-03-07_21-13-44//ibdata1.delta is 16384 bytes  

  16. Applying /tmp/data/2013-03-07_21-13-44//ibdata1.delta ...  

  17. xtrabackup: Temporary instance for recovery is set as followings.  

  18. xtrabackup:   innodb_data_home_dir = ./  

  19. xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextend  

  20. xtrabackup:   innodb_log_group_home_dir = /tmp/data/2013-03-07_21-13-44/  

  21. xtrabackup:   innodb_log_files_in_group = 1

  22. xtrabackup:   innodb_log_file_size = 2097152

  23. 130307 21:18:20 InnoDB: Using Linux native AIO  

  24. xtrabackup: Starting InnoDB instance for recovery.  

  25. xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)  

  26. 130307 21:18:20 InnoDB: The InnoDB memory heap is disabled  

  27. 130307 21:18:20 InnoDB: Mutexes and rw_locks use GCC atomic builtins  

  28. 130307 21:18:20 InnoDB: Compressed tables use zlib 1.2.3  

  29. 130307 21:18:20 InnoDB: Using Linux native AIO  

  30. 130307 21:18:20 InnoDB: Warning: innodb_file_io_threads is deprecated. Please use innodb_read_io_threads and innodb_write_io_threads instead  

  31. 130307 21:18:20 InnoDB: Initializing buffer pool, size = 100.0M  

  32. 130307 21:18:20 InnoDB: Completed initialization of buffer pool  

  33. 130307 21:18:20 InnoDB: highest supported file format is Barracuda.  

  34. InnoDB: ##########################################################  

  35. InnoDB:                          WARNING!  

  36. InnoDB: The log sequence number in ibdata files is higher  

  37. InnoDB: than the log sequence number in the ib_logfiles! Are you sure  

  38. InnoDB: you are using the right ib_logfiles to start up the database?  

  39. InnoDB: Log sequence number in ib_logfiles is 59606124, log  

  40. InnoDB: sequence numbers stamped to ibdata file headers are between  

  41. InnoDB: 59607052 and 59607052.  

  42. InnoDB: ##########################################################  

  43. InnoDB: The log sequence number in ibdata files does not match  

  44. InnoDB: the log sequence number in the ib_logfiles!  

  45. 130307 21:18:20  InnoDB: Database was not shut down normally!  

  46. InnoDB: Starting crash recovery.  

  47. InnoDB: Reading tablespace information from the .ibd files...  

  48. InnoDB: Last MySQL binlog file position 0 107, file name /var/log/mysql/mysql-bin.000023  

  49. 130307 21:18:29  InnoDB: Waiting for the background threads to start  

  50. 130307 21:18:30 Percona XtraDB (http://www.percona.com) 1.1.8-20.1 started; log sequence number 59606124  

  51. [notice (again)]  

  52.  If you use binary log and don't use any hack of group commit,  

  53.  the binary log position seems to be:  

  54. InnoDB: Last MySQL binlog file position 0 107, file name /var/log/mysql/mysql-bin.000023  

  55. xtrabackup: starting shutdown with innodb_fast_shutdown = 1

  56. 130307 21:18:30  InnoDB: Starting shutdown...  

  57. 130307 21:18:34  InnoDB: Shutdown completed; log sequence number 59607339  

  58. innobackupex: Starting to copy non-InnoDB files in '/tmp/data/2013-03-07_21-13-44/'  

  59. innobackupex: to the full backup directory '/tmp/restore'  

  60. innobackupex: Copying '/tmp/data/2013-03-07_21-13-44/xtrabackup_binlog_info' to '/tmp/restore/xtrabackup_binlog_info'  

  61. innobackupex: Copying '/tmp/data/2013-03-07_21-13-44/test/test.frm' to '/tmp/restore/test/test.frm'  

  62. innobackupex: Copying '/tmp/data/2013-03-07_21-13-44/test/db.opt' to '/tmp/restore/test/db.opt'  

  63. 130307 21:18:34  innobackupex: completed OK!  

然后再进入恢复的目录/tmp/data
 
  
  1. root@client2:/var/lib/mysql# cd /tmp/data  

  2. root@client2:/tmp/data# ll  

  3. total 3276  

  4. drwxr-xr-x  3 root root    4096 Mar  7 21:13 ./  

  5. drwxrwxrwt 14 root root    4096 Mar  7 21:18 ../  

  6. drwxr-xr-x  3 root root    4096 Mar  7 21:18 2013-03-07_21-13-44/  

  7. -rw-r--r--  1 root root    3780 Mar  7 21:02 test-201303072101.log  

  8. -rw-r--r--  1 root root 3336909 Mar  7 21:02 test-201303072101.tar.gz  

  9. root@client2:/tmp/data# cd 2013-03-07_21-13-44/  

  10. root@client2:/tmp/data/2013-03-07_21-13-44# ll  

  11. total 2288  

  12. drwxr-xr-x 3 root root    4096 Mar  7 21:18 ./  

  13. drwxr-xr-x 3 root root    4096 Mar  7 21:13 ../  

  14. -rw-r--r-- 1 root root     260 Mar  7 21:13 backup-my.cnf  

  15. -rw-r--r-- 1 root root  212992 Mar  7 21:13 ibdata1.delta  

  16. -rw-r--r-- 1 root root      18 Mar  7 21:13 ibdata1.meta  

  17. drwxr-xr-x 2 root root    4096 Mar  7 21:14 test/  

  18. -rw-r--r-- 1 root root      13 Mar  7 21:14 xtrabackup_binary  

  19. -rw-r--r-- 1 root root      23 Mar  7 21:14 xtrabackup_binlog_info  

  20. -rw-r--r-- 1 root root      84 Mar  7 21:14 xtrabackup_checkpoints  

  21. -rw-r--r-- 1 root root 2097152 Mar  7 21:18 xtrabackup_logfile  

跟全部备份一样,把test恢复到/var/lib/mysql里
 
  
  1. root@client2:/tmp/data/2013-03-07_21-13-44# rsync -avz test ib* /var/lib/mysql/  

  2. sending incremental file list  

  3. test/  

  4. test/db.opt  

  5. test/test.frm  

  6. sent 381 bytes  received 54 bytes  870.00 bytes/sec  

  7. total size is 8621  speedup is 19.82  

  8. root@client2:/tmp/data/2013-03-07_21-13-44# cd /var/lib/mysql/  

  9. root@client2:/var/lib/mysql# ll  

  10. total 77860  

  11. drwx------  8 mysql mysql     4096 Mar  7 21:19 ./  

  12. drwxr-xr-x 38 root  root      4096 Mar  7 19:52 ../  

  13. -rw-r--r--  1 root  root         0 Jan  5 14:22 debian-5.5.flag  

  14. drwx------  2 mysql mysql     4096 Feb 11 17:39 django/  

  15. -rw-rw----  1 mysql mysql 69206016 Mar  7 21:17 ibdata1  

  16. -rw-rw----  1 mysql mysql  5242880 Mar  7 21:17 ib_logfile0  

  17. -rw-rw----  1 mysql mysql  5242880 Mar  7 21:11 ib_logfile1  

  18. drwx------  2 mysql mysql     4096 Jan  5 22:55 monitor/  

  19. drwx------  2 mysql root      4096 Jan  5 14:22 mysql/  

  20. -rw-rw----  1 root  root         6 Jan  5 14:22 mysql_upgrade_info  

  21. drwx------  2 mysql mysql     4096 Jan  5 14:22 performance_schema/  

  22. drwxr-xr-x  2 root  root      4096 Mar  7 21:14 test/  

  23. drwxr-xr-x  2 mysql mysql     4096 Mar  7 19:58 xtrbackup/  

然后修改用户与组
 
  
  1. root@client2:/var/lib/mysql# chown -R mysql:mysql test/  

  2. root@client2:/var/lib/mysql# ll  

  3. total 77860  

  4. drwx------  8 mysql mysql     4096 Mar  7 21:19 ./  

  5. drwxr-xr-x 38 root  root      4096 Mar  7 19:52 ../  

  6. -rw-r--r--  1 root  root         0 Jan  5 14:22 debian-5.5.flag  

  7. drwx------  2 mysql mysql     4096 Feb 11 17:39 django/  

  8. -rw-rw----  1 mysql mysql 69206016 Mar  7 21:17 ibdata1  

  9. -rw-rw----  1 mysql mysql  5242880 Mar  7 21:17 ib_logfile0  

  10. -rw-rw----  1 mysql mysql  5242880 Mar  7 21:11 ib_logfile1  

  11. drwx------  2 mysql mysql     4096 Jan  5 22:55 monitor/  

  12. drwx------  2 mysql root      4096 Jan  5 14:22 mysql/  

  13. -rw-rw----  1 root  root         6 Jan  5 14:22 mysql_upgrade_info  

  14. drwx------  2 mysql mysql     4096 Jan  5 14:22 performance_schema/  

  15. drwxr-xr-x  2 mysql mysql     4096 Mar  7 21:14 test/  

  16. drwxr-xr-x  2 mysql mysql     4096 Mar  7 19:58 xtrbackup/  

然后启动msyql,并查看test数据库里test表的内容
 
  
  1. root@client2:/var/lib/mysql# service mysql start  

  2. mysql start/running, process 13109  

  3. root@client2:/var/lib/mysql# mysql -u root -p  

  4. Enter password:  

  5. Welcome to the MySQL monitor.  Commands end with ; or \g.  

  6. Your MySQL connection id is 36  

  7. Server version: 5.5.28-0ubuntu0.12.04.3-log (Ubuntu)  

  8. Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.  

  9. Oracle is a registered trademark of Oracle Corporation and/or its  

  10. affiliates. Other names may be trademarks of their respective  

  11. owners.  

  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  

  13. mysql> use test  

  14. Reading table information for completion of table and column names  

  15. You can turn off this feature to get a quicker startup with -A  

  16. Database changed  

  17. mysql> select * from test;  

  18. +------+  

  19. | id   |  

  20. +------+  

  21. |    1 |  

  22. |    2 |  

  23. |    3 |  

  24. |    4 |  

  25. |    5 |  

  26. |   11 |  

  27. |   12 |  

  28. |   13 |  

  29. |   14 |  

  30. |   15 |  

  31. +------+  

  32. 10 rows in set (0.00 sec)  

可以看到增量备份已经恢复完成。