PostgreSQL 15 で pg_basebackup したデータの復旧テスト

pg_basebackupしたデータからの復旧テスト

バックアップしたデータ

base.tar.gz
backup_manifest
pg_wal.tar.gz
package_versions
  • package_versions は dpkg -l |grep postgres を記録したもの。
  • 残り3つは pg_basebackup が出力したファイル。

package_versionsの内容

rc  pgdg-keyring                          2018.2                                  all          keyring for apt.postgresql.org
ii  postgresql-15                         15.1-1.pgdg22.04+1                      amd64        The World's Most Advanced Open Source Relational Database
ii  postgresql-client                     15+246.pgdg22.04+1                      all          front-end programs for PostgreSQL (supported version)
ii  postgresql-client-15                  15.1-1.pgdg22.04+1                      amd64        front-end programs for PostgreSQL 15
ii  postgresql-client-common              246.pgdg22.04+1                         all          manager for multiple PostgreSQL client versions
ii  postgresql-common                     246.pgdg22.04+1                         all          PostgreSQL database-cluster manager
ii  postgresql-contrib                    15+246.pgdg22.04+1                      all          additional facilities for PostgreSQL (supported version)
ii  postgresql-doc                        15+246.pgdg22.04+1                      all          documentation for the PostgreSQL database management system
ii  postgresql-doc-15                     15.1-1.pgdg22.04+1                      all          documentation for the PostgreSQL database management system

dockerコンテナで復旧テスト

コンテナの起動

$ docker run -v /x/backup/postgres/20230121-050015:/backup:ro --rm -it ubuntu:22.04 bash

OSバージョンの確認

# cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.1 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

必要そうなパッケージのインストール

apt update
apt install whiptail locales lv nano sudo curl ca-certificates gnupg lsb-release

ロケールの作成

  • ubuntuコンテナだと en_us.UTF-8 がない。
  • DBデータ復旧後の起動でログに問題が報告されるので、用意する。
apt update && apt install whiptail locales
dpkg-reconfigure locales

バックアップデータの確認とWALアーカイブの展開

  • dockerコンテナ起動時にバインドマウントしたフォルダ
  • WALアーカイブは適当にフォルダを作って展開しておく
  • パーミッションをDBが読めるものにする
# export BACKUP_DIR=/backup
# find $BACKUP_DIR -ls
# mkdir /walArchive
# cd /walArchive
# tar xzf $BACKUP_DIR/pg_wal.tar.gz
# chmod -R a+rw .
# find . -ls

Postgres リポジトリの導入

# curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor > /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg
# echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
# cat /etc/apt/sources.list.d/pgdg.list

バージョンを指定してインストール

apt update
apt-get install postgresql-15=15.1-1.pgdg22.04+1 postgresql-client-15=15.1-1.pgdg22.04+1

インストール中にタイムゾーンを尋ねられる。今回は Asia/Tokyo を指定した。

DB起動/接続のテスト

# service --status-all
# service postgresql start
# service --status-all
(postgresサービスが起動していること)
# ps auxwf
(プロセス一覧にpostgresが見えること)
# lv /var/log/postgresql/postgresql-15-main.log
2023-01-21 10:49:52.237 JST [6265] LOG:  starting PostgreSQL 15.1 (Ubuntu 15.1-1.pgdg22.04+1) on x86_64-pc-linux-gnu,
 compiled by gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0, 64-bit
2023-01-21 10:49:52.237 JST [6265] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2023-01-21 10:49:52.237 JST [6265] LOG:  could not bind IPv6 address "::1": Cannot assign requested address
2023-01-21 10:49:52.242 JST [6265] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-01-21 10:49:52.249 JST [6268] LOG:  database system was shut down at 2023-01-21 10:49:23 JST
2023-01-21 10:49:52.254 JST [6265] LOG:  database system is ready to accept connections

接続テスト

# sudo -u postgres psql -c "\\l"
                                                 List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
(3 rows)

DBを止める

# service postgresql stop
# ps auxwf
(postgresのプロセスが存在しないこと)
# lv /var/log/postgresql/postgresql-15-main.log
2023-01-21 10:53:47.786 JST [6265] LOG:  received fast shutdown request
2023-01-21 10:53:47.796 JST [6265] LOG:  aborting any active transactions
2023-01-21 10:53:47.798 JST [6265] LOG:  background worker "logical replication launcher" (PID 6271) exited with exit
 code 1
2023-01-21 10:53:47.798 JST [6266] LOG:  shutting down
2023-01-21 10:53:47.801 JST [6266] LOG:  checkpoint starting: shutdown immediate
2023-01-21 10:53:47.830 JST [6266] LOG:  checkpoint complete: wrote 43 buffers (0.3%); 0 WAL file(s) added, 0 removed
, 0 recycled; write=0.006 s, sync=0.012 s, total=0.033 s; sync files=11, longest=0.003 s, average=0.002 s; distance=2
52 kB, estimate=252 kB
2023-01-21 10:53:47.834 JST [6265] LOG:  database system is shut down

データ破損

  • DBは止めておく
cd /var/lib/postgresql/15/main/
rm -fr ./*

データの復旧

cd /var/lib/postgresql/15/main/
tar xvfz $BACKUP_DIR/base.tar.gz

restore_command の確認と設定

# grep -rin restore /etc/postgresql/15/main/ 
./postgresql.conf:268:#restore_command = ''             # command to use to restore an archived logfile segment
./postgresql.conf:269:                          # placeholders: %p = path of file to restore
./postgresql.conf:282:#recovery_target_name = ''        # the named restore point to which recovery will proceed

設定されてないので設定する。

nano /etc/postgresql/15/main/postgresql.conf

CTRL-W を押して restore_command を入力してEnter。 既存の行は変えず、次の行を挿入。

restore_command = 'cp /walArchive/%f %p'
archive_cleanup_command = 'pg_archivecleanup /walArchive %r'

CTRL-Oを押してファイル名をかえずにEnterして保存。 CTRL-Xで終了。

起動

service postgresql start

ログの確認

lv /var/log/postgresql/postgresql-15-main.log
(snip)
2023-01-21 10:57:04.170 JST [6401] LOG:  restored log file "00000001000000770000003B" from archive
2023-01-21 10:57:04.211 JST [6401] LOG:  redo starts at 77/3B000028
(snip)

データを読めているか

# sudo -u postgres psql -c "\\l"
                                                  List of databases
   Name    |   Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges
-----------+-----------+----------+-------------+-------------+------------+-----------------+-----------------------
 lemmy     | lemmy     | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 mastodon2 | mastodon2 | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 mastodon3 | mastodon3 | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 matrix1   | matrix1   | UTF8     | C           | C           |            | libc            |
 misskey11 | misskey11 | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 misskey12 | misskey12 | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 postgres  | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 template0 | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |           |          |             |             |            |                 | postgres=CTc/postgres
 template1 | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |           |          |             |             |            |                 | postgres=CTc/postgres
(9 rows)

この記事に含まれないもの

  • /etc/postgresql/15/main/ の復旧。 多分 /etc まるごとバックアップしてると思うので…