文章目录
一、逻辑备份
1.1 逻辑备份工具
PG提供了pg_dump、pg_dumpall两种方式进行逻辑备份,其区别就是pg_dumpall只能将数据库全部数据集dump到一个脚本文件中,而pg_dump可以选择指定数据库进行逻辑备份。两者均可以保证dump数据的一致性,且不阻塞其他会话的正常读写操作。
1、pg_dump基本语法
1)基本语法
pg_dump [connection_option] [option] [dbname]
connection_option
参数 | 含义 |
---|---|
-h/–host | 主机host |
-p/–port | 端口port |
-U | 用户名username |
-w | 从不提示密码,若服务器请求需要密码认证,且密码无法通过其他方式获取会导致命令连接失败 |
-W | 强制pg_dump连接一个数据库之前提示密码,一般不需要关注该参数 |
–role | 若验证用户权限不够该命令可保证切换至到相应权限的角色下 |
dbname | 指定逻辑备份数据库 |
option
参数 | 含义 |
---|---|
-a/–data-only | 只输出数据、不输出表定义SQL语句,只针对纯文本格式有意义 |
-b/–blob | dump是否包含大对象,该参数默认打开 |
-c/–clean | 是否包含清理对象语句,只针对纯文本格式有意义 |
-C/–create | 是否输出create database语句,只针对纯文本格式有意义 |
-E/–encoding | 指定字符集编码格式 |
-f/–file | 输出到指定文件 |
-F/–format | p、c、d、t四种格式,详情如下 |
-n/–schema | 只转储满足匹配下的schema包含的对象、可指定多个模式、也可以使用通配符匹配 |
-N/–exclude-schema | 不转储满足匹配条件下指定的schema下的对象 |
-O/–no-owner | 表示不把对象所有权设置为源数据库中的owner |
-s/–schema-only | 只输出对象定义,不输出数据 |
-t/–table | 只转储满足匹配的表、视图、序列 |
-T/–exclude-table | 不转储满足匹配条件下的表 |
-Z/–compress=0…9 | 压缩级别,0表示不压缩。默认不压缩 |
–insert | 将数据转储为SQL语句格式,该参数下恢复速度会很慢 |
–column-inserts | 转储为Insert into table(col,…) values的格式,避免了–insert下源目标端表列顺序不一致导致数据恢复报错 |
–lock-wait-timeout | 指定在dump时等待获取共享表锁的超时时间 |
–no-tablespaces | 转储数据不输出选择指定的表空间,表示恢复数据都使用默认表空间 |
pg_dump 支持4种格式
- p, plain:
默认格式, 备份输出为可读的text文本. 还原时在数据库中直接执行备份文本的SQL即可.
- c, custom
可自定义的归档格式, 同时默认开启了数据压缩,还原时可以调整备份对象的还原顺序,同时支持选择还原的对象。
备份写入到一个文件中. 需要注意文件系统支持的单个文件大小。
这个格式必须使用pg_restore命令进行还原。
- d, directory
目录归档格式, 与custom格式类似, 需要使用pg_restore还原. 但是目录归档格式下会创建一个目录, 然后每个表或者每个大对象对应一个备份输出文件.
加上TOC文件名描述备份的详细信息, 这个格式默认支持压缩, 同时支持并行导出.
- t, tar
tar归档格式, 不支持压缩, 同时限制每个表最大不能超过8GB, 同样需要使用pg_restore还原.
2、备份命令示例
- 备份指定数据库下所有对象
# pg_dump -U postgres -p 5432 -h 127.0.0.1 db1 -f db1.sql
- 备份指定数据库下的指定表
# 备份public下的t1、t2表
# pg_dump -U postgres -p 5432 -h 127.0.0.1 db1 -n public -t t1 -t t2
# 备份public下的满足t1*的所有表
# pg_dump -U postgres -p 5432 -h 127.0.0.1 db1 -n public -t 't1*'
- 将逻辑备份转储为custom-format的归档文件
# pg_dump -U postgres -p 5432 -h 127.0.0.1 db1 -Fc -f db1.dump
- 将逻辑备份通过directory-format进行归档备份,并开启一定并发线程
# pg_dump -U postgres -p 5432 -h 127.0.0.1 db1 -Fd -j 5 -f ./aa
# cd aa/
# ll
total 16
-rw-r--r-- 1 root root 35 Sep 3 22:44 3107.dat.gz
-rw-r--r-- 1 root root 35 Sep 3 22:44 3108.dat.gz
-rw-r--r-- 1 root root 25 Sep 3 22:44 3109.dat.gz
-rw-r--r-- 1 root root 3887 Sep 3 22:44 toc.dat
1.2 逻辑备份恢复
PG提供了pg_restore的命令可以为通过pg_dump转储的数据进行逻辑恢复。对于SQL脚本可通过psql进行恢复
1、pg_dump基本语法
pg_restore [connection_option] [option] [filename]
connection_option与pg_dump类似,不同之处是pg_restore恢复具体数据库时需要使用-d dbname来指定。
options
参数 | 含义 |
---|---|
filename | 需要恢复的备份文件 |
-a/–no-data | 只恢复数据,不恢复数据定义 |
-c/–clean | 恢复数据前先清空对应数据 |
-C/–create | 在恢复数据前先创建 |
-d/–dbname | 指定恢复到具体数据库中 |
-e/–exit-on-error | 表示恢复数据库遇到报错则退出,默认报错仍继续并最终显示一个错误计数 |
-F/–format | 指定恢复的备份文件格式,一般而言pg_restore会自行判断,若需要指定可以指定t、d、c |
-I/–index | 只恢复命名的索引 |
-j | 开启多个并发进行恢复 |
-n namespaces 或 --schema | 只恢复指定名字的模式或者表数据,可配合-t使用 |
-O/–no-owner | 默认恢复对象是不指定owner |
–no-tablespaces | 恢复数据均恢复至默认表空间 |
-P | 只恢复指定函数 |
-s/–schema-only | 只恢复指定表结构 |
-t/–table | 只恢复指定表 |
-T/–trigger | 只恢复指定触发器 |
-f | 指定恢复文件 |
2、psql恢复指定SQL脚本
# psql -U postgres -p 5433 -h 127.0.0.1 -d db1 -f /root/db1.sql
postgres=# \c db1
psql (12.4, server 12.2)
You are now connected to database "db1" as user "postgres".
db1=# \d
List of relations
Schema | Name | Type | Owner
--------+-----------+----------+----------
public | t1 | table | postgres
public | t1_id_seq | sequence | postgres
public | t2 | table | postgres
public | v_t1 | view | postgres
(4 rows)
3、pg_restore使用示例
- 恢复format=c下的数据
# pg_dump -U postgres -p 5432 -h 127.0.0.1 db1 -Fc -f db1.dump
# pg_restore -U postgres -p 5433 -h 127.0.0.1 -d postgres -C /root/db1.dump //-C指定创建对应的dbname,若不使用-C则会恢复带-d指定数据库下
# psql -U postgres -p 5433 -h127.0.0.1 postgres
psql (12.4, server 12.2)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
db1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
postgres=# \c db1
psql (12.4, server 12.2)
You are now connected to database "db1" as user "postgres".
db1=# \d
List of relations
Schema | Name | Type | Owner
--------+-----------+----------+----------
public | t1 | table | postgres
public | t1_id_seq | sequence | postgres
public | t2 | table | postgres
public | v_t1 | view | postgres
(4 rows)
- 将dump文件恢复至new_dbname中
# pg_restore -U postgres -p 5433 -h 127.0.0.1 -d db2 /root/db1.dump //需要提前创建好new_dbname
db1=# \c db2
psql (12.4, server 12.2)
You are now connected to database "db2" as user "postgres".
db2=# \d
List of relations
Schema | Name | Type | Owner
--------+-----------+----------+----------
public | t1 | table | postgres
public | t1_id_seq | sequence | postgres
public | t2 | table | postgres
public | v_t1 | view | postgres
(4 rows)
- 恢复指定表
# pg_restore -U postgres -p 5433 -h 127.0.0.1 -d postgres -n public -t t1 -C /root/db1.dump
- 恢复-d格式下备份文件至已存在部分对象的数据库中
# pg_restore -U postgres -p 5433 -h 127.0.0.1 -d db1 -c /root/aa/ //-c表示先做清理后恢复