Postgresql 中文操作指南

26.2. File System Level Backup #

一种备选的备份策略是直接复制 PostgreSQL 用于在数据库中存储数据的文件; Section 19.2说明了这些文件的位置。你可以使用任何你首选的方法进行文件系统备份;例如:

An alternative backup strategy is to directly copy the files that PostgreSQL uses to store the data in the database; Section 19.2 explains where these files are located. You can use whatever method you prefer for doing file system backups; for example:

tar -cf backup.tar /usr/local/pgsql/data

然而,有两个限制使得此方法不可行,或者至少劣于 pg_dump 方法:

There are two restrictions, however, which make this method impractical, or at least inferior to the pg_dump method:

另一种文件系统备份方法是制作数据目录的“一致快照”,如果文件系统支持该功能(且你愿意相信它已正确实现)。典型过程是制作包含数据库的卷的“冻结快照”,然后将整个数据目录(不仅仅是一部分,见上文)从快照复制到备份设备,然后释放冻结快照。即使数据库服务器正在运行,此方法也能正常工作。然而,使用此方法创建的备份将以数据库服务器未正确关闭的状态保存数据库文件;因此,当你在备份的数据上启动数据库服务器时,它将认为之前的服务器实例已崩溃,并将重放 WAL 日志。这不是问题;只需注意它(并确保在你的备份中包含 WAL 文件)。你可以在获取快照之前执行 CHECKPOINT 以缩短恢复时间。

An alternative file-system backup approach is to make a “consistent snapshot” of the data directory, if the file system supports that functionality (and you are willing to trust that it is implemented correctly). The typical procedure is to make a “frozen snapshot” of the volume containing the database, then copy the whole data directory (not just parts, see above) from the snapshot to a backup device, then release the frozen snapshot. This will work even while the database server is running. However, a backup created in this way saves the database files in a state as if the database server was not properly shut down; therefore, when you start the database server on the backed-up data, it will think the previous server instance crashed and will replay the WAL log. This is not a problem; just be aware of it (and be sure to include the WAL files in your backup). You can perform a CHECKPOINT before taking the snapshot to reduce recovery time.

如果你的数据库分布在多个文件系统中,则可能无法对所有卷获取完全同时的冻结快照。例如,如果你的数据文件和 WAL 日志位于不同的磁盘上,或者表空间位于不同的文件系统上,则可能无法使用快照备份,因为快照 must 是同时的。在此类情况下,在信任一致快照技术之前非常仔细地阅读文件系统文档。

If your database is spread across multiple file systems, there might not be any way to obtain exactly-simultaneous frozen snapshots of all the volumes. For example, if your data files and WAL log are on different disks, or if tablespaces are on different file systems, it might not be possible to use snapshot backup because the snapshots must be simultaneous. Read your file system documentation very carefully before trusting the consistent-snapshot technique in such situations.

如果无法同时建立快照,一种选择是关闭数据库服务器足够长的时间以建立所有冻结快照。另一种选择是执行连续归档基础备份 ( Section 26.3.2),因为此类备份对备份期间的文件系统更改免疫。这需要在备份过程中启用连续归档;使用连续归档恢复 ( Section 26.3.4)进行还原。

If simultaneous snapshots are not possible, one option is to shut down the database server long enough to establish all the frozen snapshots. Another option is to perform a continuous archiving base backup (Section 26.3.2) because such backups are immune to file system changes during the backup. This requires enabling continuous archiving just during the backup process; restore is done using continuous archive recovery (Section 26.3.4).

另一种选择是用 rsync 执行文件系统备份。这是通过在数据库服务器运行时首先运行 rsync,然后关闭数据库服务器(以允许执行 rsync --checksum),进而完成的。(—​checksum 必须,因为 rsync 的文件修改时间粒度仅为一秒。) 第二个 rsync 将比第一个更快,因为它只需传输相对较少的数据,且最终结果将保持一致,因为服务器已关闭。此方法允许以最短的停机时间执行文件系统备份。

Another option is to use rsync to perform a file system backup. This is done by first running rsync while the database server is running, then shutting down the database server long enough to do an rsync --checksum. (—​checksum is necessary because rsync only has file modification-time granularity of one second.) The second rsync will be quicker than the first, because it has relatively little data to transfer, and the end result will be consistent because the server was down. This method allows a file system backup to be performed with minimal downtime.

请注意,文件系统备份通常会大于 SQL 转储。(pg_dump 无需转储索引的内容,例如,仅需传输重新创建它们的命令。) 然而,执行文件系统备份可能会更快。

Note that a file system backup will typically be larger than an SQL dump. (pg_dump does not need to dump the contents of indexes for example, just the commands to recreate them.) However, taking a file system backup might be faster.