Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dfd8f7b22b |
@@ -0,0 +1,14 @@
|
|||||||
|
# MongoDB Restore Configuration
|
||||||
|
# Copy this file to .env and update with your values
|
||||||
|
# This configuration should match your local MongoDB instance
|
||||||
|
|
||||||
|
# MongoDB credentials (for the target database)
|
||||||
|
MONGO_USERNAME=admin
|
||||||
|
MONGO_PASSWORD=password
|
||||||
|
MONGO_HOST=localhost
|
||||||
|
MONGO_PORT=27017
|
||||||
|
MONGO_DATABASE=adastradb
|
||||||
|
MONGO_AUTHSOURCE=admin
|
||||||
|
|
||||||
|
# Note: The connection string will be constructed as:
|
||||||
|
# mongodb://<MONGO_USERNAME>:<MONGO_PASSWORD>@<MONGO_HOST>:<MONGO_PORT>/<MONGO_DATABASE>?authSource=<MONGO_AUTHSOURCE>
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
# Environment configuration file (contains sensitive credentials)
|
||||||
|
.env
|
||||||
|
!.env.example
|
||||||
|
|
||||||
|
# OS files
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
*.log
|
||||||
@@ -0,0 +1,277 @@
|
|||||||
|
# MongoDB Restore Utility (Standalone)
|
||||||
|
|
||||||
|
This directory contains a standalone tool for restoring MongoDB database backups created with `db-dump.sh`.
|
||||||
|
It provides interactive navigation through backup directories and restores them to a configured MongoDB instance.
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
### 1. Configure Connection Details
|
||||||
|
|
||||||
|
Copy the example configuration:
|
||||||
|
```bash
|
||||||
|
cp .env.example .env
|
||||||
|
```
|
||||||
|
|
||||||
|
Edit `.env` with your MongoDB connection details:
|
||||||
|
```
|
||||||
|
MONGO_USERNAME=admin
|
||||||
|
MONGO_PASSWORD=password
|
||||||
|
MONGO_HOST=localhost
|
||||||
|
MONGO_PORT=27017
|
||||||
|
MONGO_DATABASE=adastradb
|
||||||
|
MONGO_AUTHSOURCE=admin
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Restore a Backup
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./db-restore.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
The script will:
|
||||||
|
1. Verify MongoDB tools are installed
|
||||||
|
2. Load your configuration
|
||||||
|
3. Ask for the path to your backups directory
|
||||||
|
4. Guide you through the directory structure
|
||||||
|
5. Show a summary of what will be restored
|
||||||
|
6. Ask for confirmation
|
||||||
|
7. Restore the backup
|
||||||
|
|
||||||
|
## How It Works
|
||||||
|
|
||||||
|
### Configuration
|
||||||
|
|
||||||
|
The script requires a `.env` file with these variables:
|
||||||
|
- `MONGO_USERNAME` - MongoDB admin username
|
||||||
|
- `MONGO_PASSWORD` - MongoDB admin password
|
||||||
|
- `MONGO_HOST` - MongoDB server hostname/IP
|
||||||
|
- `MONGO_PORT` - MongoDB server port
|
||||||
|
- `MONGO_DATABASE` - Target database name
|
||||||
|
- `MONGO_AUTHSOURCE` - Authentication source (usually `admin`)
|
||||||
|
|
||||||
|
### Interactive Navigation
|
||||||
|
|
||||||
|
The script intelligently navigates through your backup directory structure:
|
||||||
|
|
||||||
|
```
|
||||||
|
dumps/
|
||||||
|
├── prod/ ← Choose this
|
||||||
|
│ ├── 20260414_1632/ ← Then this
|
||||||
|
│ │ └── (database folders) ← Then this
|
||||||
|
│ └── 20260414_1645/
|
||||||
|
└── uat/
|
||||||
|
└── 20260414_1700/
|
||||||
|
```
|
||||||
|
|
||||||
|
At each level, the script:
|
||||||
|
1. Shows available subdirectories
|
||||||
|
2. Asks you to choose one
|
||||||
|
3. Continues until it finds `.bson` files (backup data)
|
||||||
|
|
||||||
|
### Backup Detection
|
||||||
|
|
||||||
|
The script automatically stops navigation when it finds a directory containing `.bson` files, which are the actual backup data files.
|
||||||
|
|
||||||
|
Example backup structure:
|
||||||
|
```
|
||||||
|
~/Works/dbdump/dumps/
|
||||||
|
└── bo-uat-cosmodb/
|
||||||
|
└── 20260414_1732/
|
||||||
|
└── admin/ ← BSON files here
|
||||||
|
├── system.profile.bson
|
||||||
|
└── system.version.bson
|
||||||
|
```
|
||||||
|
|
||||||
|
## Example Use Case
|
||||||
|
|
||||||
|
Given this backup structure:
|
||||||
|
```
|
||||||
|
~/Works/dbdump/dumps/
|
||||||
|
├── bo-prod-cosmodb/
|
||||||
|
│ └── 20260414_1600/
|
||||||
|
│ └── AdAstra/
|
||||||
|
│ ├── collection1.bson
|
||||||
|
│ └── collection1.metadata.json
|
||||||
|
└── bo-uat-cosmodb/
|
||||||
|
└── 20260414_1732/
|
||||||
|
└── AdAstra/
|
||||||
|
├── orders.bson
|
||||||
|
└── orders.metadata.json
|
||||||
|
```
|
||||||
|
|
||||||
|
Restore process:
|
||||||
|
```bash
|
||||||
|
./db-restore.sh
|
||||||
|
|
||||||
|
# Script asks: Dumps path: ~/Works/dbdump/dumps
|
||||||
|
# Shows: 1) bo-prod-cosmodb 2) bo-uat-cosmodb
|
||||||
|
# You choose: 2
|
||||||
|
# Shows: 1) 20260414_1732
|
||||||
|
# You choose: 1
|
||||||
|
# Shows: 1) AdAstra
|
||||||
|
# You choose: 1
|
||||||
|
# Script finds BSON files and shows summary
|
||||||
|
```
|
||||||
|
|
||||||
|
## Restore Summary
|
||||||
|
|
||||||
|
Before executing the restore, the script displays:
|
||||||
|
|
||||||
|
```
|
||||||
|
MongoDB Connection:
|
||||||
|
Host: localhost
|
||||||
|
Port: 27017
|
||||||
|
Username: admin
|
||||||
|
Database: adastradb
|
||||||
|
AuthSource: admin
|
||||||
|
|
||||||
|
Backup Source:
|
||||||
|
Location: /home/user/Works/dbdump/dumps/bo-uat-cosmodb/20260414_1732
|
||||||
|
Backup files: 42
|
||||||
|
Total size: 250M
|
||||||
|
```
|
||||||
|
|
||||||
|
You must confirm with "yes" to proceed.
|
||||||
|
|
||||||
|
## Connection String
|
||||||
|
|
||||||
|
The script constructs the MongoDB connection string as:
|
||||||
|
|
||||||
|
```
|
||||||
|
mongodb://<username>:<password>@<host>:<port>/<database>?authSource=<authSource>
|
||||||
|
```
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```
|
||||||
|
mongodb://admin:password@localhost:27017/adastradb?authSource=admin
|
||||||
|
```
|
||||||
|
|
||||||
|
## Directory Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
dbrestore/
|
||||||
|
├── db-restore.sh # Main script
|
||||||
|
├── .env # Configuration (created by you, git-ignored)
|
||||||
|
├── .env.example # Example configuration
|
||||||
|
├── .gitignore # Git ignore rules
|
||||||
|
└── README.md # This file
|
||||||
|
```
|
||||||
|
|
||||||
|
## Security
|
||||||
|
|
||||||
|
- Configuration file (`.env`) has restricted permissions
|
||||||
|
- Contains sensitive credentials (username, password)
|
||||||
|
- Never commit actual `.env` file to version control
|
||||||
|
- Use `.env.example` as template
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- **MongoDB Database Tools**: Download from https://www.mongodb.com/try/download/database-tools
|
||||||
|
- Specifically requires `mongorestore` command
|
||||||
|
- Bash shell
|
||||||
|
- Network access to MongoDB server
|
||||||
|
- Backup files created by `db-dump.sh` or `mongodump`
|
||||||
|
|
||||||
|
## Installation of MongoDB Database Tools
|
||||||
|
|
||||||
|
### macOS
|
||||||
|
```bash
|
||||||
|
brew tap mongodb/brew
|
||||||
|
brew install mongodb-database-tools
|
||||||
|
```
|
||||||
|
|
||||||
|
### Linux (Ubuntu/Debian)
|
||||||
|
```bash
|
||||||
|
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2004-x86_64-100.8.0.tgz
|
||||||
|
tar -xzf mongodb-database-tools-ubuntu2004-x86_64-100.8.0.tgz
|
||||||
|
sudo cp mongodb-database-tools-ubuntu2004-x86_64-100.8.0/bin/* /usr/local/bin/
|
||||||
|
```
|
||||||
|
|
||||||
|
### Windows
|
||||||
|
Download the MSI installer from https://www.mongodb.com/try/download/database-tools and run it.
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### mongorestore not found
|
||||||
|
```bash
|
||||||
|
# Check if MongoDB Database Tools is installed
|
||||||
|
mongorestore --version
|
||||||
|
|
||||||
|
# If not installed, see "Installation" section above
|
||||||
|
```
|
||||||
|
|
||||||
|
### .env file not found
|
||||||
|
```bash
|
||||||
|
# Copy the example configuration
|
||||||
|
cp .env.example .env
|
||||||
|
|
||||||
|
# Edit with your values
|
||||||
|
nano .env # or your favorite editor
|
||||||
|
```
|
||||||
|
|
||||||
|
### Connection refused
|
||||||
|
- Verify host and port are correct
|
||||||
|
- Check network connectivity to MongoDB server
|
||||||
|
- Ensure firewall allows access
|
||||||
|
- Verify MongoDB service is running (if local)
|
||||||
|
|
||||||
|
### Authentication failed
|
||||||
|
- Verify username and password in `.env`
|
||||||
|
- Check user has sufficient permissions
|
||||||
|
- Confirm `authSource` is correct (usually `admin`)
|
||||||
|
|
||||||
|
### No BSON files found
|
||||||
|
- Make sure you're pointing to the root of dumps directory
|
||||||
|
- Navigate through subdirectories until you reach the folder with actual backup files
|
||||||
|
- BSON files are typically deep in the directory structure
|
||||||
|
|
||||||
|
## Manual Restore Command
|
||||||
|
|
||||||
|
For reference, here's the command the script executes:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mongorestore --uri "mongodb://user:pass@host:port/db?authSource=admin" /path/to/dumps
|
||||||
|
```
|
||||||
|
|
||||||
|
## Related Tools
|
||||||
|
|
||||||
|
- **`dbutils/db-start.sh`** - Start MongoDB in Docker for restoring locally
|
||||||
|
- **`dbdump/db-dump.sh`** - Create backups from remote MongoDB servers
|
||||||
|
- **`dbrestore/db-restore.sh`** - Restore backups to a MongoDB instance (this tool)
|
||||||
|
|
||||||
|
## Workflow Example
|
||||||
|
|
||||||
|
Complete workflow to backup and restore:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. Backup from production MongoDB (in dbdump folder)
|
||||||
|
cd ~/Works/dbdump
|
||||||
|
./db-dump.sh dump
|
||||||
|
# Select prod environment and wait for backup
|
||||||
|
|
||||||
|
# 2. Start local MongoDB (in dbutils folder)
|
||||||
|
cd ~/Works/dbutils
|
||||||
|
./db-start.sh up
|
||||||
|
|
||||||
|
# 3. Restore backup locally (in dbrestore folder)
|
||||||
|
cd ~/Works/dbrestore
|
||||||
|
./db-restore.sh
|
||||||
|
# Navigate to the backup created in step 1 and restore
|
||||||
|
```
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
For more information:
|
||||||
|
- MongoDB Documentation: https://docs.mongodb.com/
|
||||||
|
- mongorestore Manual: https://docs.mongodb.com/database-tools/mongorestore/
|
||||||
|
- Connection String Reference: https://docs.mongodb.com/manual/reference/connection-string/
|
||||||
|
|
||||||
|
## About
|
||||||
|
|
||||||
|
**Author:** Julien Gautier ([jgautier.webdev@gmail.com](mailto:jgautier.webdev@gmail.com))
|
||||||
|
|
||||||
|
**Organization:** AdAstra
|
||||||
|
|
||||||
|
**Project:** adastra_scripts
|
||||||
|
|
||||||
|
This utility was created to simplify the restoration of MongoDB database backups to local development environments for AdAstra projects.
|
||||||
Executable
+312
@@ -0,0 +1,312 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Author: Julien Gautier <jgautier.webdev@gmail.com>
|
||||||
|
# Organization: AdAstra
|
||||||
|
# Project: adastra_scripts
|
||||||
|
#
|
||||||
|
################################################################################
|
||||||
|
# MongoDB Restore Utility Script (Standalone)
|
||||||
|
#
|
||||||
|
# This script restores MongoDB database dumps from backup files.
|
||||||
|
# It provides interactive navigation through dump directories and restores
|
||||||
|
# to a configured MongoDB instance.
|
||||||
|
#
|
||||||
|
# Usage: ./db-restore.sh
|
||||||
|
#
|
||||||
|
# Configuration:
|
||||||
|
# Requires .env file with MongoDB connection details:
|
||||||
|
# - MONGO_USERNAME
|
||||||
|
# - MONGO_PASSWORD
|
||||||
|
# - MONGO_HOST
|
||||||
|
# - MONGO_PORT
|
||||||
|
# - MONGO_DATABASE
|
||||||
|
# - MONGO_AUTHSOURCE
|
||||||
|
#
|
||||||
|
# Example command (what the script executes):
|
||||||
|
# mongorestore --uri "mongodb://user:pass@host:port/db?authSource=admin" /path/to/dumps
|
||||||
|
#
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
ENV_FILE="${SCRIPT_DIR}/.env"
|
||||||
|
|
||||||
|
# Global variables
|
||||||
|
SELECTED_DUMP_PATH=""
|
||||||
|
|
||||||
|
# Colors
|
||||||
|
RED='\033[0;31m'
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
YELLOW='\033[1;33m'
|
||||||
|
BLUE='\033[0;34m'
|
||||||
|
CYAN='\033[0;36m'
|
||||||
|
NC='\033[0m'
|
||||||
|
|
||||||
|
# Helper functions
|
||||||
|
print_info() {
|
||||||
|
echo -e "${BLUE}┣━${NC} ${CYAN}${1}${NC}"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_success() {
|
||||||
|
echo -e "${GREEN}✓ ${1}${NC}"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_error() {
|
||||||
|
echo -e "${RED}✗ ${1}${NC}"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_warning() {
|
||||||
|
echo -e "${YELLOW}⚠ ${1}${NC}"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_separator() {
|
||||||
|
echo -e "${BLUE}┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_separator_up() {
|
||||||
|
echo -e "${BLUE}┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_separator_down() {
|
||||||
|
echo -e "${BLUE}┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_section() {
|
||||||
|
echo
|
||||||
|
print_separator_up
|
||||||
|
print_info "$1"
|
||||||
|
print_separator_down
|
||||||
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
|
check_mongorestore() {
|
||||||
|
if ! command -v mongorestore &> /dev/null; then
|
||||||
|
print_error "mongorestore is not installed or not in PATH"
|
||||||
|
print_info "Install MongoDB Database Tools from: https://www.mongodb.com/try/download/database-tools"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
print_success "mongorestore found: $(mongorestore --version | head -1)"
|
||||||
|
}
|
||||||
|
|
||||||
|
check_env_file() {
|
||||||
|
if [[ ! -f "$ENV_FILE" ]]; then
|
||||||
|
print_error "Configuration file not found: $ENV_FILE"
|
||||||
|
print_info "Please create .env file with the following variables:"
|
||||||
|
print_info " MONGO_USERNAME=<username>"
|
||||||
|
print_info " MONGO_PASSWORD=<password>"
|
||||||
|
print_info " MONGO_HOST=<host>"
|
||||||
|
print_info " MONGO_PORT=<port>"
|
||||||
|
print_info " MONGO_DATABASE=<database>"
|
||||||
|
print_info " MONGO_AUTHSOURCE=<authSource>"
|
||||||
|
print_info "You can copy from .env.example"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
load_config() {
|
||||||
|
# shellcheck source=/dev/null
|
||||||
|
source "$ENV_FILE"
|
||||||
|
|
||||||
|
# Validate required variables
|
||||||
|
if [[ -z "$MONGO_USERNAME" ]] || [[ -z "$MONGO_PASSWORD" ]] || [[ -z "$MONGO_HOST" ]] || \
|
||||||
|
[[ -z "$MONGO_PORT" ]] || [[ -z "$MONGO_DATABASE" ]] || [[ -z "$MONGO_AUTHSOURCE" ]]; then
|
||||||
|
print_error "Missing required configuration variables in .env file"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
has_bson_files() {
|
||||||
|
local dir="$1"
|
||||||
|
[[ -n $(find "$dir" -maxdepth 1 -type f -name "*.bson" 2>/dev/null | head -1) ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
has_subdirectories() {
|
||||||
|
local dir="$1"
|
||||||
|
[[ -n $(find "$dir" -maxdepth 1 -type d ! -name "." ! -name ".." 2>/dev/null | head -1) ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
navigate_to_dump() {
|
||||||
|
SELECTED_DUMP_PATH=""
|
||||||
|
local current_path="$1"
|
||||||
|
|
||||||
|
# Expand ~ in path
|
||||||
|
current_path="${current_path/#\~/$HOME}"
|
||||||
|
|
||||||
|
# Check if path exists
|
||||||
|
if [[ ! -d "$current_path" ]]; then
|
||||||
|
print_error "Path does not exist: $current_path"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If this directory has BSON files, we're at the right level
|
||||||
|
if has_bson_files "$current_path"; then
|
||||||
|
SELECTED_DUMP_PATH="$current_path"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If no subdirectories and no BSON files, this isn't a valid dump directory
|
||||||
|
if ! has_subdirectories "$current_path"; then
|
||||||
|
print_error "No BSON files found and no subdirectories in: $current_path"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Navigate through subdirectories
|
||||||
|
while true; do
|
||||||
|
print_separator_up
|
||||||
|
print_info "Select Dump Directory"
|
||||||
|
print_separator_down
|
||||||
|
echo
|
||||||
|
print_info "Current location: $current_path"
|
||||||
|
echo
|
||||||
|
|
||||||
|
# Get list of subdirectories
|
||||||
|
local subdirs=()
|
||||||
|
local count=0
|
||||||
|
|
||||||
|
# Read subdirectories into array (null-terminated, excluding . and ..)
|
||||||
|
while IFS= read -r -d '' subdir; do
|
||||||
|
subdirs+=("$subdir")
|
||||||
|
echo " $((++count))) $(basename "$subdir")"
|
||||||
|
done < <(find "$current_path" -maxdepth 1 -mindepth 1 -type d -print0 | sort -z)
|
||||||
|
|
||||||
|
if [[ $count -eq 0 ]]; then
|
||||||
|
print_error "No subdirectories found in: $current_path"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo -n "Enter your choice (1-$count): "
|
||||||
|
read choice
|
||||||
|
|
||||||
|
if [[ "$choice" =~ ^[0-9]+$ ]] && [[ $choice -ge 1 ]] && [[ $choice -le $count ]]; then
|
||||||
|
current_path="${subdirs[$((choice - 1))]}"
|
||||||
|
|
||||||
|
# Check if this directory has BSON files
|
||||||
|
if has_bson_files "$current_path"; then
|
||||||
|
SELECTED_DUMP_PATH="$current_path"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If no more subdirectories and no BSON files, error
|
||||||
|
if ! has_subdirectories "$current_path"; then
|
||||||
|
print_error "No BSON files found in: $current_path"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
# Continue loop to show next level
|
||||||
|
else
|
||||||
|
print_error "Invalid choice"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
get_backup_summary() {
|
||||||
|
local dump_path="$1"
|
||||||
|
local bson_count=$(find "$dump_path" -name "*.bson" -type f 2>/dev/null | wc -l)
|
||||||
|
local total_size=$(du -sh "$dump_path" 2>/dev/null | cut -f1)
|
||||||
|
|
||||||
|
echo "Backup files: $bson_count"
|
||||||
|
echo "Total size: $total_size"
|
||||||
|
}
|
||||||
|
|
||||||
|
show_summary() {
|
||||||
|
local dump_path="$1"
|
||||||
|
|
||||||
|
print_section "Restore Summary"
|
||||||
|
|
||||||
|
echo "MongoDB Connection:"
|
||||||
|
echo " Host: $MONGO_HOST"
|
||||||
|
echo " Port: $MONGO_PORT"
|
||||||
|
echo " Username: $MONGO_USERNAME"
|
||||||
|
echo " Database: $MONGO_DATABASE"
|
||||||
|
echo " AuthSource: $MONGO_AUTHSOURCE"
|
||||||
|
echo
|
||||||
|
echo "Backup Source:"
|
||||||
|
echo " Location: $dump_path"
|
||||||
|
echo " $(get_backup_summary "$dump_path")"
|
||||||
|
echo
|
||||||
|
echo "Connection string:"
|
||||||
|
echo " mongodb://<username>:<password>@$MONGO_HOST:$MONGO_PORT/$MONGO_DATABASE?authSource=$MONGO_AUTHSOURCE"
|
||||||
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
|
confirm_restore() {
|
||||||
|
echo -n "Do you want to proceed with the restore? (yes/no): "
|
||||||
|
read response
|
||||||
|
[[ "$response" == "yes" ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
perform_restore() {
|
||||||
|
local dump_path="$1"
|
||||||
|
|
||||||
|
print_separator_up
|
||||||
|
print_info "Starting MongoDB Restore..."
|
||||||
|
print_separator_down
|
||||||
|
echo
|
||||||
|
|
||||||
|
# Construct connection string
|
||||||
|
local connection_uri="mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOST}:${MONGO_PORT}/${MONGO_DATABASE}?authSource=${MONGO_AUTHSOURCE}"
|
||||||
|
|
||||||
|
# Perform restore
|
||||||
|
if mongorestore --uri "$connection_uri" "$dump_path" 2>&1; then
|
||||||
|
print_success "Restore completed successfully"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
print_error "Restore operation failed"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
main() {
|
||||||
|
print_separator_up
|
||||||
|
print_info "MongoDB Restore Utility"
|
||||||
|
print_separator_down
|
||||||
|
|
||||||
|
# Initial checks
|
||||||
|
check_mongorestore
|
||||||
|
check_env_file
|
||||||
|
load_config
|
||||||
|
|
||||||
|
print_success "Configuration loaded"
|
||||||
|
echo
|
||||||
|
|
||||||
|
# Ask for initial dump path
|
||||||
|
print_info "Enter the path to the dumps directory"
|
||||||
|
echo " Example: ~/Works/dbdump/dumps"
|
||||||
|
echo
|
||||||
|
echo -n "Dumps path: "
|
||||||
|
read dumps_path
|
||||||
|
|
||||||
|
if [[ -z "$dumps_path" ]]; then
|
||||||
|
print_error "Dumps path cannot be empty"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Navigate to the correct dump directory
|
||||||
|
print_info "Navigating to backup location..."
|
||||||
|
navigate_to_dump "$dumps_path"
|
||||||
|
if [[ $? -ne 0 ]]; then
|
||||||
|
print_error "Failed to select backup directory"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Show summary of what will be restored
|
||||||
|
show_summary "$SELECTED_DUMP_PATH"
|
||||||
|
|
||||||
|
# Ask for confirmation
|
||||||
|
if ! confirm_restore; then
|
||||||
|
print_info "Restore operation cancelled"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Perform the restore
|
||||||
|
if perform_restore "$SELECTED_DUMP_PATH"; then
|
||||||
|
print_success "Restore operation completed"
|
||||||
|
else
|
||||||
|
print_error "Restore operation failed"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
main "$@"
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
# MongoDB Configuration Example
|
|
||||||
# Copy this file to .env and update with your values
|
|
||||||
# The script will prompt for these values on first run
|
|
||||||
|
|
||||||
MONGO_USERNAME=admin
|
|
||||||
MONGO_PASSWORD=password
|
|
||||||
MONGO_DATABASE=adastradb
|
|
||||||
MONGO_PORT=27017
|
|
||||||
MONGO_EXPRESS_PORT=8081
|
|
||||||
MONGO_EXPRESS_USERNAME=dev
|
|
||||||
MONGO_EXPRESS_PASSWORD=dev
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
# Environment variables
|
|
||||||
.env
|
|
||||||
*.env
|
|
||||||
|
|
||||||
# Docker volumes
|
|
||||||
mongodb-data/
|
|
||||||
mongo-config/
|
|
||||||
|
|
||||||
# OS files
|
|
||||||
.DS_Store
|
|
||||||
Thumbs.db
|
|
||||||
|
|
||||||
# Logs
|
|
||||||
*.log
|
|
||||||
@@ -1,154 +0,0 @@
|
|||||||
# MongoDB Database Utility (Standalone)
|
|
||||||
|
|
||||||
This directory contains a standalone MongoDB setup for local development. It is completely independent from the main backend and frontend projects.
|
|
||||||
|
|
||||||
## Quick Start
|
|
||||||
|
|
||||||
### First Run (Setup)
|
|
||||||
|
|
||||||
```bash
|
|
||||||
./db-start.sh up
|
|
||||||
```
|
|
||||||
|
|
||||||
The script will prompt you for:
|
|
||||||
- **MongoDB username** (default: `admin`)
|
|
||||||
- **MongoDB password** (default: `password`)
|
|
||||||
- **Database name** (default: `adastradb`)
|
|
||||||
- **MongoDB port** (default: `27017`)
|
|
||||||
- **Mongo Express port** (default: `8081`)
|
|
||||||
|
|
||||||
Your configuration will be stored in `.env` file (automatically git-ignored).
|
|
||||||
|
|
||||||
### Common Commands
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Start MongoDB (prompts for credentials on first run)
|
|
||||||
./db-start.sh up
|
|
||||||
|
|
||||||
# View logs
|
|
||||||
./db-start.sh logs
|
|
||||||
|
|
||||||
# Stop MongoDB
|
|
||||||
./db-start.sh down
|
|
||||||
|
|
||||||
# Restart MongoDB
|
|
||||||
./db-start.sh restart
|
|
||||||
|
|
||||||
# Check container status
|
|
||||||
./db-start.sh status
|
|
||||||
|
|
||||||
# Reconfigure credentials
|
|
||||||
./db-start.sh reconfigure
|
|
||||||
|
|
||||||
# Remove all containers and volumes (DATA LOSS!)
|
|
||||||
./db-start.sh clean
|
|
||||||
|
|
||||||
# Show help
|
|
||||||
./db-start.sh help
|
|
||||||
```
|
|
||||||
|
|
||||||
## Accessing MongoDB
|
|
||||||
|
|
||||||
### From Your Application
|
|
||||||
- **Connection String**: `mongodb://<username>:<password>@localhost:27017/<database>?authSource=admin`
|
|
||||||
- **Example**: `mongodb://admin:password@localhost:27017/adastradb?authSource=admin`
|
|
||||||
|
|
||||||
### Using Mongo Express (GUI)
|
|
||||||
- **URL**: http://localhost:8081
|
|
||||||
- **Username**: `dev`
|
|
||||||
- **Password**: `dev`
|
|
||||||
|
|
||||||
## Data Persistence
|
|
||||||
|
|
||||||
All data is stored in Docker volumes and persists across container restarts. To permanently delete data, use:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
./db-start.sh clean
|
|
||||||
```
|
|
||||||
|
|
||||||
## Collections
|
|
||||||
|
|
||||||
The database is automatically initialized with the following collection:
|
|
||||||
|
|
||||||
- **dataImportHistory**: Stores records of all data imports for audit and recovery tracking
|
|
||||||
- Indexed on `importDate` (descending) and `status`
|
|
||||||
- Ready for future data import functionality
|
|
||||||
|
|
||||||
## File Structure
|
|
||||||
|
|
||||||
```
|
|
||||||
dbutils/
|
|
||||||
├── db-start.sh # Main script
|
|
||||||
├── docker-compose-mongodb.yml # Docker Compose configuration
|
|
||||||
├── mongo-init.js # Database initialization script
|
|
||||||
├── .env # Configuration (generated on first run, git-ignored)
|
|
||||||
├── .env.example # Example configuration
|
|
||||||
├── .gitignore # Git ignore rules
|
|
||||||
└── README.md # This file
|
|
||||||
```
|
|
||||||
|
|
||||||
## Configuration
|
|
||||||
|
|
||||||
The script uses a `.env` file to store configuration. You can:
|
|
||||||
|
|
||||||
1. **Let the script generate it**: Run `./db-start.sh up` and answer the prompts
|
|
||||||
2. **Pre-create it**: Copy `.env.example` to `.env` and modify values
|
|
||||||
3. **Edit it manually**: Edit `.env` directly and run `./db-start.sh up`
|
|
||||||
4. **Reconfigure**: Run `./db-start.sh reconfigure` to re-run the setup wizard
|
|
||||||
|
|
||||||
## Requirements
|
|
||||||
|
|
||||||
- Docker
|
|
||||||
- Docker Compose (included with Docker Desktop)
|
|
||||||
- Bash shell
|
|
||||||
|
|
||||||
## Standalone Usage
|
|
||||||
|
|
||||||
This utility can be used independently from AdAstra projects:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# From anywhere
|
|
||||||
cd /path/to/dbutils
|
|
||||||
./db-start.sh up
|
|
||||||
|
|
||||||
# Or add to PATH and use globally (optional)
|
|
||||||
export PATH=$PATH:/path/to/dbutils
|
|
||||||
db-start.sh up
|
|
||||||
```
|
|
||||||
|
|
||||||
## Troubleshooting
|
|
||||||
|
|
||||||
### Port already in use
|
|
||||||
If MongoDB or Mongo Express port is already in use:
|
|
||||||
1. Run `./db-start.sh reconfigure`
|
|
||||||
2. Choose a different port (e.g., 27018 for MongoDB, 8082 for Mongo Express)
|
|
||||||
|
|
||||||
### Docker not running
|
|
||||||
```bash
|
|
||||||
# Check Docker status
|
|
||||||
docker info
|
|
||||||
```
|
|
||||||
|
|
||||||
### Permission denied
|
|
||||||
```bash
|
|
||||||
# Make script executable
|
|
||||||
chmod +x db-start.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
## Notes
|
|
||||||
|
|
||||||
- Credentials are stored in `.env` (git-ignored for security)
|
|
||||||
- Mongo Express is included for easy database management
|
|
||||||
- Data persists in Docker volumes
|
|
||||||
- No initialization scripts are run (unlike the backend docker-compose)
|
|
||||||
- This is standalone and independent from the main project configuration
|
|
||||||
|
|
||||||
## About
|
|
||||||
|
|
||||||
**Author:** Julien Gautier ([jgautier.webdev@gmail.com](mailto:jgautier.webdev@gmail.com))
|
|
||||||
|
|
||||||
**Organization:** AdAstra
|
|
||||||
|
|
||||||
**Project:** adastra_scripts
|
|
||||||
|
|
||||||
These utilities were created to simplify MongoDB management during local development of AdAstra projects.
|
|
||||||
@@ -1,412 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Author: Julien Gautier <jgautier.webdev@gmail.com>
|
|
||||||
# Organization: AdAstra
|
|
||||||
# Project: adastra_scripts
|
|
||||||
#
|
|
||||||
################################################################################
|
|
||||||
# MongoDB Database Utility Script (Standalone)
|
|
||||||
#
|
|
||||||
# This script manages a standalone MongoDB instance for local development.
|
|
||||||
# It runs independently and requires no configuration from the backend project.
|
|
||||||
#
|
|
||||||
# Usage: ./db-start.sh <command>
|
|
||||||
#
|
|
||||||
# Commands:
|
|
||||||
# up Start MongoDB and Mongo Express (interactive setup)
|
|
||||||
# down Stop MongoDB and Mongo Express
|
|
||||||
# logs Show database logs (follow mode)
|
|
||||||
# restart Restart MongoDB and Mongo Express
|
|
||||||
# status Show container status
|
|
||||||
# clean Remove database containers and volumes
|
|
||||||
# help Show this help message
|
|
||||||
#
|
|
||||||
# Configuration:
|
|
||||||
# The script will prompt for MongoDB credentials on first run.
|
|
||||||
# Configuration is stored in .env file.
|
|
||||||
#
|
|
||||||
# Examples:
|
|
||||||
# ./db-start.sh up
|
|
||||||
# ./db-start.sh logs
|
|
||||||
# ./db-start.sh down
|
|
||||||
#
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
# Configuration
|
|
||||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
||||||
COMPOSE_FILE="${SCRIPT_DIR}/docker-compose-mongodb.yml"
|
|
||||||
ENV_FILE="${SCRIPT_DIR}/.env"
|
|
||||||
PROJECT_NAME="dbutils"
|
|
||||||
|
|
||||||
# Colors
|
|
||||||
RED='\033[0;31m'
|
|
||||||
GREEN='\033[0;32m'
|
|
||||||
YELLOW='\033[1;33m'
|
|
||||||
BLUE='\033[0;34m'
|
|
||||||
CYAN='\033[0;36m'
|
|
||||||
NC='\033[0m'
|
|
||||||
|
|
||||||
# Helper functions
|
|
||||||
print_info() {
|
|
||||||
echo -e "${BLUE}┣━${NC} ${CYAN}${1}${NC}"
|
|
||||||
}
|
|
||||||
|
|
||||||
print_success() {
|
|
||||||
echo -e "${GREEN}✓ ${1}${NC}"
|
|
||||||
}
|
|
||||||
|
|
||||||
print_error() {
|
|
||||||
echo -e "${RED}✗ ${1}${NC}"
|
|
||||||
}
|
|
||||||
|
|
||||||
print_warning() {
|
|
||||||
echo -e "${YELLOW}⚠ ${1}${NC}"
|
|
||||||
}
|
|
||||||
|
|
||||||
print_separator() {
|
|
||||||
echo -e "${BLUE}┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
|
||||||
}
|
|
||||||
|
|
||||||
print_separator_up() {
|
|
||||||
echo -e "${BLUE}┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
|
||||||
}
|
|
||||||
|
|
||||||
print_separator_down() {
|
|
||||||
echo -e "${BLUE}┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
|
|
||||||
}
|
|
||||||
|
|
||||||
check_docker() {
|
|
||||||
if ! command -v docker &> /dev/null; then
|
|
||||||
print_error "Docker is not installed or not in PATH"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
print_success "Docker found: $(docker --version)"
|
|
||||||
}
|
|
||||||
|
|
||||||
check_docker_running() {
|
|
||||||
if ! docker info > /dev/null 2>&1; then
|
|
||||||
print_error "Docker daemon is not running"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
print_success "Docker daemon is running"
|
|
||||||
}
|
|
||||||
|
|
||||||
check_docker_compose() {
|
|
||||||
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null 2>&1; then
|
|
||||||
print_error "Docker Compose is not installed or not in PATH"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
print_success "Docker Compose is available"
|
|
||||||
}
|
|
||||||
|
|
||||||
check_compose_file() {
|
|
||||||
if [[ ! -f "$COMPOSE_FILE" ]]; then
|
|
||||||
print_error "Docker Compose file not found at $COMPOSE_FILE"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
get_compose_cmd() {
|
|
||||||
if command -v docker-compose &> /dev/null; then
|
|
||||||
echo "docker-compose"
|
|
||||||
else
|
|
||||||
echo "docker compose"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
setup_credentials() {
|
|
||||||
print_separator_up
|
|
||||||
print_info "MongoDB Configuration Setup"
|
|
||||||
print_separator_down
|
|
||||||
echo
|
|
||||||
|
|
||||||
# Read username
|
|
||||||
read -p "Enter MongoDB root username (default: admin): " mongo_username
|
|
||||||
mongo_username="${mongo_username:-admin}"
|
|
||||||
|
|
||||||
# Read password
|
|
||||||
read -sp "Enter MongoDB root password (default: password): " mongo_password
|
|
||||||
echo
|
|
||||||
mongo_password="${mongo_password:-password}"
|
|
||||||
|
|
||||||
# Read database name
|
|
||||||
read -p "Enter MongoDB database name (default: adastradb): " mongo_database
|
|
||||||
mongo_database="${mongo_database:-adastradb}"
|
|
||||||
|
|
||||||
echo
|
|
||||||
print_info "MongoDB port (default: 27017): "
|
|
||||||
read -p "Enter MongoDB port or press Enter for default: " mongo_port
|
|
||||||
mongo_port="${mongo_port:-27017}"
|
|
||||||
|
|
||||||
print_info "Mongo Express port (default: 8081): "
|
|
||||||
read -p "Enter Mongo Express port or press Enter for default: " mongo_express_port
|
|
||||||
mongo_express_port="${mongo_express_port:-8081}"
|
|
||||||
|
|
||||||
echo
|
|
||||||
|
|
||||||
# Create .env file
|
|
||||||
cat > "$ENV_FILE" << EOF
|
|
||||||
# MongoDB Configuration
|
|
||||||
MONGO_USERNAME=${mongo_username}
|
|
||||||
MONGO_PASSWORD=${mongo_password}
|
|
||||||
MONGO_DATABASE=${mongo_database}
|
|
||||||
MONGO_PORT=${mongo_port}
|
|
||||||
MONGO_EXPRESS_PORT=${mongo_express_port}
|
|
||||||
MONGO_EXPRESS_USERNAME=dev
|
|
||||||
MONGO_EXPRESS_PASSWORD=dev
|
|
||||||
EOF
|
|
||||||
|
|
||||||
print_success "Configuration saved to .env"
|
|
||||||
echo
|
|
||||||
}
|
|
||||||
|
|
||||||
load_credentials() {
|
|
||||||
if [[ -f "$ENV_FILE" ]]; then
|
|
||||||
print_info "Loading configuration from .env"
|
|
||||||
# shellcheck source=/dev/null
|
|
||||||
source "$ENV_FILE"
|
|
||||||
else
|
|
||||||
print_warning "No configuration found. Running setup..."
|
|
||||||
setup_credentials
|
|
||||||
# shellcheck source=/dev/null
|
|
||||||
source "$ENV_FILE"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_up() {
|
|
||||||
print_separator_up
|
|
||||||
print_info "Starting MongoDB Database..."
|
|
||||||
print_separator_down
|
|
||||||
echo
|
|
||||||
|
|
||||||
check_docker
|
|
||||||
check_docker_running
|
|
||||||
check_compose_file
|
|
||||||
check_docker_compose
|
|
||||||
|
|
||||||
load_credentials
|
|
||||||
|
|
||||||
COMPOSE_CMD=$(get_compose_cmd)
|
|
||||||
|
|
||||||
# Export variables for docker-compose
|
|
||||||
export MONGO_USERNAME
|
|
||||||
export MONGO_PASSWORD
|
|
||||||
export MONGO_DATABASE
|
|
||||||
export MONGO_PORT
|
|
||||||
export MONGO_EXPRESS_PORT
|
|
||||||
export MONGO_EXPRESS_USERNAME
|
|
||||||
export MONGO_EXPRESS_PASSWORD
|
|
||||||
|
|
||||||
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" up -d
|
|
||||||
|
|
||||||
print_success "MongoDB and Mongo Express have been started"
|
|
||||||
print_separator_up
|
|
||||||
print_info "MongoDB Connection Details:"
|
|
||||||
print_info " Host: localhost:${MONGO_PORT}"
|
|
||||||
print_info " Username: ${MONGO_USERNAME}"
|
|
||||||
print_info " Password: ${MONGO_PASSWORD}"
|
|
||||||
print_info " Database: ${MONGO_DATABASE}"
|
|
||||||
print_separator
|
|
||||||
print_info "Mongo Express (GUI) available at: http://localhost:${MONGO_EXPRESS_PORT}"
|
|
||||||
print_info " Username: ${MONGO_EXPRESS_USERNAME}"
|
|
||||||
print_info " Password: ${MONGO_EXPRESS_PASSWORD}"
|
|
||||||
print_separator_down
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_down() {
|
|
||||||
print_separator_up
|
|
||||||
print_info "Stopping MongoDB and Mongo Express..."
|
|
||||||
print_separator_down
|
|
||||||
|
|
||||||
check_docker
|
|
||||||
check_compose_file
|
|
||||||
check_docker_compose
|
|
||||||
|
|
||||||
COMPOSE_CMD=$(get_compose_cmd)
|
|
||||||
|
|
||||||
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" down
|
|
||||||
|
|
||||||
print_success "MongoDB and Mongo Express have been stopped"
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_logs() {
|
|
||||||
print_separator_up
|
|
||||||
print_info "Showing MongoDB logs..."
|
|
||||||
print_separator_down
|
|
||||||
|
|
||||||
check_docker
|
|
||||||
check_compose_file
|
|
||||||
check_docker_compose
|
|
||||||
|
|
||||||
COMPOSE_CMD=$(get_compose_cmd)
|
|
||||||
|
|
||||||
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" logs -f
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_restart() {
|
|
||||||
print_separator_up
|
|
||||||
print_info "Restarting MongoDB and Mongo Express..."
|
|
||||||
print_separator_down
|
|
||||||
|
|
||||||
check_docker
|
|
||||||
check_docker_running
|
|
||||||
check_compose_file
|
|
||||||
check_docker_compose
|
|
||||||
|
|
||||||
load_credentials
|
|
||||||
|
|
||||||
COMPOSE_CMD=$(get_compose_cmd)
|
|
||||||
|
|
||||||
# Export variables for docker-compose
|
|
||||||
export MONGO_USERNAME
|
|
||||||
export MONGO_PASSWORD
|
|
||||||
export MONGO_DATABASE
|
|
||||||
export MONGO_PORT
|
|
||||||
export MONGO_EXPRESS_PORT
|
|
||||||
export MONGO_EXPRESS_USERNAME
|
|
||||||
export MONGO_EXPRESS_PASSWORD
|
|
||||||
|
|
||||||
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" restart
|
|
||||||
|
|
||||||
print_success "MongoDB and Mongo Express have been restarted"
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_status() {
|
|
||||||
print_separator_up
|
|
||||||
print_info "Container Status"
|
|
||||||
print_separator_down
|
|
||||||
|
|
||||||
check_docker
|
|
||||||
check_compose_file
|
|
||||||
check_docker_compose
|
|
||||||
|
|
||||||
COMPOSE_CMD=$(get_compose_cmd)
|
|
||||||
|
|
||||||
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" ps
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_clean() {
|
|
||||||
print_separator_up
|
|
||||||
print_warning "This will remove database containers and volumes"
|
|
||||||
print_warning "All data will be PERMANENTLY LOST!"
|
|
||||||
echo
|
|
||||||
read -p "Are you sure? Type 'yes' to confirm: " response
|
|
||||||
echo
|
|
||||||
|
|
||||||
if [[ "$response" == "yes" ]]; then
|
|
||||||
print_separator
|
|
||||||
print_info "Removing containers and volumes..."
|
|
||||||
print_separator
|
|
||||||
|
|
||||||
check_docker
|
|
||||||
check_compose_file
|
|
||||||
check_docker_compose
|
|
||||||
|
|
||||||
COMPOSE_CMD=$(get_compose_cmd)
|
|
||||||
|
|
||||||
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" down -v --remove-orphans
|
|
||||||
|
|
||||||
print_success "All containers and volumes have been removed"
|
|
||||||
print_info "Configuration file (.env) has been preserved"
|
|
||||||
else
|
|
||||||
print_info "Clean operation cancelled"
|
|
||||||
fi
|
|
||||||
print_separator_down
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_reconfigure() {
|
|
||||||
print_separator_up
|
|
||||||
print_warning "This will reset the MongoDB configuration"
|
|
||||||
echo
|
|
||||||
read -p "Are you sure? Type 'yes' to confirm: " response
|
|
||||||
echo
|
|
||||||
|
|
||||||
if [[ "$response" == "yes" ]]; then
|
|
||||||
print_info "Stopping services before reconfiguration..."
|
|
||||||
check_docker 2>/dev/null || true
|
|
||||||
check_compose_file 2>/dev/null || true
|
|
||||||
|
|
||||||
COMPOSE_CMD=$(get_compose_cmd)
|
|
||||||
if command -v $COMPOSE_CMD &> /dev/null; then
|
|
||||||
$COMPOSE_CMD -f "$COMPOSE_FILE" -p "$PROJECT_NAME" down 2>/dev/null || true
|
|
||||||
fi
|
|
||||||
|
|
||||||
rm -f "$ENV_FILE"
|
|
||||||
print_success "Configuration cleared"
|
|
||||||
|
|
||||||
setup_credentials
|
|
||||||
print_info "You can now run './db-start.sh up' to start with the new configuration"
|
|
||||||
else
|
|
||||||
print_info "Reconfiguration cancelled"
|
|
||||||
fi
|
|
||||||
print_separator_down
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_help() {
|
|
||||||
cat << EOF
|
|
||||||
|
|
||||||
MongoDB Database Utility Script (Standalone)
|
|
||||||
|
|
||||||
Usage: ./db-start.sh <command>
|
|
||||||
|
|
||||||
Commands:
|
|
||||||
up Start MongoDB and Mongo Express (interactive setup on first run)
|
|
||||||
down Stop MongoDB and Mongo Express
|
|
||||||
logs Show database and container logs (follow mode)
|
|
||||||
restart Restart MongoDB and Mongo Express
|
|
||||||
status Show container status
|
|
||||||
reconfigure Reset and reconfigure MongoDB credentials
|
|
||||||
clean Remove all containers and volumes (DATA LOSS!)
|
|
||||||
help Show this help message
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
./db-start.sh up
|
|
||||||
./db-start.sh logs
|
|
||||||
./db-start.sh down
|
|
||||||
./db-start.sh reconfigure
|
|
||||||
|
|
||||||
Configuration:
|
|
||||||
On first run, 'up' command will prompt for MongoDB credentials.
|
|
||||||
Configuration is stored in .env file (git-ignored).
|
|
||||||
You can reconfigure anytime with 'reconfigure' command.
|
|
||||||
|
|
||||||
EOF
|
|
||||||
}
|
|
||||||
|
|
||||||
# Main
|
|
||||||
COMMAND="${1:-help}"
|
|
||||||
|
|
||||||
case "$COMMAND" in
|
|
||||||
up|start)
|
|
||||||
cmd_up
|
|
||||||
;;
|
|
||||||
down|stop)
|
|
||||||
cmd_down
|
|
||||||
;;
|
|
||||||
logs)
|
|
||||||
cmd_logs
|
|
||||||
;;
|
|
||||||
restart)
|
|
||||||
cmd_restart
|
|
||||||
;;
|
|
||||||
status|ps)
|
|
||||||
cmd_status
|
|
||||||
;;
|
|
||||||
reconfigure)
|
|
||||||
cmd_reconfigure
|
|
||||||
;;
|
|
||||||
clean|remove)
|
|
||||||
cmd_clean
|
|
||||||
;;
|
|
||||||
help|--help|-h)
|
|
||||||
cmd_help
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
print_error "Unknown command: $COMMAND"
|
|
||||||
echo
|
|
||||||
cmd_help
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
services:
|
|
||||||
mongodb:
|
|
||||||
image: mongo:latest
|
|
||||||
container_name: dbutils-mongodb
|
|
||||||
ports:
|
|
||||||
- "${MONGO_PORT:-27017}:27017"
|
|
||||||
environment:
|
|
||||||
MONGO_INITDB_ROOT_USERNAME: ${MONGO_USERNAME}
|
|
||||||
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}
|
|
||||||
MONGO_INITDB_DATABASE: ${MONGO_DATABASE}
|
|
||||||
volumes:
|
|
||||||
- mongodb-data:/data/db
|
|
||||||
- mongodb-config:/data/configdb
|
|
||||||
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js
|
|
||||||
networks:
|
|
||||||
- dbutils-network
|
|
||||||
restart: unless-stopped
|
|
||||||
healthcheck:
|
|
||||||
test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/test --quiet
|
|
||||||
interval: 10s
|
|
||||||
timeout: 5s
|
|
||||||
retries: 5
|
|
||||||
start_period: 40s
|
|
||||||
|
|
||||||
mongo-express:
|
|
||||||
image: mongo-express:latest
|
|
||||||
container_name: dbutils-mongo-express
|
|
||||||
ports:
|
|
||||||
- "${MONGO_EXPRESS_PORT:-8081}:8081"
|
|
||||||
environment:
|
|
||||||
ME_CONFIG_MONGODB_URL: mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@mongodb:27017/?authSource=admin
|
|
||||||
ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_USERNAME}
|
|
||||||
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_PASSWORD}
|
|
||||||
ME_CONFIG_BASICAUTH_USERNAME: ${MONGO_EXPRESS_USERNAME:-dev}
|
|
||||||
ME_CONFIG_BASICAUTH_PASSWORD: ${MONGO_EXPRESS_PASSWORD:-dev}
|
|
||||||
depends_on:
|
|
||||||
- mongodb
|
|
||||||
networks:
|
|
||||||
- dbutils-network
|
|
||||||
restart: unless-stopped
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
mongodb-data:
|
|
||||||
mongodb-config:
|
|
||||||
|
|
||||||
networks:
|
|
||||||
dbutils-network:
|
|
||||||
driver: bridge
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
// Initialize MongoDB database
|
|
||||||
|
|
||||||
// Switch to the application database
|
|
||||||
db = db.getSiblingDB(process.env.MONGO_INITDB_DATABASE || 'adastradb');
|
|
||||||
|
|
||||||
// Create a collection for data import history
|
|
||||||
// This collection will track all data imports for audit and recovery purposes
|
|
||||||
db.createCollection('dataImportHistory', {
|
|
||||||
capped: false
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create an index on the importDate for better query performance
|
|
||||||
db.dataImportHistory.createIndex({ importDate: -1 });
|
|
||||||
db.dataImportHistory.createIndex({ status: 1 });
|
|
||||||
|
|
||||||
print(`✓ Database '${db.getName()}' initialized successfully`);
|
|
||||||
print(`✓ Collection 'dataImportHistory' created`);
|
|
||||||
Reference in New Issue
Block a user