【问题标题】:backup multiple tables on one single sh script在一个 sh 脚本上备份多个表
【发布时间】:2020-12-19 17:35:34
【问题描述】:

我有一个脚本可以在一行中备份多个表,如下所示:

/usr/local/pgsql/bin/pg_dump  --quote-all-identifiers --username=postgres -p 5432 -t schema.table1 -t schema.table2 -t schema.table3 -t schema.table4 -h localhost mydb | gzip -1 > file.dmp.gz

我创建了一个新的 sh 脚本,以便能够重新使用以下命令:

backup_table.sh

$TABLE=$1
$DESTINATION=$2

/usr/local/pgsql/bin/pg_dump  --quote-all-identifiers --username=postgres -p 5432 -t $TABLE -h localhost mydb | gzip -1 > $DESTINATION

如您所见,这仅适用于 1 个表,我不确定如何将多个表传递给 sh 脚本(-t table1 -t table2 -t table3 等)

我可以使用数组,但仍然不知道如何编写代码。

谢谢!

【问题讨论】:

    标签: bash postgresql backup multiple-tables


    【解决方案1】:

    如果您愿意将DESTINATION 作为第一个预期参数,那么这样的方法应该适合您:

    DESTINATION=$1
    TABLES=`echo ${@:2}|sed "s/\s/ -t /g"`
    
    /usr/local/pgsql/bin/pg_dump  --quote-all-identifiers --username=postgres -p 5432 -t $TABLES -h localhost mydb | gzip -1 > $DESTINATION
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 2014-07-10
      • 2014-03-15
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多