Simplify network file setup.
[myplc.git] / plc.d / db
1 #!/bin/bash
2 #
3 # priority: 800
4 #
5 # Bootstrap the database
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # $Id$
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16
17 # Be verbose
18 set -x
19
20 # Export so that we do not have to specify -p to psql invocations
21 export PGPORT=$PLC_DB_PORT
22
23 # Updates the database by applying all migration scripts in
24 # /usr/share/plc_api/migrations/N-up-*, where N is greater than the
25 # current subversion. At least one of the migration scripts with the
26 # same N must update plc_db_version.subversion.
27 function migrate_db()
28 {
29     subversion=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \
30                  "SELECT subversion FROM plc_db_version LIMIT 1" \
31                  $PLC_DB_NAME 2>/dev/null || echo 0)
32     shopt -s nullglob
33     for file in /usr/share/plc_api/migrations/[0-9]*-up-* ; do
34         script=$(basename $file)
35         index=${script%-up*}
36         extension=${script##*.}
37         if [ $index -gt $subversion ] ; then
38             if [ "$extension" = "sql" ] ; then
39                 dialog " - $script (dbdumped)"
40                 dump_planetlab_db "before-$script"
41                 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
42             elif [ -x $file ] ; then
43                 dialog " - $script (dbdumped)"
44                 dump_planetlab_db "before-$script"
45                 $file
46             else
47                 dialog "\nWarning: migration $file not executable"
48             fi
49             check
50         fi
51     done
52 }
53
54 function checkpoint_planetlab_db()
55 {
56     dumpfile=$1
57     pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dumpfile
58     check
59 }
60
61 function restore_planetlab_db()
62 {
63     dumpfile=$1
64     if [ -n "$dumpfile" ] ; then 
65         [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER $PLC_DB_NAME < $dumpfile
66         check
67     fi
68 }
69
70 # use a single date of this script invocation for the dump_*_db functions.
71 DATE=$(date +"%Y-%m-%d-%H-%M-%S")
72
73 # Dumps the database - optional argument to specify filename suffix
74 function dump_planetlab_db()
75 {
76     if [ -n "$1" ] ; then suffix="-$1" ; else suffix="" ; fi
77     dumpfile=/var/lib/pgsql/backups/$(date +"${PLC_DB_NAME}.${DATE}${suffix}.sql")
78     checkpoint_planetlab_db $dumpfile
79 }
80
81 function restore_drupal_db()
82 {
83     dumpfile=$1
84     if [ -n "$dumpfile" ] ; then 
85         [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER drupal < $1
86         check
87     fi
88 }
89
90 function checkpoint_drupal_db()
91 {
92     dumpfile=$1
93     pg_dump -U $PLC_DB_USER drupal > $dumpfile
94     check
95 }
96
97 function dump_drupal_db()
98 {
99     dumpfile=/var/lib/pgsql/backups/$(date +"drupal.${DATE}.sql")
100     checkpoint_drupal_db $dumpfile
101     check
102 }
103
104 # Clean up old backups
105 function clean_dumps()
106 {
107     find /var/lib/pgsql/backups '(' -name "$PLC_DB_NAME.*.sql" -o -name "drupal.*.sql" ')' -a -atime +15 | xargs rm -f
108     check
109 }
110
111 if [ "$PLC_DB_ENABLED" != "1" ] ; then
112     exit 0
113 fi
114
115 case "$1" in
116     start)
117         MESSAGE=$"Bootstrapping the database"
118         dialog "$MESSAGE"
119
120         # Apply schema updates
121         migrate_db
122
123         # Update the maintenance account username. This can't be
124         # done through the api-config script since it uses the
125         # maintenance account to access the API. The maintenance
126         # account should be person_id 1 since it is created by the
127         # DB schema itself.
128         psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
129
130         # Update the Drupal site_name variable
131         psql -U $PLC_DB_USER drupal <<EOF
132 DELETE FROM variable WHERE name = 'site_name';
133 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
134 EOF
135
136         # Bootstrap the DB
137         db-config
138         check
139
140         result "$MESSAGE"
141         ;;
142
143     migrate)
144         MESSAGE=$"Migrating the database"
145         dialog "$MESSAGE"
146
147         migrate_db
148         result "$MESSAGE"
149         ;;
150
151     dump)
152         MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups"
153         dialog "$MESSAGE"
154
155         dump_planetlab_db
156         dump_drupal_db
157         result "$MESSAGE"
158         ;;
159
160     checkpoint)
161         MESSAGE=$"Checkpointing the databases"
162         checkpoint_planetlab_db $2
163         checkpoint_drupal_db $3
164         ;;
165
166     restore)
167         MESSAGE=$"Restoring the databases from checkpoint files"
168         restore_planetlab_db $2
169         restore_drupal_db $3
170         ;;
171
172     clean-dump)
173         MESSAGE=$"Cleaning old database dumps"
174         dialog "$MESSAGE"
175
176         clean_dumps
177         result "$MESSAGE"
178         ;;
179
180     *)
181         echo "Usage: $0 [start|migrate|dump|clean-dump]"
182         exit 1
183         ;;
184 esac
185
186 exit $ERRORS