@@ -201,11 +201,24 @@ log() {
201
201
202
202
log "Starting MongoDB backup process"
203
203
204
+ # Load configuration values from config.json
205
+ CONFIG_FILE="./config.json"
206
+ if [ ! -f "\$ CONFIG_FILE" ]; then
207
+ log "❌ ERROR: Missing config.json! Exiting."
208
+ exit 1
209
+ fi
210
+
211
+ DB_USERNAME=\$ (jq -r '.db_username' "\$ CONFIG_FILE")
212
+ DB_PASSWORD=\$ (jq -r '.db_password' "\$ CONFIG_FILE")
213
+ AWS_BUCKET=\$ (jq -r '.aws_bucket' "\$ CONFIG_FILE")
214
+ AWS_REGION=\$ (jq -r '.aws_region' "\$ CONFIG_FILE")
215
+ MONGO_PORT=\$ (jq -r '.mongo_port' "\$ CONFIG_FILE")
216
+
204
217
TIMESTAMP=\$ (date +%F-%H-%M)
205
218
BACKUP_PATH="/tmp/mongo-backup-\$ TIMESTAMP.gz"
206
219
207
220
# Get domain name from config.json if available
208
- DOMAIN_CONFIG=\$ (jq -r '.domain_name' "$CONFIG_FILE ")
221
+ DOMAIN_CONFIG=\$ (jq -r '.domain_name' "\ $ CONFIG_FILE")
209
222
if [ -n "\$ DOMAIN_CONFIG" ] && [ "\$ DOMAIN_CONFIG" != "null" ] && [ "\$ DOMAIN_CONFIG" != "your.domain.com" ]; then
210
223
HOSTNAME="\$ DOMAIN_CONFIG"
211
224
log "Using domain name from config.json: \$ HOSTNAME"
@@ -225,28 +238,35 @@ CERT_FILE="/etc/ssl/mongodb/certificate.pem"
225
238
CA_FILE="/etc/ssl/mongodb/certificate_authority.pem"
226
239
TLS_ENABLED=false
227
240
TLS_ARG=""
241
+ MONGOSH_TLS_ARG=""
228
242
229
- if [ -f "$CERT_FILE " ] && grep -q "tls:" /etc/mongod.conf && grep -q "mode: requireTLS" /etc/mongod.conf; then
243
+ if [ -f "\ $ CERT_FILE" ] && grep -q "tls:" /etc/mongod.conf && grep -q "mode: requireTLS" /etc/mongod.conf; then
230
244
log "MongoDB TLS is enabled with private CA certificates. Using TLS connection for backup..."
231
245
TLS_ENABLED=true
232
- TLS_ARG="--ssl"
246
+ # For mongodump, use --ssl flags
247
+ TLS_ARG="--ssl --sslCAFile \$ CA_FILE --sslPEMKeyFile /etc/ssl/mongodb/client.pem"
248
+ # For mongosh, use --tls flags
249
+ MONGOSH_TLS_ARG="--tls --tlsCAFile \$ CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem"
250
+ log "NOTE: Client certificates are required for connections."
251
+ log " Ensure the client certificate exists at /etc/ssl/mongodb/client.pem"
233
252
elif grep -q "ssl:" /etc/mongod.conf && grep -q "mode: requireSSL" /etc/mongod.conf; then
234
253
log "MongoDB SSL is enabled (legacy configuration). Using SSL connection for backup..."
235
254
TLS_ENABLED=true
236
255
TLS_ARG="--ssl"
256
+ MONGOSH_TLS_ARG="--tls"
237
257
else
238
258
log "MongoDB TLS is not enabled. Using standard connection for backup..."
239
259
fi
240
260
241
- # Check if MongoDB is responsive
242
- if ! mongosh --host \$ HOSTNAME --port $MONGO_PORT \$ TLS_ARG -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "db.adminCommand('ping')" &>/dev/null; then
261
+ # Check if MongoDB is responsive - use mongosh with --tls flags
262
+ if ! mongosh --host \$ HOSTNAME --port \ $ MONGO_PORT \$ MONGOSH_TLS_ARG -u \ $ DB_USERNAME -p \ $ DB_PASSWORD --authenticationDatabase admin --eval "db.adminCommand('ping')" &>/dev/null; then
243
263
log "❌ ERROR: MongoDB is not responsive. Backup aborted."
244
264
exit 1
245
265
fi
246
266
247
- # Create backup
267
+ # Create backup - use mongodump with --ssl flags
248
268
log "Creating backup at \$ BACKUP_PATH"
249
- if mongodump --host \$ HOSTNAME --port $MONGO_PORT \$ TLS_ARG -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --archive=\$ BACKUP_PATH --gzip; then
269
+ if mongodump --host \$ HOSTNAME --port \ $ MONGO_PORT \$ TLS_ARG -u \ $ DB_USERNAME -p \ $ DB_PASSWORD --authenticationDatabase admin --archive=\$ BACKUP_PATH --gzip; then
250
270
log "✅ Backup created successfully"
251
271
252
272
# Check if backup file exists and has a size greater than 0
@@ -255,7 +275,7 @@ if mongodump --host \$HOSTNAME --port $MONGO_PORT \$TLS_ARG -u $DB_USERNAME -p $
255
275
256
276
# Upload to S3
257
277
log "Uploading backup to S3..."
258
- if aws s3 cp \$ BACKUP_PATH s3://$AWS_BUCKET /\$ HOSTNAME/\$ TIMESTAMP.gz --region $AWS_REGION ; then
278
+ if aws s3 cp \$ BACKUP_PATH s3://\ $ AWS_BUCKET/\$ HOSTNAME/\$ TIMESTAMP.gz --region \ $ AWS_REGION; then
259
279
log "✅ Backup uploaded to S3 successfully"
260
280
261
281
# Clean up local backup
@@ -264,7 +284,7 @@ if mongodump --host \$HOSTNAME --port $MONGO_PORT \$TLS_ARG -u $DB_USERNAME -p $
264
284
265
285
# Manage retention (keep only the 10 most recent backups)
266
286
log "Managing backup retention..."
267
- BACKUPS=\$ (aws s3 ls s3://$AWS_BUCKET /\$ HOSTNAME/ --region $AWS_REGION | awk '{print \$ 4}' | sort)
287
+ BACKUPS=\$ (aws s3 ls s3://\ $ AWS_BUCKET/\$ HOSTNAME/ --region \ $ AWS_REGION | awk '{print \$ 4}' | sort)
268
288
BACKUP_COUNT=\$ (echo "\$ BACKUPS" | wc -l)
269
289
270
290
if [ \$ BACKUP_COUNT -gt 10 ]; then
@@ -273,7 +293,7 @@ if mongodump --host \$HOSTNAME --port $MONGO_PORT \$TLS_ARG -u $DB_USERNAME -p $
273
293
274
294
log "Keeping 10 most recent backups, deleting \$ DELETE_COUNT older backups"
275
295
for FILE in \$ OLD_BACKUPS; do
276
- if aws s3 rm s3://$AWS_BUCKET /\$ HOSTNAME/\$ FILE --region $AWS_REGION ; then
296
+ if aws s3 rm s3://\ $ AWS_BUCKET/\$ HOSTNAME/\$ FILE --region \ $ AWS_REGION; then
277
297
log "Deleted old backup: \$ FILE"
278
298
else
279
299
log "Failed to delete old backup: \$ FILE"
0 commit comments