James Richters
James Richters

Reputation: 1

Batch file to overwrite backups that are 6 weeks old

I have the following Batch file which makes a daily backup and keeps it for 7 days.

@echo off
C:
CD\ProAuto
setlocal enableextensions

:: Get Numeric Date
for /F "skip=1 delims=" %%F in ('wmic PATH Win32_LocalTime GET Day^,Month^,Year /FORMAT:TABLE') do ( for /F "tokens=1-3" %%L in ("%%F") do (
   set CurDay=0%%L
   set CurMonth=0%%M
   set CurYear=%%N
   ))
set CurDay=%CurDay:~-2%
set CurMonth=%CurMonth:~-2%

:: Get numeric Day of the Week
for /f %%a in ('wmic path win32_localtime get dayofweek /format:list ^| findstr "="') do (set %%a)

:: Get Time and Date with full Month and Day of week
for /f "skip=8 tokens=2,3,4,5,6,7,8,9 delims=,: " %%D in ('robocopy /l * T: T: /ns /nc /ndl /nfl /np /njh /XF * /XD *') do (
   set "dow=%%D"
   set "month=%%E"
   set "DD=%%F"
   set "YYYY=%%G"
   set "HH=%%H"
   set "MM=%%I"
   set "SS=%%J"
   set "AMPM=%%K"
   )
IF "%AMPM%"=="AM" ( Set AMPM=am )
IF "%AMPM%"=="PM" ( Set AMPM=pm )

Set RoboLog="C:\ProAuto\Logs\~%DayofWeek% %dow%.log"
Echo QuickBooks Backup  %dow%, %month% %dd%, %yyyy% @ %hh%:%mm%:%ss%%AmPm% >%RoboLog%

:: The following checks to see if the database is locked by attempting to rename the file to the same name.
ren "C:\Users\Public\Documents\Intuit\QuickBooks\Company Files\XYZ.QBW" "XYZ.QBW" >nul && (goto UnLocked) || (goto Locked)
Goto Donelocked
:UnLocked
echo QuickBooks is UnLocked and will be backed up >>%RoboLog%
Net Use "\\192.168.0.246\Backup Files"
Net Use "\\192.168.0.252\QB_BK"
Move "\\192.168.0.252\QB_BK\%DayofWeek% %dow%*" "\\192.168.0.252\QB_BK\%DayofWeek% %dow% %CurYear%-%CurMonth%-%CurDay%"
robocopy "C:\Users\Public\Documents\Intuit\QuickBooks\Company Files" "\\192.168.0.252\QB_BK\%DayofWeek% %dow% %CurYear%-%CurMonth%-%CurDay%" /XD "QuickBooksAutoDataRecovery" /MIR /FFT /V /NP /NDL /copy:DAT /dcopy:T /r:2 /W:2 /log+:%RoboLog% /tee
Call :RoboCheck
If %RoboRC% GTR 7 Goto QBBKFail
:QBBKGood
Echo QuickBooks Daily Backup Successful! >>%RoboLog%
goto doneQBBK
:QBBKFail
Echo QuickBooks Daily Backup Had Errors! >>%RoboLog%
Echo Sending Error Email with Log to Clients >> %RoboLog%
powershell -executionpolicy bypass -file %~dp0QB_KS_Fail.ps1
:DoneQBBK
Echo Copying Log to Backup Directory >>%RoboLog%
Copy %RoboLog% "\\192.168.0.252\QB_BK\" >>%RoboLog%
Goto Donelocked
:Locked
echo QuickBooks is Locked and cannot be backed up because it is open on a client computer >>%RoboLog%
echo Generating an Open File report and Emailing it to Clients
Set OpenFileLog="C:\ProAuto\Logs\QB_Open_Files %curmonth%-%curday%-%curyear%.log"
Echo Open QuickBooks Files >> %RoboLog%
Echo Open QuickBooks Files > %OpenFileLog%
Echo %dow%, %month% %dd%, %yyyy% @ %hh%:%mm%:%ss%%AmPm% >> %RoboLog%
Echo %dow%, %month% %dd%, %yyyy% @ %hh%:%mm%:%ss%%AmPm% >> %OpenFileLog%
openfiles /query  /v | findstr /rc:"^$" /c:"Locally" /c:"remotely" /c:"----------" /c:"======" /c:"Accessed" /c:"QuickBooks" >> %RoboLog%
openfiles /query  /v | findstr /rc:"^$" /c:"Locally" /c:"remotely" /c:"----------" /c:"======" /c:"Accessed" /c:"QuickBooks" >> %OpenFileLog%
Echo Sending Open Files Email to Clients >> %RoboLog%
powershell -executionpolicy bypass -file %~dp0Locked_Email.ps1
Del %OpenFileLog%
:DoneLocked
Goto :EOF
:RoboCheck
Set RoboRC=%ErrorLevel%
Echo Return Code: %RoboRC% >>%RoboLog%
    if %RoboRC% EQU 16 echo ***FATAL ERROR*** >>%RoboLog% & echo Serious error. Robocopy did not copy any files. >>%RoboLog% & echo Either a usage error or an error due to insufficient access privileges >>%RoboLog% & echo on the source or destination directories. >>%RoboLog%
    if %RoboRC% EQU 15 echo Code 15 = Code 1 OKCOPY + Code 2 XTRA + Code 4 MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 14 echo Code 14 = Code 2 XTRA + Code 4 MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 13 echo Code 13 = Code 1 OKCOPY + Code 4 - MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 12 echo Code 12 = Code 4 MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 11 echo Code 11 = Code 1 OKCOPY + Code 2 - XTRA + Code 8 COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 10 echo Code 10 = Code 2 XTRA + Code 8 COPYFAIL: >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 9  echo Code 9 = Code 1 OKCOPY + Code 8 - COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 8  echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 7  echo Code 7 = Code 1 OKCOPY + Code 2 XTRA + Code 4 MISMATCHES: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 6  echo Code 6 = Code 2 XTRA + Code 4 MISMATCHES: >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 5  echo Code 5 = Code 1 OKCOPY + Code 4 MISMATCHES: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 4  echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 3  echo Code 3 = Code 1 OKCOPY + Code 2 XTRA: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Examine the output log for details. >>%RoboLog%
    if %RoboRC% EQU 2  echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Examine the output log for details. >>%RoboLog%
    if %RoboRC% EQU 1  echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog%
    if %RoboRC% EQU 0  echo No errors occurred, and no copying was done. >>%RoboLog% & echo The source and destination directory trees are completely synchronized >>%RoboLog%
Goto :EOF

What this does is every Monday it renames the Monday backup directory to contain the current date, then uses Robocopy to mirror the current data to the new directory. Then Tuesday it does the same thing to the Tuesday directory, etc, so each backup is retained for 7 days.

I want to modify this to make a weekly backup and keep it for 6 weeks. I'm thinking I will change the Directory name to Week 1 through Week 6 and recycle the directories in sequence, but now I do not have an easy way to determine which week I am on since there is no calendar event that repeats every 6 weeks the way I am using the day of the week for my daily backup.

So What I am looking for is some method or suggestion on how I can determine which backup directory is closest to 6 weeks old so I can re-cycle the correct one. There is the possibility that there will be no backup 6 weeks old, in which case it would just create one, just as the daily backup would if Wednesday were missing, it would just make a new Wednesday backup. I also do not want to necessarily always take the oldest backup either, for example if the machine is down and it misses a backup, I don't want to change the sequence, I want to use the one that is 6 weeks old or create a new one.

I'm wanting to figure out a way to calculate which %WeekofCycle% so I can use the following code:

Move "\\192.168.0.252\QB_BK\Week %WeekofCycle%*" "\\192.168.0.252\QB_BK\Week %WeekofCycle%* %dow% %CurYear%-%CurMonth%-%CurDay%"
robocopy "C:\Users\Public\Documents\Intuit\QuickBooks\Company Files" "\\192.168.0.252\QB_BK\Week %WeekofCycle%* %dow% %CurYear%-%CurMonth%-%CurDay%" /XD "QuickBooksAutoDataRecovery" /MIR /FFT /V /NP /NDL /copy:DAT /dcopy:T /r:2 /W:2 /log+:%RoboLog% /tee

I'm trying to figure out a way to calculate what week in the cycle it is regardless of what backups might have or might not have been done yet, and regardless if there are extra backups or non-existent backups. If I calculate I am on Week 3, I will rename the previous backup at "\192.168.0.252\QB_BK\Week 3*" to now have the day and date appended to it, then use the new name with Robocopy to update changed files.

Upvotes: 0

Views: 598

Answers (1)

Magoo
Magoo

Reputation: 80033

What I would do would be to name each destination directory as yyyymmdd - append dayname/date/other data as required, and append the destination directory name to a file (backups.log)

Having successfully backed up the current data, then

for /f "skip=42,delims=" %%a in (`sort /r backups.log`) do (
 ECHO rd /s /q "%%a"
 findstr /v /x "%%a" backups.log >dummy.txt
 move dummy.txt backups.log
)

This sorts the names of the backup directories in reverse-date order so the latest directoryname is first. The first 42 = 6 weeks * 7 days lines are skipped, and any further entries (which will be the earliest) are deleted using rd and then removed from the backups.log file by using findstr to output those lines that /v do not match /x exactly the directory that has just been removed, so any backups over the 42 retained will be deleted from the file and the filesystem.

Note that the for will process the reverse-sorted result of reading the backups.log file. This result is held in memory, and is not affected by the changes to backups.log made by the for loop.

Upvotes: 1

Related Questions