簡體   English   中英

有沒有一種方法可以使用pg_restore自動解壓縮?

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

我有一個程序可以進行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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM