postgresql備份與恢復之SQL Dump

數據是很寶貴的,要時候謹記備份的重要性. 這裏講一下經過SQL Dump方式來作備份與恢復. pg_dump 導出某一個數據庫,經過將數據庫中的結構信息及數據經過sql方式輸出來備份數據庫.它是在執行命令那一刻時數據庫一致性狀態的保存. 恢復時只許將這輸出在目標庫上重建就能夠了.sql

#使用pg_dump命令備份數據庫

pg_dump 默認輸出到控制檯,不指定參數默認是導出鏈接着的數據庫.ide

[postgres@fnddb data]$ pg_dump | more
--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET lock_timeout = 0;
......
--
-- PostgreSQL database dump complete
--

一般的作法是備份到一個文件中.工具

[postgres@fnddb ~]$ pg_dump database1 > db1.dump

能夠導出一個schema,固然也能夠只導出一個表post

[postgres@fnddb ~]$ pg_dump database2 -n schema01
--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
......
ALTER SCHEMA schema01 OWNER TO postgres;

--
-- PostgreSQL database dump complete
--
[postgres@fnddb ~]$ pg_dump -U user2 database2 -t t1 > database2.dump

pg_dump是一個客戶端命令行工具,能夠經過pg_dump --help查看測試

#恢復pg_dump出來的文件.spa

使用psql dbname < pgdumpfile命令行

dbname是使用template0建立出來的全新數據庫,不然有可能已經有對象在其中.code

先查看下要備份的數據庫中有哪些表,屬於哪些用戶server

[postgres@fnddb ~]$ psql database1
psql (9.4.1)
Type "help" for help.

database1=# \dt
        List of relations
 Schema | Name | Type  |  Owner   
--------+------+-------+----------
 public | t1   | table | user1
 public | t2   | table | user1
 public | t3   | table | user2
 public | tab2 | table | postgres
 public | tab3 | table | postgres
(5 rows)

備份數據庫database1

[postgres@fnddb ~]$ pg_dump database1 > database1.pgdmp

使用template0建立一個全新數據庫

[postgres@fnddb ~]$ createdb -T template0 db01

進行恢復並檢查

[postgres@fnddb ~]$ psql db01 < database1.pgdmp 
SET
SET
SET
......
[postgres@fnddb ~]$ psql db01
psql (9.4.1)
Type "help" for help.

db01=# \dt
        List of relations
 Schema | Name | Type  |  Owner   
--------+------+-------+----------
 public | t1   | table | user1
 public | t2   | table | user1
 public | t3   | table | user2
 public | tab2 | table | postgres
 public | tab3 | table | postgres
(5 rows)

db01=# select * from t1;
  id   
-------
 user1
(1 row)

對象權限數據都還原了

#測試一下若是某個用戶不存在會如何

刪除user2,須要把user2涉及的對象權限刪除或者受權給其餘用戶

database1=# drop role user1;
ERROR:  role "user1" cannot be dropped because some objects depend on it
DETAIL:  owner of database database1
2 objects in database db01
2 objects in database database2
database1=# reassign owned by user2 to hippo;
REASSIGN OWNED
database1=# \c database2
You are now connected to database "database2" as user "postgres".
database2=# reassign owned by user2 to hippo;
REASSIGN OWNED
database2=# \c db01
You are now connected to database "db01" as user "postgres".
db01=# reassign owned by user2 to hippo;
REASSIGN OWNED
db01=# revoke create on tablespace ts02 from user2;
REVOKE
db01=# alter database database1 owner to hippo;
ALTER DATABASE
db01=# drop role user2;
DROP ROLE
db01=# \q

嘗試恢復

[postgres@fnddb ~]$ createdb -T template0 db02
[postgres@fnddb ~]$ psql db02 < database1.pgdmp 
SET
......
ERROR:  role "user2" does not exist
......
[postgres@fnddb ~]$ psql db02
psql (9.4.1)
Type "help" for help.

db02=# \dt
        List of relations
 Schema | Name | Type  |  Owner   
--------+------+-------+----------
 public | t1   | table | user1
 public | t2   | table | user1
 public | t3   | table | postgres
 public | tab2 | table | postgres
 public | tab3 | table | postgres
(5 rows)

*受權語句因爲沒有user2因此失敗了,t3 owner變成了postgres,也就是使用psql鏈接的用戶*

能夠經過參數ON_ERROR_STOP=on來中止繼續執行恢復命令

[postgres@fnddb ~]$ createdb -T template0 db03
[postgres@fnddb ~]$ psql --set ON_ERROR_STOP=on db03 < database1.pgdmp 
SET
......
ERROR:  role "user2" does not exist
[postgres@fnddb ~]$

#經過管道直接備份恢復數據庫

經過指定-1選項,能夠將恢復進程放在一個事務中

[postgres@fnddb ~]$ hostname -i
192.168.10.74
[postgres@fnddb ~]$ psql -h 192.168.10.72
Password: 
psql (9.4.1, server 9.3.5)
Type "help" for help.

postgres=# create database db01 template template0;
CREATE DATABASE
postgres=# \q
[postgres@fnddb ~]$ pg_dump database1 | psql -1 -h 192.168.10.72 db01
Password: 
SET
......
GRANT
ERROR:  role "r2" does not exist
ERROR:  current transaction is aborted, commands ignored until end of transaction block
ERROR:  current transaction is aborted, commands ignored until end of transaction block
ERROR:  current transaction is aborted, commands ignored until end of transaction block
ERROR:  current transaction is aborted, commands ignored until end of transaction block
[postgres@fnddb ~]$ psql -h 192.168.10.72 db01
Password: 
psql (9.4.1, server 9.3.5)
Type "help" for help.

db01=# \dt
No relations found.

把須要的role建立好再試一次

db01=# create role user1;
CREATE ROLE
db01=# create role user2;
CREATE ROLE
db01=# create role r1;
CREATE ROLE
db01=# create role r2;
CREATE ROLE
db01=# create role jack;
CREATE ROLE
db01=# \q
[postgres@fnddb ~]$ pg_dump database1 | psql -1 -h 192.168.10.72 db01
Password: 
SET
.......
[postgres@fnddb ~]$ psql -h 192.168.10.72 db01
Password: 
psql (9.4.1, server 9.3.5)
Type "help" for help.

db01=# \dt
        List of relations
 Schema | Name | Type  |  Owner   
--------+------+-------+----------
 public | t3   | table | hippo
 public | tab2 | table | postgres
 public | tab3 | table | postgres
(3 rows)

#pg_dumpall命令

pg_dump只導出一個數據庫,而且不導出role,tablespace等cluster級別的對象.
pg_dumpall導出全部包括role,tablespace及全部數據庫. pg_dumpall由於要導出role,tablespace信息,因此此命令須要superuser用戶

##pg_dumpall備份全庫

[postgres@fnddb ~]$ pg_dumpall > all.pgdmp

只導出Cluster-wide數據,不包括數據庫級別的對象及數據

[postgres@fnddb ~]$ pg_dumpall -g > all.pgdmp

能夠再使用pg_dump來導出單獨一個數據庫來使用

##恢復pg_dumpall備份的文件

恢復使用psql -f infile來進行,最好是使用一個全新的cluster來進行恢復

[postgres@fnddb ~]$ pg_dumpall | psql -h 192.168.10.72 -f -
Password: 
SET
......
psql:<stdin>:34: ERROR:  directory "/var/lib/pgsql/tsdata" does not exist
psql:<stdin>:35: ERROR:  directory "/var/lib/pgsql/tsdata02" does not exist
psql:<stdin>:36: ERROR:  tablespace "ts02" does not exist
psql:<stdin>:37: ERROR:  tablespace "ts02" does not exist
psql:<stdin>:38: ERROR:  tablespace "ts02" does not exist
psql:<stdin>:45: ERROR:  tablespace "ts01" does not exist
psql:<stdin>:46: ERROR:  database "database1" does not exist
psql:<stdin>:47: ERROR:  database "database1" does not exist
psql:<stdin>:48: ERROR:  database "database1" does not exist
psql:<stdin>:49: ERROR:  database "database1" does not exist
psql:<stdin>:50: ERROR:  tablespace "ts01" does not exist
CREATE DATABASE
CREATE DATABASE
CREATE DATABASE
REVOKE
REVOKE
GRANT
GRANT
psql:<stdin>:60: \connect: FATAL:  database "database1" does not exist

在目標庫上的表空間目錄須要事先建立好 而後再重建...

###注意點

pg_dump,pg_dumpall是備份恢復某一個時刻時,數據庫的一致性狀態.. 若是須要將數據庫從崩潰火丟失數據中,恢復到運行時最新的狀態,則須要使用在線備份恢復技術.

//END

相關文章
相關標籤/搜索