@@ -48,22 +48,20 @@ For example, on the host server, create a folder on a dedicated NFS volume named
48
48
#!/bin/bash
49
49
50
50
## ============================================================
51
- ## RESTORE SCRIPT FOR THEHIVE APPLICATION STACK
51
+ ## BACKUP SCRIPT FOR THEHIVE APPLICATION STACK
52
52
## ============================================================
53
53
## PURPOSE:
54
- ## This script restores a backup of TheHive application stack,
55
- ## including its configuration, data, and logs. It is designed to
56
- ## recover from backups created using the associated backup script .
54
+ ## This script creates a backup of TheHive application stack,
55
+ ## including its configuration, data, and logs. It is designed
56
+ ## to ensure data is preserved for restoration purposes .
57
57
##
58
58
## IMPORTANT:
59
- ## - A backup is highly recommended before running a restore operation.
60
- ## This ensures you can revert to the current state if anything goes wrong.
61
- ## - Ensure that the target data folders are empty before running this script.
62
- ## Pre-existing files can cause conflicts or data corruption during the restore process.
63
- ## - This script must be run with sufficient permissions to overwrite
64
- ## application data and modify service configurations.
65
- ## - Ensure the backup folder path is correct and contains all required
66
- ## files and data.
59
+ ## - This script must be run with appropriate permissions to read all data
60
+ ## and write to the backup folders.
61
+ ## - Ensure sufficient storage is available in the backup location to avoid
62
+ ## partial or failed backups.
63
+ ## - Services (Elasticsearch, Cassandra, and TheHive) will be stopped during
64
+ ## the backup process to ensure data integrity.
67
65
##
68
66
## DISCLAIMER:
69
67
## - Users are strongly advised to test this script in a non-production
@@ -75,39 +73,40 @@ For example, on the host server, create a folder on a dedicated NFS volume named
75
73
##
76
74
## USAGE:
77
75
## 1. Update the variables at the start of the script to reflect your setup:
78
- ## - BACKUP_ROOT_FOLDER: Path to the root directory of your backups .
76
+ ## - BACKUP_ROOT_FOLDER: Root folder where backups will be stored .
79
77
## - BACKUP_TO_RESTORE: Name of the backup folder to restore.
80
78
## 2. Run the script using the following command:
81
- ## `bash ./scripts/restore .sh`
79
+ ## `bash ./scripts/backup .sh`
82
80
##
83
81
## ADDITIONAL RESOURCES:
84
82
## Refer to the official documentation for detailed instructions and
85
83
## additional information: https://docs.strangebee.com/thehive/operations/backup-restore/
86
84
##
87
85
## WARNING:
88
- ## - This script ensure Nginx, Elasticsearch, Cassandra, and TheHive services are stopped before performing the restore, and then restarts the services.
89
- ## - This script will overwrite existing data. Use it with caution .
86
+ ## - This script stops Nginx, Elasticsearch, Cassandra, and TheHive services,
87
+ ## performs the backup, and then restarts the services .
90
88
## - Do not modify the rest of the script unless necessary.
91
89
##
92
90
## ============================================================
93
91
## DO NOT MODIFY ANYTHING BELOW THIS LINE
94
92
## ============================================================
93
+
95
94
# Display help message
96
95
if [[ "$1" == "--help" || "$1" == "-h" ]]
97
96
then
98
- echo "Usage: $0 [DOCKER_COMPOSE_PATH] [BACKUP_FOLDER ]"
97
+ echo "Usage: $0 [BACKUP_ROOT_FOLDER ]"
99
98
echo
100
- echo "This script restores a backup of application data, including configurations, files, and logs."
99
+ echo "This script performs a backup of application data, including configurations, files, and logs."
101
100
echo
102
101
echo "Options:"
103
102
echo " DOCKER_COMPOSE_PATH Optional. Specify the path of the folder with the docker-compose.yml."
104
103
echo " If not provided, you will be prompted for a folder, with a default of '.'."
105
- echo " BACKUP_FOLDER Optional. Specify the folder containing the data to restore ."
106
- echo " If not provided, you will be prompted for a folder or exit; no default folder is used ."
104
+ echo " BACKUP_ROOT_FOLDER Optional. Specify the root folder where backups will be stored ."
105
+ echo " If not provided, you will be prompted for a folder, with a default of './backup' ."
107
106
echo
108
107
echo "Examples:"
109
- echo " $0 /path/to/docker-compose-folder /path/to/backup-folder restores backup stored in the specified folder."
110
- echo " $0 Prompt for docker compose folder and backup folder to restore ."
108
+ echo " $0 /path/to/docker-compose-folder /path/to/backup Perform backup with specified root folder."
109
+ echo " $0 Prompt for docker compose folder and backup root folder ."
111
110
exit 0
112
111
fi
113
112
@@ -133,62 +132,62 @@ For example, on the host server, create a folder on a dedicated NFS volume named
133
132
134
133
if [[ -z "$2" ]]
135
134
then
136
- read -p "Enter the backup root folder [default: None ]: " BACKUP_FOLDER
137
- [[ -z "${BACKUP_FOLDER}" ]] && echo "No backup folder specified, exiting." && exit 1
135
+ read -p "Enter the backup root folder [default: ./backup ]: " BACKUP_ROOT_FOLDER
136
+ BACKUP_ROOT_FOLDER=${BACKUP_ROOT_FOLDER:-"./backup"}
138
137
else
139
- BACKUP_FOLDER ="$2"
138
+ BACKUP_ROOT_FOLDER ="$2"
140
139
fi
141
140
142
- ## Check if the backup folder to restore exists, else exit
143
- [[ -d ${BACKUP_FOLDER} ]] || { echo "Backup folder not found, exiting"; exit 1; }
144
-
145
141
146
- # Define the log file and start logging. Log file is stored in the current folder
147
142
DATE="$(date +"%Y%m%d-%H%M%z" | sed 's/+/-/')"
148
- LOG_FILE="./restore_log_ ${DATE}.log "
149
- exec &> >(tee -a "$LOG_FILE")
143
+ BACKUP_FOLDER="${BACKUP_ROOT_FOLDER}/ ${DATE}"
144
+
150
145
151
- # Log the start time
152
- echo "Restoration process started at: $(date)"
146
+ ## Stop services
147
+ docker compose -f ${DOCKER_COMPOSE_PATH}/docker-compose.yml stop
153
148
154
- ## Exit if docker compose is running
155
- docker compose ps | grep -q "Up" && { echo "Docker Compose services are running. Exiting. Stop services and remove data before retoring data"; exit 1; }
149
+ ## Create the backup directory
150
+ mkdir -p "${BACKUP_FOLDER}" || { echo "Creating backup folder failed"; exit 1; }
151
+ echo "Created backup folder: ${BACKUP_FOLDER}"
156
152
153
+ ## Define the log file and start logging
154
+ LOG_FILE="${BACKUP_ROOT_FOLDER}/backup_log_${DATE}.log"
155
+ exec &> >(tee -a "$LOG_FILE")
157
156
158
- # Copy TheHive data
159
- echo "Restoring TheHive data and configuration..."
160
- rsync -aW --no-compress ${BACKUP_FOLDER}/thehive/ ${DOCKER_COMPOSE_PATH}/thehive || { echo "TheHive config restore failed"; exit 1; }
161
157
162
- # Copy Cortex data
163
- echo "Restoring Cortex data and configuration..."
164
- rsync -aW --no-compress ${BACKUP_FOLDER}/cortex/ ${DOCKER_COMPOSE_PATH}/cortex || { echo "Cortex config restore failed"; exit 1; }
165
158
166
- # Copy Casssandra data
167
- echo "Restoring Cassandra data ..."
168
- rsync -aW --no-compress ${BACKUP_FOLDER}/cassandra/ ${DOCKER_COMPOSE_PATH}/cassandra || { echo "Cassandra data restore failed"; exit 1; }
159
+ ## Prepare folders tree
160
+ mkdir -p ${BACKUP_FOLDER}/{thehive,cassandra,elasticsearch,nginx,certificates}
161
+ echo "Created folder structure under ${BACKUP_FOLDER}"
169
162
163
+ ## Copy TheHive data
164
+ echo "Starting TheHive backup..."
165
+ rsync -aW --no-compress ${DOCKER_COMPOSE_PATH}/thehive/ ${BACKUP_FOLDER}/thehive || { echo "TheHive backup failed"; exit 1; }
166
+ echo "TheHive backup completed."
170
167
171
- # Copy Elasticsearch data
172
- echo "Restoring Elasticsearch data ..."
173
- rsync -aW --no-compress ${BACKUP_FOLDER}/elasticsearch/ ${DOCKER_COMPOSE_PATH}/elasticsearch || { echo "Elasticsearch data restore failed"; exit 1; }
168
+ ## Copy Casssandra data
169
+ echo "Starting Cassandra backup..."
170
+ rsync -aW --no-compress ${DOCKER_COMPOSE_PATH}/cassandra/ ${BACKUP_FOLDER}/cassandra || { echo "Cassandra backup failed"; exit 1; }
171
+ echo "Cassandra backup completed."
174
172
173
+ ## Copy Elasticsearch data
174
+ echo "Starting Elasticsearch backup..."
175
+ rsync -aW --no-compress ${DOCKER_COMPOSE_PATH}/elasticsearch/ ${BACKUP_FOLDER}/elasticsearch || { echo "Elasticsearch config backup failed"; exit 1; }
176
+ echo "Elasticsearch backup completed."
175
177
176
- # Copy Nginx certificates
177
- echo "Restoring Nginx data and configuration..."
178
- rsync -a ${BACKUP_FOLDER}/nginx/ ${DOCKER_COMPOSE_PATH}/nginx ||
179
- { echo " Nginx configuration and certificates restore failed"; exit 1; }
180
- rsync -a ${BACKUP_FOLDER}/certificates/ ${DOCKER_COMPOSE_PATH}/certificates ||
181
- { echo " certificates restore failed"; exit 1; }
178
+ ## Copy Nginx certificates
179
+ echo "Starting backup of Nginx and certificates..."
180
+ rsync -aW --no-compress ${DOCKER_COMPOSE_PATH}/nginx/ ${BACKUP_FOLDER}/nginx || { echo " Backup of Nginx failed"; exit 1; }
181
+ rsync -aW --no-compress ${DOCKER_COMPOSE_PATH}/certificates/ ${BACKUP_FOLDER}/certificates || { echo " Backup of Nginx and certificates failed"; exit 1; }
182
+ echo "Backup of certificates completed."
182
183
183
- echo "Restoration process completed at: $(date)"
184
- ```
184
+ ## Restart services
185
+ echo "Restarting services..."
186
+ docker compose up -d -f ${DOCKER_COMPOSE_PATH}/docker-compose.yml
185
187
186
- ### Restart the services
187
188
188
- !!! Example ""
189
189
190
- ```bash
191
- docker compose up -d
190
+ echo "Backup process completed at: $(date)"
192
191
```
193
192
194
193
---
0 commit comments