Skip to content

Instantly share code, notes, and snippets.

@austinorth
Created February 27, 2019 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save austinorth/f3d887d04c0a3f26fdd7499e0c4db8ec to your computer and use it in GitHub Desktop.
Save austinorth/f3d887d04c0a3f26fdd7499e0c4db8ec to your computer and use it in GitHub Desktop.
Bash script to backup each schema in a database in a separate backup file.
#!/bin/bash
if [ $# -eq 0 ]; then
echo "Usage: ./backup-schemas-separately.sh yourdatabasename" 1>&2
exit 1;
fi;
db=$1
schemas=$(psql -U postgres -w -h localhost -qAt -c "SELECT nspname FROM pg_catalog.pg_namespace;" "${db}")
for schema in $schemas; do
pg_dump -F c -w -h localhost -U postgres -n "${schema}" "${db}" > /home/$USER/backups/"${schema}".custom
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment