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
22 function extend_db() {
24 for file in /usr/share/plc_api/extensions/*-up*; do
27 extension=${script##*.}
28 version=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \
29 "SELECT version FROM plc_db_extensions WHERE name='$name' LIMIT 1" \
30 $PLC_DB_NAME 2>/dev/null | awk 'BEGIN { ver=0 } /^[0-9]+$/ { ver=$1 } END { print ver }')
31 if [ $version -eq 0 ]; then
32 if [ "$extension" = "sql" ]; then
33 dialog " - $script (dbdumped)"
34 dump_planetlab_db "before-$script"
35 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
36 elif [ -x $file ]; then
37 dialog " - $script (dbdumped)"
38 dump_planetlab_db "before-$script"
41 dialog "\nWarning: extension $file not executable"
45 for file in /usr/share/plc_api/extensions/$name/migrations/[0-9]*-up-*; do
48 extension=${script##*.}
49 if [ $index -gt $version ]; then
50 if [ "$extension" = "sql" ]; then
51 dialog " - $script (dbdumped)"
52 dump_planetlab_db "before-$script"
53 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
54 elif [ -x $file ]; then
55 dialog " - $script (dbdumped)"
56 dump_planetlab_db "before-$script"
59 dialog "\nWarning: migration $file not executable"
67 # Updates the database by applying all migration scripts in
68 # /usr/share/plc_api/migrations/N-up-*, where N is greater than the
69 # current subversion. At least one of the migration scripts with the
70 # same N must update plc_db_version.subversion.
71 function migrate_db() {
72 subversion=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \
73 "SELECT subversion FROM plc_db_version LIMIT 1" \
74 $PLC_DB_NAME 2>/dev/null || echo 0)
76 for file in /usr/share/plc_api/migrations/[0-9]*-up-*; do
77 script=$(basename $file)
79 extension=${script##*.}
80 if [ $index -gt $subversion ]; then
81 if [ "$extension" = "sql" ]; then
82 dialog " - $script (dbdumped)"
83 dump_planetlab_db "before-$script"
84 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
85 elif [ -x $file ]; then
86 dialog " - $script (dbdumped)"
87 dump_planetlab_db "before-$script"
90 dialog "\nWarning: migration $file not executable"
97 function checkpoint_planetlab_db() {
99 pg_dump -U $PLC_DB_USER $PLC_DB_NAME >$dumpfile
103 function restore_planetlab_db() {
105 if [ -n "$dumpfile" ]; then
106 [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER $PLC_DB_NAME <$dumpfile
111 # use a single date of this script invocation for the dump_*_db functions.
112 DATE=$(date +"%Y-%m-%d-%H-%M-%S")
114 # Dumps the database - optional argument to specify filename suffix
115 function dump_planetlab_db() {
116 if [ -n "$1" ]; then suffix="-$1"; else suffix=""; fi
117 dumpfile=/var/lib/pgsql/backups/$(date +"${PLC_DB_NAME}.${DATE}${suffix}.sql")
118 checkpoint_planetlab_db $dumpfile
121 function restore_drupal_db() {
123 if [ -n "$dumpfile" ]; then
124 [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER drupal <$1
129 function checkpoint_drupal_db() {
131 pg_dump -U $PLC_DB_USER drupal >$dumpfile
135 function dump_drupal_db() {
136 dumpfile=/var/lib/pgsql/backups/$(date +"drupal.${DATE}.sql")
137 checkpoint_drupal_db $dumpfile
141 # Clean up old backups
142 function clean_dumps() {
143 find /var/lib/pgsql/backups '(' -name "$PLC_DB_NAME.*.sql" -o -name "drupal.*.sql" ')' -a -atime +15 | xargs rm -f
147 [ $PLC_DB_ENABLED -ne 1 ] && exit 0
150 MESSAGE=$"Bootstrapping the database"
153 # Apply schema updates
157 # Update the maintenance account username. This can't be
158 # done through the api-config script since it uses the
159 # maintenance account to access the API. The maintenance
160 # account should be person_id 1 since it is created by the
162 psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
164 # Update the Drupal site_name variable
165 # also turn off drupal native user registration
166 psql -U $PLC_DB_USER drupal <<EOF
167 DELETE FROM variable WHERE name = 'site_name';
168 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
169 DELETE FROM variable WHERE name = 'user_register';
170 INSERT INTO variable (name, value) VALUES ('user_register', 's:1:"0";');
182 MESSAGE=$"Migrating the database"
190 MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups"
199 MESSAGE=$"Checkpointing the databases"
200 checkpoint_planetlab_db $2
201 checkpoint_drupal_db $3
205 MESSAGE=$"Restoring the databases from checkpoint files"
206 restore_planetlab_db $2
211 MESSAGE=$"Cleaning old database dumps"
219 MESSAGE="Ignoring request to stop myplc databases"
225 echo "Usage: $0 [start|migrate|dump|checkpoint|restore|clean-dump|stop]"