5 # Bootstrap the database
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
11 # Source function library and configuration
12 . /etc/plc.d/functions
13 . /etc/planetlab/plc_config
18 # Export so that we do not have to specify -p to psql invocations
19 export PGPORT=$PLC_DB_PORT
25 for file in /usr/share/plc_api/extensions/*-up*; do
28 extension=${script##*.}
29 version=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \
30 "SELECT version FROM plc_db_extensions WHERE name='$name' LIMIT 1" \
31 $PLC_DB_NAME 2>/dev/null | awk 'BEGIN { ver=0 } /^[0-9]+$/ { ver=$1 } END { print ver }')
32 if [ $version -eq 0 ]; then
33 if [ "$extension" = "sql" ] ; then
34 dialog " - $script (dbdumped)"
35 dump_planetlab_db "before-$script"
36 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
37 elif [ -x $file ] ; then
38 dialog " - $script (dbdumped)"
39 dump_planetlab_db "before-$script"
42 dialog "\nWarning: extension $file not executable"
46 for file in /usr/share/plc_api/extensions/$name/migrations/[0-9]*-up-*; do
49 extension=${script##*.}
50 if [ $index -gt $version ] ; then
51 if [ "$extension" = "sql" ] ; then
52 dialog " - $script (dbdumped)"
53 dump_planetlab_db "before-$script"
54 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
55 elif [ -x $file ] ; then
56 dialog " - $script (dbdumped)"
57 dump_planetlab_db "before-$script"
60 dialog "\nWarning: migration $file not executable"
68 # Updates the database by applying all migration scripts in
69 # /usr/share/plc_api/migrations/N-up-*, where N is greater than the
70 # current subversion. At least one of the migration scripts with the
71 # same N must update plc_db_version.subversion.
74 subversion=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \
75 "SELECT subversion FROM plc_db_version LIMIT 1" \
76 $PLC_DB_NAME 2>/dev/null || echo 0)
78 for file in /usr/share/plc_api/migrations/[0-9]*-up-* ; do
79 script=$(basename $file)
81 extension=${script##*.}
82 if [ $index -gt $subversion ] ; then
83 if [ "$extension" = "sql" ] ; then
84 dialog " - $script (dbdumped)"
85 dump_planetlab_db "before-$script"
86 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
87 elif [ -x $file ] ; then
88 dialog " - $script (dbdumped)"
89 dump_planetlab_db "before-$script"
92 dialog "\nWarning: migration $file not executable"
99 function checkpoint_planetlab_db()
102 pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dumpfile
106 function restore_planetlab_db()
109 if [ -n "$dumpfile" ] ; then
110 [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER $PLC_DB_NAME < $dumpfile
115 # use a single date of this script invocation for the dump_*_db functions.
116 DATE=$(date +"%Y-%m-%d-%H-%M-%S")
118 # Dumps the database - optional argument to specify filename suffix
119 function dump_planetlab_db()
121 if [ -n "$1" ] ; then suffix="-$1" ; else suffix="" ; fi
122 dumpfile=/var/lib/pgsql/backups/$(date +"${PLC_DB_NAME}.${DATE}${suffix}.sql")
123 checkpoint_planetlab_db $dumpfile
126 function restore_drupal_db()
129 if [ -n "$dumpfile" ] ; then
130 [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER drupal < $1
135 function checkpoint_drupal_db()
138 pg_dump -U $PLC_DB_USER drupal > $dumpfile
142 function dump_drupal_db()
144 dumpfile=/var/lib/pgsql/backups/$(date +"drupal.${DATE}.sql")
145 checkpoint_drupal_db $dumpfile
149 # Clean up old backups
150 function clean_dumps()
152 find /var/lib/pgsql/backups '(' -name "$PLC_DB_NAME.*.sql" -o -name "drupal.*.sql" ')' -a -atime +15 | xargs rm -f
156 [ $PLC_DB_ENABLED -ne 1 ] && exit 0
159 MESSAGE=$"Bootstrapping the database"
162 # Apply schema updates
166 # Update the maintenance account username. This can't be
167 # done through the api-config script since it uses the
168 # maintenance account to access the API. The maintenance
169 # account should be person_id 1 since it is created by the
171 psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
173 # Update the Drupal site_name variable
174 # also turn off drupal native user registration
175 psql -U $PLC_DB_USER drupal <<EOF
176 DELETE FROM variable WHERE name = 'site_name';
177 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
178 DELETE FROM variable WHERE name = 'user_register';
179 INSERT INTO variable (name, value) VALUES ('user_register', 's:1:"0";');
191 MESSAGE=$"Migrating the database"
199 MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups"
208 MESSAGE=$"Checkpointing the databases"
209 checkpoint_planetlab_db $2
210 checkpoint_drupal_db $3
214 MESSAGE=$"Restoring the databases from checkpoint files"
215 restore_planetlab_db $2
220 MESSAGE=$"Cleaning old database dumps"
228 MESSAGE="Ignoring request to stop myplc databases"
234 echo "Usage: $0 [start|migrate|dump|checkpoint|restore|clean-dump|stop]"