postgresql 災(zāi)備自動配置腳本-barman

前置條件:

postgreql版本:12+
barman版本:2.+
運行環(huán)境:centos系列加上網(wǎng)

腳本:

結(jié)合末尾的執(zhí)行參數(shù)看更容易理解

cat pg_disaster_recovery_with_barman.sh
#!/bin/bash
set -o nounset #遇到變量不存在,腳本報錯
set -o xtrace # 打印執(zhí)行命令
set -o errexit # 錯誤退出
set -o pipefail # 只要一個子命令失敗铃慷,整個管道命令就失敗漩蟆,腳本就會終止執(zhí)行
target=$1

# 設(shè)置本地yum源
function set_pg_repo(){
echo "[barman-pg]
name=barman-pg-rhel7
baseurl=https://download.postgresql.org/pub/repos/yum/12/redhat/rhel-8-x86_64/
gpgcheck=0" > /etc/yum.repos.d/barman-pg.repo
}

function set_pg_admin_secret(){
echo "${pg_host}:5432:*:${pg_user}:${pg_password}" > ${HOME}/.pgpass
chmod 600 ${HOME}/.pgpass

}

function create_pg_user_barman(){
psql -h ${pg_host} -U ${pg_user} -c "CREATE USER barman WITH superuser LOGIN REPLICATION createdb createrole ENCRYPTED password '${pg_password}';"
}

function install_barman(){
    yum install epel-release -y
    set_pg_repo
    yum install  barman -y
    yum  install  postgresql12 -y
}


function config_barman(){
set_pg_repo
install_barman
set_pg_admin_secret
create_pg_user_barman
mkdir -p  /var/lib/barman/
echo "${pg_host}:5432:*:barman:${pg_password}" >/var/lib/barman/.pgpass
chown barman:barman /var/lib/barman/.pgpass
chmod 600 /var/lib/barman/.pgpass  
if su - barman -c "psql -c 'SELECT version()' -U barman -h ${pg_host} postgres"
then
    echo "barman user is ok"
else
    echo "barman user in pg is bad"
    exit 1
fi
sed  -e 's/streaming_barman/barman/' -e "s/=pg/=${pg_host}/" -e 's/;path_prefix/path_prefix/'  /etc/barman.d/streaming-server.conf-template > /etc/barman.d/streaming-server.conf
echo "export PATH=\$PATH:/usr/pgsql-12/bin" > /etc/profile.d/pg.sh
source /etc/profile.d/pg.sh

barman receive-wal --create-slot streaming
if ! barman switch-xlog --force --archive --archive-timeout 60 streaming  # if failed, do it baragain
then
    barman switch-xlog --force --archive --archive-timeout 60 streaming 
fi
barman check streaming
}

if test $target = "barman"
then
    pg_host=$2
    pg_user=$3
    pg_password=$4
    config_barman
    echo "config barman successful"
    echo "execute barman backup streaming to start backup"
fi

使用方式

例如

bash pg_disaster_recovery_with_barman.sh barman 10.0.0.1 postgres 123456

備份恢復(fù)

本地恢復(fù)

barman list-backup streaming
chown barman /var/lib/postgresql
barman recover --target-time "2021-07-07 17:56:09+08:00" --target-action promote  streaming 20210707T173939 /var/lib/postgresql
#barman recover  streaming 20210707T145829 /var/lib/postgresql
chown -R postgres:postgres /var/lib/postgresql
mv -f /opt/data/postgresql/postgresql.auto.conf{.origin,}
mv -f  /opt/data/postgresql/postgresql.conf{.origin,} 

遠(yuǎn)程恢復(fù)

#為barman用戶生成證書
su - barman -c "ssh-keygen -t rsa -P '' -b 4096 -C 'barman@pg-barman' -m PEM  -f /var/lib/barman/pg.pem"
# 把公鑰/var/lib/barman/pg.pem放在pg恢復(fù)的目標(biāo)數(shù)據(jù)庫主機(jī)上有權(quán)限的用戶喻鳄,具體請了解linux公鑰交換認(rèn)證
su - barman -c "cat /var/lib/barman/pg.pem.pub"
#恢復(fù)示例
barman recover --remote-ssh-command "ssh root@10.0.0.1 -i /var/lib/barman/pg.pem" --target-time "2021-07-07 17:56:09+08:00" --target-action promote  streaming 20210707T173939 /opt/data/postgresql

工作中使用的系宫,效果很好,遇到問題請留言討論

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市腹殿,隨后出現(xiàn)的幾起案子舷蒲,更是在濱河造成了極大的恐慌,老刑警劉巖扇谣,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件昧捷,死亡現(xiàn)場離奇詭異,居然都是意外死亡罐寨,警方通過查閱死者的電腦和手機(jī)靡挥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鸯绿,“玉大人跋破,你說我怎么就攤上這事∑亢” “怎么了毒返?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長舷手。 經(jīng)常有香客問我拧簸,道長,這世上最難降的妖魔是什么聚霜? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任狡恬,我火速辦了婚禮,結(jié)果婚禮上蝎宇,老公的妹妹穿的比我還像新娘弟劲。我一直安慰自己,他們只是感情好姥芥,可當(dāng)我...
    茶點故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布兔乞。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪庸追。 梳的紋絲不亂的頭發(fā)上霍骄,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天,我揣著相機(jī)與錄音淡溯,去河邊找鬼读整。 笑死,一個胖子當(dāng)著我的面吹牛咱娶,可吹牛的內(nèi)容都是我干的米间。 我是一名探鬼主播,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼膘侮,長吁一口氣:“原來是場噩夢啊……” “哼屈糊!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起琼了,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤逻锐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后雕薪,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體昧诱,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年蹦哼,在試婚紗的時候發(fā)現(xiàn)自己被綠了鳄哭。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,030評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡纲熏,死狀恐怖妆丘,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情局劲,我是刑警寧澤勺拣,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站鱼填,受9級特大地震影響药有,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜苹丸,卻給世界環(huán)境...
    茶點故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一愤惰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧赘理,春花似錦宦言、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蜘澜。三九已至,卻和暖如春响疚,著一層夾襖步出監(jiān)牢的瞬間鄙信,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工忿晕, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留装诡,地道東北人。 一個月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓杏糙,卻偏偏與公主長得像慎王,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子宏侍,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容