每個月25日自動刪除資料庫前一個月的異地備份檔案,假設備份檔案的名稱為 DATABASENAME_DB_201203051300.BAK 這樣的格式
@ECHO OFF:INITSET LOCALBAK=D:\BAK
:DATETIMESET DATE_STR=%DATE%SET YEAR_STR=%DATE_STR:~0,4%SET MONTH_STR=%DATE_STR:~5,2%SET DAY_STR=%DATE_STR:~8,2%
SET /A PRE_YEAR_STR=%YEAR_STR%-1SET /A PRE_MONTH_STR=%MONTH_STR%-1
IF %PRE_MONTH_STR% EQU 0 (SET PRE_MONTH_STR=12& SET YEAR_STR=%PRE_YEAR_STR%)IF %PRE_MONTH_STR% LSS 10 (SET PRE_MONTH_STR=0%PRE_MONTH_STR%)
:DELETEDEL %LOCALBAK%\DATABASENAME_DB_%YEAR_STR%%PRE_MONTH_STR%*.BAK /Q
EXIT
範例二
自動複製備份目錄下的當日備份檔案到別台主機,假設是另外兩台主機。
@ECHO OFF:INITSET SVIP01=123.14.20.245SET SVIP02=123.14.20.246
SET USERNAM=ADMINISTRATORSET PW=BALABALABASET LOCALBAK=D:\DB_BAKSET REMOTEBAK=RBAK
:DATETIMESET DATE_STR=%DATE%SET YEAR_STR=%DATE_STR:~0,4%SET MONTH_STR=%DATE_STR:~5,2%SET DAY_STR=%DATE_STR:~8,2%
:CONNECTREM SV01IF EXIST Z: NET USE Z: /DELETENET USE Z: \\%SVIP01%\D$ %PW% /USER:%USERNAM%XCOPY %LOCALBAK%\DATABASE_DB_%YEAR_STR%%MONTH_STR%%DAY_STR%*.BAK Z:\%REMOTEBAK%\ /Y
REM SV02IF EXIST Z: NET USE Z: /DELETENET USE Z: \\%SVIP02%\D$ %PW% /USER:%USERNAM%XCOPY %LOCALBAK%\DATABASE_DB_%YEAR_STR%%MONTH_STR%%DAY_STR%*.BAK Z:\%REMOTEBAK%\ /Y
EXIT
範例三
自動重開機指定次數,範例為100次,Count.txt 內容為重開機次數 ,Log.txt 需自行命名建立。
@ECHO OFF
:ENVSETREM 設定次數設定檔,預設100SET COUNTFILENAME=C:\RESTART\COUNT.TXTREM 設定記錄檔位置SET LOGFILENAME=C:\RESTART\LOG.TXTREM 設定倒數秒數SET COUNTDOWN=50
FOR /F "TOKENS=1-4 DELIMS=/ " %%A IN ("%DATE%") DO (SET _MYDATE=%%A%%B%%C)FOR /F "TOKENS=1-3 DELIMS=: " %%E IN ("%TIME%") DO (SET _MYDATE=%_MYDATE%-%%E%%F%%G)FOR /F "TOKENS=1-2 DELIMS=. " %%H IN ("%_MYDATE%") DO (SET _MYDATE=%%H)
ECHO CURRENT DATE TIME IS : %_MYDATE%ECHO COUNT FILE NAME: C:\RESTART\COUNT.TXTECHO LOG FILE NAME: C:\RESTART\LOG.TXTECHO ----------------------------------------------------------------------
:JOBSTARTFOR /F %%I IN (%COUNTFILENAME%) DO SET /A RVAL=%%I-1FOR /F %%I IN (%COUNTFILENAME%) DO ECHO RESTART REMAIN: %RVAL% TIMES & ECHO %_MYDATE% REMAIN %%I TIMES >> %LOGFILENAME%FOR /F %%I IN (%COUNTFILENAME%) DO IF %RVAL% GTR 0 (CMD /C SHUTDOWN -R -T %COUNTDOWN% & ECHO %RVAL% > %COUNTFILENAME% & GOTO COMPLETE)GOTO FAIL
:FAILECHO 指定次數重開機已完成。PAUSE > NUL
:COMPLETEECHO %COUNTDOWN% 秒後重開機設定完成,請查閱LOG檔ECHO.ECHO 您可以輸入 "C" 取消本次重開機 (否則在時間到時重開)PAUSE > NULSHUTDOWN /A
範例四
多台伺服器檔案快速更新,範例為三台,可自行擴充。
@ECHO OFFREM ---------------------------DATE TIME--------------------------SET DATE_STR=%DATE%SET YEAR_STR=%DATE_STR:~0,4%SET MONTH_STR=%DATE_STR:~5,2%SET DAY_STR=%DATE_STR:~8,2%
REM -------------------------FILES LOCATION-----------------------SET FILES_LOCAL=D:\FILES_LOCALSET LOGS_LOCAL=D:\UPDATELOGSSET FILESREMOTE=WEBROOT\PROJECT
REM ---------------------------SERVER IP--------------------------SET SV01=123.14.2.174SET SV02=123.14.2.175SET SV03=123.14.2.176
ECHO 檔案快速更新ECHO.REM ---------------------------USER NAME--------------------------SET /P USERNAM=USER NAME:REM ---------------------------USER PWD--------------------------SET /P PW=PASSWORD:
:JOBSTARTREM ----------------------------INITIAL---------------------------IF EXIST Z: NET USE Z: /DELETE
REM ------------------SV01 START----------------------------------------NET USE Z: \\%SV01%\D$ %PW% /USER:%USERNAM%XCOPY %FILES_LOCAL%\*.* Z:\%FILESREMOTE%\ /E /YECHO UPDATE %SV01%: %YEAR_STR%.%MONTH_STR%.%DAY_STR% @ %TIME%ECHO UPDATE %SV01%: %YEAR_STR%.%MONTH_STR%.%DAY_STR% @ %TIME% >> %LOGS_LOCAL%\UPDATELOG_%YEAR_STR%.%MONTH_STR%.%DAY_STR%.TXTNET USE Z: /DELETEPING 127.0.0.1 > NULLPING 127.0.0.1 > NULLREM ------------------SV01 END------------------------------------------
REM ------------------SV02 START----------------------------------------NET USE Z: \\%SV02%\D$ %PW% /USER:%USERNAM%XCOPY %FILES_LOCAL%\*.* Z:\%FILESREMOTE%\ /E /YECHO UPDATE %SV02%: %YEAR_STR%.%MONTH_STR%.%DAY_STR% @ %TIME%ECHO UPDATE %SV02%: %YEAR_STR%.%MONTH_STR%.%DAY_STR% @ %TIME% >> %LOGS_LOCAL%\UPDATELOG_%YEAR_STR%.%MONTH_STR%.%DAY_STR%.TXTNET USE Z: /DELETEPING 127.0.0.1 > NULLPING 127.0.0.1 > NULLREM ------------------SV02 END------------------------------------------
REM ------------------SV03 START----------------------------------------NET USE Z: \\%SV03%\D$ %PW% /USER:%USERNAM%XCOPY %FILES_LOCAL%\*.* Z:\%FILESREMOTE%\ /E /YECHO UPDATE %SV03%: %YEAR_STR%.%MONTH_STR%.%DAY_STR% @ %TIME%ECHO UPDATE %SV03%: %YEAR_STR%.%MONTH_STR%.%DAY_STR% @ %TIME% >> %LOGS_LOCAL%\UPDATELOG_%YEAR_STR%.%MONTH_STR%.%DAY_STR%.TXTNET USE Z: /DELETEPING 127.0.0.1 > NULLPING 127.0.0.1 > NULLREM ------------------SV03 END------------------------------------------
:QUITPAUSE
0 意見:
張貼留言
留言........