在 PostgreSQL 數(shù)據(jù)庫(kù);目前是不支持類似 Oracle 閃回特性;我們知道 Oracle 閃回特性在數(shù)據(jù)恢復(fù)是非常簡(jiǎn)單給力。增加數(shù)據(jù)庫(kù)操作人員(開(kāi)發(fā),DBA)的容錯(cuò)率。Oracle 閃回特性使用場(chǎng)景:
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供儋州網(wǎng)站建設(shè)、儋州做網(wǎng)站、儋州網(wǎng)站設(shè)計(jì)、儋州網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、儋州企業(yè)網(wǎng)站模板建站服務(wù),10多年儋州做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。對(duì)于 PostgreSQL 閃回;校長(zhǎng)(德哥)有寫(xiě)過(guò)一系列的文章PostgreSQL flashback(閃回)功能實(shí)現(xiàn)與介紹;如今我取巧實(shí)現(xiàn) PostgreSQL 閃回特性。原理參考我之前的文章PostgreSQL恢復(fù)誤刪數(shù)據(jù)。
實(shí)現(xiàn) flashback 就是一個(gè)有效的數(shù)據(jù)庫(kù)備份/恢復(fù)。
實(shí)現(xiàn) flashback 可以是在線服務(wù)器上操作,也可以是在另一臺(tái)服務(wù)器上操作。換句話說(shuō):可以是一臺(tái)服務(wù)器或者是兩臺(tái)服務(wù)器來(lái)實(shí)現(xiàn)。在一臺(tái)服務(wù)器上需要更改數(shù)據(jù)庫(kù)端口。由于本環(huán)境有清理目錄操作;避免在在線服務(wù)器上誤刪目錄。所以建議用兩條服務(wù)器來(lái)實(shí)現(xiàn)。
本文檔采用兩臺(tái)服務(wù)器來(lái)實(shí)現(xiàn)。兩臺(tái)服務(wù)器時(shí)間校準(zhǔ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
加入定時(shí)任務(wù) crontab 中 例如:每天凌晨過(guò)5分執(zhí)行備份
5 0 * * * pg_backup.sh
執(zhí)行閃回查詢/數(shù)據(jù)庫(kù)恢復(fù)。多用時(shí)間點(diǎn)進(jìn)行恢復(fù)即(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ù)庫(kù)誤操作實(shí)現(xiàn)閃回到某個(gè)時(shí)間點(diǎn)。支持多次閃回。執(zhí)行閃回 之前;需要配置 recovery.conf 中的 “recovery_target_time” 時(shí)間點(diǎn)。即恢復(fù)到哪個(gè)時(shí)間點(diǎn)。
第一次執(zhí)行;執(zhí)行
pg_flashback.sh 1
往后執(zhí)行閃回;通稱再次執(zhí)行;執(zhí)行命令
pg_flashback.sh 2
當(dāng)然往后加也行例如: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腳本。部署成功了是沒(méi)這個(gè)操作。因?yàn)槊刻於加卸〞r(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) -- 獲取時(shí)間點(diǎn);這個(gè)時(shí)間點(diǎn)“2018-11-10 14:56:56”是我目標(biāo)恢復(fù)時(shí)間點(diǎn)。表lottu有4條記錄。lottu=# select now(); now ------------------------------ 2018-11-10 14:56:56.30188+08 (1 row) -- 在時(shí)間點(diǎn)“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” 設(shè)置為 “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
閃回?cái)?shù)據(jù)庫(kù)成功啟動(dòng);登錄查看表lottu情況:表數(shù)據(jù)未找回;這不是我所需閃回的目標(biāo)
[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” 設(shè)置為 “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
再次閃回也成功了。我們?cè)诳聪卤韑ottu的情況
[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 導(dǎo)出表 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ù)庫(kù)上執(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)
在整個(gè)閃回過(guò)程;除了最后一步是在正式服務(wù)器上執(zhí)行。前面其他操作都是在閃回?cái)?shù)據(jù)庫(kù)操作。
PostgreSQL flashback(閃回) 功能實(shí)現(xiàn)與介紹
鏈接:https://pan.baidu.com/s/1j8ZNj3yjxj82MnomzxQkfA
提取碼:j4xd
創(chuàng)新互聯(lián)面向全國(guó)提供域名注冊(cè)、虛擬主機(jī)、云服務(wù)器、服務(wù)器托管與租用,如需了解,請(qǐng)聯(lián)系QQ:171356849微信:zh18159893430 咨詢,謝謝!