X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;ds=sidebyside;f=plc.d%2Fdb;h=8f5fc14cda557af5298f80bbd555216220cdf1ac;hb=391310e122de0536c08f62bd46acd3b3b7b13964;hp=b249ca95712923fd419cc504a308fdf80a9fd8d0;hpb=40608f0c316b6d0a3e5c2dbdfcdb2316e5e8c0eb;p=myplc.git diff --git a/plc.d/db b/plc.d/db index b249ca9..8f5fc14 100755 --- a/plc.d/db +++ b/plc.d/db @@ -7,7 +7,7 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id$ +# $Id: db 316 2007-04-24 17:25:09Z thierry $ # # Source function library and configuration @@ -20,15 +20,72 @@ set -x # Export so that we do not have to specify -p to psql invocations export PGPORT=$PLC_DB_PORT -case "$1" in - start) - if [ "$PLC_DB_ENABLED" != "1" ] ; then - exit 0 +# Updates the database by applying all migration scripts in +# /usr/share/plc_api/migrations/N-up-*, where N is greater than the +# current subversion. At least one of the migration scripts with the +# same N must update plc_db_version.subversion. +function migrate_db() +{ + subversion=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \ + "SELECT subversion FROM plc_db_version LIMIT 1" \ + $PLC_DB_NAME 2>/dev/null || echo 0) + shopt -s nullglob + for file in /usr/share/plc_api/migrations/[0-9]*-up-* ; do + script=$(basename $file) + index=${script%-up*} + extension=${script##*.} + if [ $index -gt $subversion ] ; then + if [ "$extension" = "sql" ] ; then + dialog " - $script (dbdumped)" + dump_planetlab_db "before-$script" + psql -U $PLC_DB_USER -f $file $PLC_DB_NAME + elif [ -x $file ] ; then + dialog " - $script (dbdumped)" + dump_planetlab_db "before-$script" + $file + else + dialog "\nWarning: migration $file not executable" + fi + check fi + done +} + +# Dumps the database - optional argument to specify filename suffix +function dump_planetlab_db() +{ + if [ -n "$1" ] ; then suffix="-$1" ; else suffix="" ; fi + dump=/var/lib/pgsql/backups/$(date +"$PLC_DB_NAME.%Y-%m-%d-%H-%M-%S${suffix}.sql") + pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dump + check +} + +function dump_drupal_db() +{ + dump=/var/lib/pgsql/backups/$(date +"drupal.%Y-%m-%d-%H-%M-%S.sql") + pg_dump -U $PLC_DB_USER drupal > $dump + check +} +# Clean up old backups +function clean_dumps() +{ + find /var/lib/pgsql/backups '(' -name "$PLC_DB_NAME.*.sql" -o -name "drupal.*.sql" ')' -a -atime +15 | xargs rm -f + check +} + +if [ "$PLC_DB_ENABLED" != "1" ] ; then + exit 0 +fi + +case "$1" in + start) MESSAGE=$"Bootstrapping the database" dialog "$MESSAGE" + # Apply schema updates + migrate_db + # Update the maintenance account username. This can't be # done through the api-config script since it uses the # maintenance account to access the API. The maintenance @@ -48,6 +105,36 @@ EOF result "$MESSAGE" ;; + + migrate) + MESSAGE=$"Migrating the database" + dialog "$MESSAGE" + + migrate_db + result "$MESSAGE" + ;; + + dump) + MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups" + dialog "$MESSAGE" + + dump_planetlab_db + dump_drupal_db + result "$MESSAGE" + ;; + + clean-dump) + MESSAGE=$"Cleaning old database dumps" + dialog "$MESSAGE" + + clean_dumps + result "$MESSAGE" + ;; + + *) + echo "Usage: $0 [start|migrate|dump|clean-dump]" + exit 1 + ;; esac exit $ERRORS