-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport-db.sh
More file actions
executable file
·50 lines (43 loc) · 1.51 KB
/
import-db.sh
File metadata and controls
executable file
·50 lines (43 loc) · 1.51 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
# Set up logging
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}
log "Starting WordPress database initialization script"
# Load environment variables from .env
if [ -f .env ]; then
export $(cat .env | grep -v '#' | sed 's/\r$//' | xargs)
log "Successfully loaded environment variables from .env"
else
log "ERROR: .env file not found"
exit 1
fi
# Drop and recreate database
log "Dropping and recreating database '${MYSQL_DATABASE}'"
if docker exec -i $(docker compose ps -q db) mysql -u root -p${MYSQL_ROOT_PASSWORD} \
-e "DROP DATABASE IF EXISTS ${MYSQL_DATABASE}; CREATE DATABASE ${MYSQL_DATABASE};"; then
log "Successfully recreated database"
else
log "ERROR: Failed to recreate database"
exit 1
fi
# Import database dump
log "Importing database dump from dump.sql"
if docker exec -i $(docker compose ps -q db) mysql \
-u ${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DATABASE} < dump.sql; then
log "Successfully imported database dump"
else
log "ERROR: Failed to import database dump"
exit 1
fi
# Update WordPress URLs
log "Updating WordPress URLs to ${BASE_URL}"
if docker exec -i $(docker compose ps -q db) mysql \
-u ${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DATABASE} \
-e "UPDATE wp_options SET option_value='${BASE_URL}' WHERE option_name='siteurl' OR option_name='home';"; then
log "Successfully updated WordPress URLs"
else
log "ERROR: Failed to update WordPress URLs"
exit 1
fi
log "WordPress database initialization completed successfully"