-
Notifications
You must be signed in to change notification settings - Fork 1
/
lf_export
executable file
·39 lines (35 loc) · 1.24 KB
/
lf_export
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
#!/bin/sh
if [ -z "$1" -o -z "$2" ]; then
echo "Usage: $0 <dbname> <filename>.sql.gz"
exit 1
fi
EXPORT_DBNAME=liquid_feedback_autoexport
retval=0
echo "Cleanup direct_population_snapshot"
echo "DROP TABLE direct_population_snapshot_bak; CREATE TABLE direct_population_snapshot_bak AS (SELECT * FROM direct_population_snapshot); DROP TABLE direct_population_snapshot; ALTER TABLE direct_population_snapshot_bak RENAME TO direct_population_snapshot;" | psql liquid_feedback
echo "Dropping database \"$EXPORT_DBNAME\" if existent..."
dropdb "$EXPORT_DBNAME" 2> /dev/null
echo "Copying database \"$1\" to new database \"$EXPORT_DBNAME\"..."
# TODO: use character encoding of original database
if (createdb "$EXPORT_DBNAME" && pg_dump "$1" | psql -f - "$EXPORT_DBNAME" > /dev/null)
then
echo "Deleting private data in copied database..."
if psql -v ON_ERROR_STOP=1 -c 'SELECT delete_private_data()' "$EXPORT_DBNAME" > /dev/null
then
echo "Dumping and compressing copied database to \"$2\"..."
if pg_dump --no-owner --no-privileges "$EXPORT_DBNAME" | gzip -9 > "$2"
then
true
else
retval=4
fi
else
retval=3
fi
else
retval=2
fi
echo "Dropping database \"$EXPORT_DBNAME\"..."
dropdb "$EXPORT_DBNAME"
echo "DONE."
exit $retval