简体   繁体   English

有没有一种方法可以使用pg_restore自动解压缩?

[英]Is there a way to unzip automatically with pg_restore?

I had a program which makes PostgreSQL backups, and gives the user the chance to zip these backups: 我有一个程序可以进行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)

Now, I am using pg_restore to restore them, but I am not able to do it if the file is compressed. 现在,我正在使用pg_restore还原它们,但是如果文件被压缩,我将无法执行。 Is there any way to do it directly with pg_restore (like in pg_dump or pg_dumpall) or do I have to check the kind of compressing used and unzip them with Python? 有什么方法可以直接使用pg_restore做到(例如pg_dump或pg_dumpall),还是我必须检查使用的压缩类型并用Python解压缩?

Finally I managed this solution for restoring the backups generated by pg_dump (not pg_dumpall yet): 最终,我管理了此解决方案来还原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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM