Tai icon indicating copy to clipboard operation
Tai copied to clipboard

使用git 私有仓库进行数据备份(只提供方法,不推荐这样做)

Open NLick47 opened this issue 10 months ago • 0 comments

需要先配置好git,在 gitxx上创建一个私有仓库 并配置好ssh。并在脚本BACKUP_DIR创建目录下添加你的远程仓库

@echo off
setlocal enabledelayedexpansion
set "BACKUP_DIR=D:\db-backup"
set "SOURCE_DB_PATH=Z:\Tai\Data\data.db"
set "TARGET_DB_NAME=backup.db"
set "REPO_BRANCH=main"


if not exist "%BACKUP_DIR%" (
    mkdir "%BACKUP_DIR%"
)

cd /d  "%BACKUP_DIR%"

if not exist ".git" (
    call :log_error "Not a git repository"
    exit /b 1
)




if not exist "%SOURCE_DB_PATH%" (
    call :log_error "Source database file does not exist %SOURCE_DB_PATH%"
)

cd /d "%BACKUP_DIR%" 
powershell -Command "Copy-Item '%SOURCE_DB_PATH%' '%BACKUP_DIR%\%TARGET_DB_NAME%' -Force"

git add . 
git commit -m "DB Backup: %date% %time%"

git branch -M %REPO_BRANCH%

set retry_count=0 
:retry 
git push origin %REPO_BRANCH% 2>&1 | tee -a git_error.log 
if !errorlevel! neq 0 ( 
    set /a retry_count+=1 
    if !retry_count! lss 3 ( 
        timeout /t 30 
        goto retry 
    ) 
    call :log_error "Push failure" 
    exit /b 1 
)

endlocal 
exit /b 0


:log_error 
echo [%date% %time%] %~1 >> backup.log 
exit /b

将这段脚本创建为backup.bat,在windows的任务计划中添加这个脚本。设为开机执行 或 开机执行并只有网络连接时启动。如果一定要使用这种方式最好更换执行方式,如每周或每月,减少社区服务器资源浪费

NLick47 avatar Feb 21 '25 03:02 NLick47