-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgsql_export_views
50 lines (42 loc) · 1.17 KB
/
pgsql_export_views
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
#
# Script for exporting views from database dump files.
#
# Howto:
# pg_dump -Fc -s -U <user> -d <database> -n <schema> -h <host> | pg_restore -l | grep "VIEWS" > view.out
# Variables
DUMPDIR=/var/local/pgbackup
PGRESTORE=/usr/bin/pg_restore
DAYOFWEEK=`/bin/date +\%w`
HOMEDIR=`pwd`
# Check arguments
if [ $# = 0 ]; then
/bin/cat <<eof
Usage: $0 <Database> <Schema>
This script exports all views from all or a named schema
from an PostgreSQL database dump file.
The export will be put into the file "yourviews.sql"
in the current directory!
<Database> Name of the database
<Schema> Name of the schema (or all for all schemas)
eof
exit 1;
fi
# Arguments
DATABASE=$1
SCHEMA=$2
DUMPFILE=backup_$DATABASE.dump;
if [ "$SCHEMA" == "" ]; then
echo "Please name schema or use 'all' for all schemas!";
exit 1;
fi
if [ "$SCHEMA" != "all" ]; then
`$PGRESTORE -n $SCHEMA -l $DUMPDIR/$DUMPFILE | /bin/grep VIEW >viewlist.txt`;
else
`$PGRESTORE -l $DUMPDIR/$DUMPFILE | /bin/grep VIEW >viewlist.txt`;
fi
`$PGRESTORE -L viewlist.txt $DUMPDIR/$DUMPFILE >yourviews.sql`
echo "You'll find the exported functions file at $HOMEDIR/yourviews.sql"
echo ""
`/bin/rm -rf viewlist.txt`
# end of file