-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql-export.sh
More file actions
executable file
·33 lines (24 loc) · 1.04 KB
/
mysql-export.sh
File metadata and controls
executable file
·33 lines (24 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
# Load environment variables
export $(grep -E 'PROJECT_NAME|WORDPRESS_DB_' .env | xargs)
if [ -z "$PROJECT_NAME" ] || [ -z "$WORDPRESS_DB_NAME" ] || [ -z "$WORDPRESS_DB_USER" ] || [ -z "$WORDPRESS_DB_PASSWORD" ]; then
echo "❌ Required environment variables not set in .env"
exit 1
fi
# Get the container ID for the DB service
CONTAINER_ID=$(docker compose --env-file .env -p "$PROJECT_NAME" ps -q db)
if [ -z "$CONTAINER_ID" ]; then
echo "❌ Could not find a running 'db' container for project '$PROJECT_NAME'"
exit 1
fi
# Generate timestamped filename
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
OUTPUT_FILE="./${WORDPRESS_DB_NAME}_${TIMESTAMP}.sql"
echo "📤 Exporting database '$WORDPRESS_DB_NAME' to $OUTPUT_FILE..."
docker compose --env-file .env -p "$PROJECT_NAME" exec db \
sh -c "mysqldump -u$WORDPRESS_DB_USER -p$WORDPRESS_DB_PASSWORD $WORDPRESS_DB_NAME" > "$OUTPUT_FILE"
if [ $? -ne 0 ]; then
echo "❌ Export failed. Please check your credentials or running containers."
exit 1
fi
echo "✅ Export complete: $OUTPUT_FILE"