バックアップ方法は3種類ある。
(1).SQLによるバックアップ
(2).ファイルシステムレベルのバックアップ
(3).オンラインバックアップ
まずは(1).SQLによるバックアップを試してみる。
$ pg_dump -U username -f ~/backup_file.txt dbname
これだけ。非常にスマートにバックアップ完了。
問題は、リストア。
まず、移動先のデータベースを作成する。
$ createdb -T template_postgis -U postgres -O hoge hogedb
PostGIS付きのデータベースだ。
で、リストアする。
$ psql hogedb > backup_file_name
これではinvalidエラーが多発。
テーブル権限の問題みたい。ユーザー権限を見てみると
testdb=# select * from pg_user;
usename | usesysid | usecreatedb | usesuper | usecatupd | passwd | valuntil | useconfig
----------+----------+-------------+----------+-----------+----------+----------+-----------
postgres | 10 | t | t | t | ******** | |
hoge | 16386 | t | t | t | ******** | |
hogeは全てOK
ロール権限は?
hogedb=# select * from pg_roles;
rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcatupdate | rolcanlogin | rolconnlimit | rolpassword | rolvaliduntil | rolconfig | oid
----------+----------+------------+---------------+-------------+--------------+-------------+--------------+-------------+---------------+-----------+-------
postgres | t | t | t | t | t | t | -1 | ******** | | | 10
つまり、postgresでデータベースを作るとなんでもOKというわけ。
いろいろ試行錯誤したが、結局権限とか関係なくてまったく空のDBを作成する必要があった。
よって正解は以下のとおり。
$ dropdb hogedb (いったんDBを消す)
$ createdb -U postgres -O ownername hogedb (空のDBを作成)
$ psql -U username hogedb < ~/backup_file.txt (リストア)
さらにエラーが。。textsearch-jaを入れているがこれのせいでエラーが出てる。
調べてみると、textsearch-jaを使っている場合、
バックアップ前にいったん削除しないといけないようだ。
★バックアップの前にtextsearch-jaをアンインストールする
$ psql -U hoge -d hogedb -f "/usr/share/postgresql/8.4/contrib/textsearch_ja-8.4.2/uninstall_textsearch_ja.sql"
$ psql -U hoge -d hogedb -f "/usr/share/postgresql/8.4/contrib/uninstall_tsearch2.sql"
★textsearch-jaをアンインストールしたらデータベースのバックアップ開始
$ pg_dump -U hoge -f ~/hogedb_20100823.back uki2db
★バックアップとったらtextsearch-jaを入れ直す。
$ psql -U hoge -d hogedb -f "/usr/share/postgresql/8.4/contrib/tsearch2.sql"
$ psql -U hoge -d hogedb -f "/usr/local/pgsql/share/contrib/textsearch_ja.sql"
★リストア先にて
既存のデータベースをクリア&クリエイト
$ dropdb hogedb
$ createdb -U postgres -O hoge hogedb
★textsearch-jaを先に入れる。
$ psql -U hoge -d hogedb -f "/usr/share/postgresql/8.4/contrib/tsearch2.sql"
$ psql -U hoge -d hogedb -f "/usr/local/pgsql/share/contrib/textsearch_ja.sql"
★リストア開始
$ psql -U hoge hogedb < ~/hogedb_20100823.back
※参考資料:ココ
バックアップスクリプト
(例)
#!/bin/sh
# 日付文字列
DATE=`date "+%Y%m%d"`
# パスの設定
PATH=/usr/local/sbin:/usr/bin:/bin
# バックアップDB
DB_NAME=hogedb
# バックアップ先ディレクトリ
BKUP_DIR=/vol/backup/pgsql
# hogedbデータベースバックアップ
psql -U hoge -d hogedb -f "/usr/local/pgsql/share/contrib/uninstall_textsearch_ja.sql"
psql -U hoge -d hogedb -f "/usr/share/postgresql/8.4/contrib/uninstall_tsearch2.sql"
pg_dump -U hoge -f $BKUP_DIR/$DB_NAME\_$DATE hogedb
psql -U hoge -d hogedb -f "/usr/share/postgresql/8.4/contrib/tsearch2.sql"
psql -U hoge -d hogedb -f "/usr/local/pgsql/share/contrib/textsearch_ja.sql"
スケジューリング(crontabを使う)
毎日午前0:05にバックアップ
$ crontab -u postgres -e
# m h dom mon dow command<
5 0 * * * /var/django/script/pgsql-backup.sh
参考サイト
ec2サーバーでcrontabが動かない。
chkconfig --list cronでみると
cron 0:off 1:off 2:off 3:off 4:off 5:off 6:off
確かに動いてない。
$ chkconfig cron on
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'cron' missing LSB tags and overrides
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
......
とエラーになる。
service cron startすると
cron start/running, process 2442
で動き出した。
しかし相変わらず
chkconfig --list cronでみると
cron 0:off 1:off 2:off 3:off 4:off 5:off 6:off
だ。。原因不明。
参考サイト