-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.sh
executable file
·147 lines (122 loc) · 4.53 KB
/
backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
# SPDX-FileCopyrightText: 2022 Wilfred Nicoll <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
# backup.sh
# This script backs up the persistent storage for a project and its modules
# Required modules (space-separated list in the form "module_1 module_2 module_3")
MODULES="ryo-ingress-proxy ryo-mariadb ryo-wellknown"
# Help and error messages
#########################
helpMessage()
{
echo "backup.sh: Back up the persistent storage for a project and its modules"
echo "Usage: ./backup.sh -n hostname -v version"
echo "Flags:"
echo -e "-n hostname \t\t(Mandatory) Name of the host from which to back up project and module container persistent storage"
echo -e "-s stamp \t\t(Mandatory) A stamp (e.g. date, time, name) to identify the backup"
echo -e "-h \t\t\tPrint this help message"
echo ""
exit 1
}
errorMessage()
{
echo "Invalid option or input variables are missing"
echo "Use \"./backup.sh -h\" for help"
exit 1
}
# Command-line input handling
#############################
while getopts n:s:h flag
do
case "${flag}" in
n) hostname=${OPTARG};;
s) stamp=${OPTARG};;
h) helpMessage ;;
?) errorMessage ;;
esac
done
if [ -z "$hostname" ] || [ -z "$stamp" ]; then
errorMessage
fi
# Script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Get Project ID from configuration file
PROJECT_ID="$(yq eval '.project_id' "$SCRIPT_DIR"/configuration/configuration_"$hostname".yml)"
# Info
echo "Starting backup of "$PROJECT_ID" on "$hostname""
# Stop project containers
echo "Stopping project containers on "$hostname""
/bin/bash "$SCRIPT_DIR"/scripts-project/stop-project-containers.sh -n "$hostname"
# Ask whether to backup modules
echo ""
echo "Back up all modules?"
echo "Modules for this project are: "$MODULES""
echo ""
echo "If this is the only project deployed on the host "$hostname", then answer 'y' (the default)."
echo "If you choose 'n', you will still be able to select whether to back up each module individually."
echo ""
echo -n "Back up all modules? "
read -e -p "[y/n]: " BACKUP_MODULES
BACKUP_MODULES="${BACKUP_MODULES:-"y"}"
BACKUP_MODULES="${BACKUP_MODULES,,}"
# Check input
while [ ! "$BACKUP_MODULES" == "y" ] && [ ! "$BACKUP_MODULES" == "n" ]
do
echo "Invalid option "${BACKUP_MODULES}". Please try again."
echo -n "Back up all modules (default is 'y')? "
read -e -p "[y/n]: " BACKUP_MODULES
BACKUP_MODULES="${BACKUP_MODULES:-"y"}"
BACKUP_MODULES="${BACKUP_MODULES,,}"
done
if [ "$BACKUP_MODULES" == "y" ]; then
echo "Backing up all modules."
for module in $MODULES
do
# Stop module containers
/bin/bash "$SCRIPT_DIR"/../"$module"/scripts-module/stop-module-containers.sh -n "$hostname"
# Back up module
/bin/bash "$SCRIPT_DIR"/../"$module"/scripts-module/backup-module.sh -n "$hostname" -s "$stamp"
# Start module containers
/bin/bash "$SCRIPT_DIR"/../"$module"/scripts-module/start-module-containers.sh -n "$hostname"
done
else
echo "Checking for each module."
for module in $MODULES
do
# Get user input for whether to do module backup (default yes)
echo ""
echo "Checking whether to back up "$module" module."
echo "Default is 'y'."
echo -n "Back up "$module" module? "
read -e -p "[y/n]: " BACKUP_MODULE
BACKUP_MODULE="${BACKUP_MODULE:-"y"}"
BACKUP_MODULE="${BACKUP_MODULE,,}"
# Check input
while [ ! "$BACKUP_MODULE" == "y" ] && [ ! "$BACKUP_MODULE" == "n" ]
do
echo "Invalid option "${BACKUP_MODULE}". Please try again."
echo -n "Back up "$module" module (default is 'y')? "
read -e -p "[y/n]: " BACKUP_MODULE
BACKUP_MODULE="${BACKUP_MODULE:-"y"}"
BACKUP_MODULE="${BACKUP_MODULE,,}"
done
if [ "$BACKUP_MODULE" == "y" ]; then
# Stop module containers
/bin/bash "$SCRIPT_DIR"/../"$module"/scripts-module/stop-module-containers.sh -n "$hostname"
# Back up module
/bin/bash "$SCRIPT_DIR"/../"$module"/scripts-module/backup-module.sh -n "$hostname" -s "$stamp"
# Start module containers
/bin/bash "$SCRIPT_DIR"/../"$module"/scripts-module/start-module-containers.sh -n "$hostname"
else
echo ""
echo "Skipping "$module" module backup."
fi
done
fi
# Back up project
echo "Backing up project containers on "$hostname""
/bin/bash "$SCRIPT_DIR"/scripts-project/backup-project.sh -n "$hostname" -s "$stamp"
# Start project containers
echo "Starting project containers on "$hostname""
/bin/bash "$SCRIPT_DIR"/scripts-project/start-project-containers.sh -n "$hostname"
echo "Backup completed"