備份
定時維護
定時維護,強制執行保留策略、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
or
select 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,檢查以下配置
- listen_addresses:改爲本機IP
- port:改爲本機端口
- shared_buffers:改爲本機合適的內存配置
- shared_preload_libraries:選擇本機已安裝的插件
修改pg_hba.conf,設置合適的訪問權限
檢查Barman備份的streaming目錄下是否有.partial後綴的文件,如果將該文件拷貝到恢復目錄的pg_wal下,並刪除.partial後綴
啓動Postgres
查看Postgre日誌,如出現以下內容,表示恢復到了指定的2019-01-04 01:07:30.563802+08時間點
2019-01-14 10:09:12 CST [30671]: [140-1] user=,db=,app=,client= LOG: recovery stopping before commit of transaction 23584393, time 2019-01-04 01:07:30.563802+08
2019-01-14 10:09:12 CST [30671]: [141-1] user=,db=,app=,client= LOG: recovery has paused
2019-01-14 10:09:12 CST [30671]: [142-1] user=,db=,app=,client= HINT: Execute pg_wal_replay_resume() to continue.
此時數據庫是隻讀的,可以打開數據庫查看數據是否恢復正確
- 如果正確,使用超級管理員用戶執行select pg_wal_replay_resume(),讓數據庫從只讀變爲可讀寫
- 如果不正確,停止數據庫,修改恢復目標,再次重啓恢復
參考:https://www.postgresql.org/docs/current/recovery-target-settings.html
遠程還原
還原目標服務器需要先安裝與備份數據庫相同版本的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,檢查以下配置
- listen_addresses:改爲本機IP
- port:改爲本機端口
- shared_buffers:改爲本機合適的內存配置
- shared_preload_libraries:選擇本機已安裝的插件
修改pg_hba.conf,設置合適的訪問權限
檢查Barman備份的streaming目錄下是否有.partial後綴的文件,如果將該文件拷貝到恢復目錄的pg_wal下,並刪除.partial後綴
啓動Postgres
查看Postgre日誌,如出現以下內容,表示恢復到了指定的2019-01-04 01:07:30.563802+08時間點
2019-01-14 10:09:12 CST [30671]: [140-1] user=,db=,app=,client= LOG: recovery stopping before commit of transaction 23584393, time 2019-01-04 01:07:30.563802+08
2019-01-14 10:09:12 CST [30671]: [141-1] user=,db=,app=,client= LOG: recovery has paused
2019-01-14 10:09:12 CST [30671]: [142-1] user=,db=,app=,client= HINT: Execute pg_wal_replay_resume() to continue.
此時數據庫是隻讀的,可以打開數據庫查看數據是否恢復正確
- 如果正確,使用超級管理員用戶執行select pg_wal_replay_resume(),讓數據庫從只讀變爲可讀寫
- 如果不正確,停止數據庫,修改恢復目標,再次重啓恢復
參考:https://www.postgresql.org/docs/current/recovery-target-settings.html