pg_rman工具是實現PostgreSQL數據庫在線備份,物理備份和恢復的備份工具。可以實現全量備份,增量備份。
全量: full
增量: incremental
歸檔: 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
命令選項:
-D, --pgdata=PATH 數據存儲目錄的路徑
-A, --arclog-path=PATH 歸檔wal (預寫)日誌的路徑
-S, --srvlog-path=PATH 數據庫服務器存儲日誌的路徑。
-B, --backup-path=PATH 備份數據存儲的路徑。
-c, --check 檢查
-v, --verbose 顯示詳細消息
-P, --progress 顯示已處理文件的進度
備份選項:
-b, --backup-mode=MODE 全備,增備,歸檔
-s, --with-serverlog 備份服務器日誌文件
-Z, --compress-data 使用zlib壓縮數據備份
-C, --smooth-checkpoint 在備份前做平滑檢查點
-F, --full-backup-on-error 切換到完全備份模式,注意:此選項僅用於--backup-mode = incremental或archive
--keep-data-generations=NUM 保留NUM代完整數據備份
--keep-data-days=NUM 保持足夠的數據備份以恢復到N天前
--keep-arclog-files=NUM 保留NUM個歸檔的WAL日誌
--keep-arclog-days=DAY 保持存檔的WAL在DAY天內修改
--keep-srvlog-files=NUM 保留NUM個服務器日誌
--keep-srvlog-days=DAY 在DAY天內修改服務器日誌
--standby-host=HOSTNAME 從待機狀態進行備份時備用主機
--standby-port=PORT 從待機狀態進行備份時的備用端口
還原選項:
--recovery-target-time 恢復將繼續進行的時間戳
--recovery-target-xid 事務ID,恢復將繼續進行
--recovery-target-inclusive 我們是否在恢復目標之後停止
--recovery-target-timeline 恢復到特定的時間
--hard-copy 複製archivelog而不是符號鏈接
目錄選項:
-a 顯示已刪除的備份
刪除選項:
-f 強制刪除比規定日期更舊的備份
連接選項:
-d, --dbname=DBNAME 連接指定庫
-h, --host=HOSTNAME 數據庫主機名/IP地址
-p, --port=PORT 數據庫端口
-U, --username=USERNAME 數據庫用戶名
-w, --no-password 從不提示密碼
-W, --password 強制密碼提示
通用選項:
-q, --quiet 不顯示任何INFO或者DEBUG信息。
--debug 查看debug信息。
-
開始使用pg_rman工具:
-
創建備份目錄
# mkdir /database-backup/postgresql-backup/{fullbackup,walbackup,pg_srvlog} -p # chown -R postgres.postgres /database-backup/postgresql-backup/
- 設置環境變量:
$ 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 #立即生效
- 修改postgresql.conf配置文件:
$ 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 初始化
$ 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全量備份
$ pg_rman backup --backup-mode=full --with-serverlog --progress INFO: copying database files Processed 1298 of 1298 files, skipped 0 INFO: copying archived WAL files Processed 3 of 3 files, skipped 0 INFO: copying server log files INFO: backup complete INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
- pg_rman 校驗備份集
pg_rman 的備份必須都是經過驗證過的,否則不能進行恢復和增量備份。 $ pg_rman validate INFO: validate: "2018-10-14 14:55:34" backup, archive log files and server log files by CRC INFO: backup "2018-10-14 14:55:34" is valid
-
-
pg_rman 列出備份集
$ 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增量備份:
增量備份是基於文件系統的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刪除備份集
$ 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 =====================================================================
注意: 創建增量備份之前必須先創建一個全量備份。
- pg_rman restore 還原
模擬數據庫誤刪除: $ /etc/init.d/postgresql stop $ ps -ef | grep -i postmaster | grep -v grep $cd data $ ls -l
$ mv data data-bak # 將之間的目錄作爲備份。
$ mkdir data # 創建數據目錄。
$ chmod 700 ./data
-
使用pg_rman restore還原:
$ pg_rman restore
$ cd $PGDATA $ ls -l
$ 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
-
還原時,注意事項:
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。
增量備份和全量備份, 備份還原已經完成。
需要注意的是:
物理備份時的全量和增量備份時可以在線備份(不停機備份).
但是還原時,需要停機進行還原。
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
參考:
https://github.com/ossc-db/pg_rman/tree/master
http://ossc-db.github.io/pg_rman/index.html