pg_rman是一款开源的备份恢复软件,支持在线和基于PITR的备份恢复方式。
pg_rman类似于oracle的rman,可以进行全量、增量、归档日志的备份。
运行模式:
安装部署
Releases · ossc-db/pg_rman · GitHub
1、需要根据PG Server的版本,下载安装不同的版本。
2、需要提前使用编译安装的方式来安装PG Server
bash
# 拷贝源码
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
开启归档
bash
# 创建归档目录并授权
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();
物理备份
bash
# 初始化,实际上就是需要一个目录,这个目录将用于存放备份的文件
pg_rman init -B /home/postgres/backup
# 全量备份
pg_rman backup --backup-mode=full -B /home/postgres/backup
pg_rman show -B /home/postgres/backup
pg_rman validate -B /home/postgres/backup
# 增量备份
pg_rman backup --backup-mode=incremental -B /home/postgres/backup
pg_rman show -B /home/postgres/backup
pg_rman validate -B /home/postgres/backup
备份恢复
bash
--不完全恢复
pg_ctl stop
rm -rf /pgccc/pgdata/*
pg_rman restore -B /home/postgres/backup \
--recovery-target-time="2024-04-16 12:30:00"
--完全恢复
pg_ctl stop
rm -rf /pgccc/pgdata/*
pg_rman restore -B /home/postgres/backup
清理备份集
bash
--按指定时间从catalog删除备份集
pg_rman -B /home/postgres/backup delete "2024-04-16 12:30:00" -f
pg_rman purge -B /home/postgres/backup