Im trying to modify the below script to create the folder woth a time/date stamp each time the batch file is ran. Currently the batch file stops in error if a folder exist. Whats the easiest was to accomplish this?
Code: Select all
@echo off
:
: BackupESM.bat
:
: This batch file will save a backup of the following Enterprise Service Manager information:
: - Configuration files
: - Database files:
: - Registry settings
: - ESM default process inbox files
: - ESM default process shared files
:
: This information will be stored on the same drive as the ESM, in a folder called
: \ESM_Backup_5_x\Latest.
setlocal
set BACKUP_DIR=\ESM_Backup_5_x\Latest
echo.
rem Getting the installation directory...
start /W regedit /E %TEMP%\ESM_Reg.reg "HKEY_LOCAL_MACHINE\SOFTWARE\MarchNetworks\ESM"
if exist %TEMP%\ESM_Reg.reg goto gotFolder
rem Getting the installation directory again (such as from Win2008 R2)
start /W regedit /E %TEMP%\ESM_Reg.reg "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MarchNetworks\ESM"
if not exist %TEMP%\ESM_Reg.reg (
echo Error: Could not read registry of the Enterprise Service Manager.
goto exitError
)
:gotFolder
rem Figure out the installation folder and drive
for /F "tokens=2 delims==" %%l in ('type "%TEMP%\ESM_Reg.reg" ^| find "InstallationDir"') do (
set INSTALL_DIR=%%l
set INSTALL_DRIVE=%%~dl
)
if '%INSTALL_DIR%' == '' (
echo Error: Could not determine the Installation folder of the Enterprise
echo Service Manager.
goto exitError
)
rem Strip the quotes from the installation directory so that we can append paths to it
set INSTALL_DIR=%INSTALL_DIR:"=%
rem Prepend the backup directory with the installation drive
set BACKUP_DIR=%INSTALL_DRIVE%%BACKUP_DIR%
set RESTORE_FILE=%BACKUP_DIR%\Restore.bat
rem Check for the installation directory
if not exist "%INSTALL_DIR%" (
echo The Enterprise Service Manager could not be found in
echo "%INSTALL_DIR%"
goto exitError
)
rem Make sure that the backup folder doesn't already exist
if exist %BACKUP_DIR% (
@echo %datestr%%timestr%
echo The folder %BACKUP_DIR% already exists.
echo Rename it and run this script again.
goto exitError
)
if not exist %BACKUP_DIR% mkdir %BACKUP_DIR%
if not exist %BACKUP_DIR% (
echo There was an error creating the %BACKUP_DIR% folder.
echo Do you have the appropriate permissions?
goto exitError
)
rem Enable read permissions on Configuration folder...
cacls "%INSTALL_DIR%\Configuration" /T /E /G Everyone:F >NUL
call :CreateRestore
echo Copying files to the backup folder...
call :Backup "%INSTALL_DIR%\Configuration" %BACKUP_DIR%\Configuration
call :Backup "%INSTALL_DIR%\Database" %BACKUP_DIR%\Database
call :Backup "%INSTALL_DRIVE%\esm\sharedFiles" %BACKUP_DIR%\sharedFiles
call :Backup "%INSTALL_DRIVE%\esm\Inbox" %BACKUP_DIR%\Inbox
rem Backup the registry information, doesn't support auto restore in
rem Resotre.bat because of safe reason
copy %TEMP%\ESM_Reg.reg %BACKUP_DIR%\ > NUL
del /f /q %TEMP%\ESM_Reg.reg
echo. >>%RESTORE_FILE%
echo echo Restore complete >>%RESTORE_FILE%
echo pause >>%RESTORE_FILE%
echo.
echo The Enterprise Service Manager configuration and database files have
echo been backed up to %BACKUP_DIR%.
echo Please rename this folder to something meaningful to you.
echo.
pause
goto :EOF
:exitError
echo.
echo Backup Failed!
echo.
pause
exit /B 1
:Backup
if not exist %1 (
echo Warning: Cannot find %1
goto :EOF
)
mkdir %2
echo %1
xcopy /q /e %1 %2 >NUL
: write to the Restore file
echo if not exist %1 mkdir %1 >>%RESTORE_FILE%
echo xcopy /y /q /e %~n2 %1 >NUL >>%RESTORE_FILE%
goto :EOF
:CreateRestore
echo @echo off >%RESTORE_FILE%
echo. >>%RESTORE_FILE%
echo : >>%RESTORE_FILE%
echo : Restore.bat >>%RESTORE_FILE%
echo : >>%RESTORE_FILE%
echo : This batch file will restore the Enterprise Service Manager that is >>%RESTORE_FILE%
echo : in this folder. >>%RESTORE_FILE%
echo. >>%RESTORE_FILE%
echo echo Are you sure you want to restore the backed-up ESM files? >>%RESTORE_FILE%
echo echo Press Ctrl-C to quit. >>%RESTORE_FILE%
echo pause >>%RESTORE_FILE%
echo. >>%RESTORE_FILE%
echo rem regedit /s ESM_Reg.reg >>%RESTORE_FILE%
echo cacls "%INSTALL_DIR%\Configuration" /T /E /G Everyone:F >NUL >>%RESTORE_FILE%
echo. >>%RESTORE_FILE%
goto :EOF