diff scripts/remote-backup-all.sh @ 326:b35f4af06ed4 production vv2.6.0

flow: Merged <release> 'v2.6.0' to <master> ('production').
author aldrich@ssdt-ohio.org
date Tue, 12 Nov 2019 16:16:00 +0000
parents 4285b8a2762e
children
line wrap: on
line diff
--- a/scripts/remote-backup-all.sh	Wed May 01 15:33:00 2019 +0100
+++ b/scripts/remote-backup-all.sh	Tue Nov 12 16:16:00 2019 +0000
@@ -12,7 +12,6 @@
 # This will backup both usasdb and uspsdb,
 # along with the top level contents of the project directory
 #Environment variables can be used for REMOTE_BACKUP_TARGET and REMOTE_USERNAME
-
 remoteTarget=${1:-$REMOTE_BACKUP_TARGET}
 userName=${2:-$REMOTE_USERNAME}
 projectDir=${3:-$PWD}
@@ -30,20 +29,40 @@
 
 project=$(composeGetProject)
 
-echo "project is $project"
-echo "Containers are $container1 and $container2"
-echo "Remote target is $remoteTarget"
-echo "Username is $userName"
+#If the project is empty, we want to stop the process because this is being run from the wrong directory
 
 if [ "$project" == "" ]; then
    echo "no project available"
    exit 1
 fi
 
+## first will try to get IRNfrom usasdb; if that is empty it will try to get it from uspsdb
 
-backupFile1=./backup/${project}-${container1}.$(date +%Y-%m-%d-%H-%M-%S).backup
-backupFile2=./backup/${project}-${container2}.$(date +%Y-%m-%d-%H-%M-%S).backup
-backupFile3=./backup/${project}-directory.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz
+IRN=$(docker-compose exec -T $container1 psql --username=postgres --dbname=$container1 -t -c 'select irn from organization') 
+
+if [ "$IRN" == "" ]; then
+IRN=$(docker-compose exec -T $container2 psql --username=postgres --dbname=$container2 -t -c 'select irn from organization') 
+fi
+
+##Trim function in postgres didn't work - so take out the extra space this way.
+IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
+
+## if both database are empty, it will put IRN 000000
+if [ "$IRN" == "" ]; then
+   echo "no IRN set, using 000000"
+    IRN=000000
+fi
+
+echo "project is $project"
+echo "Containers are $container1 and $container2"
+echo "Remote target is $remoteTarget"
+echo "Username is $userName"
+echo "IRN is $IRN"
+
+
+backupFile1=./backup/${IRN}${project}-${container1}.$(date +%Y-%m-%d-%H-%M-%S).backup
+backupFile2=./backup/${IRN}${project}-${container2}.$(date +%Y-%m-%d-%H-%M-%S).backup
+backupFile3=./backup/${IRN}${project}-directory.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz
 
 #usasdb
 echo "starting backup of $container1 for $project"
@@ -52,16 +71,17 @@
   if [[ $( grep --count "CREATE TABLE" ${backupFile1} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile1} ) -eq 0 ]]; then
      echo "ERROR: backup verification FAILED"
      echo "ERROR:  $(tail ${backupFile1})"
-     else
+	 echo "Error: empty database"
+	 #else  We still gzip the backup file
+  fi
      gzip ${backupFile1}
 
      echo "completed backup of $container1 for $project to ${backupFile1}"
 
-  fi
 
 
 #####
-#uspsdb is being used
+#uspsdb
 
 echo "starting backup of $container2 for $project"
 docker-compose exec -T $container2 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container2 ; (exit $?) " > ${backupFile2}
@@ -69,29 +89,39 @@
   if [[ $( grep --count "CREATE TABLE" ${backupFile2} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile2} ) -eq 0 ]]; then
      echo "ERROR: backup verification FAILED"
      echo "ERROR:  $(tail ${backupFile2})"
-     else
+	 echo "Error: empty database"
+     #else  We still gzip the backup file
+  fi	 
      gzip ${backupFile2}
 
      echo "completed backup of $container2 for $project to ${backupFile2}"
 
-  fi
+  
 
 #######
 
 #backup of all files in current directory
 echo "starting backup of current directory for $project"
 
-tar -czf ${backupFile3} . --exclude=./backup
+#tar -czf ${backupFile3} . --exclude=./backup
+tar --exclude=./backup -czf ${backupFile3} . 
 
 echo "completed backup of all files for $project to ${backupFile3}"
 
+#Create remote directory before scp
+host=`echo $userName@$remoteTarget | sed -e 's/:.*//'`
 
+target=`echo $remoteTarget | sed -e 's/.*://'`
+
+
+ssh $host mkdir -p $target/$project
+
+#SCP files
 #
-#
-scp ${backupFile1}.gz  ${backupFile2}.gz ${backupFile3} $userName@$remoteTarget
+scp ${backupFile1}.gz  ${backupFile2}.gz ${backupFile3} $userName@$remoteTarget/$project
 
 echo " "
 
-echo "completed sending ${backupFile1}.gz, ${backupFile2}.gz, and ${backupFile3} to ${remoteTarget} as user $userName"
+echo "completed sending ${backupFile1}.gz, ${backupFile2}.gz, and ${backupFile3} to ${remoteTarget}/${project} as user $userName"