备份恢复对于每个数据来说都是非常重要的。一般的数据库都支持冷备份的方式,冷备份可以保证数据库在此刻的完整性。但是其缺点也非常的明显,为保持数据一致性。冷备份期间数据库中相关数据是不能够使用的,就大大影响的系统的可用性。不管怎样冷备份在很多的情况下还是很有用的。

数据库的冷备份一般支持两种方式:

1,操作系统级别的命令备份(cp,copy)

2,数据库工具备份(pg_dump)

针对postgresql数据库的pg_dump工具进行了一下测试(还碰到一个小问题)。

pg_dump工具命令与参数的参考:

代码

pg_dump dumps a database as a text file or to other formats.

Usage:
pg_dump [OPTION]... [DBNAME]

General options:
-f, --file=FILENAME output file name
-F, --format=c|t|p output file format (custom, tar, plain text)
-i, --ignore-version proceed even when server version mismatches
pg_dump version
-v, --verbose verbose mode
-Z, --compress=0-9 compression level for compressed formats
--help show this help, then exit
--version output version information, then exit

Options controlling the output content:
-a, --data-only dump only the data, not the schema
-b, --blobs include large objects in dump
-c, --clean clean (drop) schema prior to create
-C, --create include commands to create database in dump
-d, --inserts dump data as INSERT commands, rather than COPY
-D, --column-inserts dump data as INSERT commands with column names
-E, --encoding=ENCODING dump the data in encoding ENCODING
-n, --schema=SCHEMA dump the named schema(s) only
-N, --exclude-schema=SCHEMA do NOT dump the named schema(s)
-o, --oids include OIDs in dump
-O, --no-owner skip restoration of object ownership
in plain text format
-s, --schema-only dump only the schema, no data
-S, --superuser=NAME specify the superuser user name to use in
plain text format
-t, --table=TABLE dump the named table(s) only
-T, --exclude-table=TABLE do NOT dump the named table(s)
-x, --no-privileges do not dump privileges (grant/revoke)
--disable-dollar-quoting disable dollar quoting, use SQL standard quoting
--disable-triggers disable triggers during data-only restore
--use-set-session-authorization
use SESSION AUTHORIZATION commands instead of
ALTER OWNER commands to set ownership

Connection options:
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port number
-U, --username=NAME connect as specified database user
-W, --password force password prompt (should happen automatically)

If no database name is supplied, then the PGDATABASE environment
variable value is used.

Report bugs to <pgsql-bugs@postgresql.org>.

通过pg_dump工具分别进行数据库,表,schema方式的备份。

1,数据库备份

代码

[postgre@daduxiong bin]$ dropdb wyz
[postgre@daduxiong bin]$ createdb wyz
[postgre@daduxiong bin]$ psql wyz
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

wyz=# create table abc(id integer);
CREATE TABLE
wyz=# insert into abc values(1);
INSERT 0 1
wyz=# \q
[postgre@daduxiong bin]$ pg_dump wyz >/usr/local/pgsql/data/wyz.dmp
[postgre@daduxiong bin]$ dropdb wyz
[postgre@daduxiong bin]$ createdb wyz
[postgre@daduxiong bin]$ psql wyz </usr/local/pgsql/data/wyz.dmp
SET
SET
SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
REVOKE
REVOKE
GRANT
GRANT
[postgre@daduxiong bin]$ psql wyz
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

wyz=# select * from abc;
id
----
1
(1 row)

wyz=#
对于空间不足或者文件较大时可以采用压缩的方式。
[postgre@daduxiong bin]$ pg_dump wyz |gzip >/usr/local/pgsql/data/wyz.gz

在恢复数据库时,一定要确认数据库已经创建。

使用pg_dump工具对数据库进行备份只是备份了数据库中的数据,对于配置文件等非数据文件没有一起备份。显然不如使用tar与gzip命令更方便。

2,表备份

代码

[postgre@daduxiong ~]$ psql wyz
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

wyz=# select * from abc;
id
----
1
(1 row)

wyz=# insert into abc values(2);
INSERT 0 1
wyz=# \q
[postgre@daduxiong ~]$ pg_dump wyz --table abc >/usr/local/pgsql/data/abc.sql
[postgre@daduxiong ~]$ psql wyz
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

wyz=# drop table abc;
DROP TABLE
wyz=# \q
[postgre@daduxiong ~]$ more /usr/local/pgsql/data/abc.sql
--
-- PostgreSQL database dump
--

SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;

SET search_path = public, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: abc; Type: TABLE; Schema: public; Owner: postgre; Tablespace:
--

CREATE TABLE abc (
id integer
);

ALTER TABLE public.abc OWNER TO postgre;

--
-- Data for Name: abc; Type: TABLE DATA; Schema: public; Owner: postgre
--

COPY abc (id) FROM stdin;
1
2
\.

--
-- PostgreSQL database dump complete
--

[postgre@daduxiong ~]$ psql wyz </usr/local/pgsql/data/abc.sql
SET
SET
SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
[postgre@daduxiong ~]$ psql wyz
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

wyz=# select count(*) from abc;
count
-------
2
(1 row)

wyz=#

表备份时还可以根据参数的选用将表的数据dump成insert语句。

3,SCHEMA备份

代码

[postgre@daduxiong ~]$ rm -rf /usr/local/pgsql/data/123.sql
[postgre@daduxiong ~]$ pg_dump wyz -N postgre -h daduxiong -f /usr/local/pgsql/data/123.sql
[postgre@daduxiong ~]$ pg_dump wyz -N wyz -h daduxiong -f /usr/local/pgsql/data/234.sql
[postgre@daduxiong ~]$ more /usr/local/pgsql/data/123.sql
--
-- PostgreSQL database dump
--

SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;

SET search_path = public, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: abc; Type: TABLE; Schema: public; Owner: postgre; Tablespace:
--

CREATE TABLE abc (
id integer
);

ALTER TABLE public.abc OWNER TO postgre;

--
-- Data for Name: abc; Type: TABLE DATA; Schema: public; Owner: postgre
--

COPY abc (id) FROM stdin;
1
2
\.

--
-- Name: public; Type: ACL; Schema: -; Owner: postgre
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM postgre;
GRANT ALL ON SCHEMA public TO postgre;
GRANT ALL ON SCHEMA public TO PUBLIC;

--
-- PostgreSQL database dump complete
--

[postgre@daduxiong ~]$ more /usr/local/pgsql/data/234.sql
--
-- PostgreSQL database dump
--

SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;

SET search_path = public, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: abc; Type: TABLE; Schema: public; Owner: postgre; Tablespace:
--

CREATE TABLE abc (
id integer
);

ALTER TABLE public.abc OWNER TO postgre;

--
-- Data for Name: abc; Type: TABLE DATA; Schema: public; Owner: postgre
--

COPY abc (id) FROM stdin;
1
2
\.

--
-- Name: public; Type: ACL; Schema: -; Owner: postgre
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM postgre;
GRANT ALL ON SCHEMA public TO postgre;
GRANT ALL ON SCHEMA public TO PUBLIC;

--
-- PostgreSQL database dump complete
--

在我的wyz数据库中有两个用户(postgre,wyz),可备份测试的时候都是备份的postgre用户的数据。
在采用参数-n的时候,提示命令错误。

[postgre@daxuxiong ~]$ pg_dump wyz -n wyz -f /usr/local/pgsql/data/456.sql
pg_dump: No matching schemas were found.

怀疑是bug,有不同结果的朋友请指正。
postgresql当前版本:8.3.10

转载于:https://www.cnblogs.com/daduxiong/archive/2010/08/30/1812403.html

postgresql 备份恢复(一)相关推荐

  1. PostgreSQL备份恢复实现

    点击上方"蓝字" 关注我们,享更多干货! 本文主要介绍pg_dump.pg_dumpall.copy.pg_basebackup的使用. 一. pg_basebackup 1. p ...

  2. 数据库误操作后悔药来了:AnalyticDB PostgreSQL教你实现分布式一致性备份恢复

    简介:本文将介绍AnalyticDB PostgreSQL版备份恢复的原理与使用方法. 一.背景 AnalyticDB PostgreSQL版(简称ADB PG)是阿里云数据库团队基于PostgreS ...

  3. PostgreSQL的pg_basebackup备份恢复详解

    备份 pg_basebackup -D /tmp/pg_backup/ -Ft -Pv -U postgres -h 1.15.57.253 -p5432 -R -D 空文件,没有该目录会自动创建 F ...

  4. PostgreSQL之pgdump备份恢复

    逻辑备份在恢复时,介于逻辑备份与故障时间点之间的数据难以恢复,故一般不采取逻辑备份方式进行数据库备份,但逻辑适用于跨平台跨版本的数据迁移: 逻辑备份恢复主要以下三种: pg_dump pg_dumpa ...

  5. mysql 8.0数据备份恢复_MySQL 8.0 增强逻辑备份恢复工具介绍-爱可生

    作者:杨涛涛 资深数据库专家,专研 MySQL 十余年.擅长 MySQL.PostgreSQL.MongoDB 等开源数据库相关的备份恢复.SQL 调优.监控运维.高可用架构设计等.目前任职于爱可生, ...

  6. PG13用pg_rman进行备份恢复

    pg_rman进行备份恢复 环境参数: linux版本:CentOS 7.6 PG版本:13.2 docker版本: 18.06.3 1.在容器内源码安装PostgreSQL13.2 要先根据搭建Ce ...

  7. PostgreSQL重启恢复---XLOG 2.0

    XLOG 2.0 预备知识 <PostgreSQL重启恢复-XLOG 1.0> 概述 在<PostgreSQL重启恢复-XLOG>中,我们查询的XLOG的组织结构.XLOG写入 ...

  8. 详解mysql备份恢复的三种实现方式

    一.Mysql备份策略: 完整备份: 完整备份就是指对某一个时间点上的所有数据或应用进行的一个完整拷贝,对数据量大的,备份时间较长,当然数据在恢复的时候快. 增量备份: 备份自上一次备份(包括完整备份 ...

  9. Green Plum 非并行备份恢复方案

    Green Plum 非并行备份恢复方案 环境 centos 7 主 145.170.41.153(master) root/123456 145.170.41.154(segment) root/1 ...

最新文章

  1. 利用BP神经网络教计算机识别语音特征信号(代码部分SL)
  2. Java循环中删除一个列表元素
  3. [Java入门笔记] 面向对象三大特征之:封装
  4. POJ 2135 最小费用最大流
  5. 艰苦的网购火车票,周末可以不用再秒杀余票了
  6. 非常好用的游戏数据保存类:ScriptableObject
  7. python调用curl_Python3模拟curl发送post请求操作示例
  8. 【AR】DroidCam笔记本调用手机摄像头(smartphone's camera as pc webcam)
  9. 用Java实现一个台球小游戏
  10. 【深入kotlin】 - 匿名函数、闭包和接收者
  11. 在头条号和西瓜视频发布视频,播放量20万,却是零收益?
  12. B. Ternary Sequence
  13. 高速公路ETC卡签之我见2-卡片消费
  14. 手机加密聊天软件的实现(基于android系统)
  15. 白屏时间first paint 和可交互时间dom ready的关系
  16. error: expected ‘=‘, ‘,‘, ‘;‘, ‘asm‘ or ‘__attribute__‘ before ‘{‘ token
  17. tabindex标签的用法
  18. 论文笔记--GMAN: A Graph Multi-Attention Network for Traffic Prediction
  19. java 函数 作为参数_如何在Java中将函数作为参数传递?
  20. 微型计算机原理指令系统,微机原理与接口技术 指令系统.ppt

热门文章

  1. Redhat Linux 防火墙
  2. python实验收获和建议_python实验收获与反思 100字_考试后的反思100字
  3. Linux的基本原则
  4. Python IDE 详细攻略,拿来吧你~
  5. 如何友好的把Python和Bash结合在一起
  6. 还在用Matplotlib? 又一可视化神器pyecharts登场
  7. python段错误原因_python – 捕获崩溃的子进程的“分段错误”...
  8. java中toarray()的 用法_java容器中toArray的用法
  9. rola物联网框架_如何搭建一个物联网系统框架?
  10. 什么是cep算子_Flink中的CEP复杂事件处理 (源码分析)