暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL数据库恢复工具(下部分)


一、pg_restore功能概述

一、pg_restore功能概述


1.1、pg_restore命令支持多种备份文件格式


pg_restore命令是PostgreSQL数据库的一个命令行工具,用于将备份文件恢复到数据库中。它可以将由pg_dump生成的备份文件还原到一个新的数据库中,或者将备份文件中的数据合并到一个已有的数据库中。pg_restore支持多种备份格式,包括自定义格式、tar格式和纯文本格式。

以下是一些常用的pg_restore选项:
  • -d:指定目标数据库名称。
  • -U:指定连接目标数据库所使用的用户名。
  • -h:指定连接目标数据库所使用的主机名。
  • -p:指定连接目标数据库所使用的端口号。
  • -t:指定要恢复的表名称。
  • -c:清空目标数据库并重新创建。
  • -O:不恢复对象所有者信息。
  • -j:指定并行恢复的进程数。

如果您需要更多关于pg_restore的信息,请参考PostgreSQL官方文档。

1.2、pg_restore帮助文档


  • 说明:如果您需要了解如何使用 pg_restore 或查看所有可用的选项,可以在命令行中运行 pg_restore --help 命令。这将显示一个详细的帮助菜单,列出了所有可用的选项和它们的说明。
  • 代码如下:
[postgres@node1 /]$ pg_restore --help
pg_restore restores a PostgreSQL database from an archive created by pg_dump.

用法:
  sys_restore [选项]... [文件名]

一般选项:
  -d, --dbname=名字        连接数据库名字
  -f, --file=文件名        输出文件名(-为输出到stdout)
  -F, --format=c|d|t       备份文件格式(应该自动进行)
  -l, --list               打印归档文件的 TOC 概述
  -v, --verbose            详细模式
  -V, --version            输出版本信息, 然后退出
  -?, --help               显示此帮助, 然后退出

恢复控制选项:
  -a, --data-only              只恢复数据, 不包括模式
  -c, --clean                  在重新创建之前,先清除(删除)数据库对象
  -C, --create                 创建目标数据库
  -e, --exit-on-error          发生错误退出, 默认为继续
  -E, --skip-errors            跳过COPY时遇到的错误行
  -I, --index=NAME             恢复指定名称的索引
  -j, --jobs=NUM               执行多个并行任务进行恢复工作
  -L, --use-list=FILENAME      从这个文件中使用指定的内容表排序输出
  -n, --schema=NAME            在这个模式中只恢复对象
  -N, --exclude-schema=NAME    不恢复此模式中的对象
  
模式
  -O, --no-owner               不恢复对象所属者
  -P, --function=NAME(args)    恢复指定名字的函数
  -s, --schema-only            只恢复模式, 不包括数据
  -S, --superuser=NAME         使用指定的超级用户来禁用触发器
  -t, --table=NAME             恢复命名关系(表、视图等)
  -T, --trigger=NAME           恢复指定名字的触发器
  -x, --no-privileges          跳过处理权限的恢复 (grant/revoke)
  -1, --single-transaction     作为单个事务恢复
  --disable-triggers           在只恢复数据的过程中禁用触发器
  --enable-row-security        启用行安全性
  --if-exists                  当删除对象时使用IF EXISTS
  --no-comments                不恢复注释
  --no-data-for-failed-tables  对那些无法创建的表不进行
                               数据恢复
  --no-publications            不恢复发行
  --no-security-labels         不恢复安全标签信息
  --no-subscriptions           不恢复订阅
--no-table-access-method     不恢复表访问方法
  --no-tablespaces             不恢复表空间的分配信息
  --section=SECTION            恢复命名节 (数据前、数据及数据后)
  --strict-names               要求每个表和(或)schema包括模式以匹配至少一个实体
  --use-set-session-authorization
                               使用 SESSION AUTHORIZATION 命令代替
                               ALTER OWNER 命令来设置所有权

联接选项:
  -h, --host=主机名        数据库服务器的主机名或套接字目录
  -p, --port=端口号        数据库服务器的端口号
  -U, --username=名字      以指定的数据库用户联接
  -w, --no-password        永远不提示输入口令
  -W, --password           强制口令提示 (自动)
  --role=ROLENAME          在恢复前执行SET ROLE操作

The options -I, -n, -N, -P, -t, -T, and --section can be combined and specified
multiple times to select multiple objects.

If no input file name is supplied, then standard input is used.

Report bugs to <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>
[postgres@node1 /]$
二、恢复dmp格式备份文件

2.1、恢复到新库


  • 说明:备份二进制格式的test4.dmp
  • pg_dump -F c -f /postgres/backup/test4.dmp -C -E UTF8 -U postgres test

2.1.1、将一个备份文件导入到一个新的数据库customer中


  • 代码如下:
[postgres@node1 ~]$  createdb test5
[postgres@PostgreSQL_Beta ~]$
[postgres@PostgreSQL_Beta ~]$
[postgres@node1 ~]$  pg_restore -dtest5  /postgres/backup/test4.dmp

2.1.2、再次验证还原结果:还原成功


  • 代码如下:

[postgres@node1 ~]$  psql -dtest5
psql (16.2)
Type "help" for help.

test5=# \dt
              List of relations
 Schema |       Name       | Type  |  Owner
--------+------------------+-------+----------
 public | actor            | table | postgres
 public | address          | table | postgres
 public | category         | table | postgres
 public | city             | table | postgres
 public | country          | table | postgres
 public | customer         | table | postgres
 public | film             | table | postgres
 public | film_actor       | table | postgres
 public | film_category    | table | postgres
 public | inventory        | table | postgres
 public | language         | table | postgres
 public | payment          | table | postgres
 public | payment_p2007_01 | table | postgres
 public | payment_p2007_02 | table | postgres
 public | payment_p2007_03 | table | postgres
 public | payment_p2007_04 | table | postgres
 public | payment_p2007_05 | table | postgres
 public | payment_p2007_06 | table | postgres
 public | rental           | table | postgres
 public | staff            | table | postgres
 public | store            | table | postgres
(21 rows)

test5=#

2.2、恢复指定表(包含数据、表结构)


2.2.1、模拟误删表actor


  • 代码如下:

test4=# drop table actor cascade;
NOTICE:  drop cascades to 4 other objects
DETAIL:  drop cascades to view actor_info
drop cascades to view film_list
drop cascades to view nicer_but_slower_film_list
drop cascades to constraint film_actor_actor_id_fkey on table film_actor
DROP TABLE
test4=#
test4=# \q

2.2.2、再次验证还原结果:还原成功


  • 代码如下:

[postgres@node1 ~]$  pg_restore -dtest4 -tactor  /postgres/backup/test4.dmp
[postgres@PostgreSQL_Beta ~]$
[postgres@node1 ~]$  psql -dtest4
psql (16.2)
Type "help" for help.

test4=# select count(*) from actor;
 count
-------
   200
(1 row)

test4=#

2.3、只恢复表结构


2.3.1、模拟误删表actor


  • 代码如下:

[postgres@node1 ~]$  psql -dtest5
psql (16.2)
Type "help" for help.

test5=# drop table actor cascade;
DROP TABLE
test5=# \q

2.3.2、只恢复指定表actor的表结构,不含数据


  • 代码如下:

[postgres@node1 ~]$  pg_restore -dtest5 -tactor -s /postgres/backup/test4.dmp

2.3.3、恢复数据库后,再次验证数据,发现actor已经恢复成功


  • 代码如下:
[postgres@node1 ~]$  psql -dtest5
psql (16.2)
Type "help" for help.

test5=# \dt+ actor;
                                    List of relations
 Schema | Name  | Type  |  Owner   | Persistence | Access method |  Size   | Description
--------+-------+-------+----------+-------------+---------------+---------+-------------
 public | actor | table | postgres | permanent   | heap          | 0 bytes |
(1 row)

test5=#

2.4、只恢复表数据


2.4.1、只恢复指定表actor的数据,不含对象


  • 代码如下:
[postgres@node1 ~]$  pg_restore -dtest5 -tactor -a /postgres/backup/test4.dmp

2.4.2、恢复数据库后,再次验证数据,发现actor已经恢复成功

  • 代码如下:
[postgres@node1 ~]$  psql -dtest5
psql (16.2)
Type "help" for help.

test5=# select count(*)from actor;
 count
-------
   200
(1 row)

三、恢复tar格式备份文件

3.1、备份test数据库


  • 代码如下:
pg_dump -Upostgres  -dtest -F t -E UTF8 -f "/postgres/backup/test.tar"

3.2、将tar备份文件恢复到数据库test中


  • 代码如下:
pg_dump -Upostgres  -dtest -F t -E UTF8 -f "/postgres/backup/test.tar"

3.2.1、模拟删除表test数据库


  • 代码如下:
[postgres@node1 ~]$  psql
psql (16.2)
Type "help" for help.

postgres=# \l
                                                     List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | Locale | ICU Rules |   Access privilege
s
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-------------------
----
 postgres  | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |        |           |
 server    | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |        |           |
 template0 | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |        |           | =c/postgres
   +
           |          |          |                 |             |             |        |           | postgres=CTc/postg
res
 template1 | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |        |           | =c/postgres
   +
           |          |          |                 |             |             |        |           | postgres=CTc/postg
res
 test      | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |        |           |
(5 rows)

postgres=# drop database test;
DROP DATABASE
postgres=# \l
                                                     List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | Locale | ICU Rules |   Access privilege
s
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-------------------
----
 postgres  | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |        |           |
 server    | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |        |           |
 template0 | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |        |           | =c/postgres
   +
           |          |          |                 |             |             |        |           | postgres=CTc/postg
res
 template1 | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |        |           | =c/postgres
   +
           |          |          |                 |             |             |        |           | postgres=CTc/postg
res
(4 rows)

postgres=#

3.2.2、恢复test数据库


  • 说明:恢复数据库后,再次验证数据test,显示恢复成功
  • 代码如下:
[postgres@PostgreSQL_Beta ~]$
[postgres@node1 ~]$  createdb test
[postgres@PostgreSQL_Beta ~]$
[postgres@PostgreSQL_Beta ~]$
[postgres@node1 ~]$  pg_restore -Upostgres  -dtest   /postgres/backup/test.tar
[postgres@PostgreSQL_Beta ~]$

3.2.3、再次验证数据test,显示test数据库已恢复成功


  • 代码如下:
[postgres@node1 ~]$  psql -dtest
psql (16.2)
Type "help" for help.

test=# \dt
              List of relations
 Schema |       Name       | Type  |  Owner
--------+------------------+-------+----------
 public | actor            | table | postgres
 public | address          | table | postgres
 public | category         | table | postgres
 public | city             | table | postgres
 public | country          | table | postgres
 public | customer         | table | postgres
 public | film             | table | postgres
 public | film_actor       | table | postgres
 public | film_category    | table | postgres
 public | inventory        | table | postgres
 public | language         | table | postgres
 public | payment          | table | postgres
 public | payment_p2007_01 | table | postgres
 public | payment_p2007_02 | table | postgres
 public | payment_p2007_03 | table | postgres
 public | payment_p2007_04 | table | postgres
 public | payment_p2007_05 | table | postgres
 public | payment_p2007_06 | table | postgres
 public | rental           | table | postgres
 public | staff            | table | postgres
 public | store            | table | postgres
(21 rows)

test=#
test=#

3.3、只恢复表的定义


3.3.1、恢复test数据库


  • 说明:恢复数据库后,再次验证数据test,显示恢复成功

  • 代码如下:
[postgres@PostgreSQL_Beta ~]$
[postgres@node1 ~]$  createdb test1
[postgres@PostgreSQL_Beta ~]$
[postgres@PostgreSQL_Beta ~]$
[postgres@node1 ~]$  pg_restore -Upostgres -dtest1 -s -v /postgres/backup/postgres.tar
pg_restore: connecting to database for restore
pg_restore: creating SCHEMA "cssd"
pg_restore: creating COMMENT "SCHEMA public"
pg_restore: creating SCHEMA "server"
pg_restore: creating SCHEMA "test1"
pg_restore: creating SCHEMA "test2"
pg_restore: creating SCHEMA "test3"
pg_restore: creating SCHEMA "test4"
pg_restore: creating SCHEMA "test5"
....................................................................
pg_restore: creating FK CONSTRAINT "public.payment payment_rental_id_fkey"
pg_restore: creating FK CONSTRAINT "public.payment payment_staff_id_fkey"
pg_restore: creating FK CONSTRAINT "public.rental rental_customer_id_fkey"
pg_restore: creating FK CONSTRAINT "public.rental rental_inventory_id_fkey"
pg_restore: creating FK CONSTRAINT "public.rental rental_staff_id_fkey"
pg_restore: creating FK CONSTRAINT "public.staff staff_address_id_fkey"
pg_restore: creating FK CONSTRAINT "public.staff staff_store_id_fkey"
pg_restore: creating FK CONSTRAINT "public.store store_address_id_fkey"
pg_restore: creating FK CONSTRAINT "public.store store_manager_staff_id_fkey"
pg_restore: creating ACL "SCHEMA public"
[postgres@PostgreSQL_Beta ~]$

test1=# \dn
      List of schemas
  Name  |       Owner
--------+-------------------
 cssd   | postgres
 public | pg_database_owner
 server | postgres
 test1  | postgres
 test2  | postgres
 test3  | postgres
 test4  | postgres
 test5  | postgres
(8 rows)

test1=#

3.4、只恢复数据(不包含对象)


3.4.1、重建对象


  • 说明:根据实际情况,恢复前,可确认是否清除数据,若需要清除,可按以下操作清除现有数据。

  • 代码如下:
[postgres@node1 ~]$  pg_restore -Upostgres -dtest1 -c -s -v /postgres/backup/postgres.tar
pg_restore: connecting to database for restore
pg_restore: dropping FK CONSTRAINT store store_manager_staff_id_fkey
pg_restore: dropping FK CONSTRAINT store store_address_id_fkey
pg_restore: dropping FK CONSTRAINT staff staff_store_id_fkey
....................................................
pg_restore: creating FK CONSTRAINT "public.payment payment_rental_id_fkey"
pg_restore: creating FK CONSTRAINT "public.payment payment_staff_id_fkey"
pg_restore: creating FK CONSTRAINT "public.rental rental_customer_id_fkey"
pg_restore: creating FK CONSTRAINT "public.rental rental_inventory_id_fkey"
pg_restore: creating FK CONSTRAINT "public.rental rental_staff_id_fkey"
pg_restore: creating FK CONSTRAINT "public.staff staff_address_id_fkey"
pg_restore: creating FK CONSTRAINT "public.staff staff_store_id_fkey"
pg_restore: creating FK CONSTRAINT "public.store store_address_id_fkey"
pg_restore: creating FK CONSTRAINT "public.store store_manager_staff_id_fkey"
pg_restore: creating ACL "SCHEMA public"

[postgres@node1 ~]$  psql -Upostgres -dtest1
psql (16.2)
Type "help" for help.

test1=# select count(*) from actor;
 count
-------
     0
(1 row)

test1=# \q

3.4.2、恢复数据


  • 说明:只恢复数据,可用“-a”。

  • 代码如下:
[postgres@node1 ~]$  pg_restore -Upostgres -dtest1 -a -v /postgres/backup/postgres.tar
pg_restore: connecting to database for restore
pg_restore: processing data for table "public.actor"
pg_restore: processing data for table "public.address"
pg_restore: while PROCESSING TOC:
......................................................
pg_restore: executing SEQUENCE SET rental_rental_id_seq
pg_restore: executing SEQUENCE SET staff_staff_id_seq
pg_restore: executing SEQUENCE SET store_store_id_seq
pg_restore: warning: errors ignored on restore: 11
[postgres@PostgreSQL_Beta ~]$

3.5、恢复指定对象(不包括数据)


3.5.1、模拟删除表payment_p2007_06


  • 代码如下:
test1=# drop table payment_p2007_06 cascade;
NOTICE:  drop cascades to rule payment_insert_p2007_06 on table payment
DROP TABLE
test1=# \q

3.5.2、恢复对象


  • 说明:只恢复对象,可用“-s”。

  • 代码如下:
[postgres@node1 ~]$  pg_restore -Upostgres -dtest1 -tpayment_p2007_06 -s -v /postgres/backup/postgres.tar
pg_restore: connecting to database for restore
pg_restore: creating TABLE "public.payment_p2007_06"
[postgres@PostgreSQL_Beta ~]$

3.5.3、确认是否恢复成功


  • 代码如下:
[postgres@node1 ~]$  psql -Upostgres -dtest1
psql (16.2)
Type "help" for help.
 
test1=# \dt+ payment_p2007_06;
                                         List of relations
 Schema |       Name       | Type  |  Owner   | Persistence | Access method |  Size   | Description
--------+------------------+-------+----------+-------------+---------------+---------+-------------
 public | payment_p2007_06 | table | postgres | permanent   | heap          | 0 bytes |
(1 row)

test1=#

3.6、恢复指定表的表数据(不包括结构)


3.6.1、模拟删除表actor


  • 代码如下:
test1=#
test1=# delete from actor;
DELETE 200
test1=# \q

3.6.2、恢复test1数据库


  • 代码如下:
[postgres@node1 ~]$  pg_restore -Upostgres -dtest1 -tactor -a -v /postgres/backup/postgres.tar
pg_restore: connecting to database for restore
pg_restore: processing data for table "public.actor"
[postgres@node1 ~]$  

3.6.3、确认是否恢复成功


  • 代码如下:
[postgres@node1 ~]$  
[postgres@node1 ~]$  psql -Upostgres -dtest1
psql (16.2)
Type "help" for help.

test1=# select count(*) from actor;
 count
-------
   200
(1 row)

test1=#

四、psql恢复数据

4.1、备份文本格式test6.sql文件


  • 代码如下:

[postgres@node1 ~]$  pg_dump -f /postgres/backup/test6.sql
[postgres@node1 ~]$ 

4.2、备份文件test6.sql恢复到test6数据库


  • 代码如下:
[postgres@node1 ~]$  psql -f /postgres/backup/test6.sql test6
SET
SET
SET
............................................
CREATE SCHEMA
ALTER SCHEMA
COMMENT
REVOKE
GRANT
GRANT
[postgres@node1 ~]$ 


五、批量导入数据文件

5.1、恢复到新数据库中test7


  • 代码如下:
[postgres@node1 ~]$  createdb test7
[postgres@node1 ~]$  
[postgres@node1 ~]$  psql -dtest7
psql (16.2)
Type "help" for help.

test7=# \i /postgres/backup/test6.sql
SET
SET
set_config
------------

(1 row)

SET
SET
CREATE SCHEMA
ALTER SCHEMA
COMMENT
CREATE SCHEMA
.............................
ALTER TABLE
ALTER TABLE
REVOKE
GRANT
GRANT
test7=#

六、问题分享

  • 1、数据库已存在:如果尝试恢复的数据库已经存在,可以使用 -C 或 --create 选项来创建一个新的数据库,并将备份数据导入其中。例如:pg_restore -C -d new_database_name backup_file.dump。
  • 2、权限问题:如果遇到权限不足的问题,确保使用具有足够权限的用户执行恢复命令。如果需要,可以临时提升权限或更改文件所有权。
  • 3、文件缺失或损坏:检查所有备份文件是否完整且未损坏。如果文件丢失或损坏,需要重新获取备份或从其他来源恢复。
  • 4、版本不兼容:确保备份文件与当前数据库版本兼容。如果版本不匹配,可能需要升级或降级数据库。
  • 5、数据冲突:如果恢复的数据与现有数据冲突,可以考虑先备份现有数据,再进行恢复,或者在恢复前进行数据清理。
  • 6、操作错误:确保遵循正确的恢复流程,避免因操作不当导致的问题。

PostgreSQL 数据库还原时,可能会遇到多种问题,以下是一些常见错误及其解决方案:


七、总结

  • 确认 PostgreSQL 数据库备份文件的有效性 为了确保PostgreSQL数据库备份文件的有效性,您可以采取以下几种方法:

  • 备份文件大小检查:检查备份文件的大小是否与数据库的实际大小相符。如果备份文件大小明显小于数据库实际大小,可能意味着备份不完整。

  • 备份文件校验:对于支持校验和的备份格式,如SQL Server的.bak文件和Oracle的.dmp文件,可以计算备份文件的校验和,然后与备份文件中存储的校验和进行比较。如果校验和不匹配,可能意味着备份文件损坏或丢失数据。

  • 数据库恢复测试:定期从备份文件中恢复数据库,检查恢复过程是否顺利,以及恢复后的数据库是否与原始数据库一致。这可以确保备份文件中的数据可用且完整。

  • 数据库日志备份检查:对于支持事务日志备份的数据库(如SQL Server和Oracle),检查事务日志备份是否与数据备份一致。如果事务日志备份不完整或丢失,可能无法恢复到最后一个完整备份点。

  • 备份存储位置检查:确保备份文件存储在一个安全且可访问的位置。如果备份文件存储在易受攻击或不可访问的位置,可能会在需要时无法恢复数据。

  • 备份策略审计:定期审计数据库备份策略,确保备份频率、保留期和备份方法符合业务需求和合规要求。如果备份策略不再适用或不符合要求,可能需要调整和改进。

  • 以上方法可以帮助您验证PostgreSQL数据库备份文件的有效性,确保在必要时能够成功恢复数据。

最后修改时间:2024-06-17 12:24:21
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论