@echo off rem ============================================================================ rem blog full-repo backup - entry point for Windows Task Scheduler rem rem scripts/backup-run.mjs is what gets invoked. It: rem - loads .workbuddy-backup\openlist-backup.env rem - runs scripts/backup-bundle.mjs (git bundle --all -> AES-256-GCM encrypt rem -> WebDAV upload to OpenList on the ZTE F50 -> sha256 readback check) rem - emails an alert through scripts/send_mail.js when the backup fails rem rem OpenList is the ONLY off-site copy, so a silent failure would mean running rem with no backup at all. Hence failures raise an email instead of just rem appending to a log nobody reads. rem rem Notes: rem * This file is intentionally pure ASCII. cmd.exe parses a .bat/.cmd using rem the *current* code page (GBK on zh-CN) while node emits UTF-8. A UTF-8 rem Chinese comment can swallow the CR/LF and break the following line rem (seen in practice: a rem line got executed as a command). ASCII is a rem subset of UTF-8, so plain-English text mixes safely with node output. rem For the same reason the alert subject/body (which is Chinese) is built rem inside backup-run.mjs, never here. rem * node resolution order: PATH -> WorkBuddy managed -> D:\nodejs rem * Log appends to .workbuddy-backup\logs\backup.log, rotated at 5 MB rem * .workbuddy-backup is git-ignored; logs, passphrase and SMTP creds rem never enter git rem * Exit code is passed through (0 = success), visible in Task Scheduler rem ============================================================================ setlocal set "REPO=%~dp0.." if "%REPO:~-1%"=="\" set "REPO=%REPO:~0,-1%" set "LOG=%REPO%\.workbuddy-backup\logs" set "SCRIPT=%REPO%\scripts\backup-run.mjs" if not exist "%LOG%" mkdir "%LOG%" rem --- rotate log at 5 MB. Guarded by if exist: on the very first run the file rem does not exist yet and for/f would fail with a "path not found" error --- if exist "%LOG%\backup.log" for %%F in ("%LOG%\backup.log") do if %%~zF GTR 5242880 move /y "%LOG%\backup.log" "%LOG%\backup.prev.log" >nul 2>&1 rem --- locate node --- set "NODE=" for %%P in (node.exe) do if not defined NODE set "NODE=%%~$PATH:P" if not defined NODE if exist "%USERPROFILE%\.workbuddy\binaries\node\versions\22.22.2-6\node.exe" set "NODE=%USERPROFILE%\.workbuddy\binaries\node\versions\22.22.2-6\node.exe" if not defined NODE if exist "D:\nodejs\node.exe" set "NODE=D:\nodejs\node.exe" if not defined NODE call :fail "node not found in PATH, WorkBuddy managed dir, or D:\nodejs" if not exist "%SCRIPT%" call :fail "script not found: %SCRIPT%" rem --- ASCII timestamp. Do NOT use %date%: on zh-CN it carries localized rem weekday text (e.g. Chinese "Tuesday") which would mix encodings --- set "STAMP=%TIME%" for /f "delims=" %%I in ('powershell -NoProfile -Command "[DateTime]::Now.ToString('yyyy-MM-dd HH:mm:ss')" 2^>nul') do set "STAMP=%%I" rem --- arguments: use %* when given (handy for manual runs, e.g. --dry / --list) --- rem keep 7 = one week of daily snapshots (~600 MB each, ~4.2 GB total, the rem F50 has 256 GB). OpenList is the only off-site copy, so a wider window rem is worth the space. Pass --notify-success to also get a daily OK mail. set "ARGS=%*" if "%ARGS%"=="" set "ARGS=--verify --keep 7" echo. >> "%LOG%\backup.log" echo [%STAMP%] ===== backup start ===== >> "%LOG%\backup.log" "%NODE%" "%SCRIPT%" %ARGS% >> "%LOG%\backup.log" 2>&1 set "RC=%ERRORLEVEL%" echo [%STAMP%] ===== backup end, exit=%RC% ===== >> "%LOG%\backup.log" endlocal & exit /b %RC% :fail echo [%TIME%] ERROR: %~1 >> "%LOG%\backup.log" endlocal & exit /b 1