麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 數(shù)據(jù)庫(kù) > PostgreSQL > 正文

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

2020-01-31 15:20:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

最近工作上使用的數(shù)據(jù)庫(kù)一直是Postgresql,這是一款開源的數(shù)據(jù)庫(kù),而且任何個(gè)人可以將該數(shù)據(jù)庫(kù)用于商業(yè)用途。在使用Postgresql的時(shí)候,讓我最明顯的感覺就是這數(shù)據(jù)庫(kù)做的真心好,雖然說(shuō)數(shù)據(jù)庫(kù)的安裝包真的很小,但是性能和操作的便捷是一點(diǎn)也不輸給其他商業(yè)的大型數(shù)據(jù)庫(kù),另外在命令行界面下對(duì)該數(shù)據(jù)庫(kù)直接進(jìn)行操作的感覺真的是很爽。在使用數(shù)據(jù)庫(kù)的時(shí)候,我們作為小公司的數(shù)據(jù)庫(kù)管理員有一項(xiàng)工作是不可能避免的,那就是數(shù)據(jù)的備份和恢復(fù)問(wèn)題。PostgreSQL雖然各個(gè)方面的有點(diǎn)很多,但是在數(shù)據(jù)庫(kù)備份這方面,它是不支持增量備份的,這點(diǎn)確實(shí)讓人覺得很是可惜啊。不過(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在做寫入操作時(shí),對(duì)數(shù)據(jù)文件做的任何修改信息,首先會(huì)寫入WAL日志(預(yù)寫日志),然后才會(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/backupsmkdir -p /data/pg10/archive_wals

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

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

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

wal_level = replicaarchive_mode = onarchive_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ù)不能沒有它,建議我們根據(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 1postgres=# INSERT INTO test_restore (ival) VALUES (2);INSERT 0 1postgres=# INSERT INTO test_restore (ival) VALUES (3);INSERT 0 1postgres=# INSERT INTO test_restore (ival) VALUES (4);INSERT 0 1postgres=# 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文件是寫滿16MB才會(huì)進(jìn)行歸檔,測(cè)試階段可能寫入會(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/dbcp $PGHOME/share/recovery.conf.sample /pgdata/10/data/recovery.confchmod 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/dbwaiting for server to start....2018-10-14 11:26:56.949 UTC [8397] LOG: listening on IPv4 address "0.0.0.0", port 254322018-10-14 11:26:56.949 UTC [8397] LOG: listening on IPv6 address "::", port 254322018-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 UTC2018-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 directory2018-10-14 11:26:57.052 UTC [8398] LOG: restored log file "00000002.history" from archive/data/pg10/archive_w : decoded 16777216 bytes2018-10-14 11:26:57.077 UTC [8398] LOG: restored log file "000000020000000000000016" from archive2018-10-14 11:26:57.191 UTC [8398] LOG: redo starts at 0/160000602018-10-14 11:26:57.193 UTC [8398] LOG: consistent recovery state reached at 0/160001302018-10-14 11:26:57.193 UTC [8397] LOG: database system is ready to accept read only connections/data/pg10/archive_w : decoded 16777216 bytes2018-10-14 11:26:57.217 UTC [8398] LOG: restored log file "000000020000000000000017" from archive doneserver started/data/pg10/archive_w : decoded 16777216 bytes2018-10-14 11:26:57.384 UTC [8398] LOG: restored log file "000000020000000000000018" from archive/data/pg10/archive_w : decoded 16777216 bytes2018-10-14 11:26:57.513 UTC [8398] LOG: restored log file "000000020000000000000019" from archive/data/pg10/archive_w : decoded 16777216 bytes2018-10-14 11:26:57.699 UTC [8398] LOG: restored log file "00000002000000000000001A" from archive/data/pg10/archive_w : decoded 16777216 bytes2018-10-14 11:26:57.805 UTC [8398] LOG: restored log file "00000002000000000000001B" from archive/data/pg10/archive_w : decoded 16777216 bytes2018-10-14 11:26:57.982 UTC [8398] LOG: restored log file "00000002000000000000001C" from archive/data/pg10/archive_w : decoded 16777216 bytes2018-10-14 11:26:58.116 UTC [8398] LOG: restored log file "00000002000000000000001D" from archive/data/pg10/archive_w : decoded 16777216 bytes2018-10-14 11:26:58.310 UTC [8398] LOG: restored log file "00000002000000000000001E" from archive2018-10-14 11:26:58.379 UTC [8398] LOG: recovery stopping at restore point "domac-1014", time 2018-10-14 11:17:20.680941+002018-10-14 11:26:58.379 UTC [8398] LOG: recovery has paused2018-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ì)武林網(wǎng)的支持。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 91短视频网址 | 91九色蝌蚪在线 | 在线成人亚洲 | 涩涩99 | 国产精品国产三级国产在线观看 | 一级电影免费 | 国产88久久久国产精品免费二区 | 91 在线视频观看 | 亚洲精品在线观看免费 | 鲁丝一区二区三区不属 | 中文字幕综合在线观看 | 成人视屏在线观看 | 96视频在线免费观看 | 蜜桃视频在线观看免费 | 中文字幕精品在线播放 | 一级黄色在线免费观看 | 毛片电影网址 | 成人午夜视频免费看 | 一级毛片在线观看免费 | 久久国产精品免费视频 | 娇喘在线| 久久免费观看一级毛片 | 黄色特级视频 | 97视频| 日韩精品中文字幕一区 | 伊人久操视频 | 毛片免费视频播放 | 国产精品免费久久久 | 内地av在线 | 91精品动漫在线观看 | 国产亚洲精品久久777777 | 一级黄色片在线看 | 日韩视频―中文字幕 | av在线看网站 | 国产美女视频一区二区三区 | 国产免费一区视频 | 成人福利视频 | 国产精品区一区二区三区 | 久久久一区二区 | 亚洲骚综合 | 久久一本日日摸夜夜添 |