Skip to content

Commit deb433b

Browse files
committed
feat(docker): automatic selection of .env file
1 parent b9d2679 commit deb433b

1 file changed

Lines changed: 45 additions & 59 deletions

File tree

scripts/restore_backup.sh

Lines changed: 45 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -69,46 +69,63 @@ print_info "Selected file: $BACKUP_FILENAME"
6969
echo ""
7070

7171
# =============================================================================
72-
# Section 2.2: Select .env file
72+
# Section 2.1: Select docker-compose configuration to start containers
7373
# =============================================================================
7474

75-
print_info "Select .env file:"
7675
echo ""
77-
echo " 1. Root .env file"
78-
echo " 2. backend/.env file"
79-
echo " 3. frontend/.env file"
76+
print_info "Select a docker-compose configuration to start containers after restoring the backup:"
77+
echo ""
78+
echo " 1. Root (from sources)"
79+
echo " 2. Root (stable)"
80+
echo " 3. Root (latest)"
81+
echo " 4. Frontend"
82+
echo " 5. Backend"
8083
echo ""
8184

85+
# 2.1.2 User selects a configuration by number
8286
while true; do
83-
read -r -p "Enter .env file number (1-3): " env_choice
87+
read -r -p "Enter configuration number (1-5): " COMPOSE_FILE
8488

85-
if ! [[ "$env_choice" =~ ^[0-9]+$ ]]; then
86-
print_error "Enter a number from 1 to 3"
89+
if ! [[ "$COMPOSE_FILE" =~ ^[0-9]+$ ]]; then
90+
print_error "Enter the file number"
8791
continue
8892
fi
8993

90-
if [ "$env_choice" -lt 1 ] || [ "$env_choice" -gt 3 ]; then
91-
print_error "Enter a number from 1 to 3"
94+
if [ "$COMPOSE_FILE" -lt 1 ] || [ "$COMPOSE_FILE" -gt 5 ]; then
95+
print_error "Entered number does not match any file from the list"
9296
continue
9397
fi
9498

95-
case "$env_choice" in
96-
1) ENV_FILE="$SCRIPT_DIR/../.env" ;;
97-
2) ENV_FILE="$SCRIPT_DIR/../backend/.env" ;;
98-
3) ENV_FILE="$SCRIPT_DIR/../frontend/.env" ;;
99-
esac
10099
break
101100
done
102101

102+
case "$COMPOSE_FILE" in
103+
1) COMPOSE_LABEL="Root (from sources)" ;;
104+
2) COMPOSE_LABEL="Root (stable)" ;;
105+
3) COMPOSE_LABEL="Root (latest)" ;;
106+
4) COMPOSE_LABEL="Frontend" ;;
107+
5) COMPOSE_LABEL="Backend" ;;
108+
esac
109+
110+
echo ""
111+
print_info "Selected configuration: $COMPOSE_LABEL"
112+
echo ""
113+
114+
# =============================================================================
115+
# Section 2.2: Select .env file based on docker-compose choice (section 2.1)
116+
# =============================================================================
117+
118+
case "$COMPOSE_FILE" in
119+
1|2|3) ENV_FILE="$SCRIPT_DIR/../.env" ;;
120+
4) ENV_FILE="$SCRIPT_DIR/../frontend/.env" ;;
121+
5) ENV_FILE="$SCRIPT_DIR/../backend/.env" ;;
122+
esac
123+
103124
if [ ! -f "$ENV_FILE" ]; then
104125
print_error "Error: .env file not found at $ENV_FILE. Cannot proceed without database configuration."
105126
exit 1
106127
fi
107128

108-
echo ""
109-
print_info "Selected .env file: $ENV_FILE"
110-
echo ""
111-
112129
# =============================================================================
113130
# Section 3: Read .env file and extract database configuration
114131
# =============================================================================
@@ -229,49 +246,18 @@ else
229246
fi
230247

231248
# =============================================================================
232-
# Section 6: Start containers
249+
# Section 6: Run the selected docker-compose command
233250
# =============================================================================
234251

235-
# 6.1 Ask user which docker-compose configuration to use
236-
echo ""
237-
print_info "Select a docker-compose configuration to start containers:"
238-
echo ""
239-
echo " 1. Root (latest)"
240-
echo " 2. Root (stable)"
241-
echo " 3. Root (from sources)"
242-
echo " 4. Frontend"
243-
echo " 5. Backend"
244-
echo ""
245-
246-
# 6.2 User selects a configuration by number
247-
while true; do
248-
read -r -p "Enter configuration number (1-5): " COMPOSE_FILE
249-
250-
if ! [[ "$COMPOSE_FILE" =~ ^[0-9]+$ ]]; then
251-
print_error "Enter the file number"
252-
continue
253-
fi
254-
255-
if [ "$COMPOSE_FILE" -lt 1 ] || [ "$COMPOSE_FILE" -gt 5 ]; then
256-
print_error "Entered number does not match any file from the list"
257-
continue
258-
fi
259-
260-
break
261-
done
262-
263-
echo ""
264-
265-
# 6.3 Run the selected docker-compose command
266252
case "$COMPOSE_FILE" in
267253
1)
268-
output=$(TAG=latest docker compose -f "$PROJECT_DIR/docker-compose.yml" up -d 2>&1)
254+
output=$(docker compose -f "$PROJECT_DIR/docker-compose.src.yml" up -d 2>&1)
269255
;;
270256
2)
271257
output=$(TAG=stable docker compose -f "$PROJECT_DIR/docker-compose.yml" up -d 2>&1)
272258
;;
273259
3)
274-
output=$(docker compose -f "$PROJECT_DIR/docker-compose.src.yml" up -d 2>&1)
260+
output=$(TAG=latest docker compose -f "$PROJECT_DIR/docker-compose.yml" up -d 2>&1)
275261
;;
276262
4)
277263
# Compose resolves .env from the compose file's directory; pass the selected file explicitly.
@@ -290,19 +276,19 @@ else
290276
fi
291277

292278
echo ""
293-
print_info "Done! Database \"$POSTGRES_DB\" has been successfully restored from \"$BACKUP_FILENAME\"."
279+
print_info "Done! Database '${POSTGRES_DB}' has been successfully restored from '${BACKUP_FILENAME}'."
294280

295281
echo ""
296282
echo -e "${GREEN}"
297-
cat << 'EOF'
283+
cat <<'EOF'
298284
__
299-
'. \
300-
'- \
285+
. \
286+
- \
301287
/ /_ .---.
302288
/ | \\,.\/--.// )
303289
| \// )/ /
304-
\ ' ^ ^ / )
305-
'.____. .___/
290+
\ ^ ^ / )
291+
.____. .___/
306292
.\ \ | /
307293
\______/
308294
Done!

0 commit comments

Comments
 (0)