See Logger.
@echo off rem Version 1.1.0 rem Last Update: 2011-06-24 rem Usage: rem rem Backup.bat TargetName TargetDir ContentType Frequency HaltOnTargetDirError rem rem ContentType = 'All', 'CS' or 'Batch' rem Frequency = 'Hourly', 'Daily' or 'Weekly' rem HaltOnTargetDirError = 'True' or 'False' rem rem Example: rem rem Backup.bat DevLocal C:\DevLocal CS Daily True rem rem NOTE: Do NOT put double quotes in the arguments - it won't work. If the directory has a space, use 8.3 formatted names: rem rem Backup.bat FooBar z:\mydocu~1\visual~1\FooBar CS Hourly rem set LoggerExe=C:\Bin\Logger.exe set SummaryLogFile=C:\Bin\Logs\Summary.log set FullLogFile=C:\Bin\Logs\Backup.log echo Info: Backup: **** Starting **** | %LoggerExe% >> %SummaryLogFile% set AllFlags= set AllSearch=* set CSFlags=-x!*.dll -x!*.zip -x!*.pdf -x!*.pdb -x!*.exe set CSSearch=* set BatchFlags= set BatchSearch=*.bat if [%1]==[] goto ErrorInCommandLineOptions if [%2]==[] goto ErrorInCommandLineOptions if [%3]==[] goto ErrorInCommandLineOptions if [%4]==[] goto ErrorInCommandLineOptions if [%5]==[] goto ErrorInCommandLineOptions set TargetName=%1 set TargetDir=%2 set ContentType=%3 set Frequency=%4 set HaltOnTargetDirError=%5 set BackupDir=C:\Dev\Backups\%TargetName% :CheckHaltOnTargetDirError if "%HaltOnTargetDirError%"=="True" goto CheckTargetDir if "%HaltOnTargetDirError%"=="False" goto CheckTargetDir echo ERROR: The HaltOnTargetDirError must be 'True' or 'False'. echo ERROR: Backup: The HaltOnTargetDirError must be 'True' or 'False'. | %LoggerExe% >> %SummaryLogFile% echo. goto ErrorInCommandLineOptions :CheckTargetDir if exist "%TargetDir%" goto CheckBackupDir echo ERROR: The target directory (%TargetDir%) does not exist or can not be accessed. echo ERROR: Backup: The target directory (%TargetDir%) does not exist or can not be accessed. | %LoggerExe% >> %SummaryLogFile% if "%HaltOnTargetDirError%"=="False" goto Done pause goto Done :CheckBackupDir if exist "%BackupDir%" goto CheckContentType echo ERROR: The backup directory (%BackupDir%) does not exist or can not be accessed. echo ERROR: Backup: The backup directory (%BackupDir%) does not exist or can not be accessed. | %LoggerExe% >> %SummaryLogFile% pause goto Done :CheckContentType if "%ContentType%"=="All" goto ContentTypeIsAll if "%ContentType%"=="CS" goto ContentTypeIsCS if "%ContentType%"=="Batch" goto ContentTypeIsBatch echo ERROR: The content type must be 'All', 'CS' or 'Batch'. echo ERROR: Backup: The content type must be 'All', 'CS' or 'Batch'. | %LoggerExe% >> %SummaryLogFile% echo. goto ErrorInCommandLineOptions :ContentTypeIsAll set Flags=%AllFlags% set Search=%AllSearch% goto CheckFrequency :ContentTypeIsCS set Flags=%CSFlags% set Search=%CSSearch% goto CheckFrequency :ContentTypeIsBatch set Flags=%BatchFlags% set Search=%BatchSearch% goto CheckFrequency :CheckFrequency if "%Frequency%"=="Hourly" goto FrequencyIsHourly if "%Frequency%"=="Daily" goto FrequencyIsDaily if "%Frequency%"=="Weekly" goto FrequencyIsWeekly echo ERROR: The frequency must be 'Hourly', 'Daily' or 'Weekly'. echo ERROR: Backup: The frequency must be 'Hourly', 'Daily' or 'Weekly'. | %LoggerExe% >> %SummaryLogFile% echo. goto ErrorInCommandLineOptions :FrequencyIsHourly set CreateBackupCommand="C:\Program Files\7-Zip\7z.exe" u -r %Flags% "%BackupDir%\Daily_Backup_01.7z" -u- -up0q0x2y2z0w2!"%BackupDir%\%Frequency%_Backup_Temp.7z" "%TargetDir%\%Search%" goto CheckDailyBackup :FrequencyIsDaily set CreateBackupCommand="C:\Program Files\7-Zip\7z.exe" a -r %Flags% "%BackupDir%\%Frequency%_Backup_Temp.7z" "%TargetDir%\%Search%" goto CleanUpTemps :FrequencyIsWeekly set CreateBackupCommand=copy "%BackupDir%\Daily_Backup_01.7z" "%BackupDir%\%Frequency%_Backup_Temp.7z" goto CheckDailyBackup :CheckDailyBackup if exist "%BackupDir%\Daily_Backup_01.7z" goto CleanUpTemps echo ERROR: The daily backup ("%BackupDir%\Daily_Backup_01.7z") does not exist or can not be accessed. echo ERROR: Backup: The daily backup ("%BackupDir%\Daily_Backup_01.7z") does not exist or can not be accessed. | %LoggerExe% >> %SummaryLogFile% pause goto Done :CleanUpTemps rem Clean up now, just in case the last run died halfway through. del "%BackupDir%\%Frequency%_Backup_Temp.7z" del "%BackupDir%\%Frequency%_Compare.7z" del "%BackupDir%\%Frequency%_Backup_Temp_Contents.txt" del "%BackupDir%\%Frequency%_Backup_01_Contents.txt" :CreateBackup echo Pre-check and set up is done. Beginning backup. echo Info: Backup: [%TargetName% %Frequency%] Pre-check and set up is done. Beginning backup. | %LoggerExe% >> %SummaryLogFile% %CreateBackupCommand% 2>&1 | %LoggerExe% >> %FullLogFile% if not exist "%BackupDir%\%Frequency%_Backup_01.7z" goto RollDone rem Determine if there is any differences between the current backup and the previous backup. rem 7Zip includes the name of the file in the content list, so we need to make sure the filename is the same for each backup. We only want to know if the *contents* are different. rem If DOS had a more functional 'tail' command we wouldn't need to do this... rename "%BackupDir%\%Frequency%_Backup_Temp.7z" "%Frequency%_Compare.7z" 2>&1 | %LoggerExe% >> %FullLogFile% "C:\Program Files\7-Zip\7z.exe" l -slt "%BackupDir%\%Frequency%_Compare.7z" > "%BackupDir%\%Frequency%_Backup_Temp_Contents.txt" rename "%BackupDir%\%Frequency%_Compare.7z" "%Frequency%_Backup_Temp.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_01.7z" "%Frequency%_Compare.7z" 2>&1 | %LoggerExe% >> %FullLogFile% "C:\Program Files\7-Zip\7z.exe" l -slt "%BackupDir%\%Frequency%_Compare.7z" > "%BackupDir%\%Frequency%_Backup_01_Contents.txt" rename "%BackupDir%\%Frequency%_Compare.7z" "%Frequency%_Backup_01.7z" 2>&1 | %LoggerExe% >> %FullLogFile% fc "%BackupDir%\%Frequency%_Backup_Temp_Contents.txt" "%BackupDir%\%Frequency%_Backup_01_Contents.txt" IF ERRORLEVEL 1 GOTO :DifferencesExist ECHO No differences found - throwing the backup away. echo Info: Backup: [%TargetName% %Frequency%] No differences found - throwing the backup away. | %LoggerExe% >> %SummaryLogFile% REM No differences found, so just clean up. del "%BackupDir%\%Frequency%_Backup_01_Contents.txt" 2>&1 | %LoggerExe% >> %FullLogFile% del "%BackupDir%\%Frequency%_Backup_Temp_Contents.txt" 2>&1 | %LoggerExe% >> %FullLogFile% del "%BackupDir%\%Frequency%_Backup_Temp.7z" 2>&1 | %LoggerExe% >> %FullLogFile% GOTO :done :DifferencesExist ECHO Differences found - storing the backup. echo Info: Backup: [%TargetName% %Frequency%] Differences found - storing the backup. | %LoggerExe% >> %SummaryLogFile% del "%BackupDir%\%Frequency%_Backup_01_Contents.txt" del "%BackupDir%\%Frequency%_Backup_Temp_Contents.txt" :RollExistingBackups del "%BackupDir%\%Frequency%_Backup_20.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_19.7z" "%Frequency%_Backup_20.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_18.7z" "%Frequency%_Backup_19.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_17.7z" "%Frequency%_Backup_18.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_16.7z" "%Frequency%_Backup_17.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_15.7z" "%Frequency%_Backup_16.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_14.7z" "%Frequency%_Backup_15.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_13.7z" "%Frequency%_Backup_14.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_12.7z" "%Frequency%_Backup_13.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_11.7z" "%Frequency%_Backup_12.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_10.7z" "%Frequency%_Backup_11.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_09.7z" "%Frequency%_Backup_10.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_08.7z" "%Frequency%_Backup_09.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_07.7z" "%Frequency%_Backup_08.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_06.7z" "%Frequency%_Backup_07.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_05.7z" "%Frequency%_Backup_06.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_04.7z" "%Frequency%_Backup_05.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_03.7z" "%Frequency%_Backup_04.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_02.7z" "%Frequency%_Backup_03.7z" 2>&1 | %LoggerExe% >> %FullLogFile% rename "%BackupDir%\%Frequency%_Backup_01.7z" "%Frequency%_Backup_02.7z" 2>&1 | %LoggerExe% >> %FullLogFile% :RollDone rename "%BackupDir%\%Frequency%_Backup_Temp.7z" "%Frequency%_Backup_01.7z" 2>&1 | %LoggerExe% >> %FullLogFile% goto :Done :ErrorInCommandLineOptions echo Error in command line arguments. echo. echo Usage: echo. echo Backup.bat TargetName TargetDir ContentType Frequency HaltOnTargetDirError echo. echo ContentType = 'CS' or 'Batch' echo Frequency = 'Hourly', 'Daily' or 'Weekly' echo HaltOnTargetDirError = 'True' or 'False' echo. echo Example: echo. echo Backup.bat DevLocal C:\DevLocal CS Daily True echo. echo NOTE: Do NOT put double quotes in the arguments - it won't work. If the directory has a space, use 8.3 formatted names: echo. echo Backup.bat FooBar z:\mydocu~1\visual~1\FooBar CS Hourly echo. goto :Done :Done echo Info: Backup: **** Done **** | %LoggerExe% >> %SummaryLogFile%
@echo off rem Version 1.0.0 rem Last Update: 2011-06-14 rem Usage: rem rem Backup.bat TargetName TargetDir ContentType Frequency HaltOnTargetDirError rem rem ContentType = 'All', 'CS' or 'Batch' rem Frequency = 'Hourly', 'Daily' or 'Weekly' rem HaltOnTargetDirError = 'True' or 'False' rem rem Example: rem rem Backup.bat DevLocal C:\DevLocal CS Daily True rem rem NOTE: Do NOT put double quotes in the arguments - it won't work. If the directory has a space, use 8.3 formatted names: rem rem Backup.bat FooBar z:\mydocu~1\visual~1\FooBar CS Hourly rem set AllFlags= set AllSearch=* set CSFlags=-x!*.dll -x!*.zip -x!*.pdf -x!*.pdb -x!*.exe set CSSearch=* set BatchFlags= set BatchSearch=*.bat if [%1]==[] goto ErrorInCommandLineOptions if [%2]==[] goto ErrorInCommandLineOptions if [%3]==[] goto ErrorInCommandLineOptions if [%4]==[] goto ErrorInCommandLineOptions if [%5]==[] goto ErrorInCommandLineOptions set TargetName=%1 set TargetDir=%2 set ContentType=%3 set Frequency=%4 set HaltOnTargetDirError=%5 set BackupDir=C:\Dev\Backups\%TargetName% :CheckHaltOnTargetDirError if "%HaltOnTargetDirError%"=="True" goto CheckTargetDir if "%HaltOnTargetDirError%"=="False" goto CheckTargetDir echo ERROR: The HaltOnTargetDirError must be 'True' or 'False'. eventcreate /ID 1 /t ERROR /l APPLICATION /so "Backup" /d "The HaltOnTargetDirError must be 'True' or 'False'." echo. goto ErrorInCommandLineOptions :CheckTargetDir if exist "%TargetDir%" goto CheckBackupDir echo ERROR: The target directory (%TargetDir%) does not exist or can not be accessed. eventcreate /ID 1 /t ERROR /l APPLICATION /so "Backup" /d "The target directory (%TargetDir%) does not exist or can not be accessed." if "%HaltOnTargetDirError%"=="False" goto Done pause goto Done :CheckBackupDir if exist "%BackupDir%" goto CheckContentType echo ERROR: The backup directory (%BackupDir%) does not exist or can not be accessed. eventcreate /ID 1 /t ERROR /l APPLICATION /so "Backup" /d "The backup directory (%BackupDir%) does not exist or can not be accessed." pause goto Done :CheckContentType if "%ContentType%"=="All" goto ContentTypeIsAll if "%ContentType%"=="CS" goto ContentTypeIsCS if "%ContentType%"=="Batch" goto ContentTypeIsBatch echo ERROR: The content type must be 'All', 'CS' or 'Batch'. eventcreate /ID 1 /t ERROR /l APPLICATION /so "Backup" /d "The content type must be 'All', 'CS' or 'Batch'." echo. goto ErrorInCommandLineOptions :ContentTypeIsAll set Flags=%AllFlags% set Search=%AllSearch% goto CheckFrequency :ContentTypeIsCS set Flags=%CSFlags% set Search=%CSSearch% goto CheckFrequency :ContentTypeIsBatch set Flags=%BatchFlags% set Search=%BatchSearch% goto CheckFrequency :CheckFrequency if "%Frequency%"=="Hourly" goto FrequencyIsHourly if "%Frequency%"=="Daily" goto FrequencyIsDaily if "%Frequency%"=="Weekly" goto FrequencyIsWeekly echo ERROR: The frequency must be 'Hourly', 'Daily' or 'Weekly'. eventcreate /ID 1 /t ERROR /l APPLICATION /so "Backup" /d "The frequency must be 'Hourly', 'Daily' or 'Weekly'." echo. goto ErrorInCommandLineOptions :FrequencyIsHourly set CreateBackupCommand="C:\Program Files\7-Zip\7z.exe" u -r %Flags% "%BackupDir%\Daily_Backup_01.7z" -u- -up0q0x2y2z0w2!"%BackupDir%\%Frequency%_Backup_Temp.7z" "%TargetDir%\%Search%" goto CheckDailyBackup :FrequencyIsDaily set CreateBackupCommand="C:\Program Files\7-Zip\7z.exe" a -r %Flags% "%BackupDir%\%Frequency%_Backup_Temp.7z" "%TargetDir%\%Search%" goto CleanUpTemps :FrequencyIsWeekly set CreateBackupCommand=copy "%BackupDir%\Daily_Backup_01.7z" "%BackupDir%\%Frequency%_Backup_Temp.7z" goto CheckDailyBackup :CheckDailyBackup if exist "%BackupDir%\Daily_Backup_01.7z" goto CleanUpTemps echo ERROR: The daily backup ("%BackupDir%\Daily_Backup_01.7z") does not exist or can not be accessed. eventcreate /ID 1 /t ERROR /l APPLICATION /so "Backup" /d "The daily backup ('%BackupDir%\Daily_Backup_01.7z') does not exist or can not be accessed." pause goto Done :CleanUpTemps rem Clean up now, just in case the last run died halfway through. del "%BackupDir%\%Frequency%_Backup_Temp.7z" del "%BackupDir%\%Frequency%_Compare.7z" del "%BackupDir%\%Frequency%_Backup_Temp_Contents.txt" del "%BackupDir%\%Frequency%_Backup_01_Contents.txt" :CreateBackup eventcreate /ID 1 /t INFORMATION /l APPLICATION /so "Backup %TargetName% %Frequency%" /d "Pre-check and set up is done. Beginning backup." %CreateBackupCommand% if not exist "%BackupDir%\%Frequency%_Backup_01.7z" goto RollDone rem Determine if there is any differences between the current backup and the previous backup. rem 7Zip includes the name of the file in the content list, so we need to make sure the filename is the same for each backup. We only want to know if the *contents* are different. rem If DOS had a more functional 'tail' command we wouldn't need to do this... rename "%BackupDir%\%Frequency%_Backup_Temp.7z" "%Frequency%_Compare.7z" "C:\Program Files\7-Zip\7z.exe" l -slt "%BackupDir%\%Frequency%_Compare.7z" > "%BackupDir%\%Frequency%_Backup_Temp_Contents.txt" rename "%BackupDir%\%Frequency%_Compare.7z" "%Frequency%_Backup_Temp.7z" rename "%BackupDir%\%Frequency%_Backup_01.7z" "%Frequency%_Compare.7z" "C:\Program Files\7-Zip\7z.exe" l -slt "%BackupDir%\%Frequency%_Compare.7z" > "%BackupDir%\%Frequency%_Backup_01_Contents.txt" rename "%BackupDir%\%Frequency%_Compare.7z" "%Frequency%_Backup_01.7z" fc "%BackupDir%\%Frequency%_Backup_Temp_Contents.txt" "%BackupDir%\%Frequency%_Backup_01_Contents.txt" IF ERRORLEVEL 1 GOTO :DifferencesExist ECHO No differences found - throwing the backup away. eventcreate /ID 1 /t INFORMATION /l APPLICATION /so "Backup %TargetName% %Frequency%" /d "No differences found - throwing the backup away." REM No differences found, so just clean up. del "%BackupDir%\%Frequency%_Backup_01_Contents.txt" del "%BackupDir%\%Frequency%_Backup_Temp_Contents.txt" del "%BackupDir%\%Frequency%_Backup_Temp.7z" GOTO :done :DifferencesExist ECHO Differences found - storing the backup. eventcreate /ID 1 /t INFORMATION /l APPLICATION /so "Backup %TargetName% %Frequency%" /d "Differences found - storing the backup." del "%BackupDir%\%Frequency%_Backup_01_Contents.txt" del "%BackupDir%\%Frequency%_Backup_Temp_Contents.txt" :RollExistingBackups del "%BackupDir%\%Frequency%_Backup_20.7z" rename "%BackupDir%\%Frequency%_Backup_19.7z" "%Frequency%_Backup_20.7z" rename "%BackupDir%\%Frequency%_Backup_18.7z" "%Frequency%_Backup_19.7z" rename "%BackupDir%\%Frequency%_Backup_17.7z" "%Frequency%_Backup_18.7z" rename "%BackupDir%\%Frequency%_Backup_16.7z" "%Frequency%_Backup_17.7z" rename "%BackupDir%\%Frequency%_Backup_15.7z" "%Frequency%_Backup_16.7z" rename "%BackupDir%\%Frequency%_Backup_14.7z" "%Frequency%_Backup_15.7z" rename "%BackupDir%\%Frequency%_Backup_13.7z" "%Frequency%_Backup_14.7z" rename "%BackupDir%\%Frequency%_Backup_12.7z" "%Frequency%_Backup_13.7z" rename "%BackupDir%\%Frequency%_Backup_11.7z" "%Frequency%_Backup_12.7z" rename "%BackupDir%\%Frequency%_Backup_10.7z" "%Frequency%_Backup_11.7z" rename "%BackupDir%\%Frequency%_Backup_09.7z" "%Frequency%_Backup_10.7z" rename "%BackupDir%\%Frequency%_Backup_08.7z" "%Frequency%_Backup_09.7z" rename "%BackupDir%\%Frequency%_Backup_07.7z" "%Frequency%_Backup_08.7z" rename "%BackupDir%\%Frequency%_Backup_06.7z" "%Frequency%_Backup_07.7z" rename "%BackupDir%\%Frequency%_Backup_05.7z" "%Frequency%_Backup_06.7z" rename "%BackupDir%\%Frequency%_Backup_04.7z" "%Frequency%_Backup_05.7z" rename "%BackupDir%\%Frequency%_Backup_03.7z" "%Frequency%_Backup_04.7z" rename "%BackupDir%\%Frequency%_Backup_02.7z" "%Frequency%_Backup_03.7z" rename "%BackupDir%\%Frequency%_Backup_01.7z" "%Frequency%_Backup_02.7z" :RollDone rename "%BackupDir%\%Frequency%_Backup_Temp.7z" "%Frequency%_Backup_01.7z" goto :Done :ErrorInCommandLineOptions echo Error in command line arguments. echo. echo Usage: echo. echo Backup.bat TargetName TargetDir ContentType Frequency HaltOnTargetDirError echo. echo ContentType = 'CS' or 'Batch' echo Frequency = 'Hourly', 'Daily' or 'Weekly' echo HaltOnTargetDirError = 'True' or 'False' echo. echo Example: echo. echo Backup.bat DevLocal C:\DevLocal CS Daily True echo. echo NOTE: Do NOT put double quotes in the arguments - it won't work. If the directory has a space, use 8.3 formatted names: echo. echo Backup.bat FooBar z:\mydocu~1\visual~1\FooBar CS Hourly echo. goto :Done :Done echo Done.