美文网首页Windows
windows 自动备份文件bat脚本

windows 自动备份文件bat脚本

作者: 小沙 | 来源:发表于2014-07-02 09:35 被阅读1845次
    @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脚本

        本文链接:https://www.haomeiwen.com/subject/hdujtttx.html