Thursday, April 28, 2011

robocopy for launching multiple concurrent sessions

Hi Readers,

I am doing a project in which we have to copy large amount of data from one datacenter to another.

For this I have researched a bit & found a script that can perform the job but my requirement was to get input data from a file

so that I can control the robocopy process just by editing the file.

Script number 1:(found on internet)

Create a temp folder as staging folder to control the sessions, create sub folders(folders that are under root source folders)  inside temp folders

Option Explicit

Dim objShell, objFSO
Dim strOptions, strLog, strSource, strDestination, strFolder
Dim txtFile, strFileName, strPath
Dim objBaseFolder, objFolder
Dim Strlogfile
Set objShell = CreateObject("WScript.Shell")      ' Instantiate the Windows Script Host
Set objFSO = CreateObject("Scripting.FileSystemObject")      'Instatiate the File System Object (FSO)

Set objBaseFolder = objFSO.GetFolder("C:\Temp")

For Each objFolder In objBaseFolder.SubFolders
  strFolder = objFolder.Name
  strSource      = "C:\testrcpy" & "\" & strFolder      'Copy source --- change accordingly
  strDestination = "D:\restore" & "\" & strFolder      'Copy Destination ---change accordingly
  strOptions       = " /MIR /copyall /r:3 /w:1 /zb /fp  /V /ts"      'Robocopy options.  See Below.

  strLog         = strFolder & ".log"      'Log file name.  Each job creates a unique log file.
  Strlogfile = "/log:"
  objShell.Run "robocopy" & " " & strSource & " " & strDestination & " " & strOptions & " " & Strlogfile & strlog

  WScript.Sleep 500
Next

Script Number 2:

Enhanced the script to read from file & implemented find command to log only differences.

Option Explicit

Dim objShell, objFSO
Dim strOptions, strLog, strSource, strDestination, strFolder
Dim txtFile, strFileName, strPath
Dim objBaseFolder, objFolder
Dim Strlogfind, strsource1, strDestination1, strFolder1
Set objShell = CreateObject("WScript.Shell")      ' Instantiate the Windows Script Host
Set objFSO = CreateObject("Scripting.FileSystemObject")      'Instatiate the File System Object (FSO)
Set txtFile =       objFSO.OpenTextFile("D:\ProductionRunning\readfile.txt", 1)   ' read from text file --change accordingly


do until txtFile.AtEndOfStream

strFolder      = txtFile.ReadLine   'read folder names
strFolder1 = chr(34) & strFolder & chr(34)  'add quotes to avoid space errors



  strSource      = "\\source-fs\d$\Shares" & "\" & strFolder      'Copy source
  strsource1 =  chr(34) & strSource & chr(34)  'add quotes

  strDestination = "\\destination-fs\e$\Restore\Shares" & "\" & strFolder      'Copy Destination
  strDestination1 = chr(34) & strDestination & chr(34) 'add quotes


  objShell.run "cmd /k D:\ProductionRunning\rcpy.bat "& " " & strSource1 & " " & strDestination1 & " " & strFolder1 &""  'batch file command

  WScript.Sleep 60000  ' launch session every 60 secs
loop
txtFile.Close

batch file:

robocopy %1 %2 /MIR /copyall /r:4 /w:1 /zb /fp  /V /ts | find /i /v "same" >> D:\ProductionRunning\logs\%3_Share_1.txt

if you have some similar requirement where you have to copy & sync data , above scripts can be utilized.

No comments:

Post a Comment