0
点赞
收藏
分享

微信扫一扫

CSS-0_2 CSS和继承(inherit & initial)

毅会 2024-06-25 阅读 35

1.pg_dump备份恢复

2.pg_dump常用参数

-h host,指定数据库主机名,或者 IP 
-p port,指定端口号 
-U user,指定连接使用的用户名 
-W,按提示输入密码 
dbname,指定连接的数据库名称,实际上也是要备份的数据库名称。 
-f,--file:输出到指定文件中 
-F,--format=c|d|t|p: 
 c 为自定义格式,也是二进制格式,压缩存储,只能使用 pg_restore 来还原, 可
以指定还原的表, 编辑 TOC 文件, 定制还原的顺序, 表, 索引等。 
 d 为目录 
 t 表示输出为 tar 包 
 p 为纯文本 SQL,大库不推荐; 
-j,--jobs=num:指定并行导出的并行度 
-a,--data-only:只导出数据,不导出表结构 
-c,--clean:是否生成清理该数据库对象的语句,比如 drop table 
-C,--create:是否输出一条创建数据库语句 
-n,--schema:只转存匹配 schema 的模式内容 
-N,--exclude-scheam:不转存匹配 schema 的模式内容 
-O,--no-owner,不设置导出对象的所有权 
-s,--schema-only:只导致对象定义模式,不导出数据 
-t,--table:只转存匹配到的表,视图,序列,可以使用多个-t 匹配多个表 
-T,--exclude-table:不转存匹配到的表。 
--inserts:使用 insert 命令形式导出数据,这种方式比默认的 copy 方式慢很多,但是可
用于将数据导入到非 PostgreSQL 数据库。 
--column-inserts:导出的数据,有显式列名 

3.备份

3.1指定库备份

1.导出sql文件

-- insert 命令形式导出库的数据 
pg_dump -h 127.0.0.1 -U postgres -p 5432 -W testdb --inserts > testdb.sql 
pg_dump testdb --inserts > testdb.sql 
pg_dump testdb --inserts --rows-per-insert=2 > testdb.sql --每次插入 2 行

2.导出指定对象

-- 要转储一个数据库到一个自定义格式归档文件: 
pg_dump -Fc testdb > testdb.dump 
-- 使用 5 个并行任务转储一个数据库到一个目录格式的归档 
pg_dump -Fd testdb -j 5 -f dumpdir

3.2单表备份

-- 备份单个表 
pg_dump -h 127.0.0.1 -U postgres -p 5432 -W testdb -t t1 --inserts > testdb.sql 

-- 备份多个表 
pg_dump -h 127.0.0.1 -U postgres -p 5432 -W testdb -t t1 -t t2 --inserts > 
testdb.sql

-- 如果只想备份 schema 模式中所有以 t 开头的表,但是不包括 t1 表 
pg_dump -t "public.t*" -T public.t1 testdb > testdb.sql 

-- 转储所有 testdb 的数据库对象,但是不包含以 1 结尾的表 
pg_dump -T '*1' testdb > testdb.sql 

-- 转储 testdb 中 public 和 test 这两个 schema 中的内容 
pg_dump -Fc -n public -n test testdb -f testdb.dump 

-- 转储 testdb 中除了 public schema 中的数据以外的所有数据 
pg_dump -Fc -N public testdb -f testdb.dump

3.3只备份数据

pg_dump -h 127.0.0.1 -U postgres -p 5432 -W testdb --inserts -a > testdb.sql

3.4只备份表结构

pg_dump -h 127.0.0.1 -U postgres -p 5432 -W testdb -s > testdb.sql

4.恢复

--恢复一个文本文档 
psql newdb < testdb.sql 

-- 要把一个归档文件重新载入到一个(新创建的)名为 newdb 的数据库: 
pg_restore -d newdb testdb.dump 

-- 把一个归档文件重新装载到同一个数据库(该归档正是从这个数据库中转储得来)中,
丢掉那个数据库中的当前内容 
pg_restore -d newdb --clean testdb.dump 

-- 备份后直接进行恢复,文件不落地 
pg_dump testdb| psql newdb 

-- 并行备份恢复 
pg_dump -Fd -j4 testdb -f dumpdir 
pg_restore -d newdb -j4 dumpdir

4.1利用toc文件选择性恢复

-- 根据二进制备份文件生成 toc 文件 
方式一:pg_restore -l testdb.dump > testdb.toc 
方式二:pg_restore -l -f testdb.toc testdb.dump 

-- 修改 toc 文件,用‘;’号注释掉不用还原的内容: 
 
-- 以 toc 文件列表做恢复 
pg_restore -Fc -L testdb.toc -d newdb testdb.dump 
-- 检查发现 t1 表没有被导入。

4.2使用unix管道压缩备份恢复

-- 导出并且压缩 
pg_dump testdb -f testdb.sql | gzip testdb.sql 

-- 解压并且导入,压缩文件不变: 
gunzip -c testdb.sql.gz | psql testdb 

-- 分割备份 
pg_dump testdb | split -b 1m 

-- 恢复 
cat filename* | psql dbname

5.迁移大表

5.1如果需要迁移多个大表怎么办?

示例:

pg_dump -Fd testdb -j 5 -f dump_dir 
pg_restore -d newdb -j 5 dump_dir

5.2 如果大多数表都很小,但有一张表非常大,如何迁移? 

5.3 如何使用多个线程迁移单个大表?

#suppose the filename is parallel_migrate.py 
import os 
import sys 
#source info 
source_url = sys.argv[1] 
source_table = sys.argv[2] 
 
#dest info 
dest_url = sys.argv[3] 
dest_table = sys.argv[4] 
#others 
total_threads=int(sys.argv[5]); 
size=int(sys.argv[6]); 
interval=size/total_threads; 
start=0; 
end=start+interval;
for i in range(0,total_threads): 
 if(i!=total_threads-1): 
 select_query = '\"\COPY (SELECT * from ' + source_table + ' WHERE 
id>='+str(start)+' AND id<'+str(end)+") TO STDOUT\""; 
 read_query = "psql \"" + source_url + "\" -c " + select_query 
 write_query = "psql \"" + dest_url + "\" -c \"\COPY " + dest_table +" 
FROM STDIN\"" 
 os.system(read_query+'|'+write_query + ' &') 
 else: 
 select_query = '\"\COPY (SELECT * from '+ source_table +' WHERE 
id>='+str(start)+") TO STDOUT\""; 
 read_query = "psql \"" + source_url + "\" -c " + select_query 
 write_query = "psql \"" + dest_url + "\" -c \"\COPY " + dest_table +" 
FROM STDIN\"" 
 os.system(read_query+'|'+write_query) 
 start=end; 
 end=start+interval;

 5.3.1如何调用并进行加载程序脚本

python parallel_migrate.py "source_connection_string" source_table 

"destination_connection_string" destination_table number_of_threads


count_of_table 
示例: 
python parallel_migrate.py "host=192.168.1.102 port=5432 dbname=postgres 
user=postgres password=postgres sslmode=prefer" t1 "host=192.168.1.102 
port=5432 dbname=newdb user=postgres password=postgres sslmode=prefer" 
t1 4 1000000

 5.3.2对比 Parallel Loader 与 pg_dump/pg_restore 迁移大表的性能

为了比较 pg_dump 和 pg_restore 与 Parallel Loader 脚本的性能,使用这两种技术将 1

TB 表从 testdb 数据库迁移到 newdb 数据库。

可以看到,Parallel Loader 脚本的执行速度比 pg_dump 和 pg_restore 快了 3 倍以上。

parallel loaderpg_dump&pg_restore

7 小时 45 分钟

超过一天

  

举报

相关推荐

0 条评论