PostgreSQL 循环导出schema的脚本

Posted 一泽涟漪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PostgreSQL 循环导出schema的脚本相关的知识,希望对你有一定的参考价值。

需要备份的schema列表

技术图片
$ cat need_backup_schema.txt 
pipeline_na_16q3_v4
pipeline_na_16q4_v8
pipeline_na_16q4_v8_ap_delta
pipeline_na_16q4_v8_dup_ids_fix
pipeline_na_17q1_v9_admin_rel
pipeline_na_17q1_v9_rel
pipeline_na_17q2_v10_rel
pipeline_na_17q2_v9_rel_usa_fixes
pipeline_na_17q3_17q1_v11_rel
pipeline_na_17q3_v11_base_rel
pipeline_na_17q3_v11_delta_rel
pipeline_na_17q3_v11_rel
pipeline_na_17q4_v11_base_rel
pipeline_na_17q4_v11_rel
uber_anz_geocoder_16q4_v1
uber_anz_geocoder_16q4_v14
uber_anz_geocoder_16q4_v15
uber_anz_geocoder_17q1_v15
uber_eu_geocoder_16q3_v14
uber_eu_geocoder_17q1_v14
uber_na_geocoder_16q1_v13
uber_na_geocoder_16q1_v13_patch
uber_na_geocoder_16q3_v14
uber_na_geocoder_16q4_v14
uber_na_geocoder_16q4_v15
uber_na_geocoder_17q1_v14
uber_na_geocoder_17q1_v14_mex
uber_na_geocoder_17q2_v15
uber_na_geocoder_17q3_v16
uber_na_geocoder_17q4_v16
uber_sea_geocoder_17q1_v1
uber_sea_geocoder_17q1_v3
uber_sea_geocoder_17q2_v1
uber_sea_geocoder_17q2_v2
View Code

循环备份的脚本

#! /bin/sh -x

database="TnGeo-Here-Data"
current_day=`date +%Y%m%d`
backup_dir="/data/02/pg_backup/exp"
log_dir="/data/02/pg_backup/log"
mail_list="******"
for schema in `cat need_backup_schema.txt`;
  do
    /usr/local/pgsql/bin/pg_dump --verbose --schema=${schema} ${database} | gzip > ${backup_dir}/${database}_${schema}_${current_day}.sql.gz | tee ${log_dir}/${database}_${schema}_${current_day}.log
    if $? != 0;
       then 
         mail -s "${schema} backup failed" ${mail_list} <  /dev/null
         exit 1
    fi  
  done
exit 0

备份完成后生成的备份名

$ ls -ltrh
total 208G
-rw-r--r-- 1 postgres appuser  22G Dec 13 06:15 TnGeo-Here-Data_pipeline_na_16q3_v4_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  13G Dec 13 07:07 TnGeo-Here-Data_pipeline_na_16q4_v8_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  473 Dec 13 07:07 TnGeo-Here-Data_pipeline_na_16q4_v8_ap_delta_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 2.6M Dec 13 07:07 TnGeo-Here-Data_pipeline_na_16q4_v8_dup_ids_fix_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  473 Dec 13 07:07 TnGeo-Here-Data_pipeline_na_17q1_v9_admin_rel_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  12G Dec 13 07:56 TnGeo-Here-Data_pipeline_na_17q1_v9_rel_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  18G Dec 13 09:11 TnGeo-Here-Data_pipeline_na_17q2_v10_rel_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  11G Dec 13 09:52 TnGeo-Here-Data_pipeline_na_17q2_v9_rel_usa_fixes_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 3.2G Dec 13 10:06 TnGeo-Here-Data_pipeline_na_17q3_17q1_v11_rel_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  13G Dec 13 11:10 TnGeo-Here-Data_pipeline_na_17q3_v11_base_rel_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 6.3G Dec 13 11:59 TnGeo-Here-Data_pipeline_na_17q3_v11_delta_rel_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 5.9K Dec 13 11:59 TnGeo-Here-Data_pipeline_na_17q3_v11_rel_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  13G Dec 13 13:08 TnGeo-Here-Data_pipeline_na_17q4_v11_base_rel_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 4.0K Dec 13 13:08 TnGeo-Here-Data_pipeline_na_17q4_v11_rel_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 756M Dec 13 13:10 TnGeo-Here-Data_uber_anz_geocoder_16q4_v1_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 755M Dec 13 13:13 TnGeo-Here-Data_uber_anz_geocoder_16q4_v14_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 756M Dec 13 13:16 TnGeo-Here-Data_uber_anz_geocoder_16q4_v15_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 762M Dec 13 13:18 TnGeo-Here-Data_uber_anz_geocoder_17q1_v15_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  14G Dec 13 14:19 TnGeo-Here-Data_uber_eu_geocoder_16q3_v14_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  14G Dec 13 15:19 TnGeo-Here-Data_uber_eu_geocoder_17q1_v14_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 8.1G Dec 13 15:59 TnGeo-Here-Data_uber_na_geocoder_16q1_v13_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  98M Dec 13 16:00 TnGeo-Here-Data_uber_na_geocoder_16q1_v13_patch_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 9.1G Dec 13 16:36 TnGeo-Here-Data_uber_na_geocoder_16q3_v14_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 9.2G Dec 13 17:11 TnGeo-Here-Data_uber_na_geocoder_16q4_v14_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 2.3K Dec 13 17:11 TnGeo-Here-Data_uber_na_geocoder_16q4_v15_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 9.2G Dec 13 17:47 TnGeo-Here-Data_uber_na_geocoder_17q1_v14_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 3.8K Dec 13 17:47 TnGeo-Here-Data_uber_na_geocoder_17q1_v14_mex_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  11G Dec 13 18:32 TnGeo-Here-Data_uber_na_geocoder_17q2_v15_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  12G Dec 13 19:30 TnGeo-Here-Data_uber_na_geocoder_17q3_v16_20191213.sql.gz
-rw-r--r-- 1 postgres appuser  11G Dec 13 20:14 TnGeo-Here-Data_uber_na_geocoder_17q4_v16_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 139M Dec 13 20:16 TnGeo-Here-Data_uber_sea_geocoder_17q1_v1_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 324M Dec 13 20:18 TnGeo-Here-Data_uber_sea_geocoder_17q1_v3_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 320M Dec 13 20:19 TnGeo-Here-Data_uber_sea_geocoder_17q2_v1_20191213.sql.gz
-rw-r--r-- 1 postgres appuser 449M Dec 13 20:22 TnGeo-Here-Data_uber_sea_geocoder_17q2_v2_20191213.sql.gz

 

以上是关于PostgreSQL 循环导出schema的脚本的主要内容,如果未能解决你的问题,请参考以下文章

气流导出模式仅从 PostgreSQL 到 bigquery

postgresql 导入 导出(一张表)

使用 pgAdmin 导出 Postgresql 表数据

postgresql自动导出SQL语句查出的数据

如何在 PostgreSQL 循环中使用 FOREACH

PostgreSQL 数据库导入导出