本篇內(nèi)容介紹了“怎么恢復(fù)PostgreSQL數(shù)據(jù)文件損壞”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
定西ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
在數(shù)據(jù)文件損壞,數(shù)據(jù)庫無法正常啟動的時候,Oracle可通過數(shù)據(jù)庫備份+歸檔日志+在線日志實現(xiàn)數(shù)據(jù)庫的完整恢復(fù),與之類似,PostgreSQL也可以通過數(shù)據(jù)庫備份+歸檔WAL日志+在線WAL日志進行完整恢復(fù).
1.執(zhí)行備份
2.構(gòu)造測試數(shù)據(jù)
3.刪除數(shù)據(jù)文件(保留WAL日志文件)
4.使用備份+在線WAL日志文件進行完整恢復(fù)
參數(shù)配置
archive_mode = on archive_command = '/home/xdb/archive.sh' wal_level = replica max_wal_size = 4GB min_wal_size = 1024MB
詳細請參考 Backup&Recovery#1(基本操作)
執(zhí)行備份
查看當(dāng)前的LSN
testdb=# select pg_current_wal_lsn(); pg_current_wal_lsn -------------------- 0/39A63C78 (1 row)
使用工具pg_basebackup對數(shù)據(jù)庫進行備份
testdb=# \q [xdb@localhost testdb]$ pg_basebackup -D /data/backup/0312-1/ -l 0312-1 -v -F tar -z pg_basebackup: initiating base backup, waiting for checkpoint to complete pg_basebackup: checkpoint completed pg_basebackup: write-ahead log start point: 0/3A000108 on timeline 15 pg_basebackup: starting background WAL receiver pg_basebackup: created temporary replication slot "pg_basebackup_2978" pg_basebackup: write-ahead log end point: 0/3A0001D8 pg_basebackup: waiting for background process to finish streaming ... pg_basebackup: base backup completed [xdb@localhost testdb]$ [xdb@localhost testdb]$ ll /data/backup/0312-1/ total 44384 -rw-------. 1 xdb xdb 45427619 Mar 12 17:30 base.tar.gz -rw-------. 1 xdb xdb 18927 Mar 12 17:30 pg_wal.tar.gz [xdb@localhost testdb]$ #### [xdb@localhost ~]$ psql -d testdb psql (11.2) Type "help" for help. testdb=# checkpoint; CHECKPOINT testdb=#
構(gòu)造測試數(shù)據(jù)
創(chuàng)建數(shù)據(jù)表,插入數(shù)據(jù)
testdb=# create table tbl01(id int,c1 char(200),c2 char(200)); CREATE TABLE testdb=# insert into tbl01 select f,f||'c1',f||'c2' from generate_series(1,100000) f; INSERT 0 100000 testdb=# select pg_current_wal_lsn(); pg_current_wal_lsn -------------------- 0/3DD39618 (1 row) testdb=# create table tbl02(id int,c1 char(200),c2 char(200)); CREATE TABLE testdb=# insert into tbl02 select f,f||'c1',f||'c2' from generate_series(1,100000) f; INSERT 0 100000 testdb=# select pg_current_wal_lsn(); pg_current_wal_lsn -------------------- 0/40A62F20 (1 row) testdb=#
當(dāng)前在線日志文件為0000000F0000000000000040
[xdb@localhost testdb]$ ll $PGDATA/pg_wal total 196632 -rw-------. 1 xdb xdb 42 Mar 12 17:10 00000008.history -rw-------. 1 xdb xdb 85 Mar 12 17:10 0000000C.history -rw-------. 1 xdb xdb 16777216 Mar 12 17:10 0000000E0000000000000039.partial -rw-------. 1 xdb xdb 129 Mar 12 17:10 0000000E.history -rw-------. 1 xdb xdb 16777216 Mar 12 17:30 0000000F000000000000003A -rw-------. 1 xdb xdb 323 Mar 12 17:30 0000000F000000000000003A.00000108.backup -rw-------. 1 xdb xdb 16777216 Mar 12 17:32 0000000F000000000000003B -rw-------. 1 xdb xdb 16777216 Mar 12 17:32 0000000F000000000000003C -rw-------. 1 xdb xdb 16777216 Mar 12 17:32 0000000F000000000000003D -rw-------. 1 xdb xdb 16777216 Mar 12 17:32 0000000F000000000000003E -rw-------. 1 xdb xdb 16777216 Mar 12 17:32 0000000F000000000000003F -rw-------. 1 xdb xdb 16777216 Mar 12 17:32 0000000F0000000000000040 -rw-------. 1 xdb xdb 16777216 Mar 12 17:10 0000000F0000000000000041 -rw-------. 1 xdb xdb 16777216 Mar 12 17:10 0000000F0000000000000042 -rw-------. 1 xdb xdb 16777216 Mar 12 17:10 0000000F0000000000000043 -rw-------. 1 xdb xdb 16777216 Mar 12 17:30 0000000F0000000000000044 -rw-------. 1 xdb xdb 173 Mar 12 17:11 0000000F.history drwx------. 2 xdb xdb 4096 Mar 12 17:32 archive_status [xdb@localhost testdb]$
歸檔日志文件信息
[xdb@localhost testdb]$ ll /data/archivelog/20190312/ total 245772 -rw-------. 1 xdb xdb 16777216 Mar 12 17:06 0000000E0000000000000032 -rw-------. 1 xdb xdb 16777216 Mar 12 17:06 0000000E0000000000000033 -rw-------. 1 xdb xdb 323 Mar 12 17:06 0000000E0000000000000033.00000028.backup -rw-------. 1 xdb xdb 16777216 Mar 12 17:07 0000000E0000000000000034 -rw-------. 1 xdb xdb 16777216 Mar 12 17:07 0000000E0000000000000035 -rw-------. 1 xdb xdb 16777216 Mar 12 17:07 0000000E0000000000000036 -rw-------. 1 xdb xdb 16777216 Mar 12 17:07 0000000E0000000000000037 -rw-------. 1 xdb xdb 16777216 Mar 12 17:07 0000000E0000000000000038 -rw-------. 1 xdb xdb 16777216 Mar 12 17:11 0000000E0000000000000039.partial -rw-------. 1 xdb xdb 16777216 Mar 12 17:30 0000000F0000000000000039 -rw-------. 1 xdb xdb 16777216 Mar 12 17:30 0000000F000000000000003A -rw-------. 1 xdb xdb 323 Mar 12 17:30 0000000F000000000000003A.00000108.backup -rw-------. 1 xdb xdb 16777216 Mar 12 17:32 0000000F000000000000003B -rw-------. 1 xdb xdb 16777216 Mar 12 17:32 0000000F000000000000003C -rw-------. 1 xdb xdb 16777216 Mar 12 17:32 0000000F000000000000003D -rw-------. 1 xdb xdb 16777216 Mar 12 17:32 0000000F000000000000003E -rw-------. 1 xdb xdb 16777216 Mar 12 17:32 0000000F000000000000003F -rw-------. 1 xdb xdb 173 Mar 12 17:11 0000000F.history [xdb@localhost testdb]$
模擬數(shù)據(jù)文件損壞
刪除數(shù)據(jù)文件目錄,kill postgres進程
[xdb@localhost testdb]$ rm -rf ./base [xdb@localhost testdb]$ psql -d testdb psql: FATAL: database "testdb" does not exist DETAIL: The database subdirectory "base/16384" is missing. [xdb@localhost testdb]$ ps -ef|grep postgres xdb 2914 1 0 17:11 pts/2 00:00:00 /appdb/xdb/pg11.2/bin/postgres xdb 2915 2914 0 17:11 ? 00:00:00 postgres: logger xdb 2918 2914 0 17:11 ? 00:00:00 postgres: checkpointer xdb 2919 2914 0 17:11 ? 00:00:00 postgres: background writer xdb 2921 2914 0 17:11 ? 00:00:00 postgres: stats collector xdb 2925 2914 0 17:11 ? 00:00:00 postgres: walwriter xdb 2926 2914 0 17:11 ? 00:00:00 postgres: autovacuum launcher xdb 2927 2914 0 17:11 ? 00:00:00 postgres: archiver last was 0000000F000000000000003F xdb 2928 2914 0 17:11 ? 00:00:00 postgres: logical replication launcher xdb 2977 2914 0 17:30 ? 00:00:00 postgres: xdb testdb [local] idle xdb 3014 2519 0 17:33 pts/2 00:00:00 grep --color=auto postgres [xdb@localhost testdb]$ kill -9 2914 [xdb@localhost testdb]$ ps -ef|grep postgres xdb 3016 2519 0 17:34 pts/2 00:00:00 grep --color=auto postgres
執(zhí)行恢復(fù)
備份在線日志
[xdb@localhost ~]$ mkdir /data/backup/wal [xdb@localhost testdb]$ cp -R ./pg_wal/* /data/backup/wal/ [xdb@localhost testdb]$ [xdb@localhost testdb]$ ll /data/backup/wal/ total 196632 -rw-------. 1 xdb xdb 42 Mar 12 17:34 00000008.history -rw-------. 1 xdb xdb 85 Mar 12 17:34 0000000C.history -rw-------. 1 xdb xdb 16777216 Mar 12 17:34 0000000E0000000000000039.partial -rw-------. 1 xdb xdb 129 Mar 12 17:34 0000000E.history -rw-------. 1 xdb xdb 16777216 Mar 12 17:34 0000000F000000000000003A -rw-------. 1 xdb xdb 323 Mar 12 17:34 0000000F000000000000003A.00000108.backup -rw-------. 1 xdb xdb 16777216 Mar 12 17:34 0000000F000000000000003B -rw-------. 1 xdb xdb 16777216 Mar 12 17:34 0000000F000000000000003C -rw-------. 1 xdb xdb 16777216 Mar 12 17:34 0000000F000000000000003D -rw-------. 1 xdb xdb 16777216 Mar 12 17:34 0000000F000000000000003E -rw-------. 1 xdb xdb 16777216 Mar 12 17:34 0000000F000000000000003F -rw-------. 1 xdb xdb 16777216 Mar 12 17:34 0000000F0000000000000040 -rw-------. 1 xdb xdb 16777216 Mar 12 17:34 0000000F0000000000000041 -rw-------. 1 xdb xdb 16777216 Mar 12 17:34 0000000F0000000000000042 -rw-------. 1 xdb xdb 16777216 Mar 12 17:34 0000000F0000000000000043 -rw-------. 1 xdb xdb 16777216 Mar 12 17:34 0000000F0000000000000044 -rw-------. 1 xdb xdb 173 Mar 12 17:34 0000000F.history drwx------. 2 xdb xdb 4096 Mar 12 17:34 archive_status [xdb@localhost testdb]$
從數(shù)據(jù)庫備份中恢復(fù)
[xdb@localhost testdb]$ rm -rf * [xdb@localhost testdb]$ cp /data/backup/0312-1/base.tar.gz ./ [xdb@localhost testdb]$ tar zxf base.tar.gz
恢復(fù)在線日志
cp -R /data/backup/wal/0000000F0000000000000040 ./pg_wal [xdb@localhost testdb]$ ll ./pg_wal total 16384 -rw-------. 1 xdb xdb 16777216 Mar 12 17:35 0000000F0000000000000040 drwx------. 2 xdb xdb 6 Mar 12 17:30 archive_status
創(chuàng)建recovery.conf文件
[xdb@localhost testdb]$ vim recovery.conf [xdb@localhost testdb]$ cat recovery.conf #Recovery restore_command='cp /data/archivelog/20190312/%f %p' #restore_target=XX
執(zhí)行恢復(fù),啟動數(shù)據(jù)庫
[xdb@localhost testdb]$ pg_ctl start waiting for server to start....2019-03-12 17:36:21.310 CST [3030] LOG: listening on IPv4 address "0.0.0.0", port 5432 2019-03-12 17:36:21.310 CST [3030] LOG: listening on IPv6 address "::", port 5432 2019-03-12 17:36:21.328 CST [3030] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 2019-03-12 17:36:21.341 CST [3030] LOG: redirecting log output to logging collector process 2019-03-12 17:36:21.341 CST [3030] HINT: Future log output will appear in directory "pg_log". done server started
日志輸出
2019-03-12 17:36:21.341 CST,,,3030,,5c877d95.bd6,1,,2019-03-12 17:36:21 CST,,0,LOG,00000,"ending log output to stderr",,"Future log output will go to log destination ""csvlog"".",,,,,,,"" 2019-03-12 17:36:21.350 CST,,,3032,,5c877d95.bd8,1,,2019-03-12 17:36:21 CST,,0,LOG,00000,"database system was interrupted; last known up at 2019-03-12 17:30:44 CST",,,,,,,,,"" 2019-03-12 17:36:21.358 CST,,,3032,,5c877d95.bd8,2,,2019-03-12 17:36:21 CST,,0,LOG,00000,"starting archive recovery",,,,,,,,,"" 2019-03-12 17:36:21.372 CST,,,3032,,5c877d95.bd8,3,,2019-03-12 17:36:21 CST,,0,LOG,00000,"restored log file ""0000000F.history"" from archive",,,,,,,,,"" 2019-03-12 17:36:21.486 CST,,,3032,,5c877d95.bd8,4,,2019-03-12 17:36:21 CST,,0,LOG,00000,"restored log file ""0000000F000000000000003A"" from archive",,,,,,,,,"" 2019-03-12 17:36:21.693 CST,,,3032,,5c877d95.bd8,5,,2019-03-12 17:36:21 CST,1/0,0,LOG,00000,"redo starts at 0/3A000108",,,,,,,,,"" 2019-03-12 17:36:21.696 CST,,,3032,,5c877d95.bd8,6,,2019-03-12 17:36:21 CST,1/0,0,LOG,00000,"consistent recovery state reached at 0/3A0001D8",,,,,,,,,"" 2019-03-12 17:36:21.696 CST,,,3030,,5c877d95.bd6,2,,2019-03-12 17:36:21 CST,,0,LOG,00000,"database system is ready to accept read only connections",,,,,,,,,"" 2019-03-12 17:36:21.826 CST,,,3032,,5c877d95.bd8,7,,2019-03-12 17:36:21 CST,1/0,0,LOG,00000,"restored log file ""0000000F000000000000003B"" from archive",,,,,,,,,"" 2019-03-12 17:36:22.245 CST,,,3032,,5c877d95.bd8,8,,2019-03-12 17:36:21 CST,1/0,0,LOG,00000,"restored log file ""0000000F000000000000003C"" from archive",,,,,,,,,"" 2019-03-12 17:36:22.614 CST,,,3032,,5c877d95.bd8,9,,2019-03-12 17:36:21 CST,1/0,0,LOG,00000,"restored log file ""0000000F000000000000003D"" from archive",,,,,,,,,"" 2019-03-12 17:36:23.039 CST,,,3032,,5c877d95.bd8,10,,2019-03-12 17:36:21 CST,1/0,0,LOG,00000,"restored log file ""0000000F000000000000003E"" from archive",,,,,,,,,"" 2019-03-12 17:36:23.342 CST,,,3032,,5c877d95.bd8,11,,2019-03-12 17:36:21 CST,1/0,0,LOG,00000,"restored log file ""0000000F000000000000003F"" from archive",,,,,,,,,"" 2019-03-12 17:36:23.874 CST,,,3032,,5c877d95.bd8,12,,2019-03-12 17:36:21 CST,1/0,0,LOG,00000,"invalid record length at 0/40A63B08: wanted 24, got 0",,,,,,,,,"" 2019-03-12 17:36:23.874 CST,,,3032,,5c877d95.bd8,13,,2019-03-12 17:36:21 CST,1/0,0,LOG,00000,"redo done at 0/40A63AD0",,,,,,,,,"" 2019-03-12 17:36:23.874 CST,,,3032,,5c877d95.bd8,14,,2019-03-12 17:36:21 CST,1/0,0,LOG,00000,"last completed transaction was at log time 2019-03-12 17:33:32.760492+08",,,,,,,,,"" 2019-03-12 17:36:23.879 CST,,,3032,,5c877d95.bd8,15,,2019-03-12 17:36:21 CST,1/0,0,LOG,00000,"selected new timeline ID: 16",,,,,,,,,"" 2019-03-12 17:36:24.773 CST,,,3032,,5c877d95.bd8,16,,2019-03-12 17:36:21 CST,1/0,0,LOG,00000,"archive recovery complete",,,,,,,,,"" 2019-03-12 17:36:24.777 CST,,,3032,,5c877d95.bd8,17,,2019-03-12 17:36:21 CST,1/0,0,LOG,00000,"restored log file ""0000000F.history"" from archive",,,,,,,,,"" 2019-03-12 17:36:25.589 CST,,,3030,,5c877d95.bd6,3,,2019-03-12 17:36:21 CST,,0,LOG,00000,"database system is ready to accept connections",,,,,,,,,""
驗證數(shù)據(jù)
[xdb@localhost testdb]$ psql -d testdb psql (11.2) Type "help" for help. testdb=# testdb=# select count(*) from tbl01; count -------- 100000 (1 row) testdb=# select count(*) from t02; count -------- 100000 (1 row) testdb=#
時間線歷史文件,在歸檔目錄和pg_wal目錄下均存在以當(dāng)前時間線命名的history,該文件說明了該Cluster的歷史.
[xdb@localhost ~]$ cat /data/archivelog/20190312/00000010.history 7 0/27000000 no recovery target specified 8 0/2A0012E8 no recovery target specified 12 0/32000000 no recovery target specified 14 0/39A63BD0 no recovery target specified 15 0/40A63B08 no recovery target specified [xdb@localhost ~]$ cat $PGDATA/pg_wal/00000010.history 7 0/27000000 no recovery target specified 8 0/2A0012E8 no recovery target specified 12 0/32000000 no recovery target specified 14 0/39A63BD0 no recovery target specified 15 0/40A63B08 no recovery target specified [xdb@localhost ~]$
“怎么恢復(fù)PostgreSQL數(shù)據(jù)文件損壞”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
新聞名稱:怎么恢復(fù)PostgreSQL數(shù)據(jù)文件損壞
分享路徑:http://www.rwnh.cn/article10/jeedgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站營銷、Google、響應(yīng)式網(wǎng)站、品牌網(wǎng)站建設(shè)、云服務(wù)器
聲明:本網(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)