cleaning up messaging related to 'service plc stop'
[myplc.git] / plc.d / db
index 5bf4784..80c4009 100755 (executable)
--- a/plc.d/db
+++ b/plc.d/db
@@ -7,7 +7,7 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id: db,v 1.7 2007/01/31 19:53:20 mlhuang Exp $
+# $Id$
 #
 
 # Source function library and configuration
@@ -36,23 +36,68 @@ function migrate_db()
        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
-function dump_db()
+function checkpoint_planetlab_db()
 {
-    dump=/var/lib/pgsql/backups/$(date +"$PLC_DB_NAME.%Y-%m-%d-%H-%M.sql")
-    pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dump
+    dumpfile=$1
+    pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dumpfile
     check
-    dump=/var/lib/pgsql/backups/$(date +"drupal.%Y-%m-%d-%H-%M.sql")
-    pg_dump -U $PLC_DB_USER drupal > $dump
+}
+
+function restore_planetlab_db()
+{
+    dumpfile=$1
+    if [ -n "$dumpfile" ] ; then 
+       [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER $PLC_DB_NAME < $dumpfile
+       check
+    fi
+}
+
+# use a single date of this script invocation for the dump_*_db functions.
+DATE=$(date +"%Y-%m-%d-%H-%M-%S")
+
+# Dumps the database - optional argument to specify filename suffix
+function dump_planetlab_db()
+{
+    if [ -n "$1" ] ; then suffix="-$1" ; else suffix="" ; fi
+    dumpfile=/var/lib/pgsql/backups/$(date +"${PLC_DB_NAME}.${DATE}${suffix}.sql")
+    checkpoint_planetlab_db $dumpfile
+}
+
+function restore_drupal_db()
+{
+    dumpfile=$1
+    if [ -n "$dumpfile" ] ; then 
+       [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER drupal < $1
+       check
+    fi
+}
+
+function checkpoint_drupal_db()
+{
+    dumpfile=$1
+    pg_dump -U $PLC_DB_USER drupal > $dumpfile
+    check
+}
+
+function dump_drupal_db()
+{
+    dumpfile=/var/lib/pgsql/backups/$(date +"drupal.${DATE}.sql")
+    checkpoint_drupal_db $dumpfile
     check
 }
 
@@ -63,10 +108,7 @@ function clean_dumps()
     check
 }
 
-if [ "$PLC_DB_ENABLED" != "1" ] ; then
-    exit 0
-fi
-
+[ $PLC_DB_ENABLED -ne 1 ] && exit 0
 case "$1" in
     start)
        MESSAGE=$"Bootstrapping the database"
@@ -107,10 +149,23 @@ EOF
        MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups"
        dialog "$MESSAGE"
 
-       dump_db
+       dump_planetlab_db
+       dump_drupal_db
        result "$MESSAGE"
        ;;
 
+    checkpoint)
+       MESSAGE=$"Checkpointing the databases"
+       checkpoint_planetlab_db $2
+       checkpoint_drupal_db $3
+       ;;
+
+    restore)
+       MESSAGE=$"Restoring the databases from checkpoint files"
+       restore_planetlab_db $2
+       restore_drupal_db $3
+       ;;
+
     clean-dump)
        MESSAGE=$"Cleaning old database dumps"
        dialog "$MESSAGE"
@@ -119,8 +174,14 @@ EOF
        result "$MESSAGE"
        ;;
 
+    stop)
+       MESSAGE="Ignoring request to stop myplc databases"
+       dialog "$MESSAGE"
+       result ""
+       ;;
+
     *)
-        echo "Usage: $0 [start|migrate|dump|clean-dump]"
+        echo "Usage: $0 [start|migrate|dump|checkpoint|restore|clean-dump|stop]"
        exit 1
        ;;
 esac