【问题标题】:Is there a way to unzip automatically with pg_restore?有没有办法用 pg_restore 自动解压?
【发布时间】: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


【解决方案1】:

最后我管理了这个解决方案来恢复由 pg_dump(还不是 pg_dumpall)生成的备份:

if ext == 'gz':
   command = 'gunzip -c {} -k | pg_restore -U {} -h {} -p {}' \
             '-d {}'.format(file, user, server, port, new_dbname)
elif ext == 'bz2':
   command = 'bunzip2 -c {} -k | pg_restore -U {} -h {} -p {}' \
             '-d {}'.format(file, user, server, port, new_dbname)
elif ext == 'zip':
   command = 'unzip -p {} | pg_restore -U {} -h {} -p {} ' \
             '-d {}'.format(file, user, server, port, new_dbname)
else:
   command = 'pg_restore -U {} -h {} -p {} -d {} {}'.format(user,
                server, port, new_dbname, file)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多