Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@echo off & goto CONFIG
:SYNTAX
echo Syntax: %~n0 [DIFF|FULL] [folders]
goto END
:CONFIG
set BAK_DIR=[你要儲存備份檔的目錄,不要包含路徑最後的 \ 字元]
set SRC_DIR=[你要備份的目錄,不要包含路徑最後的 \ 字元。用空格或逗號區隔不同目錄。如果目錄包含空白字元,請使用 "" 括住完整路目錄名稱]
set Z_OPT=-scsUTF-8 -ssc -ssw -ms=on -mx=9 -t7z
goto START
:PARAMS
SETLOCAL
set PARAM=
for %%F in (%*) do (
if "FULL" == "%%F" (
set TYPE=FULL
) else if "DIFF" == "%%F" (
set TYPE=DIFF
) else (
if "" == "!PARAM!" (
set PARAM=%%F
) else (
set PARAM=!PARAM!, %%F
)
)
)
ENDLOCAL & set "TYPE=%TYPE%" & set "PARAM=%PARAM%"
goto :eof
:TIMESTAMP
SETLOCAL
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if ".%%i."==".LocalDateTime." set ldt=%%j
ENDLOCAL & set "DATE=%ldt:~0,8%" & set "TIME=%ldt:~8,6%"
goto :eof
:DO_FULL_BAK
SETLOCAL
set DIR=%1
set FULL=%2
echo ...performing full backup...
7za a %FULL% %Z_OPT% %DIR%
echo .
echo ...%FULL%...done^^!
ENDLOCAL
goto :eof
:DO_DIFF_BAK
SETLOCAL
set DIR=%1
set FULL=%~f2
set DIFF="%FULL:~0,-3%_diff_%DATE%_%TIME%.7z"
echo ...performing diff backup on "%FULL%"...
7za u "%FULL%" %Z_OPT% -u- -up0q3r2x2y2z0w2^^!%DIFF% %DIR%
echo .
echo ...%DIFF%...done^^!
ENDLOCAL
goto :eof
:FULL
SETLOCAL
set SRC="%~f1"
set FN=%~nx1
echo ------------------
echo Requesting full backup for %SRC%...
set FB="%BAK_DIR%\%FN%_%DATE%.7z"
if EXIST %FB% (
echo ...full backup for %SRC%: %FB% already exist, re-request diff backup instead...
call :DO_DIFF_BAK %SRC% %FB%
) else (
call :DO_FULL_BAK %SRC% %FB%
)
ENDLOCAL
goto :eof
:DIFF
SETLOCAL
set SRC="%~f1"
set FN=%~nx1
echo ------------------
echo Requesting diff backup for %SRC%...
echo ...finding full backup...
set FB=
for %%N in (%BAK_DIR%\%FN%_*.7z) do (
set STMP=%%N
set STMP=!STMP:~-11,8!
if "%%N" == "%BAK_DIR%\%FN%_!STMP!.7z" (
echo ......found: "%%N"
set FB="%%N"
)
)
if "" == "%FB%" (
echo ...full backup not found, re-request full backup instead...
call :DO_FULL_BAK %SRC% "%BAK_DIR%\%FN%_%DATE%.7z"
) else (
echo ...full backup found: %FB%
call :DO_DIFF_BAK %SRC% %FB%
)
ENDLOCAL
goto :eof
:START
SETLOCAL enabledelayedexpansion
call :PARAMS %*
if NOT "" == "%PARAM%" (
set SRC_DIR=%PARAM%
)
if "" == "%TYPE%" (
set TYPE=DIFF
)
echo BAK_DIR=%BAK_DIR%
echo SRC_DIR=%SRC_DIR%
echo TYPE=%TYPE%
call :TIMESTAMP
echo date: "%DATE%"
echo time: "%TIME%"
for %%F in (%SRC_DIR%) do (
call :%TYPE% %%F
)
:END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment