Postgresql 中文操作指南

pg_verifybackup

pg_verifybackup — 验证 PostgreSQL 集群的基准备份的完整性

pg_verifybackup — verify the integrity of a base backup of a PostgreSQL cluster

Synopsis

pg_verifybackup [ option …​]

pg_verifybackup [option…​]

Description

pg_verifybackup 用于检查使用 pg_basebackup 生成的 backup_manifest 拍摄的数据库集群备份的完整性,服务器在备份时生成了该 backup_manifest 。备份必须存储在“普通”格式中;提取“tar”格式备份后可以检查该备份。

pg_verifybackup is used to check the integrity of a database cluster backup taken using pg_basebackup against a backup_manifest generated by the server at the time of the backup. The backup must be stored in the "plain" format; a "tar" format backup can be checked after extracting it.

请务必注意,pg_verifybackup 所执行的验证不包括也不可能包括当尝试使用备份时由运行的服务器执行的每一次检查。即使您使用该工具,您仍应该执行测试恢复并验证生成的数据库是否按预期工作,以及其是否好像包含正确的数据。然而,pg_verifybackup 可以检测到通常因存储问题或用户错误而发生的一些问题。

It is important to note that the validation which is performed by pg_verifybackup does not and cannot include every check which will be performed by a running server when attempting to make use of the backup. Even if you use this tool, you should still perform test restores and verify that the resulting databases work as expected and that they appear to contain the correct data. However, pg_verifybackup can detect many problems that commonly occur due to storage problems or user error.

备份验证按照四个阶段进行。首先, pg_verifybackup 会读取 backup_manifest 文件。如果该文件不存在、无法读取、格式错误或验证不通过其自身的内部校验和,则 pg_verifybackup 将终止并附带一个致命错误。

Backup verification proceeds in four stages. First, pg_verifybackup reads the backup_manifest file. If that file does not exist, cannot be read, is malformed, or fails verification against its own internal checksum, pg_verifybackup will terminate with a fatal error.

其次, pg_verifybackup 将尝试验证当前存储在磁盘上的数据文件与服务器打算发送的数据文件完全相同,但存在一些例外情况如下所述。将检测到多余和丢失的文件,但有一些例外情况。此步骤将忽略 postgresql.auto.confstandby.signalrecovery.signal 的存在、不存在或任何修改,因为预计这些文件可能已作为备份过程的一部分创建或修改。它也不会对目标目录中的 backup_manifest 文件或 pg_wal 内的任何内容发出警告,即使这些文件未列在备份清单中。只检查文件;除了间接验证外,不验证目录的存在或不存在:如果一个目录丢失了,那么它应该包含的任何文件也必须丢失。

Second, pg_verifybackup will attempt to verify that the data files currently stored on disk are exactly the same as the data files which the server intended to send, with some exceptions that are described below. Extra and missing files will be detected, with a few exceptions. This step will ignore the presence or absence of, or any modifications to, postgresql.auto.conf, standby.signal, and recovery.signal, because it is expected that these files may have been created or modified as part of the process of taking the backup. It also won’t complain about a backup_manifest file in the target directory or about anything inside pg_wal, even though these files won’t be listed in the backup manifest. Only files are checked; the presence or absence of directories is not verified, except indirectly: if a directory is missing, any files it should have contained will necessarily also be missing.

接下来, pg_verifybackup 将对所有文件进行校验和,将校验和与清单中的值进行比较,并针对计算的校验和与存储在清单中的校验和不匹配的任何文件发出错误。此步骤不会针对前一步中产生错误的任何文件执行,因为已知它们有问题。在该步骤中也忽略了前一步中忽略的文件。

Next, pg_verifybackup will checksum all the files, compare the checksums against the values in the manifest, and emit errors for any files for which the computed checksum does not match the checksum stored in the manifest. This step is not performed for any files which produced errors in the previous step, since they are already known to have problems. Files which were ignored in the previous step are also ignored in this step.

最后, pg_verifybackup 将使用清单验证恢复该备份所需的预写式日志记录是否存在,以及是否可以读取和解析它们。 backup_manifest 包含有关将需要哪些预写式日志记录的信息, pg_verifybackup 将使用该信息来调用 pg_waldump 以解析那些预写式日志记录。将使用 —​quiet 标志,以便 pg_waldump 只报告错误,而不产生任何其他输出。虽然此验证级别足以检测明显的错误,例如丢失一个文件或内部校验和不匹配的文件,但它们不够深入以检测尝试恢复时可能发生的每一个可能的问题。例如,生成内部校验和正确但指定了无意义操作的预写式日志记录的服务器错误无法通过此方法检测。

Finally, pg_verifybackup will use the manifest to verify that the write-ahead log records which will be needed to recover the backup are present and that they can be read and parsed. The backup_manifest contains information about which write-ahead log records will be needed, and pg_verifybackup will use that information to invoke pg_waldump to parse those write-ahead log records. The —​quiet flag will be used, so that pg_waldump will only report errors, without producing any other output. While this level of verification is sufficient to detect obvious problems such as a missing file or one whose internal checksums do not match, they aren’t extensive enough to detect every possible problem that might occur when attempting to recover. For instance, a server bug that produces write-ahead log records that have the correct checksums but specify nonsensical actions can’t be detected by this method.

请注意,如果存在不必要来恢复该备份的额外 WAL 文件,此工具不会检查它们,尽管可以出于此目的单独调用 pg_waldump 。另请注意,WAL 验证与版本相关:您必须使用 pg_verifybackup 版本和 pg_waldump 版本,它们与正在检查的备份相关。相比之下,数据文件完整性检查应该适用于生成 backup_manifest 文件的任何服务器版本。

Note that if extra WAL files which are not required to recover the backup are present, they will not be checked by this tool, although a separate invocation of pg_waldump could be used for that purpose. Also note that WAL verification is version-specific: you must use the version of pg_verifybackup, and thus of pg_waldump, which pertains to the backup being checked. In contrast, the data file integrity checks should work with any version of the server that generates a backup_manifest file.

Options

pg_verifybackup 接受以下命令行参数:

pg_verifybackup accepts the following command-line arguments:

  • -e_—​exit-on-error_

    • Exit as soon as a problem with the backup is detected. If this option is not specified, pg_verifybackup will continue checking the backup even after a problem has been detected, and will report all problems detected as errors.

  • -i _path—​ignore=_path

    • Ignore the specified file or directory, which should be expressed as a relative path name, when comparing the list of data files actually present in the backup to those listed in the backup_manifest file. If a directory is specified, this option affects the entire subtree rooted at that location. Complaints about extra files, missing files, file size differences, or checksum mismatches will be suppressed if the relative path name matches the specified path name. This option can be specified multiple times.

  • -m _path—​manifest-path=_path

    • Use the manifest file at the specified path, rather than one located in the root of the backup directory.

  • -n_—​no-parse-wal_

    • Don’t attempt to parse write-ahead log data that will be needed to recover from this backup.

  • -P_—​progress_

    • Enable progress reporting. Turning this on will deliver a progress report while verifying checksums.

    • This option cannot be used together with the option —​quiet.

  • -q_—​quiet_

    • Don’t print anything when a backup is successfully verified.

  • -s_—​skip-checksums_

    • Do not verify data file checksums. The presence or absence of files and the sizes of those files will still be checked. This is much faster, because the files themselves do not need to be read.

  • -w _path—​wal-directory=_path

    • Try to parse WAL files stored in the specified directory, rather than in pg_wal. This may be useful if the backup is stored in a separate location from the WAL archive.

还可以使用其他选项:

Other options are also available:

  • -V_—​version_

    • Print the pg_verifybackup version and exit.

  • -?_—​help_

    • Show help about pg_verifybackup command line arguments, and exit.

Examples

若要创建服务器的基本备份,请使用 mydbserver 并验证备份的完整性:

To create a base backup of the server at mydbserver and verify the integrity of the backup:

$ pg_basebackup -h mydbserver -D /usr/local/pgsql/data
$ pg_verifybackup /usr/local/pgsql/data

若要创建服务器的基本备份,请使用 mydbserver ,将清单文件移动到备份目录之外,并验证备份:

To create a base backup of the server at mydbserver, move the manifest somewhere outside the backup directory, and verify the backup:

$ pg_basebackup -h mydbserver -D /usr/local/pgsql/backup1234
$ mv /usr/local/pgsql/backup1234/backup_manifest /my/secure/location/backup_manifest.1234
$ pg_verifybackup -m /my/secure/location/backup_manifest.1234 /usr/local/pgsql/backup1234

若要在忽略手动添加到备份目录的文件以及跳过校验和验证的情况下验证备份:

To verify a backup while ignoring a file that was added manually to the backup directory, and also skipping checksum verification:

$ pg_basebackup -h mydbserver -D /usr/local/pgsql/data
$ edit /usr/local/pgsql/data/note.to.self
$ pg_verifybackup --ignore=note.to.self --skip-checksums /usr/local/pgsql/data