postgresql 備份(pg_dump,pg_restore)

PG提供物理備份和邏輯備份(本篇主要講邏輯備份)
物理備份:WAL熱備份
邏輯備份:pg_dump,pg_dumpall,恢復時pg_restorenode

查看幫助命令:mysql

pg_dump --helpsql

跟MySQL本分以及參數比較像(大小寫敏感)數據庫

 

 

因爲個人是用的pgsql操做系統,超級用戶本地登陸,省略了-h 主機IP 以及-U 用戶名api

一、備份test 數據庫
pg_dump test >testdb.sql 跟MySQL同樣,生成的是sql文件操作系統

好比我把test數據庫恢復成testbak數據庫
create database testbak encoding UTF8;
[pgsql@node2 ~]$ psql testbak < /home/pgsql/test.sql
SET
SET
SET
SET
SET
set_config
------------

(1 row)rest

跟mysql 很相似。對象


也能夠指定格式的備份成*.dmp文件blog

[pgsql@node2 ~]$ pg_dump -Fc -Upgsql test > testpgsql.dmpip

恢復

create database test encoding UTF8;

[pgsql@node2 ~]$ pg_restore -d test testpgsql.dmp


2.想備份psql模式下全部表,可是不包括test2
test=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+--------------+----------+-------+------------+-------------
pgsql | blog | table | pgsql | 8192 bytes |
pgsql | persons | table | pgsql | 8192 bytes |
pgsql | students | table | pgsql | 8192 bytes |
pgsql | test_view | view | pgsql | 0 bytes |
pgsql | testtab01 | table | pgsql | 211 MB |
public | capitals | table | pgsql | 8192 bytes |
public | cities | table | pgsql | 8192 bytes |
public | duty | table | pgsql | 16 kB |
public | example | table | pgsql | 0 bytes |
public | non_capitals | table | pgsql | 8192 bytes |
public | products | table | pgsql | 8192 bytes |
public | t | table | pgsql | 0 bytes |
public | t1 | table | pgsql | 16 kB |
public | t2 | table | pgsql | 16 kB |
public | t_id_seq | sequence | pgsql | 8192 bytes |
public | test1 | table | pgsql | 20 MB |
public | test2 | table | pgsql | 8192 bytes |
public | weather | table | pgsql | 8192 bytes |
(18 rows)

[pgsql@node2 ~]$ pg_dump -t 'pgsql.t*' -T psql.t2 test > pgsqltest.sql

備份全部數據庫test 對象,不包括duty
[pgsql@node2 ~]$ pg_dump -T 'pgsql.duty' test > noduty.sql

在192.168.211.152 上備份,恢復到 192.168.211.154:
test=# create database hl;
CREATE DATABASE

[pgsql@node2 ~]$ pg_dump -h 192.168.211.152 -Upgsql test -Fc > remote_test.dmp
[pgsql@node2 ~]$ ls -lh
total 445M
-rw------- 1 pgsql pgsql 854 Apr 13 23:52 logfile
-rw-rw-r-- 1 pgsql pgsql 80M Jun 12 23:16 noduty.sql
-rw-rw-r-- 1 pgsql pgsql 75M Jun 12 23:14 pgsqltest.sql
-rw-rw-r-- 1 pgsql pgsql 106M Jun 12 23:26 remote_test.dmp
-rw-rw-r-- 1 pgsql pgsql 106M Jun 12 23:06 testpgsql.dmp
-rw-rw-r-- 1 pgsql pgsql 80M Jun 12 22:54 test.sql

testbak=# create database test;
CREATE DATABASE
[pgsql@node2 ~]$ pg_restore -h 192.168.211.152 -Upgsql -C -d test remote_test.dmp
-c, --clean clean (drop) database objects before recreating
-C, --create create the target database

最後,要講備份的 remote_test.dmp的數據從新加載到一個不是新建的不一樣名稱的數據庫 remote中,能夠使用下面命令:

createdb -T template0 remote;
pg_restore -d remote remote_test.dmp

 

 從template0而不是template1 建立數據庫,確保乾淨,沒有使用-C,而是直接恢復到remote數據庫上。

相關文章
相關標籤/搜索