postgresql 备份恢复(一)
備份恢復對于每個數據來說都是非常重要的。一般的數據庫都支持冷備份的方式,冷備份可以保證數據庫在此刻的完整性。但是其缺點也非常的明顯,為保持數據一致性。冷備份期間數據庫中相關數據是不能夠使用的,就大大影響的系統(tǒng)的可用性。不管怎樣冷備份在很多的情況下還是很有用的。
數據庫的冷備份一般支持兩種方式:
1,操作系統(tǒng)級別的命令備份(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
在恢復數據庫時,一定要確認數據庫已經創(chuàng)建。
?
使用pg_dump工具對數據庫進行備份只是備份了數據庫中的數據,對于配置文件等非數據文件沒有一起備份。顯然不如使用tar與gzip命令更方便。
2,表備份
?
代碼 [postgre@daduxiong ~]$ psql wyzWelcome 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的時候,提示命令錯誤。
pg_dump: No matching schemas were found.
?
懷疑是bug,有不同結果的朋友請指正。
postgresql當前版本:8.3.10
轉載于:https://www.cnblogs.com/daduxiong/archive/2010/08/30/1812403.html
總結
以上是生活随笔為你收集整理的postgresql 备份恢复(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET 中上下文的概念
- 下一篇: 在Linux下安装配置Oracle11g