Python 制作postgres数据库的快照

Python 制作postgres数据库的快照,python,postgresql,Python,Postgresql,我正在尝试编写一个python脚本,用于获取postgres数据库的快照并还原这些快照。试图让pg_dump>data.sql正常工作是一场噩梦,我突然想到一个想法,我想超越你们 我们的想法是按照以下逻辑拍摄快照: 1 - stop the postgres daemon 2 - copy the {postgres}/data folder out 3 - start the daemon 1 - stop the postgres daemon 2 - delete the {postgr

我正在尝试编写一个python脚本,用于获取postgres数据库的快照并还原这些快照。试图让pg_dump>data.sql正常工作是一场噩梦,我突然想到一个想法,我想超越你们

我们的想法是按照以下逻辑拍摄快照:

1 - stop the postgres daemon
2 - copy the {postgres}/data folder out
3 - start the daemon
1 - stop the postgres daemon
2 - delete the {postgres}/data folder
3 - copy the snapshot data folder in place
4 - reset the permissions of the data folder to the postgres user
5 - start the daemon
然后,要恢复快照,请执行以下操作:

1 - stop the postgres daemon
2 - copy the {postgres}/data folder out
3 - start the daemon
1 - stop the postgres daemon
2 - delete the {postgres}/data folder
3 - copy the snapshot data folder in place
4 - reset the permissions of the data folder to the postgres user
5 - start the daemon

这是一个测试数据库,所以宕机不是问题。

如果您的问题是如何从Python脚本运行命令,您可以使用
运行shell命令,如
sudo/etc/init.d/postgresql-x.x stop

但是,在我看来,您应该使用
pg_dump
pg_restore
实用程序,它们将以更安全有效的方式完成工作,并且不需要停止Postgres守护程序


看看PostgreSQL文档中的和章节。

嗯,如果你不了解postgres的内部结构,这似乎是一个糟糕的策略。pg_转储的存在是有原因的。也许你应该问一下如何让pg_转储正常工作?