濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Postgresql備份和增量恢復(fù)方案

Postgresql備份和增量恢復(fù)方案

熱門(mén)標(biāo)簽:外呼調(diào)研系統(tǒng) 打電話智能電銷機(jī)器人授權(quán) 海豐有多少商家沒(méi)有地圖標(biāo)注 重慶自動(dòng)外呼系統(tǒng)定制 地圖標(biāo)注和圖片名稱的區(qū)別 漯河外呼電話系統(tǒng) 美容工作室地圖標(biāo)注 合肥公司外呼系統(tǒng)運(yùn)營(yíng)商 辦公外呼電話系統(tǒng)

前言

最近工作上使用的數(shù)據(jù)庫(kù)一直是Postgresql,這是一款開(kāi)源的數(shù)據(jù)庫(kù),而且任何個(gè)人可以將該數(shù)據(jù)庫(kù)用于商業(yè)用途。在使用Postgresql的時(shí)候,讓我最明顯的感覺(jué)就是這數(shù)據(jù)庫(kù)做的真心好,雖然說(shuō)數(shù)據(jù)庫(kù)的安裝包真的很小,但是性能和操作的便捷是一點(diǎn)也不輸給其他商業(yè)的大型數(shù)據(jù)庫(kù),另外在命令行界面下對(duì)該數(shù)據(jù)庫(kù)直接進(jìn)行操作的感覺(jué)真的是很爽。在使用數(shù)據(jù)庫(kù)的時(shí)候,我們作為小公司的數(shù)據(jù)庫(kù)管理員有一項(xiàng)工作是不可能避免的,那就是數(shù)據(jù)的備份和恢復(fù)問(wèn)題。PostgreSQL雖然各個(gè)方面的有點(diǎn)很多,但是在數(shù)據(jù)庫(kù)備份這方面,它是不支持增量備份的,這點(diǎn)確實(shí)讓人覺(jué)得很是可惜啊。不過(guò),瑕不掩瑜,總的來(lái)說(shuō)這是一款很好的數(shù)據(jù)庫(kù)軟件。

之前,我們?cè)?《Postgresql主從異步流復(fù)制方案》 一節(jié)中,部署了Postgresql的主從異步流復(fù)制環(huán)境。主從復(fù)制的目的是為了實(shí)現(xiàn)數(shù)據(jù)的備份,實(shí)現(xiàn)數(shù)據(jù)的高可用性和容錯(cuò)行。下面主要簡(jiǎn)單地介紹下我們運(yùn)維Postgresql數(shù)據(jù)庫(kù)時(shí)的場(chǎng)景備份與恢復(fù)方案。

增量備份

PostgreSQL在做寫(xiě)入操作時(shí),對(duì)數(shù)據(jù)文件做的任何修改信息,首先會(huì)寫(xiě)入WAL日志(預(yù)寫(xiě)日志),然后才會(huì)對(duì)數(shù)據(jù)文件做物理修改。當(dāng)數(shù)據(jù)庫(kù)服務(wù)器掉重啟時(shí),PostgreSQL在啟動(dòng)時(shí)會(huì)首先讀取WAL日志,對(duì)數(shù)據(jù)文件進(jìn)行恢復(fù)。因此,從理論上講,如果我們有一個(gè)數(shù)據(jù)庫(kù)的基礎(chǔ)備份(也稱為全備),再配合WAL日志,是可以將數(shù)據(jù)庫(kù)恢復(fù)到任意時(shí)間點(diǎn)的。

上面的知識(shí)點(diǎn)很重要,因?yàn)槲覀儓?chǎng)景的增量備份說(shuō)白了就是通過(guò)基礎(chǔ)備份 + 增量WAL日志 進(jìn)行重做恢復(fù)的。

增量備份設(shè)置

為了演示相關(guān)功能,我們基于 《Postgresql主從異步流復(fù)制方案》 一節(jié)中的環(huán)境pghost1服務(wù)器上,創(chuàng) 建相關(guān)管理目錄

切換到 postgres 用戶下

mkdir -p /data/pg10/backups
mkdir -p /data/pg10/archive_wals

backups目錄則可以用來(lái)存放基礎(chǔ)備份

archive_wals目錄自然用來(lái)存放歸檔了

接下來(lái)我們修改我們的postgresql.conf文件的相關(guān)設(shè)置

wal_level = replica

archive_mode = on

archive_command = '/usr/bin/lz4 -q -z %p /data/pg10/archive_wals/%f.lz4'

archive_command 參數(shù)的默認(rèn)值是個(gè)空字符串,它的值可以是一條shell命令或者一個(gè)復(fù)雜的shell腳本。

在archive_command的shell命令或腳本中可以用 %p 表示將要?dú)w檔的WAL文件的包含完整路徑信息的文件名,用 %f 代表不包含路徑信息的WAL文件的文件名。

修改wal_level和archive_mode參數(shù)都需要重新啟動(dòng)數(shù)據(jù)庫(kù)才可以生效,修改archive_command不需要重啟,只需要reload即可,例如:

postgres=# SELECT pg_reload_conf();

postgres=# show archive_command ; 

創(chuàng)建基礎(chǔ)備份

我們使用之前介紹過(guò)的pg_basebackup命令進(jìn)行基礎(chǔ)備份的創(chuàng)建, 基礎(chǔ)備份很重要,我們的數(shù)據(jù)恢復(fù)不能沒(méi)有它,建議我們根據(jù)相關(guān)業(yè)務(wù)策略,周期性生成我們的基礎(chǔ)備份。

$ pg_basebackup -Ft -Pv -Xf -z -Z5 -p 25432 -D /data/pg10/backups/

這樣,我們就成功生成我們的基礎(chǔ)數(shù)據(jù)備份了

設(shè)置還原點(diǎn)

一般我們需要根據(jù)重要事件發(fā)生時(shí)創(chuàng)建一個(gè)還原點(diǎn),通過(guò)基礎(chǔ)備份和歸檔恢復(fù)到事件發(fā)生之前的狀態(tài)。

創(chuàng)建還原點(diǎn)的系統(tǒng)函數(shù)為:pg_create_restore_point,它的定義如下:

postgres=# SELECT pg_create_restore_point('domac-201810141800');

恢復(fù)到指定還原點(diǎn)

接下來(lái),我們通過(guò)一個(gè)示例,讓我們的數(shù)據(jù)還原到我們?cè)O(shè)置的還原點(diǎn)上

首先,我們創(chuàng)建一張測(cè)試表:

CREATE TABLE test_restore(
 id SERIAL PRIMARY KEY,
 ival INT NOT NULL DEFAULT 0,
 description TEXT,
 created_time TIMESTAMPTZ NOT NULL DEFAULT now()
);

初始化一些測(cè)試數(shù)據(jù)作為基礎(chǔ)數(shù)據(jù),如下所示:

postgres=# INSERT INTO test_restore (ival) VALUES (1);
INSERT 0 1
postgres=# INSERT INTO test_restore (ival) VALUES (2);
INSERT 0 1
postgres=# INSERT INTO test_restore (ival) VALUES (3);
INSERT 0 1
postgres=# INSERT INTO test_restore (ival) VALUES (4);
INSERT 0 1

postgres=# select * from test_restore;
 id | ival | description |   created_time
----+------+-------------+-------------------------------
 1 | 1 |    | 2018-10-14 11:13:41.57154+00
 2 | 2 |    | 2018-10-14 11:13:44.250221+00
 3 | 3 |    | 2018-10-14 11:13:46.311291+00
 4 | 4 |    | 2018-10-14 11:13:48.820479+00
(4 rows)

并且按照上文的方法創(chuàng)建一個(gè)基礎(chǔ)備份。如果是測(cè)試,有一點(diǎn)需要注意,由于WAL文件是寫(xiě)滿16MB才會(huì)進(jìn)行歸檔,測(cè)試階段可能寫(xiě)入會(huì)非常少,可以在執(zhí)行完 基礎(chǔ)備份之后,手動(dòng)進(jìn)行一次WAL切換。例如:

postgres=# select pg_switch_wal();
 pg_switch_wal
---------------
 0/1D01B858
(1 row)

或者通過(guò)設(shè)置archive_timeout參數(shù),在達(dá)到timeout閾值時(shí)強(qiáng)行切換到新的WAL段。

接下來(lái),創(chuàng)建一個(gè)還原點(diǎn),如下所示:

postgres=# select pg_create_restore_point('domac-1014');
 pg_create_restore_point
-------------------------
 0/1E0001A8
(1 row)

接下來(lái)我們對(duì)數(shù)據(jù)做一些變更, 我們刪除test_restore的所有數(shù)據(jù):

postgres=# delete from test_restore;
DELETE 4

下面進(jìn)行恢復(fù)到名稱為“domac-1014”還原點(diǎn)的實(shí)驗(yàn),如下所示:

停止數(shù)據(jù)庫(kù)

$ pg_ctl stop -D /data/pg10/db

移除舊的數(shù)據(jù)目錄

$ rm -rf /data/pg10/db

$ mkdir db  chmod 0700 db

$ tar -xvf /data/pg10/backups/base.tar.gz -C /data/pg10/db

cp $PGHOME/share/recovery.conf.sample /pgdata/10/data/recovery.conf

chmod 0600 /pgdata/10/data/recovery.conf

修改 recovery.conf, 修改以下配置信息:

restore_command = '/usr/bin/lz4 -d /data/pg10/archive_wals/%f.lz4 %p'
recovery_target_name = 'domac-1014

然后啟動(dòng)數(shù)據(jù)庫(kù)進(jìn)入恢復(fù)狀態(tài),觀察日志,如下所示:

bash-4.2$ pg_ctl start -D /data/pg10/db
waiting for server to start....2018-10-14 11:26:56.949 UTC [8397] LOG: listening on IPv4 address "0.0.0.0", port 25432
2018-10-14 11:26:56.949 UTC [8397] LOG: listening on IPv6 address "::", port 25432
2018-10-14 11:26:56.952 UTC [8397] LOG: listening on Unix socket "/tmp/.s.PGSQL.25432"
2018-10-14 11:26:56.968 UTC [8398] LOG: database system was interrupted; last known up at 2018-10-14 09:26:59 UTC
2018-10-14 11:26:57.049 UTC [8398] LOG: starting point-in-time recovery to "domac-1014"
/data/pg10/archive_wals/00000002.history.lz4: No such file or directory
2018-10-14 11:26:57.052 UTC [8398] LOG: restored log file "00000002.history" from archive
/data/pg10/archive_w : decoded 16777216 bytes
2018-10-14 11:26:57.077 UTC [8398] LOG: restored log file "000000020000000000000016" from archive
2018-10-14 11:26:57.191 UTC [8398] LOG: redo starts at 0/16000060
2018-10-14 11:26:57.193 UTC [8398] LOG: consistent recovery state reached at 0/16000130
2018-10-14 11:26:57.193 UTC [8397] LOG: database system is ready to accept read only connections
/data/pg10/archive_w : decoded 16777216 bytes
2018-10-14 11:26:57.217 UTC [8398] LOG: restored log file "000000020000000000000017" from archive
 done
server started
/data/pg10/archive_w : decoded 16777216 bytes
2018-10-14 11:26:57.384 UTC [8398] LOG: restored log file "000000020000000000000018" from archive
/data/pg10/archive_w : decoded 16777216 bytes
2018-10-14 11:26:57.513 UTC [8398] LOG: restored log file "000000020000000000000019" from archive
/data/pg10/archive_w : decoded 16777216 bytes
2018-10-14 11:26:57.699 UTC [8398] LOG: restored log file "00000002000000000000001A" from archive
/data/pg10/archive_w : decoded 16777216 bytes
2018-10-14 11:26:57.805 UTC [8398] LOG: restored log file "00000002000000000000001B" from archive
/data/pg10/archive_w : decoded 16777216 bytes
2018-10-14 11:26:57.982 UTC [8398] LOG: restored log file "00000002000000000000001C" from archive
/data/pg10/archive_w : decoded 16777216 bytes
2018-10-14 11:26:58.116 UTC [8398] LOG: restored log file "00000002000000000000001D" from archive
/data/pg10/archive_w : decoded 16777216 bytes
2018-10-14 11:26:58.310 UTC [8398] LOG: restored log file "00000002000000000000001E" from archive
2018-10-14 11:26:58.379 UTC [8398] LOG: recovery stopping at restore point "domac-1014", time 2018-10-14 11:17:20.680941+00
2018-10-14 11:26:58.379 UTC [8398] LOG: recovery has paused
2018-10-14 11:26:58.379 UTC [8398] HINT: Execute pg_wal_replay_resume() to continue.

重啟后,我們對(duì)test_restore表進(jìn)行查詢,看數(shù)據(jù)是否正?;謴?fù):

postgres=# select * from test_restore;
 id | ival | description |   created_time
----+------+-------------+-------------------------------
 1 | 1 |    | 2018-10-14 11:13:41.57154+00
 2 | 2 |    | 2018-10-14 11:13:44.250221+00
 3 | 3 |    | 2018-10-14 11:13:46.311291+00
 4 | 4 |    | 2018-10-14 11:13:48.820479+00
(4 rows)

可以看到數(shù)據(jù)已經(jīng)恢復(fù)到指定的還原點(diǎn):domac-1014。

這時(shí),recovery.conf可以移除,避免下次數(shù)據(jù)重啟,數(shù)據(jù)再次恢復(fù)到該還原點(diǎn)

總結(jié)

備份和恢復(fù)是數(shù)據(jù)庫(kù)管理中非常重要的工作,日常運(yùn)維中,我們需要根據(jù)需要進(jìn)行相關(guān)策略的備份,并且周期性地進(jìn)行恢復(fù)測(cè)試,保證數(shù)據(jù)的安全。

好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

您可能感興趣的文章:
  • 啟動(dòng)PostgreSQL服務(wù)器 并用pgAdmin連接操作
  • PostgreSQL管理工具phpPgAdmin入門(mén)指南
  • docker環(huán)境下數(shù)據(jù)庫(kù)的備份(postgresql, mysql) 實(shí)例代碼
  • 在Windows下自動(dòng)備份PostgreSQL的教程
  • postgreSQL使用pgAdmin備份服務(wù)器數(shù)據(jù)的方法

標(biāo)簽:來(lái)賓 錦州 株洲 晉城 烏海 珠海 衡陽(yáng) 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Postgresql備份和增量恢復(fù)方案》,本文關(guān)鍵詞  Postgresql,備份,和,增量,恢復(fù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Postgresql備份和增量恢復(fù)方案》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Postgresql備份和增量恢復(fù)方案的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    高尔夫| 乌鲁木齐市| 五大连池市| 叶城县| 新建县| 淮南市| 平遥县| 辽阳县| 墨竹工卡县| 云安县| 收藏| 延川县| 乌鲁木齐市| 信丰县| 房山区| 铜鼓县| 绿春县| 兰州市| 凤城市| 五原县| 根河市| 龙陵县| 湟中县| 韩城市| 宜宾市| 西畴县| 当阳市| 望江县| 额济纳旗| 肇源县| 寿阳县| 香河县| 泰来县| 澎湖县| 琼海市| 临江市| 沙坪坝区| 调兵山市| 沛县| 和林格尔县| 洛扎县|