The Pg_rman tool is a backup tool for online backup, physical backup and recovery of PostgreSQL databases. can achieve full-scale backup, incremental backup.
Total: Full
Increment: Incremental
Archive: Archive
Usage: pg_rman OPTION init pg_rman OPTION backup pg_rman OPTION restore pg_rman OPTION show [DATE] pg_rman OPTION show detail [DATE] pg_rman OPTION validate [DATE] pg_rman OPTION delete DATE pg_rman OPTION purge
Command options:-D, path to--pgdata=path data store directory-A,--arclog-path=path archive wal (pre-write) log path-S,--srvlog-path=path number The path to the log that the library server stores. -B,--backup-path=path the path to the backup data store. -C,--check Check-V,--verbose display verbose message-p,--progress Show progress backup options for processed files:-B,--backup-mode=mode full, add, archive-S,--with-serverlog Backup server log file-Z,--comp Ress-data using zlib compressed data backup-C,--smooth-checkpoint do a smooth checkpoint-F before backup,--full-backup-on-error switch to full backup mode Type, note: This option is only used for--backup-mode = incremental or archive--keep-data-generations=num reserved NUM generation full Data backup--keep-data-days=num Maintain sufficient data backup to restore to n days ago--keep-arclog-files=num keep NUM archived wal logs--keep-arclog-days=day keep archived Wal within day days modified--keep -srvlog-files=num Keep NUM Server logs--keep-srvlog-days=day Modify server logs--standby-host=hostname from standby when you are backing up in day days Alternate host--standby-port=port backup port restore option when backing up from standby:--recovery-target-time recovery will continue inThe timestamp of the row--RECOVERY-TARGET-XID the transaction ID, and the recovery will continue to--recovery-target-inclusive whether we stop after recovering the target--recovery-target-time Line reverts to a specific time--hard-copy copy archivelog instead of the Symbolic link Directory option:-A displays the deleted backup delete option:-F Force removal of older backups than the specified date Connection options:-D,--dbname=dbname connection to specified library-H,--HOST=HOSTNAME database hostname/IP address-p,--port=port database Port-U,-- Username=username database User Name-W,--no-password never prompt password-W,--password mandatory password hint common option:-Q,--quiet No info or debug information is displayed. --debug View debug information.
Start using the Pg_rman tool:
Create a backup directory
# mkdir /database-backup/postgresql-backup/{fullbackup,walbackup,pg_srvlog} -p # chown -R postgres.postgres /database-backup/postgresql-backup/
- Set Environment variables:
$ vim .profile export BACKUP_PATH=/database-backup/postgresql-backup/fullbackup export ARCLOG_PATH=/database-backup/postgresql-backup/walbackup export SRVLOG_PATH=/database-backup/postgresql-backup/pg_srvlog $ source .profile #立即生效
- To modify the postgresql.conf configuration file:
$ vim data/postgresql.conf archive_mode = on archive_command = ‘DATE=`date +%Y%m%d`; DIR="/database-backup/postgresql-backup/walbackup/"; (test -d $DIR || mkdir -p $DIR) && cp %p $DIR/%f‘ 保存并退出。
- Pg_rman Init initialization
$ pg_rman init INFO: ARCLOG_PATH is set to ‘/database-backup/postgresql-backup/walbackup‘ INFO: SRVLOG_PATH is set to ‘/database-backup/postgresql-backup/pg_srvlog‘
- Pg_rman Full-scale backup
$ pg_rman backup --backup-mode=full --with-serverlog --progressINFO: copying database filesProcessed 1298 of 1298 files, skipped 0INFO: copying archived WAL filesProcessed 3 of 3 files, skipped 0INFO: copying server log filesINFO: backup completeINFO: Please execute ‘pg_rman validate‘ to verify the files are correctly copied.
- Pg_rman Verifying backup Sets
pg_rman 的备份必须都是经过验证过的,否则不能进行恢复和增量备份。$ pg_rman validateINFO: validate: "2018-10-14 14:55:34" backup, archive log files and server log files by CRCINFO: backup "2018-10-14 14:55:34" is valid
Pg_rman List Backup Sets
$ pg_rman show
$ ls -l /database-backup/postgresql-backup/fullbackup/ total 20 drwx------ 3 postgres postgres 4096 Oct 14 14:55 20181014 drwx------ 4 postgres postgres 4096 Oct 14 14:53 backup -rw-rw-r-- 1 postgres postgres 119 Oct 14 14:53 pg_rman.ini -rw-rw-r-- 1 postgres postgres 40 Oct 14 14:53 system_identifier drwx------ 2 postgres postgres 4096 Oct 14 14:53 timeline_history
- Pg_rman Incremental Backup:
增量备份是基于文件系统的update time时间线. 增量备份必须有个对应的全库备份. $ pg_rman backup --backup-mode incremental --progress --compress-data
$ pg_rman validate INFO: validate: "2018-10-14 15:59:57" backup and archive log files by CRC INFO: backup "2018-10-14 15:59:57" is valid $ pg_rman show
Pg_rman Deleting a backup set
$ pg_rman delete -f ‘2018-10-14 15:59:57‘ INFO: delete the backup with start time: "2018-10-14 15:59:57" INFO: delete the backup with start time: "2018-10-14 14:55:34" 删除增量备份的同时,也已经把全量备份给删除了。 $ pg_rman show ===================================================================== StartTime EndTime Mode Size TLI Status =====================================================================
Note: Before you create an incremental backup, you must first create a full-volume backup.
$ mv data data-bak # 将之间的目录作为备份。 $ mkdir data # 创建数据目录。 $ chmod 700 ./data
$ cat recovery.conf # recovery.conf generated by pg_rman 1.3.7 restore_command = ‘cp /database-backup/postgresql-backup/walbackup/%f %p‘ recovery_target_timeline = ‘1‘ 启动PostgreSQL数据库服务: $ /etc/init.d/postgresql start
$ ps -ef |grep -i postgres
When restoring, note the following:
restore后启动会遇到下列错误: invalid primary checkpoint record invalid secondary checkpoint record could not locate a valid checkpoint record 此时只能重置wal,并取消恢复模式 $ pg_resetwal --f $PGDATA $ mv $PGDATA/recovery.conf $PGDATA/recovery.done 使用pg_rman备份时对wal的归档是通过软链接来实现。建议添加 --hard-copy。
Incremental and full backups, the backup restore is complete.
It is important to note that:
Full-volume and incremental backups when physical backups can be backed up online (non-stop backup).
However, when restoring, downtime is required to restore.
postgres用户的环境变量配置文件内容: PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/services/install/postgresql10/bin/ export LD_LIBRARY_PATH=/services/install/postgresql10/lib:$LD_LIBRARY_PATH export LD_LIBRARY_PATH=/services/install/python3.6/lib:$LD_LIBRARY_PATH export PGDATA PATH export BACKUP_PATH=/database-backup/postgresql-backup/fullbackup export ARCLOG_PATH=/database-backup/postgresql-backup/walbackup export SRVLOG_PATH=/database-backup/postgresql-backup/pg_srvlog
Reference:
Https://github.com/ossc-db/pg_rman/tree/master
Http://ossc-db.github.io/pg_rman/index.html
Physical Backup and Online Backup tool Pg_rman use Details (ix)