Search code examples
bashpostgresqlbackupmultiple-tables

backup multiple tables on one single sh script


I have a script which backups multiple tables in a single line as follow:

/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

I've created a new sh script to be able to re-utilize the command as follow:

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

As you can see, this works for only 1 table, I'm not sure how to pass multiple tables to the sh script (-t table1 -t table2 -t table3 etc)

I could use arrays, but still, not sure how to code this.

Thanks!


Solution

  • If you are willing to make DESTINATION the first expected argument, then something like this should work for you:

    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