gentoo に pg_rman を導入する

ちょっとあんまり自信がないので、備忘として残す意味で……

  • postgresql のバージョンは 9.5
  • postgresql のデータディレクトリは /var/lib/postgresql/9.5/data
  • バックアップ先は /mnt/backup/postgresql

まず pg_rman に対応できる postgresql 本体を準備します。

pg_rman をビルドするには、postgresql が USE=“static-libs” で emerge されている必要があります。

# USE="static-libs" emerge -av postgresql

These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild   R    ] dev-db/postgresql-9.5.7:9.5::gentoo  USE="nls pam readline (selinux) server ssl *static-libs xml zlib -doc -kerberos -ldap (-libressl) -perl -pg_legacytimestamp -python -tcl -threads -uuid" LINGUAS="-af -cs -de -en -es -fa -fr -hr -hu -it -ko -nb -pl -pt_BR -ro -ru -sk -sl -sv -tr -zh_CN -zh_TW" PYTHON_SINGLE_TARGET="python3_4 -python2_7 (-python3_5) (-python3_6)" PYTHON_TARGETS="python2_7 python3_4 (-python3_5) (-python3_6)" 0 KiB

Total: 1 package (1 reinstall), Size of downloads: 0 KiB

Would you like to merge these packages? [Yes/No] y

あんまりよくわかってないんですが、/etc/postgresql-9.5/postgresql.conf に以下の3行を設定しました。

wal_level = archive
archive_mode = on
archive_command = 'test ! -f /mnt/backup/postgresql/arclog/%f && cp %p /mnt/backup/postgresql/arclog/%f

archive_command に設定するパスは前提で記述したバックアップ先のディレクトリに arclog というディレクトリを掘ることにしています。

導入先のディレクトリを作ります。今回は /home/tools/pg_rman とします。

# mkdir /home/tools/pg_rman

git clone でソースコードを落としてくるわけですが、-b <branch> の部分は使用している postgresql のバージョンに合わせて適切なものを指定します。

/home/tools $ git clone -b REL9_5_STABLE https://github.com/ossc-db/pg_rman.git /home/tools/pg_rman
Cloning into '/home/tools/pg_rman'...
remote: Counting objects: 3366, done.
remote: Compressing objects: 100% (93/93), done.
remote: Total 3366 (delta 76), reused 38 (delta 15), pack-reused 3258
Receiving objects: 100% (3366/3366), 1.22 MiB | 0 bytes/s, done.
Resolving deltas: 100% (2481/2481), done.

ソースコードを引っ張ってきたら make します。

# cd /home/tools/pg_rman
# make

ちなみにこの時、最初に言及したとおり postgresql が USE="static-libs" で emerge されていないと、以下のようなエラーが出てビルドがコケます。

  :
  :
/usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lpgcommon
/usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lpgport
collect2: error: ld returned 1 exit status

pg_rman init で初期化します。-B はバックアップ先のディレクトリ、-D はバックアップ対象の postgresql のデータディレクトリです。

# /home/tools/pg_rman/pg_rman init -B /mnt/backup/postgresql -D /var/lib/postgresql/9.5/data
INFO: ARCLOG_PATH is set to '/mnt/backup/postgresql/arclog'
INFO: SRVLOG_PATH is set to '/var/log/postgresql'

完了するとバックアップ先のディレクトリになにやらファイルができています。

# ls -l /mnt/backup/postgresql/
total 16
drwx------. 4 root root 4096 Jun 27 17:54 backup
-rw-r--r--. 1 root root   79 Jun 27 17:54 pg_rman.ini
-rw-r--r--. 1 root root   40 Jun 27 17:54 system_identifier
drwx------. 2 root root 4096 Jun 27 17:54 timeline_history

このなかで pg_rman.ini はデフォルトの設定を保持しておくものなので、pg_rman 1.3 あたりを参考に設定しておくと良いかと思います。

ARCLOG_PATH='/mnt/backup/postgresql/arclog'
SRVLOG_PATH='/var/log/postgres'

COMPRESS_DATA = YES
KEEP_ARCLOG_FILES = 10
KEEP_DATA_GENERATIONS = 3
KEEP_SRVLOG_FILES = 10

それと、ここで postgresql.conf の archive_command に指定したディレクトリを作成して、ついでに /mnt/backup/postgresql の owner を変更しておきます。

# mkdir /mnt/backup/postgresql/arclog
# chown -R postgres:postgres /mnt/backup/postgresql

まずはフルバックアップを取ってみます。

# export BACKUP_PATH=/mnt/backup/postgresql
# export PGDATA=/var/lib/postgresql/9.5/data
# /home/tools/pg_rman/pg_rman backup --username=postgres --dbname=postgres --backup-mode=full
INFO: copying database files
INFO: copying archived WAL files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
INFO: start deleting old archived WAL files from ARCLOG_PATH (keep files = 10)
INFO: start deleting old backup (keep generations = 3)
INFO: does not include the backup just taken

とくにエラーが出ていないようですね。

状態を確認してみます。

# /home/tools/pg_rman/pg_rman show
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status
=====================================================================
2017-06-27 18:01:41  2017-06-27 18:01:44  FULL  3491kB     3  DONE

StatusDONE となっているのはどうやら「バックアップを取るには取ったけど、まだ検証が済んでいないよ。」という状態のようです。

では、検証してみます。

# /home/tools/pg_rman/pg_rman validate
INFO: validate: "2017-06-27 18:01:41" backup and archive log files by CRC
INFO: backup "2017-06-27 18:01:41" is valid

……と、検証を済ませたらもう一度状態を確認してみます。

# /home/tools/pg_rman/pg_rman show
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status
=====================================================================
2017-06-27 18:01:41  2017-06-27 18:01:44  FULL  3491kB     3  OK

StatusOK になりました。どうやらこれをやって OK にしておかないとリストアやら、このフルバックアップをベースにした増分バックアップやらができないみたいです。なので、必須手順ですね。

こんどは増分バックアップを取ってみます。

# /home/tools/pg_rman/pg_rman backup --username=postgres --dbname=postgres --backup-mode=incremental
INFO: copying database files
INFO: copying archived WAL files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
INFO: start deleting old archived WAL files from ARCLOG_PATH (keep files = 10)
INFO: start deleting old backup (keep generations = 3)
INFO: does not include the backup just taken
INFO: backup "2017-06-27 18:01:41" should be kept
DETAIL: This is the 1st latest full backup.

増分バックアップを取った後の状態を確認してみます。

# /home/tools/pg_rman/pg_rman show
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status
=====================================================================
2017-06-27 18:07:52  2017-06-27 18:07:54  INCR    54kB     3  DONE
2017-06-27 18:01:41  2017-06-27 18:01:44  FULL  3491kB     3  OK

また StatusDONE になっているのでさっきと同じく検証してあげます。

# /home/tools/pg_rman/pg_rman validate

INFO: validate: "2017-06-27 18:07:52" backup and archive log files by CRC
INFO: backup "2017-06-27 18:07:52" is valid
# /home/tools/pg_rman/pg_rman show
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status
=====================================================================
2017-06-27 18:07:52  2017-06-27 18:07:54  INCR    54kB     3  OK
2017-06-27 18:01:41  2017-06-27 18:01:44  FULL  3491kB     3  OK

大丈夫そうです。

バックアップは戻すためにあるので……リストア検証すっ飛ばして痛い目見るのは定番ですねw

まずは postgresql を停止します。

# run_init /etc/init.d/postgresql-9.5 stop
Authenticating root.
Password:
 * Stopping PostgreSQL 9.5 (this can take up to 92 seconds) ...           [ ok ]

つづいてリストアです。

# /home/tools/pg_rman/pg_rman restore --username=postgres
INFO: the recovery target timeline ID is not given
INFO: use timeline ID of current database cluster as recovery target: 3
INFO: calculating timeline branches to be used to recovery target point
INFO: searching latest full backup which can be used as restore start point
INFO: found the full backup can be used as base in recovery: "2017-06-27 18:01:41"
INFO: copying online WAL files and server log files
INFO: clearing restore destination
INFO: validate: "2017-06-27 18:01:41" backup and archive log files by SIZE
INFO: backup "2017-06-27 18:01:41" is valid
INFO: restoring database files from the full mode backup "2017-06-27 18:01:41"
INFO: searching incremental backup to be restored
INFO: validate: "2017-06-27 18:07:52" backup and archive log files by SIZE
INFO: backup "2017-06-27 18:07:52" is valid
INFO: restoring database files from the incremental mode backup "2017-06-27 18:07:52"
INFO: searching backup which contained archived WAL files to be restored
INFO: backup "2017-06-27 18:07:52" is valid
INFO: restoring WAL files from backup "2017-06-27 18:07:52"
INFO: restoring online WAL files and server log files
INFO: generating recovery.conf
INFO: restore complete
HINT: Recovery will start automatically when the PostgreSQL server is started.

おぉぉ。

postgresql を起動してデータの状態を確認しましょう。

 * /etc/postgresql-9.5/postgresql.conf: correcting owner
 * /etc/postgresql-9.5/pg_hba.conf: correcting owner
 * /etc/postgresql-9.5/pg_ident.conf: correcting owner
 * /run/postgresql: correcting mode
 * Starting PostgreSQL 9.5 ...
LOG:  skipping missing configuration file "/var/lib/postgresql/9.5/data/postgresql.auto.conf"
/bin/sh: /var/lib/postgresql/9.5/data/postmaster.log: Permission denied
pg_ctl: could not start server
Examine the log output.
 * Check the log for a possible explanation of the above error.
 * The log may be located at:
 *     /var/lib/postgresql/9.5/data/postmaster.log
 * Or wherever you configured PostgreSQL 9.5 to log.                      [ !! ]
 * ERROR: postgresql-9.5 failed to start

……コケました。Permission denied とかいってますね。

 # ls -l /var/lib/postgresql/9.5/data/
total 132
-rw-------. 1 root root     4 Jun 27 18:13 PG_VERSION
-rw-------. 1 root root   222 Jun 27 18:13 backup_label
-rw-------. 1 root root   222 Jun 27 18:13 backup_label.old
drwx------. 6 root root  4096 Jun 27 18:13 base
drwx------. 2 root root  4096 Jun 27 18:13 global
drwx------. 2 root root  4096 Jun 27 18:13 pg_clog
drwx------. 2 root root  4096 Jun 27 18:13 pg_commit_ts
drwx------. 2 root root  4096 Jun 27 18:13 pg_dynshmem
lrwxrwxrwx. 1 root root    31 Jun 27 18:13 pg_hba.conf -> /etc/postgresql-9.5/pg_hba.conf
lrwxrwxrwx. 1 root root    33 Jun 27 18:13 pg_ident.conf -> /etc/postgresql-9.5/pg_ident.conf
drwx------. 4 root root  4096 Jun 27 18:13 pg_logical
drwx------. 4 root root  4096 Jun 27 18:13 pg_multixact
drwx------. 2 root root  4096 Jun 27 18:13 pg_notify
drwx------. 2 root root  4096 Jun 27 18:13 pg_replslot
drwx------. 2 root root  4096 Jun 27 18:13 pg_serial
drwx------. 2 root root  4096 Jun 27 18:13 pg_snapshots
drwx------. 2 root root  4096 Jun 27 18:13 pg_stat
drwx------. 2 root root  4096 Jun 27 18:13 pg_stat_tmp
drwx------. 2 root root  4096 Jun 27 18:13 pg_subtrans
drwx------. 2 root root  4096 Jun 27 18:13 pg_tblspc
drwx------. 2 root root  4096 Jun 27 18:13 pg_twophase
drwx------. 3 root root  4096 Jun 27 18:13 pg_xlog
-rw-------. 1 root root    88 Jun 27 18:13 postgresql.auto.conf
lrwxrwxrwx. 1 root root    35 Jun 27 18:13 postgresql.conf -> /etc/postgresql-9.5/postgresql.conf
-rw-------. 1 root root 35912 Jun 27 18:13 postmaster.log
-rw-------. 1 root root   158 Jun 27 18:13 postmaster.opts
-rw-r--r--. 1 root root   133 Jun 27 18:13 recovery.conf
-rw-r--r--. 1 root root   133 Jun 27 18:13 recovery.done

全部 root 持ちになってますねw

# chown -R postgres:postgres /var/lib/postgresql/9.5/data/*

……いいのだろうかこれで。

# run_init /etc/init.d/postgresql-9.5 start
Authenticating root.
Password:
 * /etc/postgresql-9.5/postgresql.conf: correcting owner
 * /etc/postgresql-9.5/pg_hba.conf: correcting owner
 * /etc/postgresql-9.5/pg_ident.conf: correcting owner
 * /run/postgresql: correcting mode
 * Starting PostgreSQL 9.5 ...

とりあえず起動しました。データも問題なさそうですが……ん~リストアすると全データの owner がリストア実行ユーザーになってしまうのはいいのだろうか……。

  • wiki/linux/install_pg_rman_on_gentoo
  • 最終更新: 2019/02/17 15:59