Compare commits
3 Commits
f804b7abad
...
dirsync
| Author | SHA1 | Date | |
|---|---|---|---|
| 52f28d517b | |||
| ff6c2205d1 | |||
| 8949a33481 |
@@ -0,0 +1,64 @@
|
|||||||
|
# CLAUDE.md
|
||||||
|
|
||||||
|
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||||
|
|
||||||
|
## Project Overview
|
||||||
|
|
||||||
|
A collection of standalone Bash utility scripts for MongoDB operations (backup, restore, local dev environment). No build system, package manager, or test suite — each module is a self-contained shell script.
|
||||||
|
|
||||||
|
## Modules
|
||||||
|
|
||||||
|
| Module | Script | Purpose |
|
||||||
|
|---|---|---|
|
||||||
|
| `dbdump_mongo/` | `db-dump.sh` | Dump MongoDB databases across multiple named environments |
|
||||||
|
| `dbrestore_mongo/` | `db-restore.sh` | Restore a MongoDB dump to a target instance |
|
||||||
|
| `dbutils_mongo/` | `db-start.sh` | Spin up a local MongoDB + Mongo Express via Docker |
|
||||||
|
|
||||||
|
## Running the Scripts
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Backup — interactive environment picker
|
||||||
|
cd dbdump_mongo && bash db-dump.sh
|
||||||
|
|
||||||
|
# Restore — interactive backup directory navigator
|
||||||
|
cd dbrestore_mongo && bash db-restore.sh
|
||||||
|
|
||||||
|
# Local dev database — Docker-based
|
||||||
|
cd dbutils_mongo && bash db-start.sh up # start
|
||||||
|
cd dbutils_mongo && bash db-start.sh down # stop
|
||||||
|
cd dbutils_mongo && bash db-start.sh status
|
||||||
|
cd dbutils_mongo && bash db-start.sh logs
|
||||||
|
cd dbutils_mongo && bash db-start.sh clean # remove volumes
|
||||||
|
cd dbutils_mongo && bash db-start.sh reconfigure
|
||||||
|
```
|
||||||
|
|
||||||
|
## Environment Setup
|
||||||
|
|
||||||
|
Each module requires a `.env` file (copied from the corresponding `.env.example`). The `.env` files are git-ignored. `dbdump_mongo` supports multiple named environments stored as `.env.<name>` files with `chmod 600` permissions.
|
||||||
|
|
||||||
|
## Architecture Patterns
|
||||||
|
|
||||||
|
All three scripts share the same conventions:
|
||||||
|
|
||||||
|
- **Sub-command dispatch**: a `main()` function routes `$1` to `cmd_<action>()` functions.
|
||||||
|
- **Colored output**: ANSI escape codes (`\033[...m`) for styled terminal output; consistent separator lines.
|
||||||
|
- **Prerequisite checks**: each script validates its required tools (`mongodump`, `mongorestore`, `docker`, `docker compose`) at startup and exits early with a clear message if missing.
|
||||||
|
- **Interactive prompts**: `read -p` / `select` menus guide the user through environment selection, directory navigation, and confirmation steps before any destructive action.
|
||||||
|
|
||||||
|
### dbdump_mongo — multi-environment credential model
|
||||||
|
|
||||||
|
Named environments are stored as `.env.<name>` files alongside the main `.env`. Credentials include `MONGO_USERNAME`, `MONGO_PASSWORD`, `MONGO_HOST`, `MONGO_PORT`, `APP_NAME`. Dumps land in `./dumps/<appName>/<yyyymmdd_hhmm>/`. The script generates MongoDB Atlas-compatible connection strings (ssl, replicaSet flags).
|
||||||
|
|
||||||
|
### dbrestore_mongo — backup directory navigation
|
||||||
|
|
||||||
|
The script walks through backup directory hierarchies interactively. It detects valid restore targets by checking for `.bson` files (`has_bson_files()`). Before executing `mongorestore`, it shows a summary of connection details and the selected backup path and requires explicit confirmation.
|
||||||
|
|
||||||
|
### dbutils_mongo — Docker Compose orchestration
|
||||||
|
|
||||||
|
Runs two containers on a `dbutils-network` bridge: MongoDB (configurable port, default `MONGO_PORT`) and Mongo Express (default port `8081`). Data is persisted in named volumes `mongodb-data` and `mongodb-config`. On first run, `setup_credentials()` prompts interactively and writes `.env`. The `mongo-init.js` initializer creates a `dataImportHistory` collection with indexes on `importDate` and `status`.
|
||||||
|
|
||||||
|
## External Dependencies
|
||||||
|
|
||||||
|
- `mongodump` / `mongorestore` — MongoDB Database Tools (must be installed separately)
|
||||||
|
- `docker` + `docker compose` (v2 plugin syntax)
|
||||||
|
- `bash` (scripts use `bash`-specific features, not POSIX `sh`)
|
||||||
@@ -1,3 +1,38 @@
|
|||||||
# adastra_scripts
|
# AdAstra Scripts
|
||||||
|
|
||||||
AdAstra Utility Scripts
|
A collection of utility scripts for AdAstra projects. Each script is self-contained and can be used independently.
|
||||||
|
|
||||||
|
## Scripts
|
||||||
|
|
||||||
|
### MongoDB
|
||||||
|
|
||||||
|
| Script | Description |
|
||||||
|
|---|---|
|
||||||
|
| [`dbdump_mongo/`](dbdump_mongo/) | Backup MongoDB databases with multi-environment support (prod, uat, staging, …) |
|
||||||
|
| [`dbrestore_mongo/`](dbrestore_mongo/) | Restore a MongoDB backup with interactive directory navigation |
|
||||||
|
| [`dbutils_mongo/`](dbutils_mongo/) | Spin up a local MongoDB + Mongo Express instance via Docker |
|
||||||
|
|
||||||
|
#### Typical MongoDB workflow
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. Backup from a remote MongoDB
|
||||||
|
cd dbdump_mongo && ./db-dump.sh dump
|
||||||
|
|
||||||
|
# 2. Start a local MongoDB instance
|
||||||
|
cd dbutils_mongo && ./db-start.sh up
|
||||||
|
|
||||||
|
# 3. Restore the backup locally
|
||||||
|
cd dbrestore_mongo && ./db-restore.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
Each script lists its own requirements in its README. Common dependencies:
|
||||||
|
|
||||||
|
- **Bash**
|
||||||
|
- **MongoDB Database Tools** (`mongodump` / `mongorestore`) — [download](https://www.mongodb.com/try/download/database-tools)
|
||||||
|
- **Docker & Docker Compose** (for `dbutils_mongo` only)
|
||||||
|
|
||||||
|
## Author
|
||||||
|
|
||||||
|
Julien Gautier — AdAstra
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
# Directory Sync Utility
|
||||||
|
|
||||||
|
This directory contains a tool for syncing a local directory to a remote host in real time.
|
||||||
|
It watches for file changes and transfers them immediately via SCP, making it useful for
|
||||||
|
live development on a remote server.
|
||||||
|
|
||||||
|
## Scripts
|
||||||
|
|
||||||
|
| Script | Description |
|
||||||
|
|---|---|
|
||||||
|
| `dir-sync.sh` | Improved version — recommended |
|
||||||
|
| `scpsync.sh` | Original version |
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./dir-sync.sh -s /path/to/local/dir/ -d /path/to/remote/dir/ -r sshhost
|
||||||
|
```
|
||||||
|
|
||||||
|
The script starts watching the local directory immediately. Press **Ctrl+C** to stop —
|
||||||
|
it will reset the remote repository (`git reset --hard`) and clear the remote cache before exiting.
|
||||||
|
|
||||||
|
## Options
|
||||||
|
|
||||||
|
| Option | Description |
|
||||||
|
|---|---|
|
||||||
|
| `-s, --source <path>` | Local directory absolute path |
|
||||||
|
| `-d, --dest <path>` | Remote directory absolute path |
|
||||||
|
| `-r, --remote-host <h>` | Remote SSH host (as configured in `~/.ssh/config`) |
|
||||||
|
| `-h, --help` | Print help |
|
||||||
|
|
||||||
|
If an option is omitted, the script falls back to the default values defined at the top of the script.
|
||||||
|
|
||||||
|
## How It Works
|
||||||
|
|
||||||
|
1. **File watching**: `fswatch` monitors the source directory for any change, excluding `.git/`
|
||||||
|
2. **Batch end marker**: `fswatch --batch-marker` emits a `NoOp` signal at the end of each batch
|
||||||
|
3. **File sync**: each changed file is transferred with `scp`, preserving the relative path under the destination directory
|
||||||
|
4. **Cache clear**: on each `NoOp` (end of batch), `composer front-cache-clear` is run on the remote
|
||||||
|
5. **Cleanup on exit**: Ctrl+C triggers `git reset --hard` and a final cache clear on the remote
|
||||||
|
|
||||||
|
## Default Configuration
|
||||||
|
|
||||||
|
Edit the three constants at the top of `dir-sync.sh` to set your own defaults:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
DEFAULT_LOCAL_PATH='/path/to/local/dir/'
|
||||||
|
DEFAULT_REMOTE_PATH='/path/to/remote/dir/'
|
||||||
|
DEFAULT_HOST='devac'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- [`fswatch`](https://github.com/emcrisostomo/fswatch) — file system watcher
|
||||||
|
- `scp` and `ssh` — standard OpenSSH tools
|
||||||
|
- A remote host configured in `~/.ssh/config` (password-less key auth recommended)
|
||||||
|
- `composer` available on the remote host (for cache clearing)
|
||||||
|
|
||||||
|
### Installing fswatch
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# macOS
|
||||||
|
brew install fswatch
|
||||||
|
|
||||||
|
# Linux (Ubuntu/Debian)
|
||||||
|
sudo apt install fswatch
|
||||||
|
```
|
||||||
|
|
||||||
|
## About
|
||||||
|
|
||||||
|
**Author:** Julien Gautier ([jgautier.webdev@gmail.com](mailto:jgautier.webdev@gmail.com))
|
||||||
|
|
||||||
|
**Organization:** AdAstra
|
||||||
|
|
||||||
|
**Project:** adastra_scripts
|
||||||
Executable
+160
@@ -0,0 +1,160 @@
|
|||||||
|
#!/bin/zsh
|
||||||
|
|
||||||
|
RETVAL=0
|
||||||
|
|
||||||
|
# CONF
|
||||||
|
defaultLocalPath='/path/to/local/dir/'
|
||||||
|
defaultRemotePath='/path/to/remote/dir/'
|
||||||
|
defaultHost='devac'
|
||||||
|
|
||||||
|
NONE='\033[0m' # none
|
||||||
|
GREY='\033[2m' # grey
|
||||||
|
BOLD='\033[1m' # bold
|
||||||
|
|
||||||
|
GREEN='\033[0;32m' # green
|
||||||
|
YELLOW='\033[0;33m' # yellow
|
||||||
|
RED='\033[0;31m' # red
|
||||||
|
BLUE='\033[0;34m' # blue
|
||||||
|
CYAN='\033[0;36m' # cyan
|
||||||
|
MAGENTA='\033[0;35m' # magenta
|
||||||
|
BLACK='\033[0;30m' # black
|
||||||
|
WHITE='\033[0;37m' # white
|
||||||
|
|
||||||
|
BBLUE='\033[1;94m' # blue bold
|
||||||
|
|
||||||
|
POSITIONAL=()
|
||||||
|
|
||||||
|
clear
|
||||||
|
|
||||||
|
echo -e "${NONE}${BBLUE}🔎 Lancement du script $0 ${NONE}${GREY}"
|
||||||
|
echo -e "${NONE}${BLUE}-------------------------------${NONE}${GREY}"
|
||||||
|
|
||||||
|
Usage()
|
||||||
|
{
|
||||||
|
echo -e "${BOLD}NAME${NONE}"
|
||||||
|
echo " scpsync - Synchronize local folder to a remote host"
|
||||||
|
echo " "
|
||||||
|
echo -e "${BOLD}SYNOPSIS${NONE}"
|
||||||
|
echo " sh $0 [OPTION]..."
|
||||||
|
echo " "
|
||||||
|
echo -e "${BOLD}DESCRIPTION${NONE}"
|
||||||
|
echo " -h, --help"
|
||||||
|
echo " "
|
||||||
|
echo " print this help"
|
||||||
|
echo " "
|
||||||
|
echo " -d, --dest"
|
||||||
|
echo " "
|
||||||
|
echo " remote directory absolut path"
|
||||||
|
echo " "
|
||||||
|
echo " -s, --source"
|
||||||
|
echo " "
|
||||||
|
echo " source directory absolut path"
|
||||||
|
echo " "
|
||||||
|
echo " -r, --remote-host"
|
||||||
|
echo " remote ssh host"
|
||||||
|
echo " "
|
||||||
|
echo -e "${RED} 🐞${NONE} Usage: $0 -s /path/to/local/dir/ -d /path/to/remote/dir/ -r sshhost"
|
||||||
|
}
|
||||||
|
|
||||||
|
while [[ $# -gt 0 ]]
|
||||||
|
do
|
||||||
|
key="$1"
|
||||||
|
case $key in
|
||||||
|
-h|--help)
|
||||||
|
Usage
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-d|--dest)
|
||||||
|
dirRemotePath=$2
|
||||||
|
shift # past argument
|
||||||
|
shift # past value
|
||||||
|
;;
|
||||||
|
-s|--source)
|
||||||
|
dirLocalPath=$2
|
||||||
|
shift
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-r|--remote-host)
|
||||||
|
host=$2
|
||||||
|
shift
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
# unknown option
|
||||||
|
POSITIONAL+=("$1") # save it in an array for later
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
set -- "${POSITIONAL[@]}"
|
||||||
|
|
||||||
|
if test -z "$dirLocalPath"; then
|
||||||
|
dirLocalPath=$defaultLocalPath
|
||||||
|
echo -e "${YELLOW}⚠ Local path:${NONE} ${dirLocalPath}${YELLOW} (default)${NONE}"
|
||||||
|
else
|
||||||
|
echo -e "${GREEN}⚙️ Local path:${NONE} ${dirLocalPath}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if test -z "$dirRemotePath"; then
|
||||||
|
dirRemotePath=$defaultRemotePath
|
||||||
|
echo -e "${YELLOW}⚠ Remote path:${NONE} ${dirRemotePath}${YELLOW} (default)${NONE}"
|
||||||
|
else
|
||||||
|
echo -e "${GREEN}⚙️ Remote path:${NONE} ${dirRemotePath}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$host" ]; then
|
||||||
|
host=$defaultHost
|
||||||
|
echo -e "${YELLOW}⚠ Remote host:${NONE} ${host}${YELLOW} (default)${NONE}"
|
||||||
|
else
|
||||||
|
echo -e "${GREEN}⚙️ Remote host:${NONE} ${host}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $RETVAL -eq 0 ]; then
|
||||||
|
trap exit INT
|
||||||
|
function exit() {
|
||||||
|
echo -e ""
|
||||||
|
echo -e "${NONE}${CYAN}♻️ Réinitialisation du répo distant...${NONE}${GREY}"
|
||||||
|
echo -e "${NONE}\$> ${MAGENTA}ssh ${host} \"cd ${dirRemotePath}; git reset --hard\"${NONE}"
|
||||||
|
ssh $host "cd $dirRemotePath; git reset --hard"
|
||||||
|
|
||||||
|
echo -e "\n${NONE}${CYAN}🗑️ Suppression du cache${NONE}${GREY}"
|
||||||
|
echo -e "${NONE}\$> ${MAGENTA}ssh ${host} \"cd ${dirRemotePath}; composer front-cache-clear\"${NONE}"
|
||||||
|
ssh $host "cd $dirRemotePath; composer front-cache-clear"
|
||||||
|
|
||||||
|
echo -e "${NONE}${GREEN}✔️ Ciao !${NONE}"
|
||||||
|
}
|
||||||
|
|
||||||
|
syncfile () {
|
||||||
|
fileLocalPath=$1
|
||||||
|
fileRemotePath=${fileLocalPath/$dirLocalPath/$dirRemotePath}
|
||||||
|
echo -e "\n${NONE}${CYAN}🔄 Synchronisation du fichier ${YELLOW}${fileLocalPath/$dirLocalPath/}${NONE}"
|
||||||
|
echo -e "${NONE}\$> ${MAGENTA}scp ${fileLocalPath} ${host}:${fileRemotePath}${NONE}"
|
||||||
|
scp ${fileLocalPath} $host:$fileRemotePath
|
||||||
|
}
|
||||||
|
|
||||||
|
echo -e "\n${NONE}${BBLUE}⏳ En attente de modification${NONE}${GREY}"
|
||||||
|
echo -e "${NONE}${BLUE}-----------------------------${NONE}${GREY}"
|
||||||
|
|
||||||
|
num=0
|
||||||
|
while read fileLocalPath; do
|
||||||
|
if [ "$fileLocalPath" = "NoOp" ]; then
|
||||||
|
num=0
|
||||||
|
echo -e "\n${NONE}${CYAN}🗑️ Suppression du cache${NONE}${GREY}"
|
||||||
|
echo -e "${NONE}\$> ${MAGENTA}ssh ${host} \"cd ${dirRemotePath}; composer front-cache-clear\"${NONE}\n"
|
||||||
|
ssh $host "cd $dirRemotePath; composer front-cache-clear"
|
||||||
|
echo -e "\n${NONE}${BBLUE}⏳ En attente de modification${NONE}${GREY}"
|
||||||
|
echo -e "${NONE}${BLUE}-----------------------------${NONE}${GREY}"
|
||||||
|
else
|
||||||
|
num=$((num+1))
|
||||||
|
#echo -e "${NONE}${num} - ${fileLocalPath}"
|
||||||
|
syncfile $fileLocalPath $num &
|
||||||
|
fi
|
||||||
|
done < <(fswatch --batch-marker -0 --exclude '.git/' $dirLocalPath | xargs -0 -n 1 -I {})
|
||||||
|
|
||||||
|
else
|
||||||
|
echo -e "${RED}⚠ ${NONE} Le script a rencontré une erreur"
|
||||||
|
echo "-----------------------------"
|
||||||
|
echo -e "${RED} 💀 Exit code:${NONE} $RETVAL"
|
||||||
|
exit $RETVAL
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user