root/wifidogadmin/sql/restore_database.sh

Revision 479, 2.1 kB (checked in by insultant, 8 months ago)

--

Line 
1 #!/bin/bash
2 DATABASE_NAME="wifidog";
3 USERNAME="wifidog";
4 SUPERUSER="postgres";
5 FILENAME=$1;
6
7 if [ -z $FILENAME ] ; then
8 echo "You must specify a filename as the first argument"
9 exit 1
10 fi
11
12 echo "Do I need to delete the current $DATABASE_NAME database before restoring from $FILENAME? (y/n)"
13 read delete_confirm
14 if [ $delete_confirm = "y" -o $delete_confirm = "Y" ] ; then
15 cmd="dropdb -U $SUPERUSER $DATABASE_NAME"
16 echo $cmd
17 $cmd
18 retval=$?
19 if [[ $retval -ne 0 ]] ; then
20     echo "Unable to delete the database"
21     exit 1
22 fi
23 else
24 echo "Not trying to delete database $DATABASE_NAME"
25 fi
26
27 echo "Do I need to create the user $USERNAME before restoring from $FILENAME? (y/n)"
28 read create_user_confirm
29 if [ $create_user_confirm = "y" -o $create_user_confirm = "Y" ] ; then
30 cmd="createuser -U $SUPERUSER --no-superuser --no-createdb --no-createrole --pwprompt $USERNAME"
31 echo $cmd
32 $cmd
33 retval=$?
34 if [[ $retval -ne 0 ]] ; then
35     echo "Unable to create user $USERNAME "
36     exit 1
37 fi
38 else
39 echo "Not trying to create user $USERNAME"
40 fi
41
42 echo "Creating database"
43
44 cmd="createdb -U $SUPERUSER $DATABASE_NAME --encoding=UTF-8 --owner=$USERNAME"
45 echo $cmd
46 $cmd
47 retval=$?
48 if [[ $retval -ne 0 ]] ; then
49     echo "Unable to create the database, you probably need to delete the existing database"
50     exit 1
51 fi
52
53
54 echo "Do I need to add the plpgsql language to database (as the admin user)? (y/n)"
55 read add_plpgsql_confirm
56 if [ $add_plpgsql_confirm = "y" -o $add_plpgsql_confirm = "Y" ] ; then
57 cmd="createlang -U $SUPERUSER plpgsql $DATABASE_NAME"
58 echo $cmd
59 $cmd
60 retval=$?
61 if [[ $retval -ne 0 ]] ; then
62     echo "Unable to create the plpgsql language, you may need to install the module"
63     exit 1
64 fi
65 else
66 echo "Not trying to add plpgsql"
67 fi
68
69 echo "Restoring database"
70 cmd="pg_restore -U $SUPERUSER -d $DATABASE_NAME -v $FILENAME"
71 echo $cmd
72 $cmd
73 retval=$?
74 #if [[ $retval -ne 0 ]] ; then
75 #    echo "Unable to restore the database completely"
76 #    exit 1
77 #fi
78
79 echo "Vacuuming database"
80 sql="VACUUM ANALYZE;"
81 cmd="psql -U $SUPERUSER -d $DATABASE_NAME --command \"$sql\""
82 echo $cmd
83 eval $cmd
84 retval=$?
85 if [[ $retval -ne 0 ]] ; then
86     echo "Unable to restore the database completely"
87     exit 1
88 fi
89 exit $?
Note: See TracBrowser for help on using the browser.