annotate scripts/restore-container.sh @ 605:46a67e7afd98 release/2.23.0 tip

flow: Created branch 'release/2.23.0'.
author Marc Davis <marc.davis@mcoecn.org>
date Wed, 18 Sep 2024 20:38:06 -0400
parents 5be78ce3b33c
children
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%%.*}
297
5be78ce3b33c revisions to restore container script
aldrich@nwoca.org
parents: 225
diff changeset
20 target=${target#*-}
225
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
21
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
22 project=$(composeGetProject $target)
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
23
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
24 if [ "$project" == "" ]; then
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
25 echo "no project available"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
26 exit 1
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
27 fi
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
28
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
29 echo
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
30 echo "Preparing to restore"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
31 echo "--------------------"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
32 echo " file: $sourceFile"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
33 echo " to ${project}/${target} database: $target"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
34 echo " "
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
35 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
36 echo " "
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
37
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
38 read -e -p "Continue? <y/N> " answer
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
39 case $answer in
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
40 y | Y | yes | YES ) answer="y";;
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
41 n | N | no | NO ) answer="n";;
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
42 *) answer="n"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
43 esac
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
44
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
45 if [ "$answer" == "y" ]
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
46 then
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
47 appService=${target/db/app}
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
48 echo "stopping application service $appService"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
49 docker-compose stop $appService
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
50
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
51 dbContainer=$(composeGetContainer $target)
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
52
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
53 echo "copy backup file to database container"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
54 docker cp ${sourceFile} ${dbContainer}:/tmp/restore.backup.gz
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
55
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
56 echo "starting database restore"
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
57 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
58
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
59 fi
2e0d3c70bba2 replace restore scripts with updated container restore script
smith@nwoca.org
parents:
diff changeset
60