1.pg_rman 介紹
1.1 pg_rman 介紹
• pg_rman類似於oracle的rman備份策略,實現了全量、增量、歸檔的層級的備份,可以很靈活的管理PostgreSQL數據庫的備份,pg_rman是一款開源的pg的備份恢復插件支持在線和基於PITR的備份恢復方式。
• pg_rman特點:
○ 使用簡單.一個命令即可完成備份和恢復.
○ 支持在線全備,增量備份,歸檔備份.
○ 支持備份壓縮.通過gzip工具實現頁內壓縮.
○ 自動備份維護.自動刪除過期的WAL備份文件.
○ 支持備份驗證.
○ 恢復期間無事務丟失.支持基於PITR的配置文件生成器.
• pg_rman 注意事項:
○ pg_rman 基於 pg_start_backup
○ pg_rman 基於 需要在本地安裝,不能遠程德份
○ pg_rman 需要下載對應的版本
1.2 pg_rman 下載
下載地址:
https://github.com/ossc-db/pg_rman/
PostgreSQL Rpm相關依賴、插件包下載地址:
https://yum.postgresql.org/10/redhat/rhel-6.7-x86_64/
手冊地址:
http://ossc-db.github.io/pg_rman/index.html
2.安裝使用
源碼包安裝方法:
1、下載源碼包
wget <a href="https://github.com/ossc-db/pg_rman/releases/download/V1.3.7/pg_rman-1.3.7-pg10.tar.gz" style="text-decoration-skip-ink:none;">https://github.com/ossc-db/pg_rman/releases/download/V1.3.7/pg_rman-1.3.7-pg10.tar.gz</a>
2、解壓到指定目錄
tar -zxvf pg_rman-1.3.14-pg12.tar.gz -C /opt/
3、進入到rman目錄進行編譯安裝
cd /opt/pg_rman-1.3.14-pg12/
make && make install
# 設置環境變量:
PATH=$PATH:$HOME/bin
export PG_RMAN=/u01/postgresql/pg12
export PGHOME=/u01/postgresql/pg12
export PGDATA=/u01/postgresql/data
export PGLIB=/u01/postgresql/pg12/lib
export SRVLOG_PATH=/u01/postgresql/data/log
export ARCLOG_PATH=/u01/postgresql/archive
export BACKUP_PATH=/opt/pg_rman
export PATH=$PGHOME/bin:$PATH:$PG_RMAN/bin
export PATH
3.pg_rman 使用
3.1 pg_rman的用法
pg_rman的使用相對簡單核心8個命令
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 show detail [DATE]
pg_rman OPTION validate [DATE]
pg_rman OPTION delete DATE
pg_rman OPTION purge
選項 | 功能 |
init |
初始化備份目錄 |
backup |
在線備份 |
restore |
恢復 |
show |
查看備份歷史 |
validate |
驗證備份 |
delete |
刪除備份文件 |
purge |
從備份目錄中刪除已刪除備份 |
3.2 初始化備份目錄
首先需要初始化一個backup catalog,目錄將用於存放備份的文件,目錄也會存放一些元數據,例如備份的配置文件,數據庫的systemid,時間線文件歷史
注意:在初始化的時候,要先確認環境變量是否設置正確。
root@mxl ~]# pg_rman init -B /opt/pg_rman
總用量 8
drwx------ 4 root root 35 11月 23 13:30 backup
-rw-r--r-- 1 root root 83 11月 23 13:30 pg_rman.ini
-rw-r--r-- 1 root root 40 11月 23 13:30 system_identifier
drwx------ 2 root root 6 11月 23 13:30 timeline_history
3.3 查看pg_rman的配置文件信息
[postgres@mxl pg_rman]$ cat pg_rman.ini
ARCLOG_PATH=‘/home/postgres/arch’ --歸檔目錄
SRVLOG_PATH=‘/home/postgres/data/log’ --數據庫錯誤日志目錄
COMPRESS_DATA = YES --壓縮數據
KEEP_ARCLOG_FILES = 10 --保存歸檔文件個數
KEEP_ARCLOG_DAYS = 10 --保存歸檔的天數
KEEP_DATA_GENERATIONS = 3 --備份冗余度
KEEP_DATA_DAYS = 10 --保存備份集時間
KEEP_SRVLOG_FILES = 10 --保存日志文件個數
KEEP_SRVLOG_DAYS = 10 --保存日志文件天數
3.4 pg_rman的全量備份
如果沒有調設置export BACKUP_PATH=/opt/pg_rman ,需要手動指明備份路徑 --backup-path=/opt/pg_rman
postgres@s2ahumysqlpg01-> pg_rman backup --backup-mode=full --with-serverlog -h 127.0.0.1
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.
一、pg_rman 校驗備份集:
postgres@s2ahumysqlpg01-> pg_rman show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2022-02-22 17:18:39 2022-02-22 17:19:12 FULL 942MB 5 DONE
二、未校驗備份集 Status 顯示為 DONE,開始校驗
[postgres@mxl pg_rman]$ pg_rman validate
INFO: validate: "2019-12-03 11:53:39" backup, archive log files and server log files by CRC
INFO: backup "2019-12-03 11:53:39" is valid
[postgres@mxl pg_rman]$ pg_rman show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2019-12-03 11:53:39 2018-12-03 11:54:00 FULL 461MB 1 OK
一、pg_rman 備份集查看:
[postgres@mxl pg_rman]$ pg_rman show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2019-12-03 11:53:39 2019-12-03 11:54:00 FULL 461MB 1 DONE
二、未校驗備份集 Status 顯示為 DONE,開始校驗
postgres@s2ahumysqlpg01-> pg_rman validate
INFO: validate: "2022-02-22 17:18:39" backup, archive log files and server log files by CRC
INFO: backup "2022-02-22 17:18:39" is valid
postgres@s2ahumysqlpg01-> pg_rman show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2022-02-22 17:18:39 2022-02-22 17:19:12 FULL 942MB 5 OK
3.5 pg_rman的增量備份
增量備份是基於文件系統的update time時間線,增量備份必須有個對應的全庫備份
postgres@s2ahumysqlpg01-> pg_rman backup --backup-mode=incremental --progress --compress-data -h 127.0.0.1
INFO: copying database files
INFO: copying archived WAL filesipped 3028
Processed 16 of 16 files, skipped 13
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
備份集查看:
$ pg_rman show
postgres@s2ahumysqlpg01-> pg_rman show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2022-02-22 17:22:54 2022-02-22 17:22:58 INCR 33kB 5 DONE
2022-02-22 17:18:39 2022-02-22 17:19:12 FULL 942MB 5 OK
postgres@s2ahumysqlpg01-> pg_rman validate
INFO: validate: "2022-02-22 17:22:54" backup and archive log files by CRC
INFO: backup "2022-02-22 17:22:54" is valid
postgres@s2ahumysqlpg01->
postgres@s2ahumysqlpg01->
postgres@s2ahumysqlpg01-> pg_rman show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2022-02-22 17:22:54 2022-02-22 17:22:58 INCR 33kB 5 OK
2022-02-22 17:18:39 2022-02-22 17:19:12 FULL 942MB 5 OK
postgres@s2ahumysqlpg01->
3.6 pg_rman 歸檔備份
一、pg_rman 歸檔備份:
歸檔備份就是靜態文件的拷貝。
$ pg_rman backup --backup-mode=archive --progress --compress-data
$ pg_rman validate
$ pg_rman show
二、刪除備份集
刪除備份集指定日期之前的,不需要用來指定時間點之前的備份刪除掉
[postgres@mxl ~]$ pg_rman delete " pg_rman delete "2022-02-22 17:22:58""
三、清除備份集
物理刪除已從catalog刪除的備份集
上面從備份集中刪除的備份,備份集文件夾並沒有一起刪除。
[postgres@mxl ~]$ pg_rman purge
4 pg_rman 恢復
pg_rman 恢復默認原地恢復(覆蓋式),或者使用新的$PGDATA作為恢復目標。需注意如果在本機恢復pg_rman可能會覆蓋原有的數據文件arch,pg_xlog目錄中的文件, 可以先將原數據文件做下備份,默認pg_rman restore會在$PGDATA下產生recovery.conf根據實際需要修改相關恢復參數即可。
1、recovery-target-timeline TIMELINE
Specifies recovering into a particular timeline. If not specified, the current timeline from ($PGDATA/global/pg_control) is used.
如果不指定時間線,則使用$PGDATA/global/pg_control,如果沒有$PGDATA/global/pg_control,則使用最新的全量備份集的時間線。
2、recovery-target-time TIMESTAMP
This parameter specifies the time stamp up to which recovery will proceed. If not specified, continue recovery to the latest time.
如果不指定,則恢復到最新時間
3、recovery-target-xid XID
This parameter specifies the transaction ID up to which recovery will proceed. If not specified, continue recovery to the latest xid.
如果不指定,則恢復到最新xid
4、recovery-target-inclusive
Specifies whether we stop just after the specified recovery target (true), or just before the recovery target (false). Default is true.
是否在指定的恢復目標(true)之后停止,默認為true,如果指定false意識是在恢復目標之前停止
• The following parameter determines the behavior of restore.
--hard-copy
The archive WAL are copied to archive WAL storage area. If not specified, pg_rman makes symbolic link to archive WAL where are in the backup catalog directory.
是否使用硬鏈接復制archive log,如果不指定使用符號連接(軟連接)的方式。
#示例:
#pg_rman restore --recovery-target-time "2022-02-22 17:25:30"; 下面是沒有指定的恢復
[postgres@mxl~]$ pg_rman restore
INFO: backup "2022-02-22 17:25:30" is valid
INFO: the recovery target timeline ID is not given
INFO: use timeline ID of current database cluster as recovery target: 5
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: "2022-02-22 17:25:30"
INFO: copying online WAL files and server log files
INFO: clearing restore destination
INFO: validate: "2022-02-22 17:25:30" backup, archive log files and server log files by SIZE
INFO: backup "2022-02-22 17:25:30" is valid
INFO: restoring database files from the full mode backup "2022-02-22 17:25:30"
INFO: searching incremental backup to be restored
INFO: searching backup which contained archived WAL files to be restored
INFO: backup "2022-02-22 17:25:30" is valid
INFO: restoring WAL files from backup "2022-02-22 17:25:30"
INFO: restoring online WAL files and server log files
INFO: create pg_rman_recovery.conf for recovery-related parameters.
INFO: remove an 'include' directive added by pg_rman in postgresql.conf if exists
INFO: append an 'include' directive in postgresql.conf for pg_rman_recovery.conf
INFO: generating recovery.signal
INFO: removing standby.signal if exists to restore as primary
INFO: restore complete
HINT: Recovery will start automatically when the PostgreSQL server is started. After the recovery is done, we recommend to remove recovery-related parameters configured by pg_rman.
pg_ctl start
參考:
下載:https://github.com/ossc-db/pg_rman/
依賴包:https://yum.postgresql.org/10/redhat/rhel-6.7-x86_64/
使用手冊:http://ossc-db.github.io/pg_rman/index.html