Skip to content

Instantly share code, notes, and snippets.

@nicerobot
Created February 8, 2016 18:07
Show Gist options
  • Select an option

  • Save nicerobot/c24d2659652e0a14ecb3 to your computer and use it in GitHub Desktop.

Select an option

Save nicerobot/c24d2659652e0a14ecb3 to your computer and use it in GitHub Desktop.
Backup a PostgreSQL database with each schema in a separate folder.
#!/bin/bash
export PGDATABASE=${PGDATABASE:-${1:-postgres}} PGUSER=${PGUSER:-${2:-postgres}} PGHOST=${PGHOST:-localhost}
echo '\dn' \
| psql -t \
| awk '/[a-z]/ && /etl/ && !/topology|tiger/ && !/_20/ && $1 != "public" {printf "%s\n",$1}' \
| while read n; do
echo ${n%%_*}
done \
| sort -u \
| while read n; do
echo ${n}
pg_dump -Fc -Fd -C -j2 -O -x -f ${n} -n "${n}*" 2>&1 \
| tee ${n}.log
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment