Solved

Batch file for backups

Posted on 2011-09-07
22
382 Views
Last Modified: 2012-06-27
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

@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

Open in new window


0
Comment
Question by:dan_computerx
  • 8
  • 8
  • 4
  • +1
22 Comments
 

Author Comment

by:dan_computerx
Comment Utility
The UI won't allow me to set the zone as "Windows Batch."  It only lets me drill down as far as "Scripting."

Sorry to all the people who aren't interested.  If a moderator want's to move it, I won't mind at all.
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 50 total points
Comment Utility
The only thing I noticed straight off is you are referring to %backuppath%%foldername% without a \ and there is no \ on the end of backuppath or start of foldername variables.  I would suggest as used in other places for filenames yuou need a \ on the end of this:

Set BackupPath=C:\backups\M2Sys\

Steve
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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

Open in new window

~bp
0
 

Author Comment

by:dan_computerx
Comment Utility
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.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Okay, I'll see if I can cleanup a bit.

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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

Open in new window

~bp
0
 
LVL 21

Expert Comment

by:yo_bee
Comment Utility
why not use robocopy since you mentioned it?
R2 has it already.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
@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
0
 
LVL 21

Expert Comment

by:yo_bee
Comment Utility
my bad  sorry guys
0
 

Author Comment

by:dan_computerx
Comment Utility
@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.
0
 

Author Comment

by:dan_computerx
Comment Utility
@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.

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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

Open in new window

~bp
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 450 total points
Comment Utility
Oops, one small not, remove the "goto :EOF" line, I had that in there for some testing here, since I was focusing on the folder rename, and not the rest of the script.

~bp
0
 

Author Comment

by:dan_computerx
Comment Utility
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.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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: http://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
0
 

Author Comment

by:dan_computerx
Comment Utility
Thanks.  I am so appreciative that people like you and Steve give of yourself like this.

Dan
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
fine by me, thanks.  didnt have much time to look at this one anyway sorry, only passing really.

Steve
0
 

Author Comment

by:dan_computerx
Comment Utility
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

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Nicely done, thanks.

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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.
0
 

Author Comment

by:dan_computerx
Comment Utility
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)

:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now