Skip to content

Instantly share code, notes, and snippets.

@3manuek
Last active December 26, 2015 23:58
Show Gist options
  • Save 3manuek/7233930 to your computer and use it in GitHub Desktop.
Save 3manuek/7233930 to your computer and use it in GitHub Desktop.
Dump only views from a postgres database
#!/bin/bash
VIEWS="" ; for view in $(psql -U postgres -c 'select schemaname || $$.$$ ||viewname from pg_views where schemaname::text !~ $$information_schema|pg_catalog$$ ' -tA) ; do VIEWS="$VIEWS -t $view" ; done ; /opt/pg/bin/pg_dump -Upostgres -c -s $VIEWS
Optionally you can dump into a file:
VIEWS="" ; for view in $(psql -U postgres -c 'select schemaname || $$.$$ ||viewname from pg_views where schemaname::text !~ $$information_schema|pg_catalog$$ ' -tA) ; do VIEWS="$VIEWS -t $view" ; done ; /opt/pg/bin/pg_dump -Upostgres -c -s $VIEWS -f view_dump.sql
=====
#!/usr/local/bin/bash
DB=$1
PGBIN=/usr/local/bin
VIEWS="" ; for view in $($PGBIN/psql -U $USER $DB -c 'select schemaname || $$.$$ ||viewname from pg_views where schemaname::text !~ $$information_schema|pg_catalog$$ ' -tA) ; do VIEWS="$VIEWS -t $view" ; done ; $PGBIN/pg_dump -U $USER -c -s $VIEWS -f view_dump.sql $DB
#!/usr/local/bin/bash
DB=$1
DIA=$(date +%Y%m%d%H%M)
PGBIN=/usr/local/bin
VIEWS="" ; for view in $($PGBIN/psql -U $USER $DB -c 'select schemaname || $$.$$ ||viewname from pg_views where schemaname::text !~ $$information_schema|pg_catalog$$ ' -tA) ; do VIEWS="$VIEWS -t $view" ; done ; $PGBIN/pg_dump -U $USER
-c -s $VIEWS -f view_dump.sql $DB
uuencode view_dump.sql view_dump_$DIA.sql | mail -s "Backup vistas" soporte.solversystems@gmail.com
uuencode view_dump.sql view_dump_$DIA.sql | mail -s "Backup vistas" 3manuek@esdebian.org
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment