本节描述了不同类型备份的特性。This section describes the characteristics of different types of backups.
物理(原始)备份与逻辑备份Physical (Raw) Versus Logical Backups
物理备份包括存储数据库内容的目录和文件的原始副本。这种类型的备份适用于需要在发生问题时快速恢复的大型重要数据库。Physical backups consist of raw copies of the directories and files that store database contents. This type of backup is suitable for large, important databases that need to be recovered quickly when problems occur.
逻辑备份保存以逻辑数据库结构表示的信息(CREATE DATABASE、CREATE TABLE 语句)和内容(INSERT 语句或定界文本文件)。这种类型的备份适用于数据量较小的情况,可以编辑数据值或表结构,或者在不同的机器架构上重建数据。Logical backups save information represented as logical database
structure (CREATE DATABASE,
CREATE TABLE statements) and
content (INSERT statements or
delimited-text files). This type of backup is suitable for smaller
amounts of data where you might edit the data values or table
structure, or recreate the data on a different machine
architecture.
物理备份方法具有以下特征:Physical backup methods have these characteristics:
备份由数据库目录和文件的精确副本组成。通常,这是对 MySQL 数据目录的全部或部分复制。The backup consists of exact copies of database directories and files. Typically this is a copy of all or part of the MySQL data directory.
物理备份方法比逻辑备份更快,因为它们仅涉及文件复制而无需转换。Physical backup methods are faster than logical because they involve only file copying without conversion.
输出比逻辑备份更紧凑。Output is more compact than for logical backup.
由于备份速度和紧凑性对繁忙且重要的数据库很重要,MySQL 企业备份产品执行物理备份。有关 MySQL 企业备份产品的概述,请参见 第 32.1 节,“MySQL 企业备份概述”。Because backup speed and compactness are important for busy, important databases, the MySQL Enterprise Backup product performs physical backups. For an overview of the MySQL Enterprise Backup product, see Section 32.1, “MySQL Enterprise Backup Overview”.
备份和恢复的粒度范围从整个数据目录级别到单个文件级别不等。这可能会或可能不会提供表级粒度,具体取决于存储引擎。例如,
InnoDB表可以单独存放在一个文件中,也可以与其他InnoDB表共享文件存储;每个MyISAM表唯一对应一组文件。Backup and restore granularity ranges from the level of the entire data directory down to the level of individual files. This may or may not provide for table-level granularity, depending on storage engine. For example,InnoDBtables can each be in a separate file, or share file storage with otherInnoDBtables; eachMyISAMtable corresponds uniquely to a set of files.除了数据库之外,备份还可以包括任何相关文件,如日志或配置文件。In addition to databases, the backup can include any related files such as log or configuration files.
以这种方式备份
MEMORY表的数据比较棘手,因为它们的内容未存储在磁盘上。(MySQL 企业备份产品具有在备份期间检索MEMORY表数据的功能。)Data fromMEMORYtables is tricky to back up this way because their contents are not stored on disk. (The MySQL Enterprise Backup product has a feature where you can retrieve data fromMEMORYtables during a backup.)备份仅可以移植到具有相同或相似硬件特征的其他机器上。Backups are portable only to other machines that have identical or similar hardware characteristics.
在 MySQL 服务器未运行时可以执行备份。如果服务器正在运行,则必须执行适当的锁定,以确保服务器在备份期间不更改数据库内容。MySQL Enterprise Backup 会自动对需要的表执行此锁定。Backups can be performed while the MySQL server is not running. If the server is running, it is necessary to perform appropriate locking so that the server does not change database contents during the backup. MySQL Enterprise Backup does this locking automatically for tables that require it.
物理备份工具包括 MySQL Enterprise Backup 的 mysqlbackup,用于
InnoDB或任何其他表,或用于MyISAM表的文件系统级命令(例如 cp、scp、tar、rsync)。Physical backup tools include the mysqlbackup of MySQL Enterprise Backup forInnoDBor any other tables, or file system-level commands (such as cp, scp, tar, rsync) forMyISAMtables.用于恢复:For restore:
MySQL 企业备份恢复它备份的
InnoDB和其他表。MySQL Enterprise Backup restoresInnoDBand other tables that it backed up.ndb_restore 恢复
NDB表。ndb_restore restoresNDBtables.在文件系统级别复制的文件可以使用文件系统命令复制回它们的原始位置。Files copied at the file system level can be copied back to their original locations with file system commands.
逻辑备份方法具有以下特点:Logical backup methods have these characteristics:
备份是通过查询 MySQL 服务器以获取数据库结构和内容信息来完成的。The backup is done by querying the MySQL server to obtain database structure and content information.
备份比物理方法慢,因为服务器必须访问数据库信息并将其转换为逻辑格式。如果输出写在客户端,服务器还必须将其发送到备份程序。Backup is slower than physical methods because the server must access database information and convert it to logical format. If the output is written on the client side, the server must also send it to the backup program.
输出比物理备份大,特别是当以文本格式保存时。Output is larger than for physical backup, particularly when saved in text format.
备份和恢复的粒度可以在服务器级别(所有数据库)、数据库级别(特定数据库中的所有表)或表级别。无论使用哪种存储引擎,这一点都适用。Backup and restore granularity is available at the server level (all databases), database level (all tables in a particular database), or table level. This is true regardless of storage engine.
备份不包括日志或配置文件,或数据库中不属于数据库的其他相关文件。The backup does not include log or configuration files, or other database-related files that are not part of databases.
以逻辑格式存储的备份与机器无关,并且高度可移植。Backups stored in logical format are machine independent and highly portable.
逻辑备份是在 MySQL 服务器运行时执行的。服务器不会下线。Logical backups are performed with the MySQL server running. The server is not taken offline.
逻辑备份工具包括 mysqldump 程序和
SELECT ... INTO OUTFILE语句。它们适用于任何存储引擎,即使是MEMORY也是如此。Logical backup tools include the mysqldump program and theSELECT ... INTO OUTFILEstatement. These work for any storage engine, evenMEMORY.要恢复逻辑备份,可以使用 mysql 客户端处理 SQL 格式的转储文件。要加载分隔文本文件,请使用
LOAD DATA语句或 mysqlimport 客户端。To restore logical backups, SQL-format dump files can be processed using the mysql client. To load delimited-text files, use theLOAD DATAstatement or the mysqlimport client.
在线备份与离线备份Online Versus Offline Backups
在线备份在 MySQL 服务器运行时进行,以便可以从服务器获取数据库信息。离线备份在服务器停止时进行。这一区别也可以描述为“热”备份与“冷”备份;“温”备份指的是服务器保持运行,但在你外部访问数据库文件时锁定以防止修改数据的备份类型。Online backups take place while the MySQL server is running so that the database information can be obtained from the server. Offline backups take place while the server is stopped. This distinction can also be described as “hot” versus “cold” backups; a “warm” backup is one where the server remains running but locked against modifying data while you access database files externally.
在线备份方法具有以下特点:Online backup methods have these characteristics:
备份对其他客户端的干扰较小,客户端在备份过程中可以连接到 MySQL 服务器,并且可能能够访问数据,具体取决于它们需要执行的操作。The backup is less intrusive to other clients, which can connect to the MySQL server during the backup and may be able to access data depending on what operations they need to perform.
必须小心施加适当的锁定,以防止数据修改破坏备份完整性。MySQL Enterprise Backup 产品会自动执行此类锁定。Care must be taken to impose appropriate locking so that data modifications do not take place that would compromise backup integrity. The MySQL Enterprise Backup product does such locking automatically.
离线备份方法具有以下特点:Offline backup methods have these characteristics:
客户端可能会受到不利影响,因为在备份期间服务器不可用。因此,这类备份通常从可以脱机而不影响可用性的副本进行。Clients can be affected adversely because the server is unavailable during backup. For that reason, such backups are often taken from a replica that can be taken offline without harming availability.
备份过程更简单,因为不存在客户端活动干扰的可能性。The backup procedure is simpler because there is no possibility of interference from client activity.
在线和离线之间的类似区分也适用于恢复操作,并且具有类似的特征。然而,客户受到在线恢复影响的可能性比在线备份更大,因为恢复需要更强的锁定。在备份期间,客户可能在数据备份时仍能读取数据。恢复会修改数据,而不仅仅是读取数据,因此在数据恢复过程中必须阻止客户访问数据。A similar distinction between online and offline applies for recovery operations, and similar characteristics apply. However, it is more likely for clients to be affected by online recovery than by online backup because recovery requires stronger locking. During backup, clients might be able to read data while it is being backed up. Recovery modifies data and does not just read it, so clients must be prevented from accessing data while it is being restored.
本地备份与远程备份Local Versus Remote Backups
本地备份是在运行 MySQL 服务器的同一主机上执行的,而远程备份则是在不同的主机上完成的。对于某些类型的备份,即使输出写入服务器本地,也可以从远程主机启动备份。A local backup is performed on the same host where the MySQL server runs, whereas a remote backup is done from a different host. For some types of backups, the backup can be initiated from a remote host even if the output is written locally on the server. host.
mysqldump 可以连接到本地或远程服务器。对于 SQL 输出(
CREATE和INSERT语句),可以进行本地或远程转储,并在客户端生成输出。对于分隔文本输出(使用--tab选项),数据文件会在服务器主机上创建。mysqldump can connect to local or remote servers. For SQL output (CREATEandINSERTstatements), local or remote dumps can be done and generate output on the client. For delimited-text output (with the--taboption), data files are created on the server host.SELECT ... INTO OUTFILE可以从本地或远程客户端主机发起,但输出文件会在服务器主机上创建。SELECT ... INTO OUTFILEcan be initiated from a local or remote client host, but the output file is created on the server host.物理备份方法通常在 MySQL 服务器主机上本地启动,以便服务器可以下线,尽管复制文件的目标可能是远程的。Physical backup methods typically are initiated locally on the MySQL server host so that the server can be taken offline, although the destination for copied files might be remote.
快照备份Snapshot Backups
一些文件系统实现允许拍摄“快照”。这些快照在给定时间点提供文件系统的逻辑副本,而不需要物理复制整个文件系统。(例如,该实现可能使用写时复制技术,因此只需要复制快照时间之后修改的文件系统部分。)MySQL 本身不提供拍摄文件系统快照的功能。这可以通过第三方解决方案实现,例如 Veritas、LVM 或 ZFS。Some file system implementations enable “snapshots” to be taken. These provide logical copies of the file system at a given point in time, without requiring a physical copy of the entire file system. (For example, the implementation may use copy-on-write techniques so that only parts of the file system modified after the snapshot time need be copied.) MySQL itself does not provide the capability for taking file system snapshots. It is available through third-party solutions such as Veritas, LVM, or ZFS.
完整备份与增量备份Full Versus Incremental Backups
完整备份包括在特定时间点由 MySQL 服务器管理的所有数据。增量备份则包含在特定时间段内(从一个时间点到另一个时间点)对数据所做的更改。MySQL 有不同的方法来执行完整备份,例如本节前面所述的方法。通过启用服务器的二进制日志可以实现增量备份,服务器使用二进制日志来记录数据更改。A full backup includes all data managed by a MySQL server at a given point in time. An incremental backup consists of the changes made to the data during a given time span (from one point in time to another). MySQL has different ways to perform full backups, such as those described earlier in this section. Incremental backups are made possible by enabling the server's binary log, which the server uses to record data changes.
完整恢复与时间点(增量)恢复Full Versus Point-in-Time (Incremental) Recovery
完全恢复是从完整备份中恢复所有数据。这会将服务器实例恢复到备份创建时的状态。如果该状态不够当前,则在完全恢复之后,可以恢复自完整备份以来所做的增量备份,以使服务器达到更为最新的状态。A full recovery restores all data from a full backup. This restores the server instance to the state that it had when the backup was made. If that state is not sufficiently current, a full recovery can be followed by recovery of incremental backups made since the full backup, to bring the server to a more up-to-date state.
增量恢复是恢复在特定时间段内所做的更改。这也称为时间点恢复,因为它使服务器的状态达到给定时间点的最新状态。时间点恢复基于二进制日志,通常在从备份文件进行的完全恢复之后进行,从而将服务器恢复到备份创建时的状态。然后,二进制日志文件中记录的数据更改会作为增量恢复应用,以重做数据修改并将服务器恢复到所需的时间点。Incremental recovery is recovery of changes made during a given time span. This is also called point-in-time recovery because it makes a server's state current up to a given time. Point-in-time recovery is based on the binary log and typically follows a full recovery from the backup files that restores the server to its state when the backup was made. Then the data changes written in the binary log files are applied as incremental recovery to redo data modifications and bring the server up to the desired point in time.
表维护Table Maintenance
如果表损坏,数据完整性可能会受到影响。对于InnoDB表来说,这通常不是一个问题。有关程序检查MyISAM表并在发现问题时进行修复,请参见第 9.6 节,“MyISAM 表维护与崩溃恢复”。Data integrity can be compromised if tables become corrupt. For
InnoDB tables, this is not a typical
issue. For programs to check MyISAM
tables and repair them if problems are found, see
Section 9.6, “MyISAM Table Maintenance and Crash Recovery”.
备份计划、压缩和加密Backup Scheduling, Compression, and Encryption
备份调度对于自动化备份过程非常有价值。备份输出的压缩可以减少空间需求,而输出的加密则可以更好地防止未经授权访问备份数据。MySQL 本身不提供这些功能。MySQL 企业备份产品可以压缩 InnoDB 备份,而备份输出的压缩或加密可以使用文件系统实用程序来实现。其他第三方解决方案也可能存在。Backup scheduling is valuable for automating backup procedures.
Compression of backup output reduces space requirements, and
encryption of the output provides better security against
unauthorized access of backed-up data. MySQL itself does not
provide these capabilities. The MySQL Enterprise Backup product can compress
InnoDB backups, and compression or encryption
of backup output can be achieved using file system utilities.
Other third-party solutions may be available.