…を行うスクリプトの例。
前準備
WALのarchiveを有効にしてarchive_command を設定しないと pg_stop_backup() がずっとブロックする。
WALアーカイブの出力先を作ってpostgresユーザがアクセスできるようにしておく。
# mkdir -p /xxx/backup/postgres-wal
# chown -R postgres /xxx/backup/postgres-wal
postgresql.confにWALアーカイブを設定してDBを再起動する。
archive_mode = on
archive_command = 'cp %p /xxx/backup/postgres-wal/%f'
バックアップスクリプト
#!/bin/bash
set -eu
function die(){
echo $@ >&2
exit 1
}
test ${EUID:-${UID}} = 0 || die "must be root."
echo "chdir to /xxx/backup/postgres ..."
cd /xxx/backup/postgres
LABEL=`date "+%Y%m%d-%H%M%S" | sed -e "s/[\r\n]\+//g"`
echo "create folder ..."
mkdir -p clone
echo "call pg_start_backup('$LABEL') ..."
psql -h din -U postgres postgres -c "SELECT pg_start_backup('$LABEL');"
echo "copy db data to folder $LABEL ..."
# rsync の際にエラー24が出ることがあるが、無害。
set +e
rsync -a --delete --exclude=pg_xlog --exclude=postmaster.pid \
/var/lib/postgresql/13/main/* clone
set -e
echo "call pg_stop_backup() ..."
psql -h din -U postgres postgres -c "SELECT pg_stop_backup();"
echo "make $LABEL.tgz ..."
tar cpzf $LABEL.tgz clone
You must log in or register to comment.