PG_RMAN部署及使用
PGSQL推出开源备份工具pg_rman,类似于oracle的rman备份策略,实现了全量、增量和归档等多重备份方式,可以很灵活的管理PGSQL数据库的备份,支持在线和基于PITR的备份恢复方式。
1、使用postgres用户部署
[postgres@pg14 ~]$ tar xvf pg_rman-1.3.15-pg14.tar.gz
[postgres@pg14 ~]$ cd pg_rman-1.3.15-pg14.tar.gz
[postgres@pg14 ~]$ make install
2、初始化环境
#设置备份目录,再用户配置文件里添加以下信息:
export BACKUP_PATH=/home/postgres/pg_rman_bk#使环境变量生效
[postgres@pg14 ~]$ source .bash_profile#初始化备份目录,验证归档路径,日志目录,同时在备份路径下产生目标库相关文件
[postgres@pg14 ~]$ pg_rman init
WARNING: ARCLOG_PATH is not set yet
DETAIL: The archive_command is not set in postgresql.conf.
HINT: Please set ARCLOG_PATH in pg_rman.ini or environmental variable.
INFO: SRVLOG_PATH is set to '/opt/pgdata/log'#发现未配置归档命令,在postgresql.conf添加
archive_command = 'cp %p /home/postgres/archives/%f'
archive_mode = 'on'#然后删除pg_rman_bk里的内容
[postgres@pg14 ~]$ rm -rf pg_rman_bk/*#重新初始化备份目录
[postgres@pg14 ~]$ pg_rman init
INFO: ARCLOG_PATH is set to '/home/postgres/archives'
INFO: SRVLOG_PATH is set to '/opt/pgdata/log'
[postgres@pg14 pg_rman_bk]$ ll
总用量 8
drwx------ 4 postgres dba 34 2月 27 20:57 backup
-rw-r--r-- 1 postgres dba 69 2月 27 20:57 pg_rman.ini
-rw-r--r-- 1 postgres dba 40 2月 27 20:57 system_identifier
drwx------ 2 postgres dba 6 2月 27 20:57 timeline_history
3、备份命令
[postgres@pg14 pg_rman_bk]$ pg_rman --help
pg_rman manage backup/recovery of PostgreSQL database.Usage:pg_rman OPTION init #初始化备份目录pg_rman OPTION backup #在线备份pg_rman OPTION restore #恢复pg_rman OPTION show [DATE] #查看备份历史pg_rman OPTION validate [DATE] #验证备份pg_rman OPTION delete DATE #删除备份信息pg_rman OPTION purge #删除实际的备份文件
4、备份
#全量备份
[postgres@pg14 pgdata]$ pg_rman backup --backup-mode=full -C -P
INFO: copying database files
Processed 1281 of 1281 files, skipped 0
INFO: copying archived WAL files
Processed 3 of 3 files, skipped 0
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.#查看备份
[postgres@pg14 pgdata]$ pg_rman show
=====================================================================StartTime EndTime Mode Size TLI Status
=====================================================================
2024-02-27 21:59:57 2024-02-27 22:00:00 FULL 58MB 1 OK