Batch-Sync-Script到多个服务器

时间:2018-01-12 13:42:21

标签: batch-file for-loop if-statement synchronization

我试图用robocopy同步我的7台服务器(1到6),但我已经无法创建for循环了。我的第二个问题是我不知道在哪里放置robocopy部分所以它只在服务器启动时执行。似乎脚本总是运行,因此两个if语句都被执行。

@echo off
title Sync
cls

::
:: Logging
::

::Log-Variablen werden gesetzt
set LOGDIR=.\Logs
set LOGFILE=%LOGDIR%\SR_sync.log

::Check ob LOGDIR existiert.
if not exist "%LOGDIR%" mkdir %LOGDIR%

::Backup alter Logs
if exist %LOGFILE%.3 del %LOGFILE%.3
if exist %LOGFILE%.2 move %LOGFILE%.2 %LOGFILE%.3
if exist %LOGFILE%.1 move %LOGFILE%.1 %LOGFILE%.2
if exist %LOGFILE% move %LOGFILE% %LOGFILE%.1

::
:: Synchronisierung
::

::Quellserver wird gesetzt
set sourceserver=SERVER
set targets=targets.txt

::Zielserver werden als Array gesetzt

::Log wird erstellt
echo Sync von %date% %time% >> %LOGFILE%
echo ------------------------------- >> %LOGFILE%
echo. >> %LOGFILE%
echo SOURCE:  %sourceserver% >> %LOGFILE%
echo TARGETS: %targets% >> %LOGFILE%
echo. >> %LOGFILE%
echo ------------------------------- >> %LOGFILE%

for /f %%i in (%targets%) do ( 
 echo %time% - Sync mit %%i gestartet >> %LOGFILE%
 ping %%i -n 1 -w 1000 - l 2000 -4 | findstr /r /c:"[0-9] *ms"
 set hostup=%errorlevel%
 if %hostup% == 0 ( echo %time% - %%i erreichbar. >> %LOGFILE% )
 if not %hostup% == 0 ( echo %time% - %%i nicht erreichbar. >> %LOGFILE% )
)

这失败了“(

rem @echo off
cd %windir%
title Sync
cls

::
:: Logging
::

::Log-Variablen werden gesetzt
set LOGDIR=.\Logs
set LOGFILE=%LOGDIR%\SR_sync.log

::Check ob LOGDIR existiert.
if not exist "%LOGDIR%" mkdir %LOGDIR%

::Backup alter Logs
if exist %LOGFILE%.3 del %LOGFILE%.3
if exist %LOGFILE%.2 move %LOGFILE%.2 %LOGFILE%.3
if exist %LOGFILE%.1 move %LOGFILE%.1 %LOGFILE%.2
if exist %LOGFILE% move %LOGFILE% %LOGFILE%.1

::
:: Synchronisierung
::

::Quellserver wird gesetzt
set sourceserver=SERVER
set targets=targets.txt

::Zielserver werden als Array gesetzt

::Log wird erstellt
echo Sync von %date% %time% >> %LOGFILE%
echo ------------------------------- >> %LOGFILE%
echo. >> %LOGFILE%
echo SOURCE:  %sourceserver% >> %LOGFILE%
echo TARGETS: %targets% >> %LOGFILE%
echo. >> %LOGFILE%
echo ------------------------------- >> %LOGFILE%

for /f %%i in (%targets%) do ( 
 echo %time% - Sync mit %%i gestartet >> %LOGFILE%
 ping %%i -n 1 -w 1000 - l 2000 -4 | findstr /r /c:"[0-9] *ms"
 if %errorlevel% == 0 ( 
 echo %time% - %%i erreichbar. >> %LOGFILE% && exit)
 echo %time% - Sync abgebrochen. %%i nicht erreichbar. >> %LOGFILE%
)

这很有效。

任何建议? 非常感谢提前。

0 个答案:

没有答案