標籤:
一、 安裝
PostgreSQL版本是9.4.4
# git clone -b REL9_4_STABLE https://github.com/ossc-db/pg_rman.gitInitialized empty Git repository in /opt/pg_rman/.git/remote: Counting objects: 1939, done.remote: Total 1939 (delta 0), reused 0 (delta 0), pack-reused 1939Receiving objects: 100% (1939/1939), 702.67 KiB | 311 KiB/s, done.Resolving deltas: 100% (1393/1393), done.# yum install -y pam-devel readline-devel zlib-devel# make# make install/bin/mkdir -p ‘/opt/pg94/bin‘/usr/bin/install -c pg_rman ‘/opt/pg94/bin‘# pg_rman --versionpg_rman 1.3.1
二、使用
首先需要初始化備份目錄
# mkdir -p /data/pg_backup# mkdir -p /data/test_xlog_94/fulltime# pg_rman init --backup-path=/data/pg_backup/WARNING: ARCLOG_PATH is not set yetDETAIL: The archive_command is not set in postgresql.conf.HINT: Please set ARCLOG_PATH in pg_rman.ini or environmental variable.# vim /data/pg_backup/pg_rman.iniARCLOG_PATH=‘/data/test_xlog_94/fulltime‘
在master節點上製作一個全備,在從庫上需要指定--host, --standby-host=172.17.5.47 --standby-port=5432
# pg_rman backup --backup-mode=full --backup-path=/data/pg_backup/ --pgdata=/data/test_pgdata_94/ -U postgres -d postgres INFO: copying database filesNOTICE: pg_stop_backup complete, all required WAL segments have been archivedINFO: copying archived WAL filesINFO: backup completeHINT: Please execute ‘pg_rman validate‘ to verify the files are correctly copied.# pg_rman show detail --backup-path=/data/pg_backup/============================================================================================================ StartTime Mode Duration Data ArcLog SrvLog Total Compressed CurTLI ParentTLI Status ============================================================================================================2015-09-07 02:52:22 FULL 0m 422MB 106kB ---- 394MB false 1 0 OK
使用備份進行恢複
# mkdir restore_test# pg_rman restore --backup-path=/data/pg_backup/ --pgdata=/data/restore_test/WARNING: could not open pg_controldata file "/data/restore_test//global/pg_control": No such file or directoryWARNING: could not open pg_controldata file "/data/restore_test//global/pg_control": No such file or directoryINFO: backup "2015-09-07 02:52:22" is validINFO: the recovery target timeline ID is not givenINFO: use timeline ID of latest full backup as recovery target: 1INFO: calculating timeline branches to be used to recovery target pointINFO: searching latest full backup which can be used as restore start pointINFO: found the full backup can be used as base in recovery: "2015-09-07 02:52:22"INFO: copying online WAL files and server log filesINFO: clearing restore destinationINFO: validate: "2015-09-07 02:52:22" backup and archive log files by SIZEINFO: backup "2015-09-07 02:52:22" is validINFO: restoring database files from the full mode backup "2015-09-07 02:52:22"INFO: searching incremental backup to be restoredINFO: searching backup which contained archived WAL files to be restoredINFO: backup "2015-09-07 02:52:22" is validINFO: restoring WAL files from backup "2015-09-07 02:52:22"INFO: restoring online WAL files and server log filesINFO: generating recovery.confINFO: restore completeHINT: Recovery will start automatically when the PostgreSQL server is started.
由於是用root使用者進行備份和恢複的,目錄許可權也是root,需要修改為postgres
# ls -ld /data/restore_test/drwxr-xr-x. 18 root root 4096 Sep 7 03:00 restore_test/# cat /data/restore_test/recovery.conf # recovery.conf generated by pg_rman 1.3.1restore_command = ‘cp /data/test_pgdata_94/pg_xlog/%f %p‘recovery_target_timeline = ‘1‘# chown -R postgres:postgres /data/restore_test/# chmod 0700 /data/restore_test/# ls -ld /data/restore_test/drwx------. 18 postgres postgres 4096 Sep 7 03:05 /data/restore_test/
將連接埠修改為5433,並啟動恢複的資料庫
$ pg_ctl -D /data/restore_test/ startserver starting[[email protected] data]$ [ 2015-09-07 07:05:40.291 UTC 31889 55ed3744.7c91 1 0]LOG: redirecting log output to logging collector process[ 2015-09-07 07:05:40.291 UTC 31889 55ed3744.7c91 2 0]HINT: Future log output will appear in directory "pg_log".$ psql -p 5433psql (9.4.4)Type "help" for help.
恢複到指定時間點的話,執行restore的時候使用如下參數:
--recovery-target-time time stamp up to which recovery will proceed --recovery-target-xid transaction ID up to which recovery will proceed --recovery-target-inclusive whether we stop just after the recovery target --recovery-target-timeline recovering into a particular timeline
會將相應的參數寫到recovery.conf裡面
參考:http://ossc-db.github.io/pg_rman/index.html
PostgreSQL備份之pg_rman