衆所周知,Oracle下能夠使用rman進行物理備份,支持數據庫的全量、增量、歸檔的備份模式;而PostgreSQL做爲開源數據庫,也推出了開源功工具pg_rman,一樣具有了oracle rman大部分的功能。本文寫的很早,近期才翻出來,掛到博客上,但願能幫助須要的朋友。git
[root@mydb01 ~]# wget https://github.com/ossc-db/pg_rman/releases/download/V1.3.6/pg_rman-1.3.6-1.pg10.rhel7.x86_64.rpm [root@mydb01 ~]# yum -y localinstall pg_rman-1.3.6-1.pg10.rhel7.x86_64.rpm
首先設置postgres用戶的profile,加入如下內容:github
-bash-4.2$ vi .bash_profile export PATH=/usr/pgsql-10/bin:$PATH export LD_LIBRARY_PATH=/usr/pgsql-10/lib export BACKUP_PATH=/u02/backups -bash-4.2$ vi pg_hba.conf host all all 0.0.0.0/0 md5 -bash-4.2$ vi postgresql.conf listen_addresses = '*' port = 5432 wal_level = archive ------->>若爲HOT STANDBY環境則此處設置爲hot_standby archive_mode = on archive_command = 'test ! -f /u02/archivelog/%f && cp %p /u02/archivelog/%f' [root@mydb01 ~]# systemctl restart postgresql10.service
[root@mydb01 ~]# su - postgres -bash-4.2$ pg_rman init
-bash-4.2$ pg_rman backup --backup-mode=full 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. INFO: start deleting old archived WAL files from ARCLOG_PATH (keep files = 10, keep days = 10) INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00" INFO: start deleting old backup (keep generations = 3 AND keep after = 2017-11-28 00:00:00) INFO: does not include the backup just tak
備份完成後,能夠使用下面的命令進行驗證:sql
-bash-4.2$ pg_rman validate INFO: validate: "2018-03-28 16:21:34" backup and archive log files by CRC INFO: backup "2018-03-28 16:21:34" is valid -bash-4.2$ pg_rman show ===================================================================== StartTime EndTime Mode Size TLI Status ===================================================================== 2018-03-28 16:21:34 2018-03-28 16:21:36 FULL 4465kB 1 OK
-bash-4.2$ pg_rman backup --backup-mode=incremental --with-serverlog INFO: copying database files INFO: copying archived WAL files INFO: copying server log files INFO: backup complete INFO: Please execute 'pg_rman validate' to verify the files are correctly copied. INFO: start deleting old archived WAL files from ARCLOG_PATH (keep files = 10, keep days = 10) INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00" INFO: start deleting old server files from SRVLOG_PATH (keep files = 10, keep days = 10) INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00" INFO: start deleting old backup (keep generations = 3 AND keep after = 2017-11-28 00:00:00) INFO: does not include the backup just taken INFO: backup "2018-03-28 16:21:34" should be kept DETAIL: This is the 1st latest full backup. -bash-4.2$ pg_rman validate INFO: validate: "2018-03-28 16:27:57" backup, archive log files and server log files by CRC INFO: backup "2018-03-28 16:27:57" is valid -bash-4.2$ pg_rman show ===================================================================== StartTime EndTime Mode Size TLI Status ===================================================================== 2018-03-28 16:27:57 2018-03-28 16:27:59 INCR 992kB 1 OK 2018-03-28 16:21:34 2018-03-28 16:21:36 FULL 4465kB 1 OK
-bash-4.2$ pg_rman backup --backup-mode=archive --with-serverlog INFO: copying archived WAL files INFO: copying server log files INFO: backup complete INFO: Please execute 'pg_rman validate' to verify the files are correctly copied. INFO: start deleting old archived WAL files from ARCLOG_PATH (keep files = 10, keep days = 10) INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00" INFO: start deleting old server files from SRVLOG_PATH (keep files = 10, keep days = 10) INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00" INFO: start deleting old backup (keep generations = 3 AND keep after = 2017-11-28 00:00:00) INFO: does not include the backup just taken INFO: backup "2018-03-28 16:27:57" should be kept DETAIL: This belongs to the 1st latest full backup. INFO: backup "2018-03-28 16:21:34" should be kept DETAIL: This is the 1st latest full backup. -bash-4.2$ pg_rman show ===================================================================== StartTime EndTime Mode Size TLI Status ===================================================================== 2018-03-28 16:28:51 2018-03-28 16:28:52 ARCH 26kB 1 DONE 2018-03-28 16:27:57 2018-03-28 16:27:59 INCR 992kB 1 OK 2018-03-28 16:21:34 2018-03-28 16:21:36 FULL 4465kB 1 OK
-bash-4.2$ pg_rman show 2018-03-28 16:21:34 # configuration BACKUP_MODE=FULL FULL_BACKUP_ON_ERROR=false WITH_SERVERLOG=false COMPRESS_DATA=true # result TIMELINEID=1 START_LSN=0/02000028 STOP_LSN=0/02000130 START_TIME='2018-03-28 16:21:34' END_TIME='2018-03-28 16:21:36' RECOVERY_XID=555 RECOVERY_TIME='2018-03-28 16:21:36' TOTAL_DATA_BYTES=24535637 READ_DATA_BYTES=24535483 READ_ARCLOG_BYTES=33554741 WRITE_BYTES=4465406 BLOCK_SIZE=8192 XLOG_BLOCK_SIZE=8192 STATUS=OK -bash-4.2$ pg_rman show 2018-03-28 16:27:57 # configuration BACKUP_MODE=INCREMENTAL FULL_BACKUP_ON_ERROR=false WITH_SERVERLOG=true COMPRESS_DATA=true # result TIMELINEID=1 START_LSN=0/04000028 STOP_LSN=0/040000f8 START_TIME='2018-03-28 16:27:57' END_TIME='2018-03-28 16:27:59' RECOVERY_XID=561 RECOVERY_TIME='2018-03-28 16:27:59' TOTAL_DATA_BYTES=32585789 READ_DATA_BYTES=8186187 READ_ARCLOG_BYTES=33554741 READ_SRVLOG_BYTES=518 WRITE_BYTES=992901 BLOCK_SIZE=8192 XLOG_BLOCK_SIZE=8192 STATUS=OK
-bash-4.2$ pg_rman delete "2018-03-28 16:28:51" WARNING: cannot delete backup with start time "2018-03-28 16:28:51" DETAIL: This is the archive backup necessary for successful recovery. WARNING: cannot delete backup with start time "2018-03-28 16:27:57" DETAIL: This is the incremental backup necessary for successful recovery. WARNING: cannot delete backup with start time "2018-03-28 16:21:34" DETAIL: This is the latest full backup necessary for successful recovery.
刪除/var/lib/pgsql/10/data文件夾或者清空,而後進行恢復操做。若是data文件夾是新建的,則必須注意data目錄的屬性爲700,不然在恢復的時候會報錯。數據庫
-bash-4.2$ pg_rman restore --recovery-target-time "2018-03-29 14:35:53" WARNING: pg_controldata file "/var/lib/pgsql/10/data/global/pg_control" does not exist INFO: backup "2018-03-29 14:30:11" is valid INFO: the recovery target timeline ID is not given INFO: use timeline ID of latest full backup as recovery target: 1 INFO: calculating timeline branches to be used to recovery target point INFO: searching latest full backup which can be used as restore start point INFO: found the full backup can be used as base in recovery: "2018-03-29 14:30:11" INFO: copying online WAL files and server log files INFO: clearing restore destination INFO: validate: "2018-03-29 14:30:11" backup and archive log files by SIZE INFO: backup "2018-03-29 14:30:11" is valid INFO: restoring database files from the full mode backup "2018-03-29 14:30:11" INFO: searching incremental backup to be restored INFO: searching backup which contained archived WAL files to be restored INFO: backup "2018-03-29 14:30:11" is valid INFO: restoring WAL files from backup "2018-03-29 14:30:11" INFO: restoring online WAL files and server log files INFO: generating recovery.conf INFO: restore complete HINT: Recovery will start automatically when the PostgreSQL server is started. -bash-4.2$ pg_ctl start waiting for server to start....2018-03-29 14:43:47.613 CST [1465] LOG: listening on IPv4 address "0.0.0.0", port 5432 2018-03-29 14:43:47.613 CST [1465] LOG: listening on IPv6 address "::", port 5432 2018-03-29 14:43:47.619 CST [1465] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" 2018-03-29 14:43:47.637 CST [1465] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 2018-03-29 14:43:47.650 CST [1465] LOG: redirecting log output to logging collector process 2018-03-29 14:43:47.650 CST [1465] HINT: Future log output will appear in directory "log". . done server started
更詳細的pg_rman用法,請參考官方手冊。bash