與Oracle相比,其他開源軟件基本都未自帶物理備份軟件或者功能不夠完整,要實現物理備份需借助三方插件完成。Postgresql雖然自帶pg_basebackup,但是不支持增量備份。pg_rman是pgsql眾多三方備份插件之一,其支持全量備份、增量備份、歸檔備份等,其功能類似oraclerman。
pg_rman下載地址:https://github.com/ossc-db/pg_rman/releases,下載對應pg版本的rpm文件或者源碼包文件。rpm或者源碼編譯均依賴于zlib-devel包,所以需提前安裝。
源碼安裝方法如下:
make
makeinstall
makeinstallcheck
pg數據庫要進行物理備份,跟oracle一樣,需開啟歸檔功能,操作步驟如下:
altersystem set wal_level=replica;
altersystem set archive_mode=on;
altersystem set archive_command = cp %p .../archive/%f;
重啟數據庫。
開始備份前,還需進行catalog初始化,用于保存備份信息,類似oracle控制文件或者catalog里保留的備份信息,命令如下:
pg_rmaninit -B /backup
[postgres@guassdb1~]$ pg_rman init
WARNING:ARCLOG_PATH is not set yet
DETAIL:The archive_command is not set in postgresql.conf.
HINT:Please set ARCLOG_PATH in pg_rman.ini or environmental variable.
INFO:SRVLOG_PATH is set to /data/pgdata1/pg_log
完成catalog初始化后,即可開始數據庫備份。值得說明的是在catalog中有pg_rman.ini文件,可配置備份的環境變量、備份選項、備份集保留期限等信息,其功能類似oraclerman 的configure和showall。
pg_rman.ini示例:
[postgres@spcl-****:spc**_back]$cat pg_rman.ini
ARCLOG_PATH=/backup/s***dp_arch
SRVLOG_PATH=/data/s***dp_data/log
1、數據庫全備
[postgres@guassdb1backup]$ pg_rman backup -b full -A /pg_archivelog
INFO:copying database files
INFO:copying archived WAL files
INFO:backup complete
INFO:Please execute pg_rman validate to verify the files are correctlycopied.
[postgres@guassdb1backup]$ pg_rman show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2020-10-2516:24:17 2020-10-25 16:24:20 FULL 95MB 1 DONE
---由上面紅色字體可看出備份成功后,其狀態是done,還需要進行validate操作后,備份集才可用于恢復,增量備份才會用于做對比。
[postgres@guassdb1~]$ pg_rman validate
INFO:validate: "2020-10-25 16:24:17" backup and archive logfiles by CRC
INFO:backup "2020-10-25 16:24:17" is valid
[postgres@guassdb1~]$
[postgres@guassdb1~]$
[postgres@guassdb1~]$
[postgres@guassdb1~]$ pg_rman show detail
======================================================================================================================
StartTime EndTime Mode Data ArcLog SrvLog Total Compressed CurTLI ParentTLI Status
======================================================================================================================
2020-10-2516:24:17 2020-10-25 16:24:20 FULL 41MB 67MB ---- 95MB false 1 0 OK
--validate校驗完成后,其備份集狀態變成OK
備份完成后備份集文件的目錄結構如下:
2、增量備份
pg_rmanbackup -b incremental -A /pg_archivelog
在生產環境中,需制定備份策略,通常每周全備,每天增量,同時備份歸檔日志,備份腳本示例:
每周日全備,其他時間進行增量備份
#/bin/bash
.~/.bash_profile
current_path=`dirname$0`
exportPGPASSWORD=*****
exportBACKUP_PATH=/backup***_back
LOCAL_HOST=**.**.**.**
LOCAL_PORT=5432
BACK_DAY=6
#主庫角色備份
master_backup(){
echo"master_backup"
pg_rmanbackup --backup-mode=$1 -Z -F -h ${LOCAL_HOST} -p ${LOCAL_PORT}--with-serverlog --keep-srvlog-days=7 --keep-data-generations=1--keep-arclog-files=100 --keep-arclog-days=1 -U postgres -Pv
#寫入文件短信告警
if[[ $? -ne 0 ]]; then
echo`date +%y-%m-%d %H:%M:%S: ` $1 "backup failed!" >${current_path}/backupflag
else
echo`date +%y-%m-%d %H:%M:%S: ` $1 "backup successful!" >${current_path}/backupflag
pg_rmanvalidate
pg_rmanpurge
fi
}
#從庫角色備份
slave_backup(){
echo"slave_backup"
pg_rmanbackup --backup-mode=$1 -Z -F -h $2 -p $3--standby-host=${LOCAL_HOST} --standby-port=${LOCAL_PORT}--with-serverlog --keep-srvlog-days=7 --keep-data-generations=1--keep-arclog-files=100 --keep-arclog-days=1 -U postgres -Pv
#寫入文件短信告警
if[[ $? -ne 0 ]]; then
echo`date +%y-%m-%d %H:%M:%S: ` $1 "backup failed!" >${current_path}/backupflag
else
echo`date +%y-%m-%d %H:%M:%S: ` $1 "backup successful!" >${current_path}/backupflag
pg_rmanvalidate
pg_rmanpurge
fi
}
#判斷日期,如周六全備,其它時間備份歸檔,若沒有全量備份,則做全量備份
fullback_stat=`pg_rmanshow |grep "FULL"|awk {print $8}`
weekday=`date+%w`
if[[ ${fullback_stat} =~ "OK" ]]; then
if[[ ${weekday} -eq ${BACK_DAY} ]]; then
back_mode=full
else
back_mode=incremental
fi
else
back_mode=full
fi
#數據庫進程情況
is_alive=`pg_isready-p ${LOCAL_PORT}|grep "accepting"|wc -l`
if[[ $is_alive -eq 0 ]]; then
echo"database is down"
else
#主從
is_recovery=`psql-t -c "SELECT pg_is_in_recovery();"`
echo${is_recovery}
if[[ ${is_recovery} =~ "t" ]]; then
masterhost=`psql-t -c "select sender_host from pg_stat_wal_receiver;"`
masterport=`psql-t -c "select sender_port from pg_stat_wal_receiver;"`
logger-i "slave_backup will be running"
slave_backup${back_mode} ${masterhost} ${masterport}
else
logger-i "master_backup will be running"
master_backup${back_mode}
fi
fi
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/130102.html
pg_rman備份工具(下) img{ display:block; margin:0 auto !important; width:100%; } body{ width:75%; marg...
摘要:生產環境服務器環境搭建安裝安裝在系統中通過以下命令輸入查看是否安裝正確,輸出如下創建發布目錄,并給出相應的權限服務器和后臺文件夾上傳前端文件夾改變文件所有權文件的所有權變為加入參數,文件夾以及文件夾里的所有 生產環境服務器環境搭建 安裝jdk 安裝 openjdk-8-jdk 在 ubuntu-16.04 系統中通過以下命令: 1.sudo add-apt-repository pp...
閱讀 1346·2023-01-11 13:20
閱讀 1684·2023-01-11 13:20
閱讀 1132·2023-01-11 13:20
閱讀 1858·2023-01-11 13:20
閱讀 4100·2023-01-11 13:20
閱讀 2704·2023-01-11 13:20
閱讀 1385·2023-01-11 13:20
閱讀 3597·2023-01-11 13:20