忍者ブログ
Admin / Write / Res
ちゃんとカテゴリ分けされておりませんので、 記事をお探しならブログ内検索が便利です。 ご活用くださいませー+.(≧∀≦)゚+.゚
ブログ内検索
カレンダー
03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
カウンター
アクセスカウンター
最新コメント
[04/05 Fully Vaccinated Adorable Escort Service in bengaluru 8273600238]
[03/26 Tonaldcet]
[01/16 jilibet]
[10/20 Call Girl in Delhi]
[09/07 לפרטים נוספים]
最新トラックバック
プロフィール
+ハンドル+
y_ayamori(purple)
+職業+
IT系エンジニア
+すまい+
さいたま
バーコード
[691]  [690]  [689]  [688]  [687]  [686]  [685]  [684]  [683]  [681]  [680
pg_dumpの分割 でも触れましたが、DBのダンプファイルをダンプの時点で分割しておかないとリストアの時結構大変です。
それでも1ファイルでダンプしちゃったなら、前述の方法などで分割するしかないですが、できることならダンプの時点でテーブルごとに分かれているのが理想です。
pg_dumpのオプションでなんとかなればいいのですが、3流エンジニアにはドキュメントを理解する能力がありませんでした。。。
ということで仕方ないので、例によってpythonスクリプトでダンプするためのShellスクリプトを作成し、実行することでテーブルごとにダンプファイルを作成するための簡単なものを作成しました。


python

db_name = 'db_name' 
output_path = './db_dump'
gzip = False

import subprocess
import os
os.mkdir(output_path)

if gzip:
  out_cmd = 'gzip --best -c'
  ext = '.dmp.gz'
else:
  out_cmd = 'cat'
  ext = '.dmp'

cmd = ['echo', r'\dt']
sp = subprocess.Popen(cmd, stdout=subprocess.PIPE)
cmd = ['psql', db_name, '-t']
tp = subprocess.Popen(cmd, stdin=sp.stdout, stdout=subprocess.PIPE)

open('pg_dump.sh', 'wb').write('') # initialyze
for line in tp.stdout:
  if not line.strip():  continue
  table_name = line.split('|')[1].strip()
  cmd_line = 'pg_dump {0} -t {2} | {3} > {1}/{0}_{2}{4}\n'.format(
                                                              db_name,
                                                              output_path,
                                                              table_name,
                                                              out_cmd,
                                                              ext
                                                            )
  open('pg_dump.sh', 'ab').write(cmd_line)

exit()

# The shell script from here.
sh -x ./pg_dump.sh

確認環境
Python 2.7.3
PostgreSQL 9.3.0

検索タグ
python, postgresql

拍手

PR
この記事にコメントする
Name
Title
Color
Mail
URL
Comment
Password   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
secret (管理人にのみ表示)
Copyright ©  アナログを愛するデジタル生活館 All Rights Reserved.
* material by Pearl Box   * Template by tsukika

忍者ブログ [PR]