pg_rman是一款专门为postgresql设计的在线备份恢复的工具。其支持在线和基于时间点备份方式,还可以通过创建backup catalog来维护DB cluster备份信息。
- 使用简单.一个命令即可完成备份和恢复.
- 支持在线全备,增量备份,归档备份.
- 支持备份压缩.通过gzip工具实现页内压缩.
- 自动备份维护.自动删除过期的WAL备份文件.
- 支持备份验证.
- 恢复期间无事务丢失.支持基于PITR的配置文件生成器.
1.下载安装
找到对应版本下载,默认安装到路径/usr/pgsql-13/bin
https://github.com/ossc-db/pg_rman/releases
wget https://github.com/ossc-db/pg_rman/releases/download/V1.3.14/pg_rman-1.3.14-1.pg13.rhel8.x86_64.rpm
yum install pg_rman-1.3.14-1.pg13.rhel8.x86_64.rpm
2.PostgreSQL configuration
sed -ir "s/#*max_wal_senders.*/max_wal_senders = 10/" postgresql.conf
sed -ir "s/#*max_replication_slots.*/max_replication_slots= 10/" postgresql.conf
sed -ir "s/#*hot_standby .*/hot_standby = on/" postgresql.conf
sed -ir "s/#*wal_level.*/wal_level = replica/" postgresql.conf
sed -ir "s/#*archive_mode.*/archive_mode = on/" postgresql.conf
sed -ir "s/#*archive_command.*/archive_command = '\/bin\/true'/" postgresql.conf
# 检查
cat postgresql.conf | grep "max_replication_slots\|max_wal_senders\|wal_level\|archive_mode\|archive_command\|hot_standby "
修改完重启
systemctl restart postgresql-13
3.初始化
[root@lys-mysql pg_rman]# /usr/pgsql-13/bin/pg_rman --backup-path /lys/data/pg_rman --pgdata /var/lib/pgsql/13/data --arclog-path /lys/data/arclog init
INFO: ARCLOG_PATH is set to '/lys/data/arclog'
INFO: SRVLOG_PATH is set to '/var/lib/pgsql/13/data/log'
4.全量备份
[root@lys-mysql pg_rman]# /usr/pgsql-13/bin/pg_rman --backup-path /lys/data/pg_rman --pgdata /var/lib/pgsql/13/data --arclog-path /lys/data/arclog backup -b full -U user001 -d db001 -h 127.0.0.1 -p 5432
Password for user user001:
INFO: copying database files
INFO: copying archived WAL files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
5.增量备份
增量备份前必须先验证
[root@lys-mysql pg_rman]# /usr/pgsql-13/bin/pg_rman --backup-path /lys/data/pg_rman --pgdata /var/lib/pgsql/13/data validate
INFO: validate: "2022-07-28 08:04:54" backup and archive log files by CRC
INFO: backup "2022-07-28 08:04:54" is valid
[root@lys-mysql pg_rman]# /usr/pgsql-13/bin/pg_rman --backup-path /lys/data/pg_rman --pgdata /var/lib/pgsql/13/data show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2022-07-28 08:05:51 2022-07-28 08:05:54 INCR 0B 1 ERROR
2022-07-28 08:04:54 2022-07-28 08:05:00 FULL 37MB 1 OK
[root@lys-mysql pg_rman]# /usr/pgsql-13/bin/pg_rman --backup-path /lys/data/pg_rman --pgdata /var/lib/pgsql/13/data validate
INFO: validate: "2022-07-28 08:08:05" backup and archive log files by CRC
INFO: backup "2022-07-28 08:08:05" is valid
[root@lys-mysql pg_rman]# /usr/pgsql-13/bin/pg_rman --backup-path /lys/data/pg_rman --pgdata /var/lib/pgsql/13/data show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2022-07-28 08:08:05 2022-07-28 08:08:10 INCR 24kB 1 OK
2022-07-28 08:05:51 2022-07-28 08:05:54 INCR 0B 1 ERROR
2022-07-28 08:04:54 2022-07-28 08:05:00 FULL 37MB 1 OK
6.删除备份
[root@lys-mysql pg_rman]# /usr/pgsql-13/bin/pg_rman --backup-path /lys/data/pg_rman --pgdata /var/lib/pgsql/13/data delete "2022-07-28 08:05:51"
INFO: delete the backup with start time: "2022-07-28 08:05:51"
WARNING: cannot delete backup with start time "2022-07-28 08:04:54"
DETAIL: This is the latest full backup necessary for successful recovery.
[root@lys-mysql pg_rman]# /usr/pgsql-13/bin/pg_rman --backup-path /lys/data/pg_rman --pgdata /var/lib/pgsql/13/data show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2022-07-28 08:08:05 2022-07-28 08:08:10 INCR 24kB 1 OK
2022-07-28 08:04:54 2022-07-28 08:05:00 FULL 37MB 1 OK