Repository navigation
Expand file tree
/
Copy pathdev.bat
More file actions
85 lines (74 loc) · 2.7 KB
/
Copy pathdev.bat
File metadata and controls
85 lines (74 loc) · 2.7 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@echo off
setlocal
rem The database password lives only in .env (git-ignored), never in a committed file. On the
rem first run this creates .env from .env.example and generates a password into it; every
rem compose command needs it, so it runs before any of them.
if not exist .env copy /y .env.example .env >nul
findstr /b /r /c:"MYSQL_ROOT_PASSWORD=." .env >nul
if errorlevel 1 call :generate_password
if "%1"=="" goto up
if "%1"=="up" goto up
if "%1"=="down" goto down
if "%1"=="stop" goto down
if "%1"=="reset" goto reset
if "%1"=="logs" goto logs
if "%1"=="cli" goto cli
if "%1"=="backup" goto backup
if "%1"=="restore" goto restore
echo Unknown command: %1
echo Usage: dev.bat [up^|down^|reset^|logs^|cli^|backup^|restore]
goto end
:up
echo Starting WordPress...
docker compose up --build -d
echo.
echo WordPress: http://localhost:8080
echo.
goto end
:down
docker compose down
goto end
:reset
docker compose down -v
docker compose up --build -d
echo.
echo WordPress reset complete: http://localhost:8080
goto end
:logs
rem 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
goto end
:cli
rem WP-CLI inside the WordPress container (it runs as www-data, the image's user). cmd's %*
rem ignores shift and %1.. split on "=", so the arguments are %* with the leading "cli" cut off.
set "WP_ARGS=%*"
set "WP_ARGS=%WP_ARGS:*cli=%"
docker compose exec wordpress wp %WP_ARGS%
goto end
rem backup / restore run inside the mysql container, which already holds the password in its
rem own environment ($MYSQL_ROOT_PASSWORD is expanded by sh there, not by cmd here).
:backup
echo Backing up database...
if not exist backups mkdir backups
docker compose exec -T mysql sh -c "MYSQL_PWD=$MYSQL_ROOT_PASSWORD exec mysqldump -uroot wordpress" > backups\backup_%date:~-4%%date:~3,2%%date:~0,2%.sql
echo Backup saved to backups\
goto end
:restore
if "%2"=="" (
echo Usage: dev.bat restore backups\filename.sql
goto end
)
echo Restoring database from %2...
docker compose exec -T mysql sh -c "MYSQL_PWD=$MYSQL_ROOT_PASSWORD exec mysql -uroot wordpress" < %2
echo Restore complete.
goto end
:generate_password
rem A random 48-hex-character password from .NET's cryptographic RNG.
for /f "delims=" %%p in ('powershell -NoProfile -Command "$b = New-Object byte[] 24; [Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($b); [BitConverter]::ToString($b).Replace('-','')"') do set "GENERATED_PASSWORD=%%p"
>>.env echo.
>>.env echo # Generated by dev.bat - local development only.
>>.env echo MYSQL_ROOT_PASSWORD=%GENERATED_PASSWORD%
echo Generated a local MYSQL_ROOT_PASSWORD in .env
exit /b 0
:end
endlocal