a more robust, and tested, way to restore the DBs
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Thu, 21 Nov 2024 10:04:35 +0000 (11:04 +0100)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Sun, 24 Nov 2024 15:03:53 +0000 (16:03 +0100)
plc.d/db

index 49f177a..66c173e 100755 (executable)
--- a/plc.d/db
+++ b/plc.d/db
@@ -102,14 +102,6 @@ function checkpoint_planetlab_db() {
        check
 }
 
-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")
 
@@ -126,14 +118,6 @@ function dump_planetlab_db() {
        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() {
        local dumpfile="$1"; shift
        pg_dump -U $PLC_DB_USER drupal >$dumpfile
@@ -146,6 +130,22 @@ function dump_drupal_db() {
        check
 }
 
+function restore_planetlab_db() {
+       local dumpfile="$1"; shift
+       [[ -z "$dumpfile" ]] && { echo "Usage: $0 restore (planetlab5) <dumpfile>"; return 1; }
+       psql -U postgres -c "DROP DATABASE $PLC_DB_NAME"
+       createdb -U postgres --template=template0 --encoding=UNICODE --owner=$PLC_DB_USER $PLC_DB_NAME
+       psql -a -U $PLC_DB_USER $PLC_DB_NAME -f $dumpfile
+}
+
+function restore_drupal_db() {
+       local dumpfile="$1"; shift
+       [[ -z "$dumpfile" ]] && { echo "Usage: $0 restore (drupal) <dumpfile>"; return 1; }
+       psql -U postgres -c "DROP DATABASE drupal"
+       createdb -U postgres --template=template0 --encoding=UNICODE --owner=$PLC_DB_USER drupal
+       psql -a -U $PLC_DB_USER drupal -f $dumpfile
+}
+
 # Clean up old backups
 function clean_dump() {
        local days="$1"; shift