Saturday, April 5, 2008

Copying one big drive/disk/folder to 2 or 3 smaller drives/disks

I needed to copy one big drive (~700GB) to 3 smaller drives.  Since this was going to take a while I didn't want to have to deal with any interactive input.  I basically wanted to copy files to the first drive until it was full then start copying to the 2nd until it was full and, lastly, to copy to the 3rd drive.

 

A batch file to do this follows.  It needs to run from an xp or higher command shell with delayed expansion enabled (e.g., cmd /v:on) as well as command extensions enabled (the default on windows server 2003).

 

This isn't terribly well tested but it worked for my purposes.  If the script encounters a file too big to be stored on the first drive it tries copying it to the second; if it's too big for the second it tries copying it to the third.  If that fails then it prints a line in a copyspan_errors.txt file with the name of the file.

 

Also, it uses the archive attribute to keep track of what should and should not be copied - as long as no other programs are modifying this bit then it should be safe to stop and restart the script with the same arguments.  I ran it with "copyspan.bat d:\ f: g: h:" so that everything on the very large d:\ drive would be backed up to f:, g: and h:. 

 

The batch files contents are:

 

@echo off

REM This batch must run from a command prompt that has
REM delayed expansion enabled (/V:on)

set SRC=%1
set DEST1=%2
set DEST2=%3
set DEST3=%4


REM set the archive bit on the source
REM echo %DATE% %TIME% Setting archive bit in %SRC%
attrib +A "%SRC%*" /S /D


echo %DATE% %TIME% Copying files
rem /M = only copy archive files then clear archive bit
set CPCMD=xcopy /M /F /H /K /Y

if exist copyspan_errors.txt del /q copyspan_errors.txt

set ATTR=
for /R "%SRC%" %%F IN (*) DO (
  for /F "tokens=1*" %%i in ('attrib "%%F"') DO set ATTR=%%i

  if "!ATTR!" EQU "A" (
    %CPCMD% "%%F" "%DEST1%%%~pF"

    if !ERRORLEVEL! NEQ 0 %CPCMD% "%%F" "%DEST2%%%~pF"

    if !ERRORLEVEL! NEQ 0 %CPCMD% "%%F" "%DEST3%%%~pF"

    if !ERRORLEVEL! NEQ 0 echo "%DATE% %TIME% ## error copying %%F" >> copyspan_errors.txt
  ) ELSE (
    echo Skipping already archived file "%%F"
  )
)

:end

echo %DATE% %TIME% Copy complete

No comments :

Post a Comment