DOS編程--記對(duì)文件的全量備份與更新??
rem自動(dòng)備份麻蹋,更新。
@echo off
mode con cols=100 lines=400
rem 切換到腳本所在目錄
pushd %~dp0
rem 輸入?yún)?shù)
set /p backupPath_programDirectory=請(qǐng)輸入,程序目錄 備份目錄 iis站點(diǎn)名稱:
::set backupPath_programDirectory=D:\程序目錄 D:\備份目錄
for /f "tokens=1,2,3 delims= " %%a in ("%backupPath_programDirectory%") do (
set programRootDir=%%a
set backupPath=%%b
set iisSiteName=%%c
)
rem 獲取更新包里的文件名,保存到文件filesName.txt中。
set file=filesName.txt
mkdir %backupPath% >nul 2>nul
::dir /b/s *.dll *.aspx *.xml *.js *.html *.xls *.ascx *.cshtml *.config *.Master *.cs > %file%
dir /a-d/b/s | findstr /v "backup__update.bat" | findstr /v "filesName.txt" > %file%
rem 去掉文件前面的當(dāng)前目錄的路徑名稱
set replace=%cd%
set replaceWith=
for /f "tokens=* delims= " %%i in (%file%) do (
SETLOCAL enabledelayedexpansion
set str=%%i
? ? set str=!str:%replace%=%replaceWith%!
if not "%%~nxi%" == "backup__update.bat" (
if not "%%~nxi%" == "filesName.txt" (
echo !str! >> %file%.temp
)
)
ENDLOCAL
)
move %file%.temp %file% >nul 2>nul
copy %file% %backupPath% >nul 2>nul
echo.復(fù)制程序目錄里的原文件,到備份目錄里。
echo *************************************
echo 》》》開(kāi)始備份
for /r? %%i in (*.*) do (
SETLOCAL enabledelayedexpansion
set fileFullName=%%~nxi
set fileExtensionName=%%~xi
set relativePath=
set fileRelativePath=
if not "!fileFullName!" == "backup__update.bat" (
if not "!fileFullName!" == "filesName.txt" (
for /f "tokens=* delims= " %%j in (%file%) do (
set relativePath=%%~pj
set fileRelativePath=!relativePath!"!fileFullName!"
rem 通過(guò)文件名会傲,找到文件名所在行,得到文件名稱前面的路徑名
echo "%%j" | findstr /c:"!fileFullName!" >nul
if !errorlevel! == 0 (
echo f | xcopy /y %programRootDir%!fileRelativePath! %backupPath%!fileRelativePath!
)
)
)
)
ENDLOCAL
)
echo 》》》備份結(jié)束纳像!《《《
echo *************************************
pause
rem 停止站點(diǎn)
call :IIS_Site_Stop %iisSiteName%
echo.
echo *************************************
echo 》》》開(kāi)始替換
for /r? %%i in (*.*) do (
SETLOCAL enabledelayedexpansion
set fileFullName=%%~nxi
set relativePath=
set fileRelativePath=
if not "!fileFullName!" == "backup__update.bat" (
if not "!fileFullName!" == "filesName.txt" (
for /f "tokens=* delims= " %%j in (%file%) do (
set relativePath=%%~pj
set fileRelativePath=!relativePath!"!fileFullName!"
echo "%%j" | findstr /c:"!fileFullName!" >nul
if !errorlevel! == 0 (
:continue_copy
echo f | xcopy /y %cd%!fileRelativePath! %programRootDir%!fileRelativePath!
if not !errorlevel! == 0 (
ping -n 3 127.0.0.1 >nul 2>nul
goto :continue_copy
)
)
)
)
)
ENDLOCAL
)
echo 》》》替換完成荆烈!《《《
echo *************************************
rem 啟動(dòng)站點(diǎn)
call :IIS_Site_Start %iisSiteName%
echo.
pause
exit /b
::IIS站點(diǎn)(應(yīng)用程序)
:IIS_Site_Start
echo 啟動(dòng)IIS站點(diǎn)
set var1=%1
C:\Windows\System32\inetsrv\appcmd.exe start site %var1%
C:\Windows\System32\inetsrv\appcmd.exe start apppool /apppool.name:%var1%
C:\Windows\System32\inetsrv\appcmd.exe recycle apppool /apppool.name:%var1%
GOTO:EOF
::IIS站點(diǎn)(應(yīng)用程序)
:IIS_Site_Stop
echo 關(guān)閉IIS站點(diǎn)
set var1=%1
C:\Windows\System32\inetsrv\appcmd.exe stop site %var1%
C:\Windows\System32\inetsrv\appcmd.exe stop apppool /apppool.name:%var1%
GOTO:EOF