I use Shadow Protect Desktop as my back up agent of choice, archiving backups to an internal 1TB drive. Every Sunday, I do a full image back up, and then an incremental back up every subsequent day. Shadow Protect makes it easy to do this, as it manages the image files, leaving the last 3 weeks on the disk (configuration options allow you to keep as many backups as you desire).
Then, I was manually copying the full image back up to an external USB hard drive in case a problem happens within my system that trashes both internal drives. But I had to remember to do this, and well, that can be a problem.
Shadow Protect has the ability to run any command before and after a backup operation. I decided an old-fashioned batch file would be great to delete the old full image backup file on the external drive and copy the new one over. In order to write a batch file to do that, I had to be able to select the file created the day the batch file runs, but only if it is Sunday. But I didn’t see an option to have the “post-backup command” run only on certain days within the Shadow Protect control panel.
The first order of business is to determine the day of the week in the batch file, and process the commands if it is Sunday. Here are the commands to do that:
set dow=%date:~0,3%
if %dow%==Sun goto sunday
goto theend
“dow” is a variable we just made up, and we’re calling on the “date” routine to tell us what to put into that variable. When you do a “Date” command from the Start * Run * CMD window, you get a return from the operating system like “Sun 03/08/2009″ (at least in my version of Windows XP, SP3). The command “set dow=%date:~0, 3%” is telling the operating system “OK, let’s create the variable ‘dow’ and, starting from the very first letter in the string, let’s put 3 characters from the date command into the variable. On Monday, that will be “Mon”, and on Tuesday, it will be “Tue” and so on.
Now we want to see what day it is … so we say “If the dow variable contains the letters ‘Sun’, then go the portion of the batch file called sunday”. If the letters aren’t “Sun”, then the batch file won’t jump to the portion called sunday. Instead, the system will continue to “go to the label called ‘theend’”. (Edit: I originally had “next” as the label here, but that can cause the batch file to continue processing rather than jumping … “next” is a command and should not be used as a label).
Now, let’s say we’ve determined it is Sunday and we want to copy the file over to our external drive:
:sunday
FOR /F "tokens=1-4 delims=/ " %%I IN ('DATE /t') DO SET mydate=%%J-%%K-%%L
DEL F:\*.spf
xcopy e:\*.spf f:\ /D:%mydate%
:theend
“:sunday” is the portion of the batch file we want processing to continue at if it is indeed Sunday. The line starting with “FOR /F” tells the system to tell us the full date, and fills another variable called “mydate” with the values of the month, day and year, separated by the dashes.
Next, since it is Sunday and we know we’re about to copy over the new backup file, we can delete the old backups on the external drive. Then we can use xcopy to copy the *.spf (full back up image file) from the e: drive to the external f: drive … but only if the date of the file … “/D:” … is equal to the value we stored in “mydate”. This enables us to copy only the backup file made today.
You’ll have to change the drive letters and paths to suit your situation, but here is the complete batch file again:
set dow=%date:~0,3%
if %dow%==Sun goto sunday
goto theend
:sunday
FOR /F "tokens=1-4 delims=/ " %%I IN ('DATE /t') DO SET mydate=%%J-%%K-%%L
DEL F:\*.spf
xcopy e:\*.spf f:\ /D:%mydate%
:theend
This is a simple solution without the use of add-on programs. Batch file programming used to be common place in the days of command line DOS, but its still valuable today.
Tech