Postgres流式備份(6)備份和恢復

2021-10-03 10:22:29 字數 2918 閱讀 9217

定時維護,強制執行保留策略、wal檔案管理

barman如通過rpm安裝將自動新增定時配置檔案/etc/cron.d/barman,內容如下,設定每分鐘進行一次定時維護

# m h  dom mon dow   user     command

* * * * * barman [ -x /usr/bin/barman ] && /usr/bin/barman -q cron

手動執行備份

# 備份133,133為配置的服務名稱

barman backup 133

如果備份報錯:wal archive: failed (please make sure wal shipping is setup)

在資料庫上執行

select pg_switch_xlog() ; -- for 9.6 and earlier 

orselect pg_switch_wal() ; -- for 10

在barman上執行

# 執行維護,強制開始收取wal

barman cron;

# 檢查是否可以開始備份

barman check 133;

# 開始備份

barman back_up 133;

# 備份所有服務

barman backup all

可以使用cron配置定時執行備份,修改/etc/cron.d/barman,新增全量定時備份

# m h  dom mon dow   user     command

0 1 * * * barman [ -x /usr/bin/barman ] && /usr/bin/barman -q backup all

systemctl reload crond

barman replication-status 133
# 133為配置的服務名稱

barman list-backup 133

返回結果例如

133 20190104t012032 - fri jan  4 01:20:39 2019 - size: 48.6 mib - wal size: 0 b

133 20190103t232839 - thu jan 3 23:28:42 2019 - size: 48.5 mib - wal size: 320.0 mib

第二列為backup id,第四列為備份時間

需要先安裝與備份資料庫相同版本的postgresql

barman需要對還原的目標資料夾具備許可權

chown -r barman:barman /home/pg

chmod -r 700 /home/pg

恢復備份到指定時間點,可以大於當前時間,表示恢復到最新時間

su barman

barman recover --target-time "2019-01-04 01:07:30" 133 20190103t232839 /home/pg

postgres對還原的目標資料夾具備許可權

chown -r postgres:postgres /home/pg

chmod -r 700 /home/pg

修改伺服器配置檔案postgresql.conf,檢查以下配置

修改pg_hba.conf,設定合適的訪問許可權

檢查barman備份的streaming目錄下是否有.partial字尾的檔案,如果將該檔案拷貝到恢復目錄的pg_wal下,並刪除.partial字尾

啟動postgres

檢視postgre日誌,如出現以下內容,表示恢復到了指定的2019-01-04 01:07:30.563802+08時間點

此時資料庫是唯讀的,可以開啟資料庫檢視資料是否恢復正確

如果正確,使用超級管理員使用者執行select pg_wal_replay_resume(),讓資料庫從唯讀變為可讀寫

如果不正確,停止資料庫,修改恢復目標,再次重啟恢復

參考:還原目標伺服器需要先安裝與備份資料庫相同版本的postgresql

還原前需要先對被還原伺服器做ssh免秘鑰登入,否則還原將出錯

恢復備份到指定時間點,可以大於當前時間,表示恢復到最新時間

barman recover --remote-ssh-command "ssh [email protected]" --target-time "2019-01-04 01:07:30" 133 20190103t232839 /home/pg
postgres對還原的目標資料夾具備許可權

chown -r postgres:postgres /home/pg

chmod -r 700 /home/pg

修改伺服器配置檔案postgresql.conf,檢查以下配置

修改pg_hba.conf,設定合適的訪問許可權

檢查barman備份的streaming目錄下是否有.partial字尾的檔案,如果將該檔案拷貝到恢復目錄的pg_wal下,並刪除.partial字尾

啟動postgres

檢視postgre日誌,如出現以下內容,表示恢復到了指定的2019-01-04 01:07:30.563802+08時間點

此時資料庫是唯讀的,可以開啟資料庫檢視資料是否恢復正確

如果正確,使用超級管理員使用者執行select pg_wal_replay_resume(),讓資料庫從唯讀變為可讀寫

如果不正確,停止資料庫,修改恢復目標,再次重啟恢復

參考:

Postgres流式備份(1)Barman概述

barman是使用python編寫的postgresql開源備份和恢復管理器 pg basebackup 用於資料庫全量備份 pg receivewal pg receivexlog 用於基於全量對wal增量備份,可實現很低的資料丟失時間 archive command wal檔案切換 寫滿指定大小...

Postgres流式備份(3)配置檔案

屬性 位置說明 active 伺服器預設true,false時只能用於診斷該伺服器,無法執行備份等命令 archiver 全域性 服務 預設true,指定是否啟用歸檔日誌備份 backup directory 服務服務備份存放位置 backup method 全域性 服務 預設rsync,使用rsy...

postgres 連續備份

準備 main pg 建立postgres使用者,能夠免密登入到backup pg backup pg 建立postgres使用者 1 main pg修改配置,開啟歸檔模式 不開啟,產生少量wal檔案 開啟,產生完整wal檔案 mkdir home postgres archive wals hos...