首页 > 其他分享 >postgres备份与恢复资料

postgres备份与恢复资料

时间:2023-01-08 23:33:07浏览次数:47  
标签:15 postgres 备份 id pg 资料 test bak

创建归档目录

/home/postgres/pg13/archive_log

修改参数

WAL_LEVEL

可选值:minimal,replica,logical

开启wal归档至少设置为replica

alter system set wal_level='replica';

ARCHIVE_MODE

可选值:on,off,always

开启wal归档需要设置为on

alter system set archive_mode='on';

ARCHIVE_COMMAND

alter system set archive_command='cp %p /home/postgres/pg13/archive_log/%f';

wal_level和archive_mode修改完需要重启数据库,archive_command不需要,只需要reload即可。

执行基础备份

创建repuser用户

create user repuser replication login connection limit 5 password '123456';

创建基础备份

# 构建测试表,插入第一条数据 postgres=# create table bak_test(id serial,insert_time time default localtime); CREATE TABLE postgres=# \d bak_test; Table "public.bak_test" Column | Type | Collation | Nullable | Default -------------+------------------------+-----------+----------+-------------------------------------- id | integer | | not null | nextval('bak_test_id_seq'::regclass) insert_time | time without time zone | | | LOCALTIME postgres=# insert into bak_test(id) values(1); INSERT 0 1 postgres=# select * from bak_test; id | insert_time ----+----------------- 1 | 15:21:13.445397 (1 row) # 创建基础备份 [postgres@devin-enmo ~]$ pg_basebackup -D /home/postgres/backup/ -Fp -Xs -v -P -h127.0.0.1 -p5432 -Urepuser pg_basebackup: initiating base backup, waiting for checkpoint to complete pg_basebackup: checkpoint completed pg_basebackup: write-ahead log start point: 3/B3000028 on timeline 1 pg_basebackup: starting background WAL receiver pg_basebackup: created temporary replication slot "pg_basebackup_22226" 31191/31191 kB (100%), 1/1 tablespace pg_basebackup: write-ahead log end point: 3/B3000100 pg_basebackup: waiting for background process to finish streaming ... pg_basebackup: syncing data to disk ... pg_basebackup: renaming backup_manifest.tmp to backup_manifest pg_basebackup: base backup completed

构建测试数据

# 插入第二条数据(测试用于恢复到指定时间点) postgres=# insert into bak_test(id) values(2); INSERT 0 1 postgres=# select * from bak_test; id | insert_time ----+----------------- 1 | 15:21:13.445397 2 | 15:23:42.02228 (2 rows) #2,插入第三条数据,创建还原点(测试用于恢复到指定point) postgres=# insert into bak_test(id) values(3); INSERT 0 1 postgres=# select pg_create_restore_point('my_restore_point'); pg_create_restore_point ------------------------- 3/B40002A8 (1 row) postgres=# select * from bak_test; id | insert_time ----+----------------- 1 | 15:21:13.445397 2 | 15:23:42.02228 3 | 15:24:14.376994 (3 rows) #插入第四条数据,记录事务ID(测试用于恢复到指定事务) postgres=# insert into bak_test(id) values(4); INSERT 0 1 postgres=# select txid_current(); txid_current -------------- 609 (1 row) postgres=# select * from bak_test; id | insert_time ----+----------------- 1 | 15:21:13.445397 2 | 15:23:42.02228 3 | 15:24:14.376994 4 | 15:24:45.047839 (4 rows) # 插入第五条数据(测试用于恢复到最近时间点) postgres=# insert into bak_test(id) values(5); INSERT 0 1 postgres=# select * from bak_test; id | insert_time ----+----------------- 1 | 15:21:13.445397 2 | 15:23:42.02228 3 | 15:24:14.376994 4 | 15:24:45.047839 5 | 15:25:41.962718 (5 rows)

执行恢复

restore数据库

mv /home/postgres/pg13/data /home/postgres/pg13/data.bak mv /home/postgres/backup /home/postgres/pg13/data

恢复到指定时间点

# vi postgresql.conf restore_command = 'cp /home/postgres/pg13/archive_log/%f %p' recovery_target_time = '2021-10-01 15:23:43' # 启动数据库 [postgres@devin-enmo data]$ pg_ctl start # 验证数据 postgres=# select * from bak_test; id | insert_time ----+----------------- 1 | 15:21:13.445397 2 | 15:23:42.02228 (2 rows)

恢复到指定point

# vi postgresql.conf restore_command = 'cp /home/postgres/pg13/archive_log/%f %p' recovery_target_name = 'my_restore_point' # 启动数据库 [postgres@devin-enmo data]$ pg_ctl start # 验证数据 postgres=# select * from bak_test; id | insert_time ----+----------------- 1 | 15:21:13.445397 2 | 15:23:42.02228 3 | 15:24:14.376994 (3 rows)

恢复到指定事务

# vi postgresql.conf restore_command = 'cp /home/postgres/pg13/archive_log/%f %p' recovery_target_xid = 609 # 启动数据库 [postgres@devin-enmo data]$ pg_ctl start # 验证数据 postgres=# select * from bak_test; id | insert_time ----+----------------- 1 | 15:21:13.445397 2 | 15:23:42.02228 3 | 15:24:14.376994 4 | 15:24:45.047839 (4 rows)

恢复到最近的时间点

# vi postgresql.conf restore_command = 'cp /home/postgres/pg13/archive_log/%f %p' recovery_target_timeline = 'latest' # 启动数据库 [postgres@devin-enmo data]$ pg_ctl start # 验证数据 postgres=# select * from bak_test; id | insert_time ----+----------------- 1 | 15:21:13.445397 2 | 15:23:42.02228 3 | 15:24:14.376994 4 | 15:24:45.047839 5 | 15:25:41.962718 (5 rows)

标签:15,postgres,备份,id,pg,资料,test,bak
From: https://www.cnblogs.com/zhhy/p/17035752.html

相关文章

  • dock入门资料
    docker入门参考资料:Docker入门教程-阮一峰的网络日志(ruanyifeng.com)......
  • neon serverless postgres 简单试用
    内容来自官方的docker-compose内容,主要是测试试用参考架构  从以上图可以看出,neon应该包含了几个组件,safekeeper,pageserver,计算节点,以及对象存储,运行的服务也是参......
  • neon serverless postgres 服务
    neon是开源的基于rust开发的serverlesspostgres服务支持的特性计算存储隔离分支无限存储自动缩放架构包含pageserver,计算节点的处理safekeepers,进行wal服......
  • 通知:PostgreSQL证书领取(初级)
    PCA7天训练营第13营、PCA7天训练营第14营、PCA7天训练营第15营、PCA7天训练营第16营证书已由工业和信息化部教育与考试中心进行制作。​ 该批次证书发放事宜通知如下:......
  • 基于Patroni的PostgreSQL高可用实践
    因环境有限,本文在一台机器上实现基于Patroni的PostgreSQL高可用服务测试。1、安装软件包[root@lee~]#yum-yinstallhttps://mirrors.tuna.tsinghua.edu.cn/postgresql/r......
  • etcd集群-备份恢复-添加节点-删除节点-基础命令
    1、集群环境IP分配:192.168.10.110k8s-deploy-harbor2c2g192.168.10.111k8s-master1-etcd1-haproxy12c4g192.168.10.112k8s-master2-etcd2-haproxy22c4g192.1......
  • mysql用mysqldump命令进行数据库备份与迁移
    指定备份数据库abc(带库名备份):mysqldump-uuser-ppasswd-hhost-Babc>/path/bak.sql 全部数据库备份:mysqldump-uuser-ppasswd-hhost-A>/path/bak.sql 只......
  • Oracle11g Rman备份备忘录
    0x00 rman是什么RecoveryManager(RMAN) rman就是oracle官方推出的一种物理备份工具,以oracleblock为读取单位可以结合expdp使用做备份,可以实现更小粒度的备份。rman......
  • 宝塔面板 + 阿里云盘定时备份教程
    众所周知,服务器数据要定时备份,以免数据丢失,让以后可以回滚数据。但是,宝塔面板的应用商店内暂无阿里云盘备份插件下载。所以我从Github找了一款插件给大家!(插件不支持定时......
  • PostgreSQL(02): PostgreSQL常用命令
    目录PostgreSQL(01):Ubuntu20.04/22.04PostgreSQL安装配置记录PostgreSQL(02):PostgreSQL常用命令PostgreSQL常用命令满足验证条件的用户,可以用psql命令进入p......