windows 自动备份文件bat脚本

@echo off
rem Get today's year, month, day
for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set year=%%a& set month=%%b& set day=%%c)

rem Modify the following line for your environment
set TARGET=E:\mygo
set TARGETNAME=go
set BACKUPDIR=d:\mygo
if not exist %BACKUPDIR%\%year% mkdir %BACKUPDIR%\%year%

set BACKUPFILE=%BACKUPDIR%\%year%\%TARGETNAME%_%year%%month%%day%.7z
set LOGFILE=%BACKUPDIR%\%year%\%TARGETNAME%_%year%%month%%day%_backup.log
set BACKUPSOURCEFILE= %BACKUPDIR%\%TARGETNAME%\*

REM delete old files(10 days ago)
FORFILES /p %BACKUPDIR%\%year% /M *.7z /C "cmd /c Del @path" /D -10

echo. | date | findstr "現在日期" >> %LOGFILE%
echo. | time | findstr "現在時間" >> %LOGFILE%
REM stop mysql and copy db file
REM net stop mysql4
REM echo stop mysql version 4 >> %LOGFILE%
echo Y | xcopy %TARGET%\*.* %BACKUPDIR%\%TARGETNAME%\ /S /I
REM net start mysql4
REM echo start mysql version 4 >> %LOGFILE%

rem Compress the target directory
7z a %BACKUPFILE% %BACKUPSOURCEFILE%

if not exist %BACKUPFILE% goto nofile

:nofile
echo [FAILURE] can't find %BACKUPFILE% >> %LOGFILE%
echo.>> %LOGFILE%
goto end

REM delete file
rd %BACKUPDIR%\%TARGETNAME% /S /q

set TARGET=
set TARGETNAME=
set BACKUPDIR=
set BACKUPFILE=
set LOGFILE=

:end

参考:如何定時自動備份整個網站目錄到遠端伺服器

你可能感兴趣的:(windows 自动备份文件bat脚本)