在 PostgreSQL 數(shù)據(jù)庫;目前是不支持類似 Oracle 閃回特性;我們知道 Oracle 閃回特性在數(shù)據(jù)恢復是非常簡單給力。增加數(shù)據(jù)庫操作人員(開發(fā),DBA)的容錯率。Oracle 閃回特性使用場景:
對于 PostgreSQL 閃回;校長(德哥)有寫過一系列的文章PostgreSQL flashback(閃回)功能實現(xiàn)與介紹;如今我取巧實現(xiàn) PostgreSQL 閃回特性。原理參考我之前的文章PostgreSQL恢復誤刪數(shù)據(jù)。
實現(xiàn) flashback 就是一個有效的數(shù)據(jù)庫備份/恢復。
實現(xiàn) flashback 可以是在線服務器上操作,也可以是在另一臺服務器上操作。換句話說:可以是一臺服務器或者是兩臺服務器來實現(xiàn)。在一臺服務器上需要更改數(shù)據(jù)庫端口。由于本環(huán)境有清理目錄操作;避免在在線服務器上誤刪目錄。所以建議用兩條服務器來實現(xiàn)。
本文檔采用兩臺服務器來實現(xiàn)。兩臺服務器時間校準,免密碼登錄。
cleandir腳本用于在pg_basebackup備份前;先清空備份所需的路徑
psql -h 192.168.1.201 -p $PGPORT -t -A -n -c"SELECT pg_catalog.pg_tablespace_location(oid) AS "Location" FROM pg_catalog.pg_tablespace where spcname not in ('pg_default','pg_global')" -o tablespace_location.txtawk '{print "rm -rf " $1}' tablespace_location.txt > cleandir.shecho "rm -rf $PGDATA" >> cleandir.sh
采用pg_basebackup備份;
cleandir.shpg_basebackup -F p --progress -D $PGDATA -h 192.168.1.201 -p $PGPORT -U replica
加入定時任務 crontab 中 例如:每天凌晨過5分執(zhí)行備份
5 0 * * * pg_backup.sh
執(zhí)行閃回查詢/數(shù)據(jù)庫恢復。多用時間點進行恢復即(PITR)
restore_command = 'scp postgres@192.168.1.201:**/%f %p'recovery_target_time = '2018-11-10 10:33:12'
pg_flashback.sh 腳本用于數(shù)據(jù)庫誤操作實現(xiàn)閃回到某個時間點。支持多次閃回。執(zhí)行閃回 之前;需要配置 recovery.conf 中的 “recovery_target_time” 時間點。即恢復到哪個時間點。
第一次執(zhí)行;執(zhí)行
pg_flashback.sh 1
往后執(zhí)行閃回;通稱再次執(zhí)行;執(zhí)行命令
pg_flashback.sh 2
當然往后加也行例如:pg_flashback.sh 3
pg_flashback.sh腳本如下
##=========================================================== ## pg_flashback.sh ## created by lottu ## 2018/11/07 ## usage: pg_flashback.sh 1 ##============================================================ #!/bin/bashPGHOME=/opt/pgsql96PGDATA=/data/postgres/data LANG=en_US.utf8PGPORT=5432PGUSER=postgresPGDATABASE=postgresPG_BACK_HOST_IP=192.168.1.202E_BADARGS=65E_FAIL=66if [ $# -ne 1 ];then echo "Usage: `basename $0` Please invoke this script with one command-line arguments" exit $E_BADARGSfiflashback_path=/data/flash_backif [ $1 -eq 1 ];then echo "Info: backup backupset" backup.shelse rebackup.shfi echo "Info: edit recovery.conf"#scp recovery.conf postgres@192.168.1.202:$PGDATAcp /home/postgres/recovery.conf $PGDATAecho "Info: begin start database"pg_ctl start -l /home/postgres/log/pg_server.logif [ $? -eq 0 ];then echo "Info: start database succes" sleep 10 psql -h $PG_BACK_HOST_IP -p 5432 postgres postgres -c "select pg_xlog_replay_resume()"else echo "[Error]: start database fail" exit $E_FAILfi
執(zhí)行pg_backup.sh腳本。部署成功了是沒這個操作。因為每天都有定時備份。
[postgres@Postgres202 ~]$ pg_backup.sh waiting for server to shut down......... doneserver stopped 322503/322503 kB (100%), 4/4 tablespaces
lottu=# create table lottu as select * from lottu01 ; SELECT 4 lottu=# select * from lottu; id | text ------+------- 1001 | lottu 1004 | rax 1002 | world 1003 | world (4 rows) -- 獲取時間點;這個時間點“2018-11-10 14:56:56”是我目標恢復時間點。表lottu有4條記錄。lottu=# select now(); now ------------------------------ 2018-11-10 14:56:56.30188+08 (1 row) -- 在時間點“2018-11-10 14:57:51”;誤操作1 清理表lottu的記錄lottu=# delete from lottu; DELETE 4 lottu=# select now(); now ------------------------------- 2018-11-10 14:57:51.891931+08 (1 row) -- 誤操作2 刪除表lottu。lottu=# drop table lottu; DROP TABLE
我們先在配置文件 recovery.conf 中的參數(shù)“recovery_target_time” 設置為 “2018-11-10 14:57:51”。執(zhí)行閃回pg_flashback.sh 1
[postgres@Postgres202 ~]$ pg_flashback.sh 1 Info: backup backupset Info: edit recovery.conf Info: begin start databaseserver starting Info: start database succespsql: FATAL: the database system is starting up
閃回數(shù)據(jù)庫成功啟動;登錄查看表lottu情況:表數(shù)據(jù)未找回;這不是我所需閃回的目標
[postgres@Postgres202 ~]$ psql lottu lottupsql (9.6.0) Type "help" for help. lottu=# \d lottu Table "lottu.lottu" Column | Type | Modifiers --------+---------+----------- id | integer | name | text | lottu=# select * from lottu; id | name ----+------ (0 rows)
在配置文件 recovery.conf 中的參數(shù)“recovery_target_time” 設置為 “2018-11-10 14:56:56”。再執(zhí)行閃回pg_flashback.sh 2
[postgres@Postgres202 ~]$ pg_flashback.sh 2 Info: edit recovery.conf Info: begin start databaseserver starting Info: start database successpsql: FATAL: the database system is starting up
再次閃回也成功了。我們在看下表lottu的情況
[postgres@Postgres202 ~]$ psql lottu lottupsql (9.6.0) Type "help" for help. lottu=# \d lottu Table "lottu.lottu" Column | Type | Modifiers --------+---------+----------- id | integer | name | text | lottu=# select * from lottu; id | name ------+------- 1001 | lottu 1004 | rax 1002 | world 1003 | world (4 rows)
表 lottu 的數(shù)據(jù)成功找回。在使用 pg_dump 導出表 lottu 的表結(jié)構(gòu)和數(shù)據(jù)的腳本;
[postgres@Postgres202 ~]$ pg_dump -d lottu -t lottu.lottu -f lottu.sql [postgres@Postgres202 ~]$ scp lottu.sql postgres@192.168.1.201:/home/postgres
再到正式數(shù)據(jù)庫上執(zhí)行
[postgres@Postgres201 ~]$ psql lottu lottu psql (9.6.0) Type "help" for help. lottu=# \i lottu.sql SET SET SET SET SET SET SET SET SET SET SET CREATE TABLE ALTER TABLE COPY 4 lottu=# \d lottu Table "lottu.lottu" Column | Type | Modifiers --------+---------+----------- id | integer | name | text | lottu=# select * from lottu; id | name ------+------- 1001 | lottu 1004 | rax 1002 | world 1003 | world (4 rows)
在整個閃回過程;除了最后一步是在正式服務器上執(zhí)行。前面其他操作都是在閃回數(shù)據(jù)庫操作。
PostgreSQL flashback(閃回) 功能實現(xiàn)與介紹
鏈接:https://pan.baidu.com/s/1j8ZNj3yjxj82MnomzxQkfA
提取碼:j4xd
創(chuàng)新互聯(lián)面向全國提供域名注冊、虛擬主機、云服務器、服務器托管與租用,如需了解,請聯(lián)系QQ:171356849微信:zh18159893430 咨詢,謝謝!
新聞標題:告訴你如何打造PostgreSQL閃回環(huán)境
標題URL:http://chinadenli.net/article36/ddpsg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供云服務器、做網(wǎng)站、外貿(mào)網(wǎng)站建設、網(wǎng)站內(nèi)鏈、小程序開發(fā)、響應式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)