dan_computerx
asked on
Batch file for backups
Friends,
I am trying to adapt an old script that ran backups of an ADP database to run backups of a fingerprint database. The old server was Server 2000; the new server is Server 2003 R2.
The script should do the following steps:
Rename a series of directories in a backup location
Directory1 -> Directory2
Directory2 -> Directory3
etc.
Delete the oldest directory
Stop a named service
Copy a directory from production to a backup location
Start the named service
Mirror the backup location to a remote server
You end up with a series of directories as follows:
C:\backups\Database1
C:\backups\Database2
C:\backups\Database3
C:\backups\Database4
C:\backups\Database5
And a mirror on a second server. It worked very well for years.
I'm having trouble getting the batch to run now. I suspect that the Num2 variable isn't getting set, but I'm not sure. Frankly I'm not very good with scripting :-(
Would you fix this for me? You need RoboCopy installed for it to work.
Thanks,
Dan
I am trying to adapt an old script that ran backups of an ADP database to run backups of a fingerprint database. The old server was Server 2000; the new server is Server 2003 R2.
The script should do the following steps:
Rename a series of directories in a backup location
Directory1 -> Directory2
Directory2 -> Directory3
etc.
Delete the oldest directory
Stop a named service
Copy a directory from production to a backup location
Start the named service
Mirror the backup location to a remote server
You end up with a series of directories as follows:
C:\backups\Database1
C:\backups\Database2
C:\backups\Database3
C:\backups\Database4
C:\backups\Database5
And a mirror on a second server. It worked very well for years.
I'm having trouble getting the batch to run now. I suspect that the Num2 variable isn't getting set, but I'm not sure. Frankly I'm not very good with scripting :-(
Would you fix this for me? You need RoboCopy installed for it to work.
Thanks,
Dan
@ECHO Off
REM - Make sure you have RoboCopy installed. It is part
REM - of the Server Tools and I am calling it from the default
REM - location.
REM - Set the variables for renaming, path, and number of backups
REM - And make sure they are only set for the system
SETLOCAL
Set BackupPath=C:\backups\M2Sys
Set FolderName=BioPlugin
Set CopyFrom="C:\Program Files\BioPlugin"
Set CopyTo=C:\backups\M2Sys\BioPlugin.1
Set MirrorLoc=\\arad\h$\Ariel\M2Sys\BioPlugin
Set BackupLog=C:\backups\M2SysBackupLog.txt
Set ServiceLog=%BackupPath%M2SysServices.log
Set Services="M2SYS Fingerprint Server"
Set Num=5
REM - For each Backup, do the Rename Subroutine, which either
REM - renames the folder, or deletes it if it is the last one.
For /L %%a in (%Num%,-1,1) Do Call :REN %%a
REM - Call the service stop subroutine. The CALL command waits for
REM - the referenced batch file to complete before continuing.
For %%b in (%Services%) Do Call :STOPSERVICES %%b
REM - Copy then entire BioPlugin directory to the C:\Backups\M2Sys\BioPlugin
"C:\Program Files\Windows Resource Kits\Tools\robocopy.exe" %CopyFrom% %CopyTo% /COPYALL /MIR /SEC /R:2 /W:10 /LOG+:%BackupLog% /NFL /NDL
REM - Call the service start batch file
For %%c in (%Services%) Do Call :STARTSERVICES %%c
REM - Mirror the entire %BackupPath% to your preferred server.
"C:\Program Files\Windows Resource Kits\Tools\robocopy.exe" %BackupPath% %MirrorLoc% /COPYALL /MIR /ZB /SEC /R:2 /W:300 /LOG+:%BackupLog% /NFL /NDL
:REN
REM - If Folder.Num exists, Set a Num2 variable to the next number
IF EXIST "%BackupPath%%FolderName%.%*" SET/A Num2=%*+1
REM - For Testing
ECHO "%Num2%"
REM - or kick out of the loop
IF NOT EXIST "%BackupPath%%FolderName%.%*" GoTo :EOF
REM - If this is the anything but the last folder, Rename the
REM - folder from Pathname\FolderName.[Num..1] to Pathname\FolderName.Num2
REM - And display exactly what we are doing
IF NOT %*==%Num% REN "%BackupPath%%FolderName%.%*" "%FolderName%.%Num2%"
REM - For Testing
ECHO RENAMING
REM - If this is the last folder, Delete the folder
REM - And display exactly what we are doing
IF %*==%Num% RMDIR /S /Q "%BackupPath%%FolderName%.%*"
GoTo :EoF
:STARTSERVICES
REM - Set the logfile path
REM - Write the current date and time to the logfile
>> "%ServiceLog%" ECHO %DATE%%TIME%:
REM - Start the service, and write all output
REM - From the command to the logfile
>> "%ServiceLog%" 2>&1 NET START %*
GoTo :EoF
:STOPSERVICES
REM - Write the current date and time to the logfile
>> "%ServiceLog%" ECHO %DATE%%TIME%:
REM - Start the service, and write all output
REM - From the command to the logfile
>> "%ServiceLog%" 2>&1 NET STOP %*
GoTo :EoF
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Noticed a few small things which I adjusted below. To troubleshoot further you would need to expand upon what problems you are seeing. The code could also be simplified in some places but I resisted the urge to do a further rewrite since you are not too skilled in scripts and may be comfortable with the structure of this script.
@ECHO Off
REM - Make sure you have RoboCopy installed. It is part
REM - of the Server Tools and I am calling it from the default
REM - location.
REM - Set the variables for renaming, path, and number of backups
REM - And make sure they are only set for the system
SETLOCAL
Set BackupPath=C:\backups\M2Sys
Set FolderName=BioPlugin
Set CopyFrom=C:\Program Files\BioPlugin
Set CopyTo=C:\backups\M2Sys\BioPlugin.1
Set MirrorLoc=\\arad\h$\Ariel\M2Sys\BioPlugin
Set BackupLog=C:\backups\M2SysBackupLog.txt
Set ServiceLog=C:\backups\M2SysServices.log
Set Services="M2SYS Fingerprint Server"
Set Num=5
REM - For each Backup, do the Rename Subroutine, which either
REM - renames the folder, or deletes it if it is the last one.
For /L %%a in (%Num%,-1,1) Do Call :REN %%a
REM - Call the service stop subroutine. The CALL command waits for
REM - the referenced batch file to complete before continuing.
For %%b in (%Services%) Do Call :STOPSERVICES %%b
REM - Copy then entire BioPlugin directory to the C:\Backups\M2Sys\BioPlugin
"C:\Program Files\Windows Resource Kits\Tools\robocopy.exe" "%CopyFrom%" "%CopyTo%" /COPYALL /MIR /SEC /R:2 /W:10 /LOG+:%BackupLog% /NFL /NDL
REM - Call the service start batch file
For %%c in (%Services%) Do Call :STARTSERVICES %%c
REM - Mirror the entire %BackupPath% to your preferred server.
"C:\Program Files\Windows Resource Kits\Tools\robocopy.exe" "%BackupPath%" "%MirrorLoc%" /COPYALL /MIR /ZB /SEC /R:2 /W:300 /LOG+:%BackupLog% /NFL /NDL
Goto :EOF
:REN
REM - If Folder.Num exists, Set a Num2 variable to the next number
IF EXIST "%BackupPath%\%FolderName%.%*" SET /A Num2=%*+1
REM - For Testing
ECHO "%Num2%"
REM - or kick out of the loop
IF NOT EXIST "%BackupPath%\%FolderName%.%*" GoTo :EOF
REM - If this is the anything but the last folder, Rename the
REM - folder from Pathname\FolderName.[Num..1] to Pathname\FolderName.Num2
REM - And display exactly what we are doing
IF NOT %*==%Num% REN "%BackupPath%%FolderName%.%*" "%FolderName%.%Num2%"
REM - For Testing
ECHO RENAMING
REM - If this is the last folder, Delete the folder
REM - And display exactly what we are doing
IF %*==%Num% RMDIR /S /Q "%BackupPath%%FolderName%.%*"
GoTo :EoF
:STARTSERVICES
REM - Set the logfile path
REM - Write the current date and time to the logfile
>> "%ServiceLog%" ECHO %DATE% %TIME%:
REM - Start the service, and write all output
REM - From the command to the logfile
>> "%ServiceLog%" 2>&1 NET START %*
GoTo :EoF
:STOPSERVICES
REM - Write the current date and time to the logfile
>> "%ServiceLog%" ECHO %DATE% %TIME%:
REM - Start the service, and write all output
REM - From the command to the logfile
>> "%ServiceLog%" 2>&1 NET STOP %*
GoTo :EoF
~bp
ASKER
Feel free to change anything you like. I didn't write it, and the person who did has left the company.
I get the folder copied, but I don't get the renaming. I tried putting "echo exists" at line 47 and get five lines of "exists," so I think the loop is running. I tried putting "echo "%Num2%" at line 47 and get five lines with a pair of double quotes.
I will diff against the old version tomorrow to spot the changes.
Thanks for looking at this. I really should take some basic programming/scripting classes. It is a *huge* hole in my skillset.
I get the folder copied, but I don't get the renaming. I tried putting "echo exists" at line 47 and get five lines of "exists," so I think the loop is running. I tried putting "echo "%Num2%" at line 47 and get five lines with a pair of double quotes.
I will diff against the old version tomorrow to spot the changes.
Thanks for looking at this. I really should take some basic programming/scripting classes. It is a *huge* hole in my skillset.
Okay, I'll see if I can cleanup a bit.
~bp
~bp
Okay, for me, this feels a little simpler, see what you think.
@echo off
REM Make sure you have RoboCopy installed. It is part of the
REM Server Tools and I am calling it from the default location.
REM Set the variables for renaming, path, and number of backups
setlocal EnableDelayedExpansion
set BackupPath=C:\backups\M2Sys
set FolderName=BioPlugin
set CopyFrom=C:\Program Files\BioPlugin
set CopyTo=%BackupPath%\%FolderName%.1
set MirrorLoc=\\arad\h$\Ariel\M2Sys\%FolderName%
set BackupLog=%BackupPath%\BackupLog.txt
set ServiceLog=%BackupPath%\Services.log
set Services="M2SYS Fingerprint Server"
set Num=5
REM Remove oldest backup folder
rmdir /S /Q "%BackupPath%\%FolderName%.%Num%"
REM Roll backup dirs #1 thru #5 to #2 to #6 to make room for new #1
for /L %%A in (%Num%,-1,2) do (
set /A Num2=%%A-1
if exist "%BackupPath%\%FolderName%.%%A" ren "%BackupPath%\%FolderName%.!Num2!" "%FolderName%.%%A"
)
REM - Start the service, and write all output to logfile
for %%S in (%Services%) do (
echo %DATE% %TIME% >> "%ServiceLog%"
net stop "%%~S" >> "%ServiceLog%" 2>&1
)
REM - Copy the entire BioPlugin directory to the C:\Backups\M2Sys\BioPlugin
"C:\Program Files\Windows Resource Kits\Tools\robocopy.exe" "%CopyFrom%" "%CopyTo%" /COPYALL /MIR /SEC /R:2 /W:10 /LOG+:%BackupLog% /NFL /NDL
REM - Start the service, and write all output to logfile
for %%S in (%Services%) do (
echo %DATE% %TIME% >> "%ServiceLog%"
net start "%%~S" >> "%ServiceLog%" 2>&1
)
REM - Mirror the entire %BackupPath% to your preferred server.
"C:\Program Files\Windows Resource Kits\Tools\robocopy.exe" "%BackupPath%" "%MirrorLoc%" /COPYALL /MIR /ZB /SEC /R:2 /W:300 /LOG+:%BackupLog% /NFL /NDL
endlocal
~bp
why not use robocopy since you mentioned it?
R2 has it already.
R2 has it already.
@yo_bee
Don't understand your command, can you clarify, the script is using robocopy, is there some capability that could be leveraged to simplify the folder roll?
~bp
Don't understand your command, can you clarify, the script is using robocopy, is there some capability that could be leveraged to simplify the folder roll?
~bp
my bad sorry guys
ASKER
@yo_bee
Robocopy wasn't on this server. I had to install the server tools. I put the bit about requiring it in to help the person writing the script.
Robocopy wasn't on this server. I had to install the server tools. I put the bit about requiring it in to help the person writing the script.
ASKER
@billprew
That doesn't work either.
Initially the script failed with "The system cannot find the file specified." I figured that was because there is no BioPlugin.5 directory yet, so I commented out that line.
With that line gone, the copies take place but the renaming doesn't.
That doesn't work either.
Initially the script failed with "The system cannot find the file specified." I figured that was because there is no BioPlugin.5 directory yet, so I commented out that line.
With that line gone, the copies take place but the renaming doesn't.
I still maintain the original script looks OK excep the \ on the end of the original path. Where does it break down if you change that (though Bill's script does effectively the same but neater!).
Steve
is this the wrong way around for the ren thugh:
if exist "%BackupPath%\%FolderName% .%%A" ren "%BackupPath%\%FolderName% .!Num2!" "%FolderName%.%%A"
i.e. if bioplugin.5 exists then rename bioplugin.4 to bioplugin.5, should that not be %%a and !num2! swapped.
Haven't got time to follow the logic through more than that at the mo. though sorry.
Steve
Steve
is this the wrong way around for the ren thugh:
if exist "%BackupPath%\%FolderName%
i.e. if bioplugin.5 exists then rename bioplugin.4 to bioplugin.5, should that not be %%a and !num2! swapped.
Haven't got time to follow the logic through more than that at the mo. though sorry.
Steve
Sorry, small adjustment needed, this should work now.
@echo off
REM Make sure you have RoboCopy installed. It is part of the
REM Server Tools and I am calling it from the default location.
REM Set the variables for renaming, path, and number of backups
setlocal EnableDelayedExpansion
set BackupPath=C:\backups\M2Sys
set FolderName=BioPlugin
set CopyFrom=C:\Program Files\BioPlugin
set CopyTo=%BackupPath%\%FolderName%.1
set MirrorLoc=\\arad\h$\Ariel\M2Sys\%FolderName%
set BackupLog=%BackupPath%\BackupLog.txt
set ServiceLog=%BackupPath%\Services.log
set Services="M2SYS Fingerprint Server"
set Num=5
REM Remove oldest backup folder
if exist "%BackupPath%\%FolderName%.%Num%" rmdir /S /Q "%BackupPath%\%FolderName%.%Num%"
REM Roll backup dirs #1 thru #5 to #2 to #6 to make room for new #1
for /L %%A in (%Num%,-1,2) do (
set /A Num2=%%A-1
if exist "%BackupPath%\%FolderName%.!Num2!" ren "%BackupPath%\%FolderName%.!Num2!" "%FolderName%.%%A"
)
goto :EOF
REM - Start the service, and write all output to logfile
for %%S in (%Services%) do (
echo %DATE% %TIME% >> "%ServiceLog%"
net stop "%%~S" >> "%ServiceLog%" 2>&1
)
REM - Copy the entire BioPlugin directory to the C:\Backups\M2Sys\BioPlugin
"C:\Program Files\Windows Resource Kits\Tools\robocopy.exe" "%CopyFrom%" "%CopyTo%" /COPYALL /MIR /SEC /R:2 /W:10 /LOG+:%BackupLog% /NFL /NDL
REM - Start the service, and write all output to logfile
for %%S in (%Services%) do (
echo %DATE% %TIME% >> "%ServiceLog%"
net start "%%~S" >> "%ServiceLog%" 2>&1
)
REM - Mirror the entire %BackupPath% to your preferred server.
"C:\Program Files\Windows Resource Kits\Tools\robocopy.exe" "%BackupPath%" "%MirrorLoc%" /COPYALL /MIR /ZB /SEC /R:2 /W:300 /LOG+:%BackupLog% /NFL /NDL
endlocal
~bp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That did it.
Do you mind if I provide the script for other people who are using the software? If so, what copyright/disclaimer do you want on it?
Do you mind if I split the points with dragon-it? I was thinking about a 450/50 or 400/100 split.
Thanks again.
Do you mind if I provide the script for other people who are using the software? If so, what copyright/disclaimer do you want on it?
Do you mind if I split the points with dragon-it? I was thinking about a 450/50 or 400/100 split.
Thanks again.
Great, glad that was useful.
No copyright or disclaimer needed, if you want you could put a small plug in for me by giving credit to billprew on Experts Exchange and maybe include my member page link: https://www.experts-exchange.com/M_383101.html
I'm fine with however you handle the points, no problems there. Steve (dragon-it) and I cross paths quite a bit and try to keep each other on our toes and compare different approaches to problems.
~bp
No copyright or disclaimer needed, if you want you could put a small plug in for me by giving credit to billprew on Experts Exchange and maybe include my member page link: https://www.experts-exchange.com/M_383101.html
I'm fine with however you handle the points, no problems there. Steve (dragon-it) and I cross paths quite a bit and try to keep each other on our toes and compare different approaches to problems.
~bp
ASKER
Thanks. I am so appreciative that people like you and Steve give of yourself like this.
Dan
Dan
fine by me, thanks. didnt have much time to look at this one anyway sorry, only passing really.
Steve
Steve
ASKER
If anyone is interested, here is the sanitized and commented version. I put it under CC0 1.0.
@ECHO Off
REM - M2Sys backup script created by billprew on Experts Exchange
REM - http://www.experts-exchange.com/M_383101.html
REM - http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Q_27296311.html
REM -
REM - This script is distributed in the hope that it will be useful, but
REM - WITHOUT ANY WARRANTY; without even the implied warranty of
REM - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
REM -
REM - THE USER ACCEPTS ALL RISKS
REM -
REM - This work is released to the Creative Commons under CC0 1.0 Universal
REM -
REM - This script is known to run under Windows Server 2003 R2. It should run
REM - on any Windows system.
REM -
REM - You must have RoboCopy available. I have hard coded the location of the
REM - default install. You could put it elsewhere and change lines 73 and 82.
REM -
REM - RoboCopy is available for free from
REM - http://www.microsoft.com/download/en/details.aspx?id=17657
REM -
REM - The script will maintain a set of the last NUM backups and mirror them
REM - to a remote server. For the mirror feature to work you must change line
REM - 51 to something valid and accessible to the account that the script runs
REM - under.
REM -
REM - When I am setting up this system I first stop the service, then manually
REM - copy the data directory to my backup location. That way if something
REM - goes wrong I don't lose data. I then change the variables to match my
REM - environment and run the script repeatedly to make sure the delete
REM - routine is working. The logs will show if anything has failed. Finally I
REM - check the mirror location to make sure it is mirroring the local
REM - backups. If everything looks good I copy the working script to the
REM - mirror location (It sucks when you lose a hard drive and have to
REM - recreate the script) and schedule the task. I have used versions of this
REM - system with several databases with great success. I hope it works for
REM - you too.
REM -
REM -
@echo off
REM Set the variables for renaming, path, and number of backups
setlocal EnableDelayedExpansion
set BackupPath=C:\backups\M2Sys
set FolderName=BioPlugin
set CopyFrom=C:\Program Files\BioPlugin
set CopyTo=%BackupPath%\%FolderName%.1
set MirrorLoc=\\RemoteServer\FingerprintServer\M2Sys\%FolderName%
set BackupLog=%BackupPath%\BackupLog.txt
set ServiceLog=%BackupPath%\Services.log
set Services="M2SYS Fingerprint Server"
set Num=5
REM Remove oldest backup folder
if exist "%BackupPath%\%FolderName%.%Num%" rmdir /S /Q "%BackupPath%\%FolderName%.%Num%"
REM Roll backup dirs #1 thru #5 to #2 to #6 to make room for new #1
for /L %%A in (%Num%,-1,2) do (
set /A Num2=%%A-1
if exist "%BackupPath%\%FolderName%.!Num2!" ren "%BackupPath%\%FolderName%.!Num2!" "%FolderName%.%%A"
)
REM - Start the service, and write all output to logfile
for %%S in (%Services%) do (
echo %DATE% %TIME% >> "%ServiceLog%"
net stop "%%~S" >> "%ServiceLog%" 2>&1
)
REM - Copy the entire BioPlugin directory to the C:\Backups\M2Sys\BioPlugin
"C:\Program Files\Windows Resource Kits\Tools\robocopy.exe" "%CopyFrom%" "%CopyTo%" /COPYALL /MIR /SEC /R:2 /W:10 /LOG+:%BackupLog% /NFL /NDL
REM - Start the service, and write all output to logfile
for %%S in (%Services%) do (
echo %DATE% %TIME% >> "%ServiceLog%"
net start "%%~S" >> "%ServiceLog%" 2>&1
)
REM - Mirror the entire %BackupPath% to your preferred server.
"C:\Program Files\Windows Resource Kits\Tools\robocopy.exe" "%BackupPath%" "%MirrorLoc%" /COPYALL /MIR /ZB /SEC /R:2 /W:300 /LOG+:%BackupLog% /NFL /NDL
endlocal
Nicely done, thanks.
~bp
~bp
You also want to copy the script to the mirror location as part of the script? e.g.
xcopy /d "%~0" "%MirrorLoc%"
would copy only if changed. %0 is the batch file name.
xcopy /d "%~0" "%MirrorLoc%"
would copy only if changed. %0 is the batch file name.
ASKER
I prefer to do that manually, when I know it is working. That is the same reason I use FolderName.1 rather than just FolderName. That way I don't have an automated system copying over my precious copies.
(Rubs hands together and eyes gleam maniacally as he thinks about his precious backed-up data)
:-)
(Rubs hands together and eyes gleam maniacally as he thinks about his precious backed-up data)
:-)
ASKER
Sorry to all the people who aren't interested. If a moderator want's to move it, I won't mind at all.