Question

I had a program which makes PostgreSQL backups, and gives the user the chance to zip these backups:

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. 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?

Was it helpful?

Solution

Finally I managed this solution for restoring the backups generated by pg_dump (not pg_dumpall yet):

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top