pg_rman备份及恢复【转】
前言
pg_rman是一款开源的备份恢复软件,支持在线和基于PITR的备份恢复方式
1.pg_rman介绍
pg_rman是一款开源的备份恢复软件,
支持在线和基于PITR的备份恢复方式。
pg_rman类似于oracle的rman,
可以进行全量、增量、归档日志的备份。
pg_rman的用法非常简单,支持以下几种运行模式
2.安装部署
https://github.com/ossc-db/pg_rman/releases/tag/V1.3.16
1、需要根据PG Server的版本,下载安装不同的版本。
2、需要提前使用编译安装的方式来安装PG Server
cp pg_rman-1.3.16-pg15.tar.gz /home/postgres
chown postgres:postgres -R /home/postgres/pg_rman-1.3.16-pg15.tar.gz
chmod +x /home/postgres/pg_rman-1.3.16-pg15.tar.gz
su - postgres
tar -zxvf pg_rman-1.3.16-pg15.tar.gz
cd pg_rman-1.3.16-pg15/
make && make install
[postgres@centos79 ~]$ which pg_rman
/pgccc/pgsql-15/bin/pg_rman
3.PG开启归档
mkdir -p /home/postgres/archive_dir
chown -R postgres.postgres /home/postgres/archive_dir
su - postgres
cat >> $PGDATA/postgresql.conf <<"EOF"
wal_level='replica'
archive_mode='on'
archive_command='test ! -f /home/postgres/archive_dir/%f && cp %p /home/postgres/archive_dir/%f'
restore_command='cp /home/postgres/archive_dir/%f %p'
max_wal_senders=10
EOF
--重启PG
pg_ctl restart
--登陆PG
psql -U postgres -h 192.168.6.20 -p 5432
select name,setting from pg_settings where name in ('wal_level','archive_mode','archive_command');
-- 切换归档
select pg_switch_wal();
4.物理备份
4.1 初始化
初始化,实际上就是需要一个目录,这个目录将用于存放备份的文件
[postgres@centos79 ~]$ pg_rman init -B /home/postgres/backup
4.2 全量备份
pg_rman backup --backup-mode=full -B /home/postgres/backup
pg_rman show -B /home/postgres/backup
pg_rman validate -B /home/postgres/backup
4.3 增量备份
pg_rman backup --backup-mode=incremental -B /home/postgres/backup
pg_rman show -B /home/postgres/backup
pg_rman validate -B /home/postgres/backup
5.备份恢复
5.1 不完全恢复
--不完全恢复
pg_ctl stop
rm -rf /pgccc/pgdata/*
pg_rman restore -B /home/postgres/backup \
--recovery-target-time="2024-04-16 16:23:36"
5.2 完全恢复
pg_ctl stop
rm -rf /pgccc/pgdata/*
pg_rman restore -B /home/postgres/backup
6.删除备份集
--按指定时间从catalog删除备份集
pg_rman -B /home/postgres/backup delete "2024-04-16 16:12:45" -f
[postgres@centos79 ~]$ pg_rman purge -B /home/postgres/backup
INFO: DELETED backup "2024-04-16 16:12:42" is purged
7.总结
pg_rman跑的不是流复制协议,而是文件拷贝,希望本篇文章帮助大家更好管理好PG的备份恢复
转自
掰着脚趾数备份恢复,pg_rman也算一个够牛逼的工具了!!!
https://mp.weixin.qq.com/s/w4mZ-wu2TxSeT5aZ1NGVyw