postgresql基於備份點PITR恢復

實驗目的:

  0一、基於備份點直接恢復數據庫html

  0二、基於備份點後續增量wal日誌恢復到特定的時間點sql

實驗環境:

  centos7數據庫

  postgresql9.5centos

0一、安裝postgresql9.5

postgresql9.5編譯安裝體驗 app

注意:源碼編譯操做性更增強,也能夠rpm安裝post

0二、初始化數據庫及建立歸檔目錄

su - postgresui

[postgres@lab-210 ~]$ mkdir archivedirurl

[postgres@lab-210 ~]$ initdb -D data1 -E utf-8centos7

0三、修改postgresql配置

開啓歸檔spa

tee <<-'EOF' >>data1/postgresql.auto.conf
listen_addresses = '*'
port = 5432
wal_level = hot_standby
max_wal_senders = 2
archive_mode = on
archive_command = 'cp %p /home/postgres/archivedir/%f'
logging_collector = on
EOF

 添加host認證

tee <<-'EOF' >>data1/pg_hba.conf
local replication repl trust
host replication repl 172.24.0.0/16 trust
EOF

0四、啓動數據,添加repl帳戶

[postgres@lab-210 ~]$ pg_ctl -D data1/ start
server starting
[postgres@lab-210 ~]$ LOG: redirecting log output to logging collector process
HINT: Future log output will appear in directory "pg_log".

[postgres@lab-210 ~]$
[postgres@lab-210 ~]$ ss -lnt |grep 5432
LISTEN 0 128 *:5432 *:*
LISTEN 0 128 :::5432 :::*

postgres=# create user repl with password '123123' replication login;
CREATE ROLE
postgres=#
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
repl | Replication | {}

0五、建立基線備份

postgres=# create table tab1(uuid int);
CREATE TABLE
postgres=# insert into tab1 values (1),(100);
INSERT 0 2
postgres=# select * from tab1;
uuid
------
1
100
(2 rows)

mkdir data2    //plain形式

[postgres@lab-210 ~]$ pg_basebackup -h 172.24.0.210 -p 5432 -U repl -Fp -Xs -Pv -D data2/

mkdir base   //壓縮的形式

[postgres@lab-210 ~]$ pg_basebackup -h 172.24.0.210 -p 5432 -U repl -Ft -Pv -D base/

0六、新增數據

postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | tab1 | table | postgres
(1 row)

postgres=# create table tab2(uuid int);
CREATE TABLE

postgres=# insert into tab2 select generate_series(1,100);
INSERT 0 100
postgres=#
postgres=# select count(1) from tab2;
count
-------
100
(1 row)

postgres=# select pg_switch_xlog();
pg_switch_xlog
----------------
0/A018090
(1 row)

[postgres@lab-210 ~]$ pg_ctl -D data1/ stop
waiting for server to shut down.... done
server stopped

0七、基於建立點直接恢復

[postgres@lab-210 base]$ cat backup_label   //建立備份點的時間戳記錄
START WAL LOCATION: 0/9000028 (file 000000010000000000000009)
CHECKPOINT LOCATION: 0/9000060
BACKUP METHOD: streamed
BACKUP FROM: master
START TIME: 2019-11-02 19:11:40 UTC
LABEL: pg_basebackup base backup

[postgres@lab-210 base]$ cat recovery.conf    //恢復
restore_command = 'cp /home/postgres/archivedir/%f "%p"'
recovery_target_time='2019-11-02 19:11:40 UTC'

[postgres@lab-210 base]$ ll pg_xlog/    //基於pg_basearchive備份xlog已經處理了
total 0  
drwx------. 2 postgres postgres 6 Nov 2 19:11 archive_status

[postgres@lab-210 ~]$ chmod 700 base

[postgres@lab-210 ~]$ pg_ctl -D base start
server starting

[postgres@lab-210 ~]$ cat base/pg_log/postgresql-2019-11-02_192416.log
LOG: database system was interrupted; last known up at 2019-11-02 19:11:40 UTC
LOG: starting point-in-time recovery to 2019-11-02 19:11:40+00
LOG: restored log file "000000010000000000000009" from archive
LOG: redo starts at 0/9000028
LOG: consistent recovery state reached at 0/9000130
LOG: restored log file "00000001000000000000000A" from archive
LOG: recovery stopping before commit of transaction 1833, time 2019-11-02 19:13:24.96014+00
LOG: redo done at 0/A013C20
cp: cannot stat ‘/home/postgres/archivedir/00000002.history’: No such file or directory
LOG: selected new timeline ID: 2     //建立新的時間線,開天地
LOG: archive recovery complete
cp: cannot stat ‘/home/postgres/archivedir/00000001.history’: No such file or directory
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started

[postgres@lab-210 ~]$ ll base
total 64
-rw-------. 1 postgres postgres 206 Nov 2 19:11 backup_label.old    //備份點文件

......
-rw-rw-r--. 1 postgres postgres 105 Nov 2 19:24 recovery.done   //恢復配置文件

 驗證恢復狀況

[postgres@lab-210 ~]$ psql
psql (9.5.19)
Type "help" for help.

postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | tab1 | table | postgres
(1 row)

 

postgres=# select * from tab1;
uuid
------
1
100
(2 rows)

0八、基於基線實現全量恢復

 注意:基於備份點,仍是存在新增的歸檔日誌

[postgres@lab-210 ~]$ mkdir data3
[postgres@lab-210 ~]$ tar xf base.tar -C data3

[postgres@lab-210 data3]$ cat recovery.conf    ###默認恢復存儲存的歸檔文件
restore_command = 'cp /home/postgres/archivedir/%f "%p"' 

[postgres@lab-210 ~]$ chmod 700 data3
[postgres@lab-210 ~]$ pg_ctl -D data3/ start

[postgres@lab-210 ~]$ psql
psql (9.5.19)
Type "help" for help.

postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | tab1 | table | postgres
public | tab2 | table | postgres
(2 rows)

[postgres@lab-210 pg_log]$ cat postgresql-2019-11-02_193834.log
LOG: database system was interrupted; last known up at 2019-11-02 19:11:40 UTC   //備份點建立的時間
LOG: starting archive recovery
LOG: restored log file "000000010000000000000009" from archive
LOG: redo starts at 0/9000028
LOG: consistent recovery state reached at 0/9000130
LOG: restored log file "00000001000000000000000A" from archive
LOG: restored log file "00000001000000000000000B" from archive
cp: cannot stat ‘/home/postgres/archivedir/00000001000000000000000C’: No such file or directory
LOG: redo done at 0/B000060
LOG: last completed transaction was at log time 2019-11-02 19:14:49.458612+00   //事務發生的最後時間,恢復的目標點
LOG: restored log file "00000001000000000000000B" from archive
LOG: restored log file "00000002.history" from archive
cp: cannot stat ‘/home/postgres/archivedir/00000003.history’: No such file or directory
LOG: selected new timeline ID: 3
LOG: archive recovery complete     //恢復完畢
cp: cannot stat ‘/home/postgres/archivedir/00000001.history’: No such file or directory
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started

 PS:清理歸檔點舊的日誌

相關文章
相關標籤/搜索