annotate scripts/restore-container.sh @ 266:b72855a930f1

simplify method of getting project name
author smith@nwoca.org
date Mon, 12 Nov 2018 21:12:31 +0000
parents 2e0d3c70bba2
children 5be78ce3b33c
rev   line source
225
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
1 #!/bin/bash
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
2
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
3 source "${SSDT_SCRIPTS:-$(dirname "${BASH_SOURCE[0]}")}/.functions.sh"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
4
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
5 sourceFile=${1?must provide backup file to be restored}
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
6
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
7 if [ -z "$1" ]
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
8 then
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
9 echo "Usage: `basename $0` {backup file to restore}"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
10 echo " must specify the name of the file to restore. Assumed to be in format:"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
11 echo " {service}.{timestamp}.backup.gz "
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
12 echo " where {service} is the usasdb or uspsdb container to be restored."
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
13 echo " The script will restore the backup to the appropriate container based "
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
14 echo " on the filename."
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
15 exit $E_NOARGS
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
16 fi
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
17
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
18 target=$(basename $1)
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
19 target=${target%%.*}
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
20
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
21 project=$(composeGetProject $target)
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
22
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
23 if [ "$project" == "" ]; then
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
24 echo "no project available"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
25 exit 1
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
26 fi
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
27
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
28 echo
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
29 echo "Preparing to restore"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
30 echo "--------------------"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
31 echo " file: $sourceFile"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
32 echo " to ${project}/${target} database: $target"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
33 echo " "
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
34 echo "WARNING: This operation will DELETE and replace any exising database"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
35 echo " "
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
36
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
37 read -e -p "Continue? <y/N> " answer
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
38 case $answer in
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
39 y | Y | yes | YES ) answer="y";;
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
40 n | N | no | NO ) answer="n";;
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
41 *) answer="n"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
42 esac
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
43
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
44 if [ "$answer" == "y" ]
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
45 then
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
46 appService=${target/db/app}
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
47 echo "stopping application service $appService"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
48 docker-compose stop $appService
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
49
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
50 dbContainer=$(composeGetContainer $target)
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
51
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
52 echo "copy backup file to database container"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
53 docker cp ${sourceFile} ${dbContainer}:/tmp/restore.backup.gz
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
54
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
55 echo "starting database restore"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
56 docker-compose exec -T -u postgres $target sh -c "gunzip -f /tmp/restore.backup.gz -c | psql"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
57
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
58 fi
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
59