149 lines
4.8 KiB
Bash
Executable File
149 lines
4.8 KiB
Bash
Executable File
#!/bin/bash
|
|
set -euo pipefail
|
|
|
|
# ── Charger les IDs créés par setup-portabase.sh ─────────────
|
|
source /home/ubuntu/docker/s3-backup/portabase-ids.env
|
|
|
|
BASE="http://10.0.0.4"
|
|
H="x-api-key: $API_KEY"
|
|
BDD_HOST="10.0.0.5"
|
|
SSH_KEY="/root/.ssh/key_bdd_redis" # clé SSH vers bdd-redis-prod
|
|
|
|
echo "[1/4] Attente de connexion de l'agent..."
|
|
echo " Déploiement de l'agent sur bdd-redis-prod ($BDD_HOST)..."
|
|
|
|
ssh -o StrictHostKeyChecking=no -i "$SSH_KEY" root@"$BDD_HOST" << AGENT_EOF
|
|
# Installer Docker si absent
|
|
if ! command -v docker &>/dev/null; then
|
|
echo " Installation de Docker..."
|
|
apt-get update -qq
|
|
apt-get install -y -qq ca-certificates curl gnupg lsb-release
|
|
install -m 0755 -d /etc/apt/keyrings
|
|
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
|
echo "deb [arch=\$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \$(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
|
|
apt-get update -qq
|
|
apt-get install -y -qq docker-ce docker-ce-cli containerd.io docker-compose-plugin
|
|
systemctl enable --now docker
|
|
fi
|
|
|
|
mkdir -p /opt/portabase-agent
|
|
cd /opt/portabase-agent
|
|
|
|
cat > docker-compose.yml << 'COMPOSE'
|
|
services:
|
|
portabase-agent:
|
|
image: portabase/agent:latest
|
|
container_name: portabase-agent
|
|
restart: always
|
|
network_mode: host
|
|
env_file: .env
|
|
environment:
|
|
- TZ=Europe/Paris
|
|
- DATABASES_CONFIG_FILE=config.toml
|
|
volumes:
|
|
- ./databases.toml:/config/config.toml:ro
|
|
- /var/run/docker.sock:/var/run/docker.sock
|
|
- agent-data:/data
|
|
- agent-tmp:/tmp
|
|
|
|
volumes:
|
|
agent-data:
|
|
agent-tmp:
|
|
COMPOSE
|
|
|
|
cat > .env << ENVEOF
|
|
EDGE_KEY=$EDGE_KEY
|
|
ENVEOF
|
|
chmod 600 .env
|
|
|
|
cat > databases.toml << 'TOML'
|
|
[[databases]]
|
|
name = "Production PostgreSQL - gestion_commande_db"
|
|
database = "gestion_commande_db"
|
|
type = "postgresql"
|
|
username = "admin_gestion_commande_db"
|
|
password = "1SWDxH20rV7K2Uc2PNlwCaCxfVZEtKomF0CK9OMh"
|
|
port = 5432
|
|
host = "127.0.0.1"
|
|
generated_id = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
|
|
|
|
[[databases]]
|
|
name = "Production Redis"
|
|
type = "redis"
|
|
password = "k6UYX9RtuXJVV1HUeefbSukMcSwjvVgRsh2qJGPh"
|
|
port = 6379
|
|
host = "127.0.0.1"
|
|
generated_id = "b2c3d4e5-f6a7-8901-bcde-f12345678901"
|
|
TOML
|
|
|
|
docker compose pull -q
|
|
docker compose up -d
|
|
echo " Agent démarré"
|
|
AGENT_EOF
|
|
|
|
echo "[2/4] Attente que l'agent se connecte au dashboard..."
|
|
for i in $(seq 1 30); do
|
|
LAST=$(curl -sf "$BASE/api/v1/agents/$AGENT_ID" -H "$H" | python3 -c "import sys,json; print(json.load(sys.stdin).get('lastContact',''))" 2>/dev/null || true)
|
|
if [ -n "$LAST" ] && [ "$LAST" != "None" ] && [ "$LAST" != "null" ]; then
|
|
echo " Agent connecté (lastContact=$LAST)"
|
|
break
|
|
fi
|
|
sleep 5
|
|
done
|
|
|
|
echo "[3/4] Récupération des bases détectées..."
|
|
sleep 10
|
|
DBS=$(curl -sf "$BASE/api/v1/databases" -H "$H")
|
|
echo "$DBS" | python3 -c "
|
|
import sys, json
|
|
dbs = json.load(sys.stdin)
|
|
if isinstance(dbs, list):
|
|
for db in dbs:
|
|
print(f\" - {db.get('name','?')} (id={db['id']}, type={db.get('type','?')})\")
|
|
elif isinstance(dbs, dict) and 'data' in dbs:
|
|
for db in dbs['data']:
|
|
print(f\" - {db.get('name','?')} (id={db['id']}, type={db.get('type','?')})\")
|
|
else:
|
|
print(' Format inattendu:', json.dumps(dbs)[:200])
|
|
"
|
|
|
|
# Récupérer les IDs des bases PostgreSQL
|
|
PG_DB_ID=$(echo "$DBS" | python3 -c "
|
|
import sys, json
|
|
dbs = json.load(sys.stdin)
|
|
if isinstance(dbs, dict) and 'data' in dbs: dbs = dbs['data']
|
|
for db in dbs:
|
|
if db.get('type') == 'postgresql':
|
|
print(db['id'])
|
|
break
|
|
" 2>/dev/null || true)
|
|
|
|
echo "[4/4] Configuration du schedule de backup..."
|
|
|
|
if [ -n "$PG_DB_ID" ]; then
|
|
# Attacher la DB au projet
|
|
curl -sf -X PATCH "$BASE/api/v1/databases/$PG_DB_ID" \
|
|
-H "$H" -H "Content-Type: application/json" \
|
|
-d "{\"projectId\":\"$PROJECT_ID\"}" > /dev/null
|
|
echo " PostgreSQL attaché au projet"
|
|
|
|
# Configurer le backup quotidien (tous les jours à 02:00 et 02:15 pour 15min d'intervalle)
|
|
# cron: toutes les 15 min = */15 * * * * mais le user veut "tous les jours avec 15 min d'intervalle"
|
|
# = une backup par jour, pas toutes les 15 min. Donc cron = 0 2 * * * (02h00 chaque jour)
|
|
curl -sf -X PUT "$BASE/api/v1/databases/$PG_DB_ID/backup-policy" \
|
|
-H "$H" -H "Content-Type: application/json" \
|
|
-d '{"cron":"0 2 * * *","retentionCount":30}' > /dev/null
|
|
echo " Schedule: backup quotidien à 02h00, rétention 30 jours"
|
|
else
|
|
echo " ERREUR: base PostgreSQL non trouvée. Vérifier dans l'UI."
|
|
fi
|
|
|
|
echo ""
|
|
echo "============================================"
|
|
echo " Configuration terminée !"
|
|
echo " Dashboard : $BASE"
|
|
echo " Agent : connecté depuis bdd-redis-prod"
|
|
echo " Backup : quotidien à 02h00 UTC"
|
|
echo " Rétention : 30 derniers backups"
|
|
echo "============================================"
|