postgresql备份与恢复在线备份

  postgresql同样支持在线备份,该备份方式与oracle中用户热备的方式相同,手动拷贝数据文库文件与归档日志。可以根据测试过程将备份直接写成script,通过定制,完成数据库自动备份

  postgresql的恢复支持基于时间戳与事务ID,可以通过时间戳或事务ID的方式,完成数据库的不完全恢复或者因错误操作的故障恢复。

  该测试目的:postgresql的在线备份;通过在线备份完成恢复。

  1,开启归档

  [postgre@daduxiong~]$more/usr/local/pgsql/data/postgresql.conf|greparchive_
archive_mode=on#allowsarchivingtobedone
archive_command='cp-i%p/archive/%f>/dev/null'

  2,重新启动数据库

[root@daduxiong~]#servicepostgresqlstop
StoppingPostgresql:serverstopped
ok
[root@daduxiong~]#servicepostgresqlstart
StartingPostgresql:ok

  3,启动备份

[postgre@daduxiongarchive]$psqlpostgres-c"selectpg_start_backup('hot_backup');"
pg_start_backup
-----------------
0/7000020
(1row)

  4,使用tar命令备份数据库文件,不包含pg_xlog目录

  

[postgre@daduxiongarchive]$tar--exclude$PGDATA/pg_xlog-cvjpf/archive/pgbackup.tar.bz2$PGDATA

  5,完成备份

  

[postgre@daduxiongarchive]$psqlpostgres-c"selectpg_stop_backup();"pg_stop_backup----------------0/70133A0(1row)

  6,在postgres数据库中创建表并插入记录,作为恢复时的判断。

[postgre@daduxiongarchive]$psqlpostgres
Welcometopsql8.3.10,thePostgresqlinteractiveterminal.

Type:\copyrightfordistributionterms
\hforhelpwithsqlcommands
\?forhelpwithpsqlcommands
\gorterminatewithsemicolontoexecutequery
\qtoquit

postgres=#createtableabc(idinteger);
CREATETABLE
postgres=#insertintoabcvalues(1);
INSERT01
postgres=#\q

  7,此时假设数据库出现问题,停止数据库,拷贝日志

[root@daduxiongpgsql]#servicepostgresqlstop
StoppingPostgresql:serverstopped
ok
[postgre@daduxiongarchive]$cp$PGDATA/pg_xlog/*00*/archive/

  8,删除"发生错误"的data目录

[root@daduxiongpgsql]#rm-rfdata

  9,解压之前的备份文件压缩包

[postgre@daduxiongpgsql]$tar-xvf/archive/pgbackup.tar.bz2
....省略
/usr/local/pgsql/data/global/2843
/usr/local/pgsql/data/postmaster.opts
/usr/local/pgsql/data/pg_twophase/
/usr/local/pgsql/data/postmaster.pid
/usr/local/pgsql/data/backup_label
/usr/local/pgsql/data/PG_VERSION

  10,恢复data目录,重新创建pg_xlog目录及其子目录archive_status

[root@daduxiongpgsql]#mv/archive/usr/local/pgsql/data/usr/local/pgsql
[root@daduxiongdata]#mkdirpg_xlog
[root@daduxiongdata]#chmod0700pg_xlog/
[root@daduxiongdata]#chownpostgre:postgrepg_xlog/
[root@daduxiongdata]#cdpg_xlog/
[root@daduxiongpg_xlog]#mkdirarchive_status
[root@daduxiongpg_xlog]#chmod0700archive_status/
[root@daduxiongpg_xlog]#chownpostgre:postgrearchive_status/
[root@daduxiongpg_xlog]#mv/archive/*00*/usr/local/pgsql/data/pg_xlog
[root@daduxiongpg_xlog]#cd..
[root@daduxiongdata]#ls
backup_labelpg_clogpg_multixactpg_twophasepostgresql.conf
basepg_hba.confpg_subtransPG_VERSIONpostmaster.opts
globalpg_ident.confpg_tblspcpg_xlogpostmaster.pid

  11,配置恢复配置文件

  

[root@daduxiongdata]#touchrecovery.conf
[root@daduxiongdata]#echo"restore_command='cp-i/archive/%f%p'">>recovery.conf
[root@daduxiongdata]#chownpostgre:postgrerecovery.conf
[root@daduxiongdata]#chmod0750recovery.conf

  12,启动数据库,观察数据库启动的日志

  

[root@daduxiongdata]#servicepostgresqlstart
StartingPostgresql:ok

---省略日志部分内容
LOG:selectednewtimelineID:3
LOG:restoredlogfile"00000002.history"fromarchive
LOG:archiverecoverycomplete
LOG:autovacuumlauncherstarted
LOG:databasesystemisreadytoacceptconnections

  13,验证恢复结果。检查之前创建的表与记录。

  

otted rgb(204,thePostgresqlinteractiveterminal.

Type:\copyrightfordistributionterms
\hforhelpwithsqlcommands
\?forhelpwithpsqlcommands
\gorterminatewithsemicolontoexecutequery
\qtoquit

postgres=#select*fromabc;
id
----
1
(1row)

postgres=#\q


[root@daduxiongdata]#ls-l
total80
-rw-------1postgrepostgre147Aug3110:26backup_label.old
drwx------6postgrepostgre4096Aug2711:33base
drwx------2postgrepostgre4096Aug3110:41global
drwx------2postgrepostgre4096Aug1011:06pg_clog
-rwx------1postgrepostgre3429Aug1011:10pg_hba.conf
-rwx------1postgrepostgre1460Aug1011:06pg_ident.conf
drwx------4postgrepostgre4096Aug1011:06pg_multixact
drwx------2postgrepostgre4096Aug1011:06pg_subtrans
drwx------2postgrepostgre4096Aug1011:06pg_tblspc
drwx------2postgrepostgre4096Aug1011:06pg_twophase
-rwx------1postgrepostgre4Aug1011:06PG_VERSION
drwx------3postgrepostgre4096Aug3110:35pg_xlog
-rwx------1postgrepostgre16727Aug3109:42postgresql.conf
-rwx------1postgrepostgre59Aug3110:35postmaster.opts
-rw-------1postgrepostgre47Aug3110:35postmaster.pid
-rwxr-x---1postgrepostgre39Aug3110:34recovery.done

  说明:

  该测试中采用的是系统安装认的数据,如果是生产库需要注意备份所有的表空间,不能仅仅备份软连接.

  数据库完成备份后,recovery.conf文件变成recovery.done,backup_label文件变成了backup_lable.old.

  oracle的备份通常采用RMAN工具备份,该工具功能强大,使用方便,得到大家的认可。

  开源项目组也为postgresql开发了一款类似的工具pg-rman,其功能也很强大,使用方式和特点与ORACLE的RMAN非常类似。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果是个PostGreSQL的数据,于是装数据库,但这个也不懂呀,而且本系统用的Oracle,于是得解决迁移转换的问题。 总结下来两个思路。1、PostgresToOracle多金的
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么查看PostgreSQL数据库中所有表”文章能帮助大...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正确。  pg_hba.conf文件在Postgre安装文件目录下的data文件夹中。解决方案1、进入到data目录下,找到pg_hba.conf文件
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本地localhost访问正常,在相同网段的远程机器访问报如下错误
wamp 环境 这个提示就是说你的版本低于10了。 先打印php_info(),查看自己的版本(我这边是已经处理后的,之前的忘记截图了)
psycopg2.OperationalError: SSL SYSCALL error: EOF detected 问题提示:exception psycopg2.OperationalError
项目 postgres 连接不上, 所有连接报错 :psql: FATAL: sorry, too many clients already问题原由程序使用连接未及时释放, 连接一直处于 idle 状态处理方式1、 程序里面未释放的连接, 在使用后及时释放
服务器在同一个数据目录上启动了两个PostgreSQL实例(它已经删除postmaster.pid并使用了新的端口号,因此绕过了这种行为的正常保护措施被绕过),导致PostgreSQL的误操作postgresql 报错 FATAL: the database system is
问题原因:数据库崩溃,内存不足造成 或者 数据已损坏,磁盘故障造成首先介绍一下背景,在测试Deepgreen(Greenplum升级版)数据库时,pgbench并发数设置过多,导致数据库卡死了,在进行连接、重启、关闭时,都报同样
第 11 届 PostgreSQL 中国技术大会于 2022 年 1 月 7 日至 9 日在武汉光谷会展酒店成功举办。作为 PostgreSQL 技术领域的年度盛事,postgreSQL 中文社区旨在搭建开放、合作共享的平台,基于开源,创新驱动,共同探讨数据库行业数字化发展方向和未来新机遇。
即将到来的 PostgreSQL 15 已经确认会增加 MERGE 语句,MERGE 可以在单个语句中实现 INSERT、UPDATE 以及 DELETE 操作。在此之前,我们可以使用 INSERT … ON CONFLICT DO …. 语句代替该功能。
本文介绍了PostgreSQL 访问和操作 MySQL 数据库的外部数据包装器:mysql_fdw。mysql_fdw 提供了读写 MySQL 外部表、连接池、WHERE 条件下推、返回字段下推、预编译语句、JOIN 下推、聚合函数(min、max、sum、avg、count)下推、ORDER BY 下推以及 LIMIT OFFSET 下推等功能。
本文给大家分享了一些 PostgreSQL 实用小技巧。
PostgreSQL 全球开发组于 2021-05-20 发布了 PostgreSQL 14 的第一个 beta 版本。本文介绍了该版本中的主要新特性和增强功能,包括性能、新的 multirange 类型、SQL 语句、管理、复制和恢复、安全以及其他方面。
本文介绍了 PostgreSQL 横向子查询(LATERAL)的语法的作用。SELECT 就像一个循环语句,而 LATERAL 就像是一个嵌套循环语句,对于左侧表中的每行记录执行一次子查询操作。
PostgreSQL 15 增加了大量符合 SQL/JSON 标准的函数,使得 JSON 数据的处理更加简单。
PostgreSQL 支持多个不同级别的灵活加密技术,包括密码加密、字段加密、存储加密、传输加密、认证加密以及应用加密,可以保护数据不因数据库服务器被盗、内部管理员或者不安全的网络传输而导致泄露。
目录1.编译incubator-datasketches-cpp编译datasketches-cpp项目编译datasketches-cpp项目的PythonWrapperforDatasketches2.编译incubator-datasketches-postgresql3.Verifyinginstallationofdatasketches-postgresqlwithatestdatabase4.AdvancedExamplesofdata
连接pg需要指定以下的参数:·主机地址·端口·数据库名·用户·密码 如果没有指定上面的参数的话,pg会从环境变量中寻找以下的变量值:·PGHOST或PGHOSTADDR·PGPORT·PGDATABASE·PGUSER·PGPASSWORD 如果只是指定了前面四个参数,pg会尝试寻找口令文件。
pg库也是常用的数据库之一,有些刚开始接触开发的老师可能不熟,这里简单介绍一下,总体使用方法和mysql类似,所以这里只讲配置方法。这个地方test1是数据库1,test2是数据库2,yml文件一定要主要好格式,建议用properties。MyBaitsSqlSessionFactory配置文件。不同数据源的dao层要放到对应文件夹下。TimedConfig配置文件。文件夹格式,这里直接放贴图。Bd3Config配置文件。下面是数据源的配置文件。