前言
最近工作上使用的數(shù)據(jù)庫一直是Postgresql,這是一款開源的數(shù)據(jù)庫,而且任何個(gè)人可以將該數(shù)據(jù)庫用于商業(yè)用途。在使用Postgresql的時(shí)候,讓我最明顯的感覺就是這數(shù)據(jù)庫做的真心好,雖然說數(shù)據(jù)庫的安裝包真的很小,但是性能和操作的便捷是一點(diǎn)也不輸給其他商業(yè)的大型數(shù)據(jù)庫,另外在命令行界面下對(duì)該數(shù)據(jù)庫直接進(jìn)行操作的感覺真的是很爽。在使用數(shù)據(jù)庫的時(shí)候,我們作為小公司的數(shù)據(jù)庫管理員有一項(xiàng)工作是不可能避免的,那就是數(shù)據(jù)的備份和恢復(fù)問題。PostgreSQL雖然各個(gè)方面的有點(diǎn)很多,但是在數(shù)據(jù)庫備份這方面,它是不支持增量備份的,這點(diǎn)確實(shí)讓人覺得很是可惜啊。不過,瑕不掩瑜,總的來說這是一款很好的數(shù)據(jù)庫軟件。
之前,我們?cè)?《Postgresql主從異步流復(fù)制方案》 一節(jié)中,部署了Postgresql的主從異步流復(fù)制環(huán)境。主從復(fù)制的目的是為了實(shí)現(xiàn)數(shù)據(jù)的備份,實(shí)現(xiàn)數(shù)據(jù)的高可用性和容錯(cuò)行。下面主要簡單地介紹下我們運(yùn)維Postgresql數(shù)據(jù)庫時(shí)的場景備份與恢復(fù)方案。
增量備份
PostgreSQL在做寫入操作時(shí),對(duì)數(shù)據(jù)文件做的任何修改信息,首先會(huì)寫入WAL日志(預(yù)寫日志),然后才會(huì)對(duì)數(shù)據(jù)文件做物理修改。當(dāng)數(shù)據(jù)庫服務(wù)器掉重啟時(shí),PostgreSQL在啟動(dòng)時(shí)會(huì)首先讀取WAL日志,對(duì)數(shù)據(jù)文件進(jìn)行恢復(fù)。因此,從理論上講,如果我們有一個(gè)數(shù)據(jù)庫的基礎(chǔ)備份(也稱為全備),再配合WAL日志,是可以將數(shù)據(jù)庫恢復(fù)到任意時(shí)間點(diǎn)的。
上面的知識(shí)點(diǎn)很重要,因?yàn)槲覀儓鼍暗脑隽總浞菡f白了就是通過基礎(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目錄則可以用來存放基礎(chǔ)備份
archive_wals目錄自然用來存放歸檔了
接下來我們修改我們的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ù)庫才可以生效,修改archive_command不需要重啟,只需要reload即可,例如:
postgres=# SELECT pg_reload_conf();
postgres=# show archive_command ;
創(chuàng)建基礎(chǔ)備份
我們使用之前介紹過的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),通過基礎(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)
接下來,我們通過一個(gè)示例,讓我們的數(shù)據(jù)還原到我們?cè)O(shè)置的還原點(diǎn)上
首先,我們創(chuàng)建一張測試表:
CREATE TABLE test_restore(
id SERIAL PRIMARY KEY,
ival INT NOT NULL DEFAULT 0,
description TEXT,
created_time TIMESTAMPTZ NOT NULL DEFAULT now()
);
初始化一些測試數(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ǔ)備份。如果是測試,有一點(diǎn)需要注意,由于WAL文件是寫滿16MB才會(huì)進(jìn)行歸檔,測試階段可能寫入會(huì)非常少,可以在執(zhí)行完 基礎(chǔ)備份之后,手動(dòng)進(jìn)行一次WAL切換。例如:
postgres=# select pg_switch_wal();
pg_switch_wal
---------------
0/1D01B858
(1 row)
或者通過設(shè)置archive_timeout參數(shù),在達(dá)到timeout閾值時(shí)強(qiáng)行切換到新的WAL段。
接下來,創(chuàng)建一個(gè)還原點(diǎn),如下所示:
postgres=# select pg_create_restore_point('domac-1014');
pg_create_restore_point
-------------------------
0/1E0001A8
(1 row)
接下來我們對(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ù)庫
$ 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ù)庫進(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ù)庫管理中非常重要的工作,日常運(yùn)維中,我們需要根據(jù)需要進(jìn)行相關(guān)策略的備份,并且周期性地進(jìn)行恢復(fù)測試,保證數(shù)據(jù)的安全。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
您可能感興趣的文章:- 啟動(dòng)PostgreSQL服務(wù)器 并用pgAdmin連接操作
- PostgreSQL管理工具phpPgAdmin入門指南
- docker環(huán)境下數(shù)據(jù)庫的備份(postgresql, mysql) 實(shí)例代碼
- 在Windows下自動(dòng)備份PostgreSQL的教程
- postgreSQL使用pgAdmin備份服務(wù)器數(shù)據(jù)的方法