4.2.4 恢复数据库

要将 MySQL 实例从备份恢复到数据库服务器:

在下面的示例中,使用命令恢复 在第 4.2.2 节“备份整个 MySQL 实例”中 给出的示例中创建的单文件备份。copy-back-and-apply-log除了常用的连接参数外,还使用了以下选项:

  • --defaults-file提供恢复数据的配置。 如果曾经使用过,它必须是出现在mysqlbackup命令中的第一个选项。在大多数情况下,您可以使用此选项向 mysqlbackup提供要将数据恢复到的目标服务器的配置文件。但是,当备份的以下 InnoDB 设置与目标服务器上的设置不同时,重要的是在还原期间将备份值提供给mysqlbackup并提供给mysqld当您启动已还原的服务器时(否则,还原可能会失败,或者您之后可能无法启动已还原的服务器):

    如果您不确定备份的这些设置,它们会在备份期间存储在文件中 — 您可以在创建单映像备份时backup-my.cnf 指定的临时目录中找到该文件,或者在 您创建的备份目录中找到该文件--backup-dir可以通过使用 extract命令解压备份镜像来创建。如果这些选项的值与目标服务器上的不同,请将它们添加到您提供给 mysqlbackup的配置文件中以及之后将用于启动服务器的配置文件;或者,您也可以将它们作为命令行选项提供给mysqlbackupmysqld

    对于上面列出的一些选项(即 、 innodb_data_file_pathinnodb_log_file_sizeinnodb_log_files_in_groupmysqlbackup检查您为它们提供的值以确保您之后能够使用这些值启动目标服务器:如果其中任何一个不提供,它会抛出错误与备份的实际值匹配。如果未在配置文件或命令行中 为mysqlbackup指定这些值(下例中就是这种情况),则会给出警告。

  • --datadir提供用于恢复数据的数据目录的位置。您必须为任何还原操作指定此选项。

  • --backup-image提供单文件备份的路径。

  • --backup-dir提供一个空文件夹的位置来存储在恢复过程中创建的一些临时文件。

$  ./mysqlbackup  --defaults-file=/etc/mysql/my.cnf --datadir=/var/lib/mysql \ 
    --backup-image=/home/admin/backups/my.mbi --backup-dir=/home/admin/backup-tmp copy-back-and-apply-log

MySQL Enterprise Backup version 3.12.5 Linux-2.6.18-274.el5-i686 [2014/11/12] 
Copyright (c) 2003, 2014, Oracle and/or its affiliates. All Rights Reserved.

 mysqlbackup: INFO: Starting with following command line ...
 ./mysqlbackup --defaults-file=/etc/mysql/my.cnf 
        --datadir=/var/lib/mysql --backup-image=/home/admin/backups/my.mbi 
        --backup-dir=/home/admin/backup-tmp copy-back-and-apply-log 

 mysqlbackup: INFO: 
IMPORTANT: Please check that mysqlbackup run completes successfully.
           At the end of a successful 'copy-back-and-apply-log' run mysqlbackup
           prints "mysqlbackup completed OK!".

 mysqlbackup: INFO: Backup Image MEB version string: 3.12.5 [2014/11/12]
141204 13:10:39 mysqlbackup: INFO: MEB logfile created at /home/admin/backup-tmp/meta/MEB_2014-12-04.13-10-39_copy_back_img_to_datadir.log

--------------------------------------------------------------------
                       Server Repository Options:
--------------------------------------------------------------------
  datadir = /var/lib/mysql
  innodb_data_home_dir = /var/lib/mysql
  innodb_data_file_path = ibdata1:12M:autoextend
  innodb_log_group_home_dir = /var/lib/mysql
  innodb_log_files_in_group = 2
  innodb_log_file_size = 50331648
  innodb_page_size = 16384
  innodb_checksum_algorithm = innodb

--------------------------------------------------------------------
                       Backup Config Options:
--------------------------------------------------------------------
  datadir = /home/admin/backup-tmp/datadir
  innodb_data_home_dir = /home/admin/backup-tmp/datadir
  innodb_data_file_path = ibdata1:12M:autoextend
  innodb_log_group_home_dir = /home/admin/backup-tmp/datadir
  innodb_log_files_in_group = 2
  innodb_log_file_size = 50331648
  innodb_page_size = 16384
  innodb_checksum_algorithm = innodb

 mysqlbackup: INFO: Creating 14 buffers each of size 16777216.
141204 13:10:39 mysqlbackup: INFO: Copy-back-and-apply-log operation starts with following threads
		1 read-threads    6 process-threads    1 write-threads
141204 13:10:39 mysqlbackup: INFO: Copying database directory: meta
141204 13:10:39 mysqlbackup: INFO: Copying datadir/ibdata1.
141204 13:10:39 mysqlbackup: INFO: Copying datadir/mysql/innodb_index_stats.ibd.
141204 13:10:39 mysqlbackup: INFO: Copying datadir/mysql/innodb_table_stats.ibd.
141204 13:10:39 mysqlbackup: INFO: Copying datadir/mysql/slave_master_info.ibd.
141204 13:10:39 mysqlbackup: INFO: Copying datadir/mysql/slave_relay_log_info.ibd.
141204 13:10:39 mysqlbackup: INFO: Copying database directory: datadir/mysql
141204 13:10:39 mysqlbackup: INFO: Copying datadir/mysql/slave_worker_info.ibd.
141204 13:10:39 mysqlbackup: INFO: Copying datadir/test2/tb1.ibd.
141204 13:10:39 mysqlbackup: INFO: Copying database directory: datadir/performance_schema
141204 13:10:39 mysqlbackup: INFO: Copying database directory: datadir/test
141204 13:10:39 mysqlbackup: INFO: Copying database directory: datadir/test2
141204 13:10:40 mysqlbackup: INFO: Copying database directory: datadir/mysql
141204 13:10:41 mysqlbackup: INFO: Copying database directory: datadir/performance_schema
141204 13:10:42 mysqlbackup: INFO: Copying database directory: datadir/test
141204 13:10:42 mysqlbackup: INFO: Copying database directory: datadir/test2
141204 13:10:43 mysqlbackup: INFO: Total files as specified in image: 161
141204 13:10:43 mysqlbackup: INFO: Creating server config files server-my.cnf and server-all.cnf in /var/lib/mysql
141204 13:10:43 mysqlbackup: INFO: Copy-back operation completed successfully.
 mysqlbackup: INFO: Source Image Path = /home/admin/backups/my.mbi


 mysqlbackup: INFO: Creating 14 buffers each of size 65536.
141204 13:10:43 mysqlbackup: INFO: Apply-log operation starts with following threads
		1 read-threads    1 process-threads
 mysqlbackup: INFO: Using up to 100 MB of memory.
141204 13:10:43 mysqlbackup: INFO: ibbackup_logfile's creation parameters:
          start lsn 1631744, end lsn 1631766,
          start checkpoint 1631766.
InnoDB: Doing recovery: scanned up to log sequence number 1631766
 mysqlbackup: INFO: InnoDB: Starting an apply batch of log records to the database...
InnoDB: Progress in percent: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
 mysqlbackup: INFO: InnoDB: Setting log file size to 50331648
 mysqlbackup: INFO: InnoDB: Setting log file size to 50331648
141204 13:10:44 mysqlbackup: INFO: We were able to parse ibbackup_logfile up to
          lsn 1631766.
 mysqlbackup: INFO: Last MySQL binlog file position 0 120, file name mysqld-bin.000004:120
141204 13:10:44 mysqlbackup: INFO: The first data file is '/var/lib/mysql/ibdata1'
          and the new created log files are at '/var/lib/mysql'
141204 13:10:44 mysqlbackup: INFO: Apply-log operation completed successfully.
141204 13:10:44 mysqlbackup: INFO: Full Backup has been restored successfully.

mysqlbackup completed OK!

现在原始数据库目录已从备份中恢复。根据您打算如何启动恢复的服务器,您可能需要调整恢复的数据目录的所有权。例如服务器要由用户启动mysql,使用如下命令将数据目录及其下的文件的owner属性改为mysql用户,group属性改为mysql组。

$ chown -R mysql:mysql /path/to/datadir

您现在已准备好启动恢复的数据库服务器。有关如何执行不同类型恢复的更多讨论,请参阅第 5.2 节,“执行恢复操作”