Skip to content

Commit 4a8090d

Browse files
Refactored backup and restore functionality for Dgraph and Postgres in one step, including process enhancements
1 parent c739e0a commit 4a8090d

File tree

4 files changed

+89
-92
lines changed

4 files changed

+89
-92
lines changed
Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
source ../conf.env
33
#Backup Folder Name
4-
4+
BACKUP_FOLDER_NAME=$1
55
SCRIPT_DIR=$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
66
cd ${SCRIPT_DIR}/..
77
JEMPI_DOCKER_HOME=$PWD
@@ -12,13 +12,7 @@ source ../conf.env
1212

1313
python_cmd=$(which python3 || which python)
1414
echo $python_cmd
15-
Function to stop services
16-
stop_services() {
17-
pushd "$down_dir"
18-
echo "Stopping API service"
19-
source d-stack-stop-services.sh
20-
popd
21-
}
15+
2216
# Function to start backup restore API service
2317
start_backup_restore_service() {
2418
pushd "$reboot_dir"
@@ -27,13 +21,6 @@ source ../conf.env
2721
popd
2822
}
2923

30-
start_services() {
31-
pushd "$reboot_dir"
32-
echo "Starting API service"
33-
source d-stack-start-services.sh
34-
popd
35-
}
36-
3724
# Function to stop backup restore API service
3825
stop_backup_restore_service() {
3926
pushd "$down_dir"
@@ -42,28 +29,6 @@ source ../conf.env
4229
popd
4330
}
4431

45-
while true; do
46-
echo "Backup API"
47-
# Ask the user to enter a folder name
48-
echo "Backup folder Path:- ${DGRAPH_BACKUP_DIRECTORY}"
49-
pushd ${DGRAPH_BACKUP_DIRECTORY} || exit
50-
echo
51-
echo "Recent 5 Backups list"
52-
ls -lt --time=creation --sort=time | grep '^d' | tail -n 5
53-
echo
54-
popd
55-
read -p "Please enter your Dgraph Backup Folder Name: " BACKUP_FOLDER_NAME
56-
57-
# Check if the folder exists
58-
if [ -d "${DGRAPH_BACKUP_DIRECTORY}/$BACKUP_FOLDER_NAME" ]; then
59-
echo "Folder '$BACKUP_FOLDER_NAME' exists!"
60-
break # Exit the loop if the folder exists
61-
else
62-
echo "Folder '$BACKUP_FOLDER_NAME' does not exist, at ${DGRAPH_BACKUP_DIRECTORY}. "
63-
echo "Please try again"
64-
fi
65-
done
66-
6732
BACKUP_DIR="${DGRAPH_BACKUP_DIRECTORY}/$BACKUP_FOLDER_NAME"
6833
restore_data() {
6934
pushd "$backup_restore_dir"
@@ -78,19 +43,12 @@ restore_data() {
7843
popd
7944
}
8045

81-
stop_services
8246
start_backup_restore_service
8347

84-
source helper/bootstrapper/bootstrapper-docker.sh data resetAll
85-
8648
for backup_file in ${BACKUP_DIR}/dgraph_backup*.json; do
8749
# Assuming the first directory is for alpha nodes
8850
echo "Backup file found : $backup_file"
8951
restore_data $backup_file
9052
done
9153

92-
start_services
9354
stop_backup_restore_service
94-
95-
96-
echo $BACKUP_DIR

devops/linux/docker/backup_restore/postgres-restore.sh

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
#!/bin/bash
22
source ../conf.env
3-
#Backup Folder Name
4-
while true; do
5-
# Ask the user to enter a folder name
6-
echo "Backup folder Path:- ${POSTGRES_BACKUP_DIRECTORY}"
7-
pushd ${POSTGRES_BACKUP_DIRECTORY}
8-
echo
9-
echo "Recent 5 Backups list"
10-
ls -lt --time=creation --sort=time | grep '^d' | tail -n 5
11-
echo
12-
popd
13-
read -p "Please enter your Postgres Backup Folder Name: " BACKUP_FOLDER_NAME
3+
BACKUP_FOLDER_NAME=$1
144

15-
# Check if the folder exists
16-
if [ -d "${POSTGRES_BACKUP_DIRECTORY}/$BACKUP_FOLDER_NAME" ]; then
17-
echo "Folder '$BACKUP_FOLDER_NAME' exists!"
18-
break # Exit the loop if the folder exists
19-
else
20-
echo "Folder '$BACKUP_FOLDER_NAME' does not exist, at ${DGRAPH_BACKUP_DIRECTORY}. "
21-
echo "Please try again"
22-
fi
23-
done
245
# PostgreSQL settings from environment variables
256
DB_NAME="${POSTGRESQL_DATABASE}"
267
PGHOST="${POSTGRES_HOST:-localhost}"
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
source ../conf.env
3+
#Backup Folder Name
4+
SCRIPT_DIR=$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
5+
cd ${SCRIPT_DIR}/..
6+
JEMPI_DOCKER_HOME=$PWD
7+
# JEMPI_HOME = $1
8+
down_dir="$JEMPI_DOCKER_HOME/deployment/down"
9+
reboot_dir="$JEMPI_DOCKER_HOME/deployment/reboot"
10+
backup_restore_dir="$JEMPI_DOCKER_HOME/backup_restore"
11+
RED='\033[0;31m'
12+
NC='\033[0m' # No Color
13+
14+
15+
python_cmd=$(which python3 || which python)
16+
echo $python_cmd
17+
Function to stop services
18+
stop_services() {
19+
pushd "$down_dir"
20+
echo "Stopping API service"
21+
source d-stack-stop-services.sh
22+
popd
23+
}
24+
start_services() {
25+
pushd "$reboot_dir" || exit
26+
echo "Starting API service"
27+
source d-stack-start-services.sh
28+
popd || exit
29+
}
30+
31+
while true; do
32+
echo "Backup API"
33+
# Ask the user to enter a folder name
34+
echo "Backup folder Path:- ${DGRAPH_BACKUP_DIRECTORY}"
35+
pushd ${DGRAPH_BACKUP_DIRECTORY} || exit
36+
echo
37+
echo "Recent 5 Backups list"
38+
# ls -lt --time=creation --sort=time | grep '^d' | tail -n 5
39+
find . -mindepth 1 -type d -ctime -10 -exec sh -c 'find "$1" -mindepth 1 -print -quit | grep -q .' sh {} \; -exec stat --format '%Y %n' {} + | sort -nr | head -5 | awk '{print $2}'
40+
echo
41+
popd || exit
42+
read -p "Please enter your Backup Folder Name: " BACKUP_FOLDER_NAME
43+
44+
# Check if the folder exists
45+
if [ -d "${DGRAPH_BACKUP_DIRECTORY}/$BACKUP_FOLDER_NAME" ] && [ -d "${POSTGRES_BACKUP_DIRECTORY}/$BACKUP_FOLDER_NAME" ]; then
46+
echo "Folder '$BACKUP_FOLDER_NAME' exists!"
47+
break # Exit the loop if the folder exists
48+
else
49+
echo -e "${RED}Folder '$BACKUP_FOLDER_NAME' does not exist in either ${DGRAPH_BACKUP_DIRECTORY} or ${POSTGRES_BACKUP_DIRECTORY}.${NC}"
50+
echo -e "${RED}Please try again.${NC}"
51+
fi
52+
done
53+
54+
BACKUP_DIR="$BACKUP_FOLDER_NAME"
55+
56+
if [ -n "$BACKUP_DIR" ]; then
57+
# Run the next command
58+
stop_services
59+
source helper/bootstrapper/bootstrapper-docker.sh data resetAll
60+
pushd "$JEMPI_DOCKER_HOME/backup_restore" || exit
61+
echo "Starting Dgraph database restore..."
62+
bash dgraph-restore-api.sh $BACKUP_DIR
63+
echo "Database Dgraph restore completed."
64+
popd || exit
65+
pushd "$JEMPI_DOCKER_HOME/backup_restore" || exit
66+
echo "Starting Postgres database restore..."
67+
sudo bash postgres-restore.sh $BACKUP_DIR
68+
echo "Database Postgres restore completed."
69+
popd || exit
70+
start_services
71+
72+
# Replace this with the actual command you want to run
73+
else
74+
echo -e "${RED}Enter valid Folder name.${NC}"
75+
fi
76+

devops/linux/docker/deployment/deploy-local.sh

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -144,39 +144,22 @@ initialize_db_build_all_stack_and_reboot(){
144144
yes | source d-stack-1-create-db-build-all-reboot.sh
145145
popd
146146
}
147-
restore_dgraph_db(){
148-
echo "Are you sure you want to restore the Dgraph database? (yes/no)"
149-
read dgraph_confirmation
150-
dgraph_confirmation=$(echo "$dgraph_confirmation" | tr '[:upper:]' '[:lower:]')
151-
152-
if [ "$dgraph_confirmation" == "yes" ] || [ "$dgraph_confirmation" == "y" ]; then
147+
restore_db(){
148+
echo "Are you sure you want to restore the Dgraph and Postgres database? It will wipe all data and restore from backup (Ctrl+Y for Yes, any other key for No)"
149+
read -rsn1 -p "> " answer
150+
# Call the confirm function
151+
152+
if [[ $answer == $'\x19' ]]; then
153153
pushd "$JEMPI_HOME/devops/linux/docker/backup_restore"
154154
echo "Starting Dgraph database restore..."
155-
bash dgraph-restore-api.sh
156-
echo "Database Dgraph restore completed."
155+
bash restore-dgraph-postgres.sh
156+
echo "Database Dgraph and Postgres restore completed."
157157
popd
158158
else
159-
echo "Dgraph Database restore cancelled. Moving ahead without restore."
159+
echo "Database restore cancelled. Moving ahead without restore."
160160
cd de # Continue with the rest of your script
161161
fi
162162
}
163-
restore_postgres_db(){
164-
echo "Are you sure you want to restore the Postgres database? (yes/no)"
165-
read postgres_confirmation
166-
postgres_confirmation=$(echo "$postgres_confirmation" | tr '[:upper:]' '[:lower:]')
167-
168-
if [ "$postgres_confirmation" == "yes" ] || [ "$postgres_confirmation" == "y" ]; then
169-
pushd "$JEMPI_HOME/devops/linux/docker/backup_restore"
170-
echo "Starting Postgres database restore..."
171-
sudo bash postgres-restore.sh
172-
echo "Database Postgres restore completed."
173-
popd
174-
175-
else
176-
echo "Postgres Database restore cancelled. Moving ahead without restore."
177-
# Continue with the rest of your script
178-
fi
179-
}
180163

181164
# Process user choice
182165
case $choice in
@@ -226,8 +209,7 @@ case $choice in
226209
;;
227210
7)
228211
echo "Restore Databases"
229-
restore_postgres_db
230-
restore_dgraph_db
212+
restore_db
231213
;;
232214
8)
233215
echo "Destroy"

0 commit comments

Comments
 (0)