简体   繁体   English

Postgres中的WAL归档不起作用

[英]WAL archiving in postgres is not working

I have enabled WAL archiving in postgres configuration file. 我已在postgres配置文件中启用WAL归档。 When I restarted Postgres service WAL recovery is not working. 重新启动Postgres服务时,WAL恢复无法正常工作。 There was no wal recovery entries in logs. 日志中没有wal恢复条目。

Steps I followed: 我遵循的步骤:

Created directory for wal:
mkdir -p  /var/lib/pgsql/wals/
mkdir -p  /var/lib/pgsql/backups/
chown postgres:postgres -R /var/lib/pgsql/backups/
chown postgres:postgres -R /var/lib/pgsql/wals/

Edited the postgresql.conf with the below changes:
wal_level=archive
archive_mode=on
archive_command = 'test ! -f /var/lib/pgsql/wals/%f && cp %p /var/lib/pgsql/wals/%f'

sudo service postgresql restart 10
sudo su - postgres
pg_basebackup -D /var/lib/pgsql/data #created base backup
tar -C /var/lib/pgsql/data/ -czvf /var/lib/pgsql/backups/pg_basebackup_backup.tar.gz .

Deleted two rows of data in my database and stopped the postgres service 删除数据库中的两行数据并停止了postgres服务

sudo service postgresql stop 10

Extracted the Basebackup 提取基本备份

tar xvf /var/lib/pgsql/backups/pg_basebackup_backup.tar.gz -C /var/lib/pgsql/data

Created recovery.conf with the below content and restarted postgres service 使用以下内容创建了recovery.conf并重新启动了postgres服务

echo "restore_command = 'cp /var/lib/pgsql/wals/%f %p'">/var/lib/pgsql/recovery.conf
cp /var/lib/pgsql/recovery.conf /var/lib/pgsql/data/
sudo service postgresql stop 10
sudo service postgresql start 10

There was no wal recovery entries in logs and the two rows which I deleted didn't get restored. 日志中没有沃尔玛恢复条目,删除的两行也没有恢复。

pg_basebackup can catch all WAL segments during backup. pg_basebackup可以在备份期间捕获所有WAL段。 I use basebackup into tar format with "-X stream" option and all works well. 我使用带有“ -X流”选项的basebackup转换为tar格式,并且一切正常。 See here - pg_basebackup – bash script for backup and archiving on Google storage It works excellent - I backup database 4.5+ TB big which takes almost 2 days. 请参阅此处-pg_basebackup-用于在Google存储上进行备份和归档的bash脚本效果非常好-我备份了4.5+ TB的数据库,需要大约2天的时间。

Restoration is described here - pg_basebackup / pg-barman – restore tar backup 这里描述了恢复-pg_basebackup / pg-barman –恢复tar备份

All works - we already had incidents when we had to restore from these backups. 所有工作-当我们不得不从这些备份中还原时,我们已经遇到了麻烦。

The WAL containing the two deleted rows probably hasn't been archived yet. 包含两行删除的WAL可能尚未归档。

archive_command is run whenever a 16 MB WAL segment is full or something forces a log switch. 每当16 MB WAL段已满或由于某种原因迫使日志切换时,都会运行archive_command

To be able to recover the latest changes that have not been archived yet, you have to copy the contents of pg_wal to the pg_wal directory in the restored base backup. 为了能够恢复尚未归档的最新更改,必须将pg_wal的内容复制到已还原的基本备份中的pg_wal目录中。

You can also consider streaming replication or pg_receivewal if you cannot afford to lose transactions. 如果您承受不起丢失事务的pg_receivewal ,也可以考虑使用流复制或pg_receivewal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM