5 # Bootstrap the database
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
20 # Export so that we do not have to specify -p to psql invocations
21 export PGPORT=$PLC_DB_PORT
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.
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)
33 for file in /usr/share/plc_api/migrations/[0-9]*-up-* ; do
34 script=$(basename $file)
36 extension=${script##*.}
37 if [ $index -gt $subversion ] ; then
38 if [ "$extension" = "sql" ] ; then
39 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
40 elif [ -x $file ] ; then
51 dump=/var/lib/pgsql/backups/$(date +"$PLC_DB_NAME.%Y-%m-%d-%H-%M.sql")
52 pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dump
56 # Clean up old backups
57 function clean_dumps()
59 find /var/lib/pgsql/backups -name "$PLC_DB_NAME.*.sql" -atime +15 | xargs rm -f
63 if [ "$PLC_DB_ENABLED" != "1" ] ; then
69 MESSAGE=$"Bootstrapping the database"
72 # Apply schema updates
75 # Update the maintenance account username. This can't be
76 # done through the api-config script since it uses the
77 # maintenance account to access the API. The maintenance
78 # account should be person_id 1 since it is created by the
80 psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
82 # Update the Drupal site_name variable
83 psql -U $PLC_DB_USER drupal <<EOF
84 DELETE FROM variable WHERE name = 'site_name';
85 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
96 MESSAGE=$"Migrating the database"
104 MESSAGE=$"Dumping the database"
112 MESSAGE=$"Cleaning old database dumps"
120 echo "Usage: $0 [start|migrate|dump|clean-dump]"