210 lines
5.6 KiB
Batchfile
210 lines
5.6 KiB
Batchfile
@echo off
|
||
REM 数据库管理脚本 (Windows版本)
|
||
REM 用于管理MySQL数据库容器的启动、停止、备份等操作
|
||
|
||
setlocal enabledelayedexpansion
|
||
|
||
REM 脚本配置
|
||
set "SCRIPT_DIR=%~dp0"
|
||
set "PROJECT_ROOT=%SCRIPT_DIR%..\.."
|
||
set "COMPOSE_FILE=%SCRIPT_DIR%docker-compose.database.yml"
|
||
set "ENV_FILE=%SCRIPT_DIR%.env.database"
|
||
|
||
REM 检查环境文件
|
||
:check_env_file
|
||
if not exist "%ENV_FILE%" (
|
||
echo [WARNING] 环境文件不存在: %ENV_FILE%
|
||
echo [INFO] 从模板创建环境文件...
|
||
copy "%ENV_FILE%.template" "%ENV_FILE%" >nul
|
||
echo [SUCCESS] 环境文件已创建,请根据需要修改配置
|
||
)
|
||
goto :eof
|
||
|
||
REM 创建必要的目录
|
||
:create_directories
|
||
set "DATA_DIR=%SCRIPT_DIR%data"
|
||
set "MYSQL_DIR=%DATA_DIR%\mysql"
|
||
set "LOGS_DIR=%DATA_DIR%\logs\mysql"
|
||
|
||
echo [INFO] 创建数据目录...
|
||
if not exist "%MYSQL_DIR%" mkdir "%MYSQL_DIR%"
|
||
if not exist "%LOGS_DIR%" mkdir "%LOGS_DIR%"
|
||
echo [SUCCESS] 数据目录创建完成
|
||
goto :eof
|
||
|
||
REM 启动数据库
|
||
:start_database
|
||
echo [INFO] 启动MySQL数据库容器...
|
||
|
||
call :check_env_file
|
||
call :create_directories
|
||
|
||
REM 使用docker-compose启动
|
||
docker-compose -f "%COMPOSE_FILE%" --env-file "%ENV_FILE%" up -d
|
||
|
||
echo [SUCCESS] 数据库容器启动完成
|
||
echo [INFO] 等待数据库初始化...
|
||
|
||
REM 等待健康检查通过
|
||
set /a max_attempts=30
|
||
set /a attempt=1
|
||
|
||
:wait_loop
|
||
if !attempt! gtr !max_attempts! (
|
||
echo [ERROR] 数据库启动超时
|
||
exit /b 1
|
||
)
|
||
|
||
docker-compose -f "%COMPOSE_FILE%" --env-file "%ENV_FILE%" ps | findstr "healthy" >nul
|
||
if !errorlevel! equ 0 (
|
||
echo [SUCCESS] 数据库已就绪
|
||
goto :eof
|
||
)
|
||
|
||
echo [INFO] 等待数据库启动... (!attempt!/!max_attempts!)
|
||
timeout /t 10 /nobreak >nul
|
||
set /a attempt+=1
|
||
goto wait_loop
|
||
|
||
REM 停止数据库
|
||
:stop_database
|
||
echo [INFO] 停止MySQL数据库容器...
|
||
docker-compose -f "%COMPOSE_FILE%" --env-file "%ENV_FILE%" down
|
||
echo [SUCCESS] 数据库容器已停止
|
||
goto :eof
|
||
|
||
REM 重启数据库
|
||
:restart_database
|
||
echo [INFO] 重启MySQL数据库容器...
|
||
call :stop_database
|
||
call :start_database
|
||
goto :eof
|
||
|
||
REM 查看数据库状态
|
||
:status_database
|
||
echo [INFO] 数据库容器状态:
|
||
docker-compose -f "%COMPOSE_FILE%" --env-file "%ENV_FILE%" ps
|
||
goto :eof
|
||
|
||
REM 查看数据库日志
|
||
:logs_database
|
||
set "lines=%~1"
|
||
if "%lines%"=="" set "lines=100"
|
||
echo [INFO] 显示数据库日志 (最近 %lines% 行):
|
||
docker-compose -f "%COMPOSE_FILE%" --env-file "%ENV_FILE%" logs --tail=%lines% -f
|
||
goto :eof
|
||
|
||
REM 进入数据库容器
|
||
:exec_database
|
||
echo [INFO] 进入数据库容器...
|
||
docker-compose -f "%COMPOSE_FILE%" --env-file "%ENV_FILE%" exec anxin-mysql bash
|
||
goto :eof
|
||
|
||
REM 连接到MySQL
|
||
:connect_mysql
|
||
echo [INFO] 连接到MySQL数据库...
|
||
REM 读取环境变量(简化版本,实际使用时需要解析.env文件)
|
||
docker-compose -f "%COMPOSE_FILE%" --env-file "%ENV_FILE%" exec anxin-mysql mysql -uanxin -panxin123 anxin
|
||
goto :eof
|
||
|
||
REM 备份数据库
|
||
:backup_database
|
||
set "backup_file=%SCRIPT_DIR%backup\anxin_%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%.sql"
|
||
set "backup_dir=%SCRIPT_DIR%backup"
|
||
|
||
echo [INFO] 备份数据库到: %backup_file%
|
||
|
||
REM 创建备份目录
|
||
if not exist "%backup_dir%" mkdir "%backup_dir%"
|
||
|
||
REM 执行备份
|
||
docker-compose -f "%COMPOSE_FILE%" --env-file "%ENV_FILE%" exec -T anxin-mysql mysqldump -uanxin -panxin123 --single-transaction --routines --triggers anxin > "%backup_file%"
|
||
|
||
echo [SUCCESS] 数据库备份完成: %backup_file%
|
||
goto :eof
|
||
|
||
REM 恢复数据库
|
||
:restore_database
|
||
set "backup_file=%~1"
|
||
|
||
if "%backup_file%"=="" (
|
||
echo [ERROR] 请指定有效的备份文件
|
||
exit /b 1
|
||
)
|
||
|
||
if not exist "%backup_file%" (
|
||
echo [ERROR] 备份文件不存在: %backup_file%
|
||
exit /b 1
|
||
)
|
||
|
||
echo [INFO] 从备份文件恢复数据库: %backup_file%
|
||
|
||
REM 执行恢复
|
||
docker-compose -f "%COMPOSE_FILE%" --env-file "%ENV_FILE%" exec -T anxin-mysql mysql -uanxin -panxin123 anxin < "%backup_file%"
|
||
|
||
echo [SUCCESS] 数据库恢复完成
|
||
goto :eof
|
||
|
||
REM 显示帮助信息
|
||
:show_help
|
||
echo 数据库管理脚本 (Windows版本)
|
||
echo.
|
||
echo 用法: %~nx0 [命令] [参数]
|
||
echo.
|
||
echo 命令:
|
||
echo start 启动数据库容器
|
||
echo stop 停止数据库容器
|
||
echo restart 重启数据库容器
|
||
echo status 查看数据库容器状态
|
||
echo logs [lines] 查看数据库日志 (默认100行)
|
||
echo exec 进入数据库容器
|
||
echo connect 连接到MySQL数据库
|
||
echo backup 备份数据库
|
||
echo restore ^<file^> 从备份文件恢复数据库
|
||
echo help 显示此帮助信息
|
||
echo.
|
||
echo 示例:
|
||
echo %~nx0 start # 启动数据库
|
||
echo %~nx0 logs 50 # 查看最近50行日志
|
||
echo %~nx0 restore backup.sql # 从backup.sql恢复数据库
|
||
goto :eof
|
||
|
||
REM 主函数
|
||
:main
|
||
set "command=%~1"
|
||
if "%command%"=="" set "command=help"
|
||
|
||
if "%command%"=="start" (
|
||
call :start_database
|
||
) else if "%command%"=="stop" (
|
||
call :stop_database
|
||
) else if "%command%"=="restart" (
|
||
call :restart_database
|
||
) else if "%command%"=="status" (
|
||
call :status_database
|
||
) else if "%command%"=="logs" (
|
||
call :logs_database "%~2"
|
||
) else if "%command%"=="exec" (
|
||
call :exec_database
|
||
) else if "%command%"=="connect" (
|
||
call :connect_mysql
|
||
) else if "%command%"=="backup" (
|
||
call :backup_database
|
||
) else if "%command%"=="restore" (
|
||
call :restore_database "%~2"
|
||
) else if "%command%"=="help" (
|
||
call :show_help
|
||
) else if "%command%"=="--help" (
|
||
call :show_help
|
||
) else if "%command%"=="-h" (
|
||
call :show_help
|
||
) else (
|
||
echo [ERROR] 未知命令: %command%
|
||
call :show_help
|
||
exit /b 1
|
||
)
|
||
|
||
goto :eof
|
||
|
||
REM 执行主函数
|
||
call :main %* |