星空网 > 软件开发 > 数据库

pg_dump 备份与恢复的简单操作

pg_dump 是一个用于备份PostgreSQL数据库的工具.
   该工具生成的转储格式可以分为两种,
   脚本  :     其中脚本格式是包含许多SQL命令的纯文本格式  (常用)
   归档文件:  需要重建数据库就必须和pg_restore工具一起使用
   下面按照这两种方式进行备份恢复操作。
 
环境简介:

操作对象:数据库为xzfb; 表lottu,lottu01

 
[postgres@oracle2 ~]$ psql -U lottu -d xzfb
psql (9.5.0)
Type "help" for help.
 
xzfb=> \d+
                      List of relations
 Schema |  Name   | Type  | Owner |    Size    | Description 
--------+---------+-------+-------+------------+-------------
 public | lottu   | table | lottu | 8192 bytes | 
 public | lottu01 | table | lottu | 8192 bytes | 
(2 rows)
 
xzfb=> select * from lottu;
 v_id | v_name  
------+---------
 1001 | lottu
 1002 | rax
 1003 | vincent
 1003 | vincent
(4 rows)
 
xzfb=> select * from lottu01;
  id  
------
 1001
 1002
(2 rows)

 
1.  生成脚本方式来备份,恢复

   备份整个数据库xzfb   
    先介绍-c参数; 相当于是对对象先删除;后创建的功效。

 
[postgres@oracle2 ~]$ pg_dump -c -f backup/xzfb.sql xzfb   -- 可以替换  pg_dump -c xzfb > ./backup/xzfb.sql 
[postgres@oracle2 ~]$ ll
total 36
drwxrwxr-x  2 postgres postgres 4096 Jan 28 14:35 backup
-rwxrwxr-x  1 postgres postgres 2273 Jan 27 14:27 config.sh
drwx------ 19 postgres postgres 4096 Jan 27 16:28 data
drwxrwxr-x  2 postgres postgres 4096 Jan 28 00:00 log
-rwxrwxr-x  1 postgres postgres  930 Jan 27 16:45 lottu.sh
-rw-rw-r--  1 postgres postgres 5087 Jan 27 16:40 master.log
-rwxrwxr-x  1 postgres postgres  797 Jan 27 12:57 remove.sh
-rwxrwxr-x  1 postgres postgres 2043 Jan 27 16:35 start.sh
[postgres@oracle2 ~]$ cd backup
[postgres@oracle2 backup]$ ll
total 8
-rw-rw-r-- 1 postgres postgres 1571 Jan 28 14:35 xzfb01.sql
-rw-rw-r-- 1 postgres postgres 1964 Jan 28 14:41 xzfb.sql
 

恢复整个数据库xzfb  ;由于备份的是脚本;可以直接psql执行即可。 

 
[postgres@oracle2 ~]$ dropdb -U postgres xzfb
[postgres@oracle2 ~]$ createdb -U postgres -O lottu -D lottu -e xzfb
CREATE DATABASE xzfb OWNER lottu TABLESPACE lottu;
[postgres@oracle2 ~]$ psql -U lottu -d xzfb
psql (9.5.0)
Type "help" for help.
 
xzfb=> \d
No relations found.

 从这里可以看出xzfb库已经删除了已创建;里面是没有表关联现在执行 psql -U lottu -d xzfb < ./backup/xzfb.sql 进行恢复

 
[postgres@oracle2 backup]$ psql -U lottu -d xzfb < xzfb.sql
 

进入数据库可以看到数据已经回来了


[postgres@oracle2 backup]$ psql -U lottu -d xzfb
psql (9.5.0)
Type "help" for help.
 
xzfb=> \d
        List of relations
 Schema |  Name   | Type  | Owner 
--------+---------+-------+-------
 public | lottu   | table | lottu
 public | lottu01 | table | lottu
(2 rows)
 
xzfb=> select * from lottu;
 v_id | v_name  
------+---------
 1001 | lottu
 1002 | rax
 1003 | vincent
 1003 | vincent
(4 rows)


 
对 表的数据也可以这样恢复;这里不简介了;大家备份的脚本可以查看下;里面就是在psql能运行的脚本。
 
2. 备份格式为 归档文件的备份,恢复
 
  •  使用dump格式备份和恢复

 首先介绍下

参数-F: p(plain): 纯文本格式的SQL脚本文件(缺省)。c(custom): 输出适合于pg_restore的自定义归档格式。 这是最灵活的格式,它允许对装载的数据和对象定义进行重新排列。这个格式缺省的时候是压缩的。t(tar): 输出适合于 pg_restore的tar归档文件。使用这个归档允许在恢复数据库时重新排序和/或把数据库对象排除在外。同时也可以在恢复的时候限制对哪些数据进行恢复。

 
[postgres@oracle2 ~]$ pg_dump -U postgres -Fc xzfb > ./backup/xzfb02.dump             --备份

[postgres@oracle2 ~]$ dropdb -U postgres xzfb                                                          --删库操作
[postgres@oracle2 ~]$ createdb -U postgres -O lottu -D lottu -e xzfb                             --建库操作

[postgres@oracle2 ~]$ pg_restore -U postgres -d xzfb ./backup/xzfb02.dump > ./backup/dump01.log 2>&1                --恢复操作
[postgres@oracle2 ~]$ psql -U lottu -d xzfb
psql (9.5.0)
Type "help" for help.
 
xzfb=> \d
        List of relations
 Schema |  Name   | Type  | Owner 
--------+---------+-------+-------
 public | lottu   | table | lottu
 public | lottu01 | table | lottu
(2 rows)
 
xzfb=> select * from lottu;
 v_id | v_name  
------+---------
 1001 | lottu
 1002 | rax
 1003 | vincent
 1003 | vincent
(4 rows)

恢复OK;恢复详细步骤看./backup/dump01.log内容;

 
  • 使用tar格式备份和恢复
跟上面备份,恢复没什么两样
     [postgres@oracle2 ~]$ pg_dump -U postgres -Ft xzfb > ./backup/xzfb03.tar  
     [postgres@oracle2 ~]$ pg_restore -U postgres -d xzfb ./backup/xzfb03.tar  > ./backup/dump02.log 2>&1
 
参考文献:对pg_dump参数详解:
--http://www.cnblogs.com/stephen-liu74/archive/2012/06/01/2307813.html



原标题:pg_dump 备份与恢复的简单操作

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

HOODS:https://www.goluckyvip.com/tag/21278.html
hooeye:https://www.goluckyvip.com/tag/21279.html
跨境购买行为:https://www.goluckyvip.com/tag/2128.html
Hooke Road:https://www.goluckyvip.com/tag/21280.html
HookLogic:https://www.goluckyvip.com/tag/21281.html
Hoolah:https://www.goluckyvip.com/tag/21282.html
旅游景点的打油诗 旅游景点的打油诗怎么写:https://www.vstour.cn/a/366180.html
2024深圳龙岗区文化馆免费音乐培训课程安排+报名方式:https://www.vstour.cn/a/366181.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流