怎么注册域名和网站,网站是数据,辽宁建设工程信息网清单怎么,建设银行网站诚聘英才PG物理备份与恢复之pg_basebackup 开启WAL日志归档pg_basebackup备份工具全库恢复#xff1a;recovery.conf #x1f418; 数据库版本#xff1a;PostgreSQL 10.4 开启WAL日志归档
通过数据库的全量备份和WAL日志#xff0c;可以将数据库恢复到任意时间点。每个WAL日志文件… PG物理备份与恢复之pg_basebackup 开启WAL日志归档pg_basebackup备份工具全库恢复recovery.conf 数据库版本PostgreSQL 10.4 开启WAL日志归档
通过数据库的全量备份和WAL日志可以将数据库恢复到任意时间点。每个WAL日志文件大小通常是16MB。WAL日志文件个数增长到wal_keep_segments 1个后会覆盖已有的WAL日志文件。因此最好根据业务需求定时对WAL日志进行归档。 在PG 13版本中wal_keep_segments被参数wal_keep_size取代。 创建归档目录
mkdir /pg_arch
chown -R postgres:postgres /pg_arch编辑配置文件postgresql.conf
wal_level replica
archive_mode on
archive_command DATEdate %Y%m%d; DIR/pg_arch/$DATE; (test -d $DIR || mkdir -p $DIR) cp %p $DIR/%f
wal_keep_segments 128其中%p表示包含完整路径的WAL日志文件名%f表示不包含路径信息的WAL日志文件名。
重启数据库
pg_ctl stop
pg_ctl start手动触发日志切换
postgres# checkpoint; --刷新内存脏页到磁盘
CHECKPOINTpostgres# select pg_switch_wal(); --手动归档WAL日志pg_switch_wal
---------------0/50003C0
(1 row)验证归档
[postgresdbhost ~]$ ls -lh /pgdata/pg_wal/
total 32M
-rw------- 1 postgres postgres 16M Oct 26 11:17 000000010000000000000009
-rw------- 1 postgres postgres 16M Oct 26 11:17 00000001000000000000000A
drwx------ 2 postgres postgres 244 Oct 26 11:17 archive_status[postgresdbhost ~]$ ls -lh /pg_arch/20231026/
total 16M
-rw------- 1 postgres postgres 16M Oct 26 11:17 000000010000000000000009pg_basebackup备份工具
从PostgreSQL 9.1开始支持使用pg_basebackup进行数据库物理备份热备份。pg_basebackup通过replication协议连接到数据库因此在pg_hba.conf中要允许replication连接。
[postgresdbhost ~]$ cat /pgdata/pg_hba.conf | grep replica | grep -v ^#
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
host replication repl 0.0.0.0/0 md5出于安全考虑也可以创建单独的流复制用户配置到pg_hba.conf中并禁用其他replication连接。
create role repl nosuperuser replication login connection limit 32 encrypted password Pwdabc123;对本地数据库进行全库备份
[postgresdbhost ~]$ pg_basebackup -Urepl -Ft -Xf -z -Z5 -R -Pv -D /home/postgres/backup
39635/39635 kB (100%), 2/2 tablespaces[postgresdbhost ~]$ ls backup/
16386.tar.gz base.tar.gz pg_wal.tar.gz其中纯数字命名的备份文件是对/pgdata/pg_tblspc目录下表空间的备份。
pg_basebackup支持的常用参数选项如下
-h host指定要连接的源数据库地址-p port指定要连接的源数据库端口-U username指定连接用户名-D path指定备份的存放路径。如果不存在会自动创建如果已存在且非空则会执行失败。-F p|t指定备份输出格式默认为plaint表示备份为tar包。-X f|fetch|s|streamf或fetch表示把备份过程中产生的xlog文件也备份。需要设置wal_keep_segments参数以保证备份过程中WAL日志不会被覆写。s或stream表示备份开始后启动另一个连接从源库接收WAL日志。需要设置max_wal_senders参数至少大于2。-R生成用于恢复的recovery.conf文件。-z与-Ft连用表示对备份输出的tar包进行gzip压缩得到格式为.tar.gz的备份文件。-Z level指定gzip的压缩级别取值范围为1~9数字越大压缩率越高。-l label为备份指定标签便于识别和管理备份文件。-P在备份过程中实时显示备份进度。-v备份过程中显示详细信息。
全库恢复recovery.conf
利用pg_basebackup备份恢复整个数据库。
检查表空间备份对应的恢复路径
[postgresdbhost ~]$ ll /pgdata/pg_tblspc/
total 0
lrwxrwxrwx 1 postgres postgres 14 Oct 26 13:08 16386 - /pgdata/sekiro可以得知16386.tar.gz是表空间sekiro的备份。
也可以查询pg_tablespace系统表获知
postgres# select oid,* from pg_tablespace;oid | spcname | spcowner | spcacl | spcoptions
-------------------------------------------------1663 | pg_default | 10 | | 1664 | pg_global | 10 | | 16386 | sekiro | 16384 | |
(3 rows)切换WAL日志
postgres# checkpoint;
postgres# select pg_switch_wal();删除数据
#停库
pg_ctl stop#移除数据目录下所有数据库文件
cd /pgdata rm -rf *#解压基础备份到数据目录
tar -xvf /home/postgres/backup/base.tar.gz -C /pgdata#解压表空间备份到对应的数据库表空间路径如果有
tar -xvf /home/postgres/backup/16386.tar.gz -C /pgdata/sekiro#解压日志备份到WAL日志路径如果有
tar -xvf /home/postgres/backup/pg_wal.tar.gz -C /pgdata/pg_wal在$PGDATA路径下创建用于恢复的配置文件recovery.conf
cp /usr/local/pgsql/share/recovery.conf.sample /pgdata/recovery.conf
chmod 600 recovery.conf在recovery.conf中添加恢复命令拷贝归档日志到数据库原始路径下
echo restore_commandcp /pg_arch/20231026/%f %p /pgdata/recovery.conf
echo recovery_target_timeline latest /pgdata/recovery.conf启动数据库自动读取recovery.conf进入恢复恢复
[postgresdbhost ~]$ pg_ctl start -D /pgdata
waiting for server to start....2023-10-26 14:08:30.862 CST [10503] LOG: listening on IPv4 address 0.0.0.0, port 5432
2023-10-26 14:08:30.862 CST [10503] LOG: listening on IPv6 address ::, port 5432
2023-10-26 14:08:30.864 CST [10503] LOG: listening on Unix socket /tmp/.s.PGSQL.5432
2023-10-26 14:08:30.939 CST [10503] LOG: redirecting log output to logging collector process
2023-10-26 14:08:30.939 CST [10503] HINT: Future log output will appear in directory log.done
server started恢复完成后recovery.conf会被重命名为recovery.done。
检查告警日志中的恢复信息
[postgresdbhost ~]$ tail -f /pgdata/log/server_Thu.log
cp: cannot stat ‘/pg_arch/20231026/000000020000000000000010’: No such file or directory
2023-10-26 14:08:31.609 CST [10505] LOG: redo done at 0/F0000D0
2023-10-26 14:08:31.621 CST [10505] LOG: restored log file 00000002000000000000000F from archive
cp: cannot stat ‘/pg_arch/20231026/00000003.history’: No such file or directory
2023-10-26 14:08:32.032 CST [10505] LOG: selected new timeline ID: 3
2023-10-26 14:08:32.531 CST [10505] LOG: archive recovery complete
2023-10-26 14:08:32.533 CST [10505] LOG: restored log file 00000002.history from archive
2023-10-26 14:08:32.638 CST [10503] LOG: database system is ready to accept connectionsReferences 【1】https://blog.csdn.net/jnrjian/article/details/129945206 【2】https://www.postgresql.org/docs/9.4/runtime-config-replication.html 【3】https://www.modb.pro/db/332203