Repository navigation
Expand file tree
/
Copy pathdev.sh
More file actions
66 lines (63 loc) · 2.15 KB
/
Copy pathdev.sh
File metadata and controls
66 lines (63 loc) · 2.15 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
set -euo pipefail
export MSYS_NO_PATHCONV=1 # Windows/Git Bash: keep compose volume paths intact
# The database password lives only in .env (git-ignored), never in a committed file. On the
# first run this creates .env from .env.example and generates a password into it; every
# compose command needs it, so it runs before any of them.
ensure_env() {
[ -f .env ] || cp .env.example .env
if ! grep -q '^MYSQL_ROOT_PASSWORD=.' .env; then
printf '\n# Generated by ./dev.sh on %s - local development only.\nMYSQL_ROOT_PASSWORD=%s\n' \
"$(date +%Y-%m-%d)" "$(openssl rand -hex 24)" >> .env
echo "Generated a local MYSQL_ROOT_PASSWORD in .env"
fi
}
ensure_env
# Run inside the mysql container, which already holds the password in its own environment —
# so it never passes through this script or the process argv.
in_mysql() { docker compose exec -T mysql sh -c "MYSQL_PWD=\"\$MYSQL_ROOT_PASSWORD\" exec $1"; }
case "${1:-up}" in
up)
echo "Starting WordPress..."
docker compose up --build -d
echo ""
echo "WordPress: http://localhost:8080"
;;
down | stop)
docker compose down
;;
reset)
docker compose down -v
docker compose up --build -d
echo ""
echo "WordPress reset complete: http://localhost:8080"
;;
logs)
# OpenLiteSpeed writes its errors and PHP's to files in the container, not to docker logs.
docker compose exec wordpress tail -n 50 -F /usr/local/lsws/logs/error.log /usr/local/lsws/logs/stderr.log
;;
cli)
shift
# WP-CLI inside the WordPress container (it runs as www-data, the image's user).
docker compose exec wordpress wp "$@"
;;
backup)
echo "Backing up database..."
mkdir -p backups
in_mysql "mysqldump -uroot wordpress" > "backups/backup_$(date +%Y%m%d_%H%M%S).sql"
echo "Backup saved to backups/"
;;
restore)
if [ -z "${2:-}" ]; then
echo "Usage: ./dev.sh restore backups/filename.sql"
exit 1
fi
echo "Restoring database from $2..."
in_mysql "mysql -uroot wordpress" < "$2"
echo "Restore complete."
;;
*)
echo "Usage: ./dev.sh [up|down|reset|logs|cli|backup|restore]"
exit 1
;;
esac