使用 python tarfile 备份 postgresql 数据目录。预期/忽略哪些错误

发布于 2024-11-14 22:00:14 字数 798 浏览 2 评论 0原文

我正在编写一个 python 脚本,用于自动备份 PostgreSQL 数据目录,以启用“持续归档和时间点恢复"。

我想使用 Python tarfile 库来创建存档。

复杂的是数据文件在备份过程中可能会发生变化。引用 PostgreSQL 手册

一些文件系统备份工具会发出 警告或错误,如果文件 正在尝试复制更改,而 复制收益。打底时 活动数据库的备份,这 情况是正常的,不是错误。 但是,您需要确保您 可以区分这方面的投诉 根据实际错误进行排序

...一些 GNU tar 版本返回错误 代码与致命代码无法区分 如果文件被截断,则会出现错误 tar 正在复制它。幸运的是,GNU tar 版本 1.16 及更高版本退出 1 如果文件在执行过程中被更改 备份,2 个其他错误。

从数据目录复制文件时,Python 中应该出现哪些异常?我如何确定错误是否严重?

I am writing a python script for automating the backing up of the PostgreSQL data directory to enable "Continuous Archiving and Point-In-Time Recovery".

I would like to use Pythons tarfile library to create an archive.

The complication is that the data files can change during the backup. To quote the PostgreSQL manual:

Some file system backup tools emit
warnings or errors if the files they
are trying to copy change while the
copy proceeds. When taking a base
backup of an active database, this
situation is normal and not an error.
However, you need to ensure that you
can distinguish complaints of this
sort from real errors
.

... some
versions of GNU tar return an error
code indistinguishable from a fatal
error if a file was truncated while
tar was copying it. Fortunately, GNU
tar versions 1.16 and later exit with
1 if a file was changed during the
backup, and 2 for other errors.

When copying the files from the data directory, what exceptions should I be expecting in Python? And how can I determine whether an error is critical?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小矜持 2024-11-21 22:00:14

如果您使用 tarfile 模块,则其实现基本上取决于您。 GNU tar 在处理文件之前和之后检查文件大小和 ctime,如果它们不同,则会发出警告。这不是一个实际的错误,只是 tar 觉得应该提到的东西。但在您的用例中,您并不关心这一点,因此您可以忘记它。 tarfile 模块本身肯定不会抱怨这一点。

至于其他例外,可能性是无限的。在这种情况下,Python 抛出的几乎所有异常都可能是致命的。

If you are using the tarfile module, the implementation of this is essentially up to you. GNU tar checks the file size and ctime before and after it processes a file and complains if they differ. It's not an actual error, it's just something that tar feels like it should mention. But in your use case, you don't care about that, so you can just forget about it. The tarfile module certainly won't complain about this by itself.

As far as other exceptions, the possibilities are endless. Pretty much anything that Python throws is probably fatal in this case.

对你而言 2024-11-21 22:00:14

PostgreSQL 文档中解释了连续归档。它已实现,无需创建额外的脚本 - 但您将拥有许多文件。

24.3.2。制作基础备份 - 按照说明操作即可创建 tar,数据文件不会更改。

24.3.1。设置 WAL 归档 - 在这里您必须归档新的 WAL 文件,它们也不会改变,但它们是单独的文件。

我认为将 WAL 文件添加到基础备份中并不安全。如果服务器在您将文件添加到基本备份时崩溃,则该文件可能会损坏。

Continuous Archiving is explained in PostgreSQL documentation. It is implemented and there is no need to create additional script - but you will have many files.

24.3.2. Making a Base Backup - follow instructions and you can create tar, data files will not change.

24.3.1. Setting up WAL archiving - here you have to archive new WAL files and they will not change too but they are separate files.

I think it is not safe to add WAL files to base backup. If server crashes while you are adding file to base backup it may become corrupt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文