Skip to content

Instantly share code, notes, and snippets.

@mmrwoods
Created April 14, 2014 07:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmrwoods/10623665 to your computer and use it in GitHub Desktop.
Save mmrwoods/10623665 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Create a clean SQL dump of each non-template postgres database
# Should be run as root to avoid any permissions issues
dest_dir=/var/local/backup/postgres
db_list_sql="SELECT datname FROM pg_database WHERE NOT datistemplate AND datallowconn ORDER BY datname"
for db in $(su - postgres -c "psql -At -c '$db_list_sql'"); do
su - postgres -c "pg_dump --clean $db" | gzip -9 > $dest_dir/$db.sql.gz
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment