溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

k8s中postgres基于PITR備份還原

發(fā)布時(shí)間:2020-07-23 03:13:43 來源:網(wǎng)絡(luò) 閱讀:426 作者:bestlope 欄目:系統(tǒng)運(yùn)維

參考網(wǎng)站:

postgres官網(wǎng)備份PITR文檔

Postgresql 連續(xù)歸檔和時(shí)間點(diǎn)恢復(fù)(PITR)

1、在k8s創(chuàng)建postgres數(shù)據(jù)庫

[用于創(chuàng)建postgres的yaml文件下載()

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

storageClassName: 'nfs-client'  #這里的nfs-client修改為自己k8s的持久化設(shè)備,這里使用的是已經(jīng)搭建好的nfs服務(wù)

啟動(dòng)postgres數(shù)據(jù)庫:

wget xxxxxxx.xxxxxx               #下載postgres數(shù)據(jù)庫啟動(dòng)需要的yaml文件
kubectl create namespace postgres #創(chuàng)建一個(gè)名叫postgres的namespace
kubens postgres                   #進(jìn)入這個(gè)namespace,kubens工具的作用是切換namespace需要去gitghub搜索kubectx工具,二進(jìn)制安裝即可使用
kubectl apply -f postgres/*.yaml  #啟動(dòng)postgres數(shù)據(jù)庫,所有動(dòng)作在postgres這個(gè)namespace完成

在postgres文件的配置文件中要打開的內(nèi)容:

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

查看postgres數(shù)據(jù)庫是否啟動(dòng)完成:

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

postgres在k8s啟動(dòng)成功。

2、數(shù)據(jù)準(zhǔn)備

進(jìn)入postgres操作

kubens postgres    #進(jìn)入postgres所在的namespace
kubectl exec -it postgres-0 sh   #進(jìn)入postgres命令

備份基礎(chǔ)數(shù)據(jù)庫文件

pg_basebackup   -D /backup/backup  -h postgres-0   -Fp  -R   -Pv  -l postgrebackup-20191112  #此文件為恢復(fù)的基礎(chǔ)文件

創(chuàng)建postgres日志備份目錄

mkdir /backup/archivedir   #以后postgres的日志會(huì)自動(dòng)導(dǎo)入這個(gè)目錄,也是PITR的關(guān)鍵

創(chuàng)建測(cè)試用表

psql   #進(jìn)入postgres數(shù)據(jù)庫
\c sso #選擇sso數(shù)據(jù)庫
\d     #查看該數(shù)據(jù)庫下沒有表
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

刪除測(cè)試用表

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

3、數(shù)據(jù)恢復(fù)演示

修改/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'  # 這里的時(shí)間修改為想要恢復(fù)的時(shí)間點(diǎn)
recovery_target_timeline='latest'

基礎(chǔ)數(shù)據(jù)文件恢復(fù)

mv /pgdata/postgres-0 /pgdata/postgres-0_bak        #破壞原數(shù)據(jù)文件
cp -r /backup/backup /pgdata/postgres-0             #將備份文件拷貝為數(shù)據(jù)庫文件
cd postgres-0

rm -rf pg_wal/0 && rm -rf pg_wal/archive_status/ #刪除老日志文件,以便PITR通過日志恢復(fù)

重啟postgres,使之自動(dòng)進(jìn)入恢復(fù)模式

kubectl delete pods postgres-0
kubectl get pods   

重啟成功后,即可進(jìn)入數(shù)據(jù)庫檢查是否已經(jīng)恢復(fù)到預(yù)定的數(shù)據(jù)。

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

如果出現(xiàn)操作失誤,導(dǎo)致不能進(jìn)入postgres的pod,可以將該pod的pvc刪除后,重啟pod即可重新操作。

?kubectl scale sts postgres --replicas=0  #先要關(guān)閉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不刪除
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI