我有一个程序,它使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解压缩它们?
最后,我管理了这个解决方案来恢复由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)