@echo off
chcp 65001 >nul
echo ========================================
echo   数据库备份工具
echo ========================================
echo.

cd /d %~dp0

REM 创建备份目录
if not exist backup mkdir backup

REM 生成时间戳
set YEAR=%date:~0,4%
set MONTH=%date:~5,2%
set DAY=%date:~8,2%
set HOUR=%time:~0,2%
set MINUTE=%time:~3,2%
set SECOND=%time:~6,2%

REM 去掉小时前面的空格
if "%HOUR:~0,1%" == " " set HOUR=0%HOUR:~1,1%

set TIMESTAMP=%YEAR%%MONTH%%DAY%_%HOUR%%MINUTE%%SECOND%

REM 备份数据库
copy ftp-manager.db backup\ftp-manager-%TIMESTAMP%.db >nul

if %errorlevel% == 0 (
    echo [成功] 备份完成！
    echo 文件: backup\ftp-manager-%TIMESTAMP%.db

    REM 获取文件大小
    for %%A in (backup\ftp-manager-%TIMESTAMP%.db) do echo 大小: %%~zA 字节
) else (
    echo [错误] 备份失败！
)

echo.

REM 清理30天前的备份
echo 清理30天前的旧备份...
forfiles /P backup /M ftp-manager-*.db /D -30 /C "cmd /c del @path" 2>nul
if %errorlevel% == 0 (
    echo [成功] 旧备份已清理
) else (
    echo [提示] 没有需要清理的旧备份
)

echo.
echo ========================================
pause
