k8s中postgres基於PITR備份還原

參考網站:

postgres官網備份PITR文檔

Postgresql 連續歸檔和時間點恢復(PITR)

1、在k8s創建postgres數據庫

[用於創建postgres的yaml文件下載()

需要修改70-statefulsets-postgres.yaml文件中的數據持久化方式,即

storageClassName: 'nfs-client'  #這裏的nfs-client修改爲自己k8s的持久化設備,這裏使用的是已經搭建好的nfs服務

啓動postgres數據庫:

wget xxxxxxx.xxxxxx               #下載postgres數據庫啓動需要的yaml文件
kubectl create namespace postgres #創建一個名叫postgres的namespace
kubens postgres                   #進入這個namespace,kubens工具的作用是切換namespace需要去gitghub搜索kubectx工具,二進制安裝即可使用
kubectl apply -f postgres/*.yaml  #啓動postgres數據庫,所有動作在postgres這個namespace完成

在postgres文件的配置文件中要打開的內容:

vim postgresql.conf
wal_level='hot_standby'    #wal_level至少設置爲replica
archive_mode='on'
archive_command='test ! -f /backup/archivedir/%f && cp %p /backup/archivedir/%f'

查看postgres數據庫是否啓動完成:

lopes-MacBook-Pro:postgres-demo_wal2json lope$ kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
postgres-0   1/1     Running   0          38m

postgres在k8s啓動成功。

2、數據準備

進入postgres操作

kubens postgres    #進入postgres所在的namespace
kubectl exec -it postgres-0 sh   #進入postgres命令

備份基礎數據庫文件

pg_basebackup   -D /backup/backup  -h postgres-0   -Fp  -R   -Pv  -l postgrebackup-20191112  #此文件爲恢復的基礎文件

創建postgres日誌備份目錄

mkdir /backup/archivedir   #以後postgres的日誌會自動導入這個目錄,也是PITR的關鍵

創建測試用表

psql   #進入postgres數據庫
\c sso #選擇sso數據庫
\d     #查看該數據庫下沒有表
create table test01(id int primary key,name varchar(20));
insert into test01 values(1,'a'),(2,'b'),(3,'c');
select current_timestamp;   #  2019-11-12 06:04:50.71881+00
select pg_switch_wal();     #   0/A000158

刪除測試用表

delete from test01;
select current_timestamp;   #   2019-11-12 06:07:36.529161+00
select pg_switch_wal();     #    0/C000000

3、數據恢復演示

修改/backup/backup/recovery.done文件(若是recovery.conf,則該爲recovery.done)

vim recovery.done
restore_command='cp /backup/archivedir/%f %p'
recovery_target_time='2019-11-12 06:04:50.71881+00'  # 這裏的時間修改爲想要恢復的時間點
recovery_target_timeline='latest'

基礎數據文件恢復

mv /pgdata/postgres-0 /pgdata/postgres-0_bak        #破壞原數據文件
cp -r /backup/backup /pgdata/postgres-0             #將備份文件拷貝爲數據庫文件
cd postgres-0

rm -rf pg_wal/0 && rm -rf pg_wal/archive_status/ #刪除老日誌文件,以便PITR通過日誌恢復

重啓postgres,使之自動進入恢復模式

kubectl delete pods postgres-0
kubectl get pods   

重啓成功後,即可進入數據庫檢查是否已經恢復到預定的數據。

kubectl exec -it postgres-0 sh
psql
\c sso
\d
select * from test01;

如果出現操作失誤,導致不能進入postgres的pod,可以將該pod的pvc刪除後,重啓pod即可重新操作。

 kubectl scale sts postgres --replicas=0  #先要關閉postgres才能刪除pvc
lopes-MacBook-Pro:postgres-demo_wal2json lope$ kubectl get pvc
NAME                STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
backup-postgres-0   Bound    pvc-1be89954-98f9-4f9d-a15a-780d5432d38a   30Gi       RWO            nfs-client     122m
pgdata-postgres-0   Bound    pvc-6f25fd78-282c-4604-a2f6-e9a8c767e002   30Gi       RWO            nfs-client     71m
lopes-MacBook-Pro:postgres-demo_wal2json lope$ kubectl delete pvc pgdata-postgres-0                         #刪除pgdata,backup不刪除
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章