与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
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/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...
阅读 1247·2023-01-11 13:20
阅读 1555·2023-01-11 13:20
阅读 1008·2023-01-11 13:20
阅读 1675·2023-01-11 13:20
阅读 3968·2023-01-11 13:20
阅读 2510·2023-01-11 13:20
阅读 1305·2023-01-11 13:20
阅读 3474·2023-01-11 13:20