comparison 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
comparison
equal deleted inserted replaced
303:5d759b614c90 326:b35f4af06ed4
10 # 10 #
11 # 11 #
12 # This will backup both usasdb and uspsdb, 12 # This will backup both usasdb and uspsdb,
13 # along with the top level contents of the project directory 13 # along with the top level contents of the project directory
14 #Environment variables can be used for REMOTE_BACKUP_TARGET and REMOTE_USERNAME 14 #Environment variables can be used for REMOTE_BACKUP_TARGET and REMOTE_USERNAME
15
16 remoteTarget=${1:-$REMOTE_BACKUP_TARGET} 15 remoteTarget=${1:-$REMOTE_BACKUP_TARGET}
17 userName=${2:-$REMOTE_USERNAME} 16 userName=${2:-$REMOTE_USERNAME}
18 projectDir=${3:-$PWD} 17 projectDir=${3:-$PWD}
19 18
20 19
28 container1=usasdb 27 container1=usasdb
29 container2=uspsdb 28 container2=uspsdb
30 29
31 project=$(composeGetProject) 30 project=$(composeGetProject)
32 31
33 echo "project is $project" 32 #If the project is empty, we want to stop the process because this is being run from the wrong directory
34 echo "Containers are $container1 and $container2"
35 echo "Remote target is $remoteTarget"
36 echo "Username is $userName"
37 33
38 if [ "$project" == "" ]; then 34 if [ "$project" == "" ]; then
39 echo "no project available" 35 echo "no project available"
40 exit 1 36 exit 1
41 fi 37 fi
42 38
39 ## first will try to get IRNfrom usasdb; if that is empty it will try to get it from uspsdb
43 40
44 backupFile1=./backup/${project}-${container1}.$(date +%Y-%m-%d-%H-%M-%S).backup 41 IRN=$(docker-compose exec -T $container1 psql --username=postgres --dbname=$container1 -t -c 'select irn from organization')
45 backupFile2=./backup/${project}-${container2}.$(date +%Y-%m-%d-%H-%M-%S).backup 42
46 backupFile3=./backup/${project}-directory.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz 43 if [ "$IRN" == "" ]; then
44 IRN=$(docker-compose exec -T $container2 psql --username=postgres --dbname=$container2 -t -c 'select irn from organization')
45 fi
46
47 ##Trim function in postgres didn't work - so take out the extra space this way.
48 IRN=`echo $IRN | sed -e 's/^[[:space:]]*//'`
49
50 ## if both database are empty, it will put IRN 000000
51 if [ "$IRN" == "" ]; then
52 echo "no IRN set, using 000000"
53 IRN=000000
54 fi
55
56 echo "project is $project"
57 echo "Containers are $container1 and $container2"
58 echo "Remote target is $remoteTarget"
59 echo "Username is $userName"
60 echo "IRN is $IRN"
61
62
63 backupFile1=./backup/${IRN}${project}-${container1}.$(date +%Y-%m-%d-%H-%M-%S).backup
64 backupFile2=./backup/${IRN}${project}-${container2}.$(date +%Y-%m-%d-%H-%M-%S).backup
65 backupFile3=./backup/${IRN}${project}-directory.$(date +%Y-%m-%d-%H-%M-%S).directorycontents.tar.gz
47 66
48 #usasdb 67 #usasdb
49 echo "starting backup of $container1 for $project" 68 echo "starting backup of $container1 for $project"
50 docker-compose exec -T $container1 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container1 ; (exit $?) " > ${backupFile1} 69 docker-compose exec -T $container1 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container1 ; (exit $?) " > ${backupFile1}
51 70
52 if [[ $( grep --count "CREATE TABLE" ${backupFile1} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile1} ) -eq 0 ]]; then 71 if [[ $( grep --count "CREATE TABLE" ${backupFile1} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile1} ) -eq 0 ]]; then
53 echo "ERROR: backup verification FAILED" 72 echo "ERROR: backup verification FAILED"
54 echo "ERROR: $(tail ${backupFile1})" 73 echo "ERROR: $(tail ${backupFile1})"
55 else 74 echo "Error: empty database"
75 #else We still gzip the backup file
76 fi
56 gzip ${backupFile1} 77 gzip ${backupFile1}
57 78
58 echo "completed backup of $container1 for $project to ${backupFile1}" 79 echo "completed backup of $container1 for $project to ${backupFile1}"
59 80
60 fi
61 81
62 82
63 ##### 83 #####
64 #uspsdb is being used 84 #uspsdb
65 85
66 echo "starting backup of $container2 for $project" 86 echo "starting backup of $container2 for $project"
67 docker-compose exec -T $container2 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container2 ; (exit $?) " > ${backupFile2} 87 docker-compose exec -T $container2 sh -c "gosu postgres pg_dump -Cc --if-exists --dbname=$container2 ; (exit $?) " > ${backupFile2}
68 88
69 if [[ $( grep --count "CREATE TABLE" ${backupFile2} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile2} ) -eq 0 ]]; then 89 if [[ $( grep --count "CREATE TABLE" ${backupFile2} ) -lt 200 || $( grep --count "PostgreSQL database dump complete" ${backupFile2} ) -eq 0 ]]; then
70 echo "ERROR: backup verification FAILED" 90 echo "ERROR: backup verification FAILED"
71 echo "ERROR: $(tail ${backupFile2})" 91 echo "ERROR: $(tail ${backupFile2})"
72 else 92 echo "Error: empty database"
93 #else We still gzip the backup file
94 fi
73 gzip ${backupFile2} 95 gzip ${backupFile2}
74 96
75 echo "completed backup of $container2 for $project to ${backupFile2}" 97 echo "completed backup of $container2 for $project to ${backupFile2}"
76 98
77 fi 99
78 100
79 ####### 101 #######
80 102
81 #backup of all files in current directory 103 #backup of all files in current directory
82 echo "starting backup of current directory for $project" 104 echo "starting backup of current directory for $project"
83 105
84 tar -czf ${backupFile3} . --exclude=./backup 106 #tar -czf ${backupFile3} . --exclude=./backup
107 tar --exclude=./backup -czf ${backupFile3} .
85 108
86 echo "completed backup of all files for $project to ${backupFile3}" 109 echo "completed backup of all files for $project to ${backupFile3}"
87 110
111 #Create remote directory before scp
112 host=`echo $userName@$remoteTarget | sed -e 's/:.*//'`
88 113
114 target=`echo $remoteTarget | sed -e 's/.*://'`
115
116
117 ssh $host mkdir -p $target/$project
118
119 #SCP files
89 # 120 #
90 # 121 scp ${backupFile1}.gz ${backupFile2}.gz ${backupFile3} $userName@$remoteTarget/$project
91 scp ${backupFile1}.gz ${backupFile2}.gz ${backupFile3} $userName@$remoteTarget
92 122
93 echo " " 123 echo " "
94 124
95 echo "completed sending ${backupFile1}.gz, ${backupFile2}.gz, and ${backupFile3} to ${remoteTarget} as user $userName" 125 echo "completed sending ${backupFile1}.gz, ${backupFile2}.gz, and ${backupFile3} to ${remoteTarget}/${project} as user $userName"
96 126
97 127