【发布时间】:2014-05-09 10:44:19
【问题描述】:
我有一个程序可以制作 PostgreSQL 备份,并让用户有机会压缩这些备份:
if bkp_type == 'gz':
command = 'pg_dumpall -U {} -h {} -p {} | gzip > {}'.format(user, server, port, file)
elif bkp_type == 'bz2':
command = 'pg_dumpall -U {} -h {} -p {} | bzip2 > {}'.format(user, server, port, file)
elif bkp_type == 'zip':
command = 'pg_dumpall -U {} -h {} -p {} | zip > {}'.format(user, server, port, file)
else:
command = 'pg_dumpall -U {} -h {} -p {} > {}'.format(user, server, port, file)
result = subprocess.call(command, shell=True)
现在,我正在使用 pg_restore 来恢复它们,但如果文件被压缩,我就无法做到这一点。有什么方法可以直接使用 pg_restore(例如在 pg_dump 或 pg_dumpall 中)或者我必须检查使用的压缩类型并使用 Python 解压缩它们?
【问题讨论】:
-
我可以制作一个程序将转储文件通过管道传输到 zip 程序中,为什么不简单地制作一个解压缩文件并将其通过管道传输到 pg_restore 的程序?
-
我尝试了一个 zip 文件的示例: unzip | pg_restore -U my_user -h my_server -p my_port -d path new_dbname,给我错误 pg_restore: [archiver] input file does not seem to be a valid archive
-
你能解压缩到一个普通文件并更新你的帖子以显示转储文件的开始吗?您可能对 PG 版本或文件格式有问题,没有任何证据很难说。
标签: python python-3.x postgresql unzip pg-restore