本帖最后由 郑全 于 2026-1-3 12:17 编辑
环境:
linux 10.1 + pg18.1
data: /pg18/data
备份:
pgbackrest 已执行全备份
1.当前备份情况
postgres@pg181:/home$ pgbackrest info
stanza: regcost
status: ok
cipher: none
db (current)
wal archive min/max (18): 000000010000000000000009/00000001000000000000000B
full backup: 20260102-171813F
timestamp start/stop: 2026-01-02 17:18:13+08 / 2026-01-02 17:18:15+08
wal start/stop: 000000010000000000000009 / 000000010000000000000009
database size: 23.3MB, database backup size: 23.3MB
repo1: backup size: 3.8MB
full backup: 20260102-172025F
timestamp start/stop: 2026-01-02 17:20:25+08 / 2026-01-02 17:20:27+08
wal start/stop: 00000001000000000000000B / 00000001000000000000000B
database size: 23.3MB, database backup size: 23.3MB
repo1: backup size: 3.8MB
postgres@pg181:/home$
2.准备数据
postgres@pg181:/home$ psql
psql (18.1)
Type "help" for help.
postgres=# \
invalid command \
Try \? for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(3 rows)
postgres=# create database cost;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
cost | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(4 rows)
postgres=# \c cost
You are now connected to database "cost" as user "postgres".
cost=# create table emp(empid int,last_name varchar(20));
CREATE TABLE
cost=# insert into emp values(100,'able');
INSERT 0 1
cost=# select * from emp;
empid | last_name
-------+-----------
100 | able
(1 row)
cost=#
3.进行破坏
postgres@pg181:/home$ pg_ctl stop
waiting for server to shut down.... done
server stopped
postgres@pg181:/home$ rm -fr /pg18/data/*
postgres@pg181:/home$ ls -ltr /pg18/data/
total 0
postgres@pg181:/home$ pg_ctl start
pg_ctl: directory "/pg18/data" is not a database cluster directory
postgres@pg181:/home$
4.进行恢复
postgres@pg181:/home$ pgbackrest --stanza=regcost restore
postgres@pg181:/home$
postgres@pg181:/home$
postgres@pg181:/home$
postgres@pg181:/home$ ls -ltr /pg18/data/
total 64
-rw------- 1 postgres postgres 3 Jan 1 18:29 PG_VERSION
-rw------- 1 postgres postgres 2681 Jan 1 18:29 pg_ident.conf
-rw------- 1 postgres postgres 5813 Jan 1 18:47 pg_hba.conf
-rw------- 1 postgres postgres 32596 Jan 2 16:59 postgresql.conf
-rw------- 1 postgres postgres 29 Jan 2 17:00 current_logfiles
-rw------- 1 postgres postgres 258 Jan 2 17:20 backup_label
drwx------ 4 postgres postgres 45 Jan 2 18:12 pg_wal
drwx------ 2 postgres postgres 6 Jan 2 18:12 pg_twophase
drwx------ 2 postgres postgres 6 Jan 2 18:12 pg_tblspc
drwx------ 2 postgres postgres 6 Jan 2 18:12 pg_subtrans
drwx------ 2 postgres postgres 6 Jan 2 18:12 pg_stat_tmp
drwx------ 2 postgres postgres 6 Jan 2 18:12 pg_stat
drwx------ 2 postgres postgres 6 Jan 2 18:12 pg_snapshots
drwx------ 2 postgres postgres 6 Jan 2 18:12 pg_serial
drwx------ 2 postgres postgres 6 Jan 2 18:12 pg_replslot
drwx------ 2 postgres postgres 6 Jan 2 18:12 pg_notify
drwx------ 4 postgres postgres 36 Jan 2 18:12 pg_multixact
drwx------ 2 postgres postgres 6 Jan 2 18:12 pg_dynshmem
drwx------ 2 postgres postgres 6 Jan 2 18:12 pg_commit_ts5.
drwx------ 5 postgres postgres 33 Jan 2 18:12 base
drwx------ 2 postgres postgres 18 Jan 2 18:12 pg_xact
drwx------ 4 postgres postgres 68 Jan 2 18:12 pg_logical
drwx------ 2 postgres postgres 28 Jan 2 18:12 pg_log
-rw------- 1 postgres postgres 232 Jan 2 18:12 postgresql.auto.conf
-rw------- 1 postgres postgres 0 Jan 2 18:12 recovery.signal
drwx------ 2 postgres postgres 4096 Jan 2 18:12 global
postgres@pg181:/home$
5.打开数据库
postgres@pg181:/home$ pg_ctl start
waiting for server to start....2026-01-02 18:13:19.917 CST [12520] LOG: redirecting log output to logging collector process
2026-01-02 18:13:19.917 CST [12520] HINT: Future log output will appear in directory "pg_log".
done
server started
postgres@pg181:/home$ psql
psql (18.1)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
cost | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(4 rows)
postgres=# \c cost
You are now connected to database "cost" as user "postgres".
cost=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | emp | table | postgres
(1 row)
cost=# select * from emp;
empid | last_name
-------+-----------
100 | able
(1 row)
发现破坏前的数据库回来了
6.查看备份
postgres@pg181:/home$ pgbackrest info
stanza: regcost
status: ok
cipher: none
db (current)
wal archive min/max (18): 000000010000000000000009/00000001000000000000000C
full backup: 20260102-171813F
timestamp start/stop: 2026-01-02 17:18:13+08 / 2026-01-02 17:18:15+08
wal start/stop: 000000010000000000000009 / 000000010000000000000009
database size: 23.3MB, database backup size: 23.3MB
repo1: backup size: 3.8MB
full backup: 20260102-172025F
timestamp start/stop: 2026-01-02 17:20:25+08 / 2026-01-02 17:20:27+08
wal start/stop: 00000001000000000000000B / 00000001000000000000000B
database size: 23.3MB, database backup size: 23.3MB
repo1: backup size: 3.8MB
postgres@pg181:/home$
|