8c0a2904b6ec9a7817c477327bf0344a33f3272c
[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     else
68         # XXX set error
69     fi
70 }
71
72 # use a single date of this script invocation for the dump_*_db functions.
73 DATE=$(date +"%Y-%m-%d-%H-%M-%S")
74
75 # Dumps the database - optional argument to specify filename suffix
76 function dump_planetlab_db()
77 {
78     if [ -n "$1" ] ; then suffix="-$1" ; else suffix="" ; fi
79     dumpfile=/var/lib/pgsql/backups/$(date +"${PLC_DB_NAME}.${DATE}${suffix}.sql")
80     checkpoint_planetlab_db $dumpfile
81 }
82
83 function restore_drupal_db()
84 {
85     dumpfile=$1
86     if [ -n "$dumpfile" ] ; then 
87         [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER drupal < $1
88         check
89     fi
90 }
91
92 function checkpoint_drupal_db()
93 {
94     dumpfile=$1
95     pg_dump -U $PLC_DB_USER drupal > $dumpfile
96     check
97 }
98
99 function dump_drupal_db()
100 {
101     dumpfile=/var/lib/pgsql/backups/$(date +"drupal.${DATE}.sql")
102     checkpoint_drupal_db $dumpfile
103     check
104 }
105
106 # Clean up old backups
107 function clean_dumps()
108 {
109     find /var/lib/pgsql/backups '(' -name "$PLC_DB_NAME.*.sql" -o -name "drupal.*.sql" ')' -a -atime +15 | xargs rm -f
110     check
111 }
112
113 if [ "$PLC_DB_ENABLED" != "1" ] ; then
114     exit 0
115 fi
116
117 case "$1" in
118     start)
119         MESSAGE=$"Bootstrapping the database"
120         dialog "$MESSAGE"
121
122         # Apply schema updates
123         migrate_db
124
125         # Update the maintenance account username. This can't be
126         # done through the api-config script since it uses the
127         # maintenance account to access the API. The maintenance
128         # account should be person_id 1 since it is created by the
129         # DB schema itself.
130         psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
131
132         # Update the Drupal site_name variable
133         psql -U $PLC_DB_USER drupal <<EOF
134 DELETE FROM variable WHERE name = 'site_name';
135 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
136 EOF
137
138         # Bootstrap the DB
139         db-config
140         check
141
142         result "$MESSAGE"
143         ;;
144
145     migrate)
146         MESSAGE=$"Migrating the database"
147         dialog "$MESSAGE"
148
149         migrate_db
150         result "$MESSAGE"
151         ;;
152
153     dump)
154         MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups"
155         dialog "$MESSAGE"
156
157         dump_planetlab_db
158         dump_drupal_db
159         result "$MESSAGE"
160         ;;
161
162     checkpoint)
163         MESSAGE=$"Checkpointing the databases"
164         checkpoint_planetlab_db $2
165         checkpoint_drupal_db $3
166         ;;
167
168     restore)
169         MESSAGE=$"Restoring the databases from checkpoint files"
170         restore_planetlab_db $2
171         restore_drupal_db $3
172         ;;
173
174     clean-dump)
175         MESSAGE=$"Cleaning old database dumps"
176         dialog "$MESSAGE"
177
178         clean_dumps
179         result "$MESSAGE"
180         ;;
181
182     *)
183         echo "Usage: $0 [start|migrate|dump|clean-dump]"
184         exit 1
185         ;;
186 esac
187
188 exit $ERRORS