最初に行うこと
-
まずリカバリ要件を明確に
- 目標復旧時間(RTO: Recovery Time Objective)
- 目標復旧時点(RPO: Recovery Point Objective)
- 要件に見合った方式を用いた計画を作成する
PostgreSQLのバックアップ方式
オフラインバックアップ
-
物理バックアップ
- 公式
- 「コールドバックアップ」とも
-
ファイルシステムレベルでバックアップするやつ
- データベースクラスタディレクトリをtarとかで固める
- テーブルスペースも忘れずに
- 復旧時点はバックアップ取得時点まで
オンラインバックアップ
-
論理バックアップ
- 公式
pg_dump
やpg_dumpall
でバックアップ取得するやつpg_restore
やpsql -f
で流し込む- 復旧時点はバックアップ取得時点まで
-
物理バックアップ
- 公式
- 復旧時点は、ベースバックアップ取得時点以降、任意の時点まで(PITR)
- 手順が煩雑
主なリカバリ要件/バックアップ要件
- RTOとRPOを整理する
要件と方式の整理方法
-
RPO
-
問題発生直前まで復旧する必要あり
- => オンライン物理バックアップ(PITR)
-
バックアップ取得時点まででいい
- => ほかの (復旧手順が簡潔)
-
-
RTO
-
サービス停止可能
- => オフライン物理バックアップ
-
とめちゃだめ
- => オンライン論理バックアップ
-
各バックアップ方式の注意点
コールドバックアップの注意点
postgres=# CREATE TABLESPACE myspc LOCATION '/var/lib/postgresql/data2';
CREATE TABLESPACE
postgres=# \db+
List of tablespaces
Name | Owner | Location | Access privileges | Options | Size | Description
------------+----------+---------------------------+-------------------+---------+---------+-------------
myspc | postgres | /var/lib/postgresql/data2 | | | 0 bytes |
pg_default | postgres | | | | 23 MB |
pg_global | postgres | | | | 623 kB |
(3 rows)
- データベースクラスタ直下の
pg_tblspc/
ディレクトリには、ユーザ定義テーブルスペースへのsymlinkが格納されている
postgres@bdbad7709a38:~/data$ ls -lA pg_tblspc
total 0
lrwxrwxrwx 1 postgres postgres 25 Feb 2 06:18 16385 -> /var/lib/postgresql/data2
-
これの実体も忘れずにバックアップしよう
-
やりがちなミス:
- symlinkだけバックアップしてしまう
- symlinkの格納場所にデータ実体をコピーしてしまう
-
-
pg_wal/
のバックアップにも気をつけるinitdb -X
でWAL格納先を指定していた場合、データベースクラスタ直下のpg_wal/
はsymlinkになる
postgres@bdbad7709a38:~/data$ initdb -D /home/postgres/data -X /var/lib/postgresql/data3/pg_wal
initdb -D /home/postgres/data -X /var/lib/postgresql/data3/pg_wal
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
creating directory /home/postgres/data ... ok
fixing permissions on existing directory /var/lib/postgresql/data3/pg_wal ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /home/postgres/data -l logfile start
/docker:bdbad7709a38:/home/postgres/data $ ls -ld pg_wal
lrwxrwxrwx 1 postgres postgres 32 2020-02-02 15:27 pg_wal -> /var/lib/postgresql/data3/pg_wal
- これも実体をちゃんとバックアップすること
オンライン論理バックアップの注意点
-
バックアップのフォーマットをどうする
- c: custom
- d: directory
- t: tarball
-
p: plaintext (デフォルト)
- SQL文
- これだけ、リカバリ時は
psql -f
で流し込む
-
何をバックアップする
-
pg_dump
- データベース・テーブル単位
-
逆に、グローバルなものはバックアップできない
- Roleとか
-
pg_dumpall
- グローバルデータのバックアップ用
- Format指定不可 (plaintextのみ)
- ともにテーブルスペースやラージオブジェクトもバックアップ可能
-
【補】巨大なデータベースのダンプ
- 公式
- splitコマンド使え
pg_dump dbname | split -b 1m - filename
- gzipとかで圧縮しろ
- PostgreSQLカスタム形式使え
pg_dump -Fc dbname > filename
-
カスタム形式の特徴
- zlib圧縮
- gzipと同じくらいの圧縮率
- gzipと比べて、テーブルを選択的にリストアできるという利点がある
-
【補】ソースコードからPostgreSQLをビルドするとき、
--without-zlib
でzlib圧縮を無効化できる- 当該機能(
pg_dump
,pg_restore
)は当然使えなくなる
- 当該機能(
- 並列処理しろ
pg_dump -j num -F d -f out.dir dbname
オンライン物理バックアップの注意点
- WAL領域のデータも確実にバックアップすること
-
最新のベースバックアップ取得時点よりも前への復旧が必要か?
- 必要ならば、より古いWALアーカイブファイルも必要
-
不要ならば、古いWALアーカイブファイルは不要
- 最新のものだけあればよい
- PostgreSQLは古いWALアーカイブファイルを自動的には削除してくれない
- 定期的に削除する運用が必要となる
-
pg_archivecleanup
コマンド- 公式
- PostgreSQL 9.5~
- 不要なWALアーカイブファイルを特定/削除できるやつ
pg_archivecleanup removes older WAL files from PostgreSQL archives.
Usage:
pg_archivecleanup [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILE
Options:
-d generate debug output (verbose mode)
-n dry run, show the names of the files that would be removed
-V, --version output version information, then exit
-x EXT clean up files if they have this extension
-?, --help show this help, then exit
For use as archive_cleanup_command in postgresql.conf:
archive_cleanup_command = 'pg_archivecleanup [OPTION]... ARCHIVELOCATION %r'
e.g.
archive_cleanup_command = 'pg_archivecleanup /mnt/server/archiverdir %r'
Or for use as a standalone archive cleaner:
e.g.
pg_archivecleanup /mnt/server/archiverdir 000000010000000000000010.00000020.backup
Report bugs to <pgsql-bugs@lists.postgresql.org>.