[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 978
  • Last Modified:

batch robocopy PC Backup

Using a batch file I need to backup some PCs but I need to check the space required for backup and the space available on the remote location and compare to make sure there is enough space. Its a mix of win7 and XP machines.
0
patrickm12981
Asked:
patrickm12981
  • 8
  • 6
1 Solution
 
Bill PrewCommented:
I don't think there is any way to do this with robocopy itself.

We could create a VBS script to do it, what is the source of the files to be copied?  Are you copying all files under a folder, or are they spread around?  What options are you using for ROBOCOPY, for example are you copying all files, or just new / changed, etc?  Also, will the files exist in the destination or are they guaranteed to be new?

All these things could affect the calculation of available free space to fit the copy so need more details.

~bp
0
 
patrickm12981Author Commented:
I have a batch file configured for the backup portion, it backs up the entire PC. I just need a way to get the space required for the backup and the space available on the backup location the compare the  two that way if there isn't enough space I have it try another location.
0
 
Bill PrewCommented:
Well, you could use a command like this to get the total size of a drive, and the free space.

WMIC LogicalDisk Get DeviceID^,FreeSpace^,Size^,Status /Format:csv

And this we can parse apart in a DOS FOR /F command.  The problem is that you would then need to calculate the used space on that drive, by subtracting the free space from the total. Simple in concept, but very hard in a DOS batch script, since it can't handle numbers this large.

In the past I have created a batch subroutine to do "long arithmetic" on numbers larger than the supported integer size of batch, but those do add a bit of bulk to the BAT code, so not sure if you want to go down that path?

The other approach would be to create a small VBS script that does some of the large number manipulations and returns a "fit" / "no fit" indication that the BAT script could then look at to decide how to proceed.

Thoughts?

~bp
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
patrickm12981Author Commented:
Sounds like vbs would be the way to go.
0
 
patrickm12981Author Commented:
This is my current script, I appreciate the help:

net use K: /delete
net use L: /delete
net use M: /delete
net use N: /delete

@echo off
cd \
SET TIMESTART=%TIME%

SET date=%date:~4,2%-%date:~7,2%-%date:~10,4%

::Map Drives

Net use K: "\\server\PCBackup" /user:**\***********************

Net use L: "\\server\PCBackup" /user:**\***********************

Net use M: "\\server\PCBackup" /user:**\***********************

Net use N: "\\server\PCBackup" /user:**\***********************

::Check Drive Space






::Set drive to be used

Set Usedrive=

MD "%Usedrive%:\%computername%_%date%\"

msiexec /i "\\server\SCRIPTS\rktools.msi" /q

cd "C:\Program Files\Windows Resource Kits\Tools"

ROBOCOPY c:\ "%Usedrive%:\%computername%_%date%\" /MT:32 /XJ /E /Z /R:1 /W:1 /XD *"Program Files" *Recent *Config.Msi *LocalService *NetworkService *NetHood *PrintHood *RECYCLER *WINDOWS *Quarantine *DELL *DRIVERS *TEMP *TMP *"TEMPORARY INTERNET FILES" *MSOCache *"System Volume Information" *Cookies *Google *MININT *SrvcLDAP *SrvcSMSCliConn1 *"Default User" *Administrator *"All Users" *"LiveBackup Cache" *SMSADMIN *SapWorkDir *SendTo *"Apple Computer" *ajiang *mnelson *pmartin *dkennedy *jpulis *faragon *BOOTCOPYPOOL.BIN *TZMove *sporter *itunes *Application Data\Adobe *Application Data\Java *Application Data\Sun /XF pagefile.sys ntldr mtdetect.com msdos.sys io.sys config.sys boot.ini autoexec.bat *.sqm *.torrent Data ntuser.ini ntuser.pol NTDETECT.COM index.dat desktop.ini thumbs.db *.ost *.mp3 *m4a *.tmp *.temp *.gif *.bmp *.bak *.wma *.m3u *.au *.psm *.stm *.wmf *.mpa *.mp4 *.vob *.png *.lnk *.wav *.sys

If %errorlevel% geq 16 goto XP

:XP

ROBOCOPY c:\ "%Usedrive%:\%computername%_%date%\" /XJ /E /Z /R:1 /W:1 /XD *"Program Files" *Recent *Config.Msi *LocalService *NetworkService *NetHood *PrintHood *RECYCLER *WINDOWS *Quarantine *DELL *DRIVERS *TEMP *TMP *"TEMPORARY INTERNET FILES" *MSOCache *"System Volume Information" *Cookies *Google *MININT *SrvcLDAP *SrvcSMSCliConn1 *"Default User" *Administrator *"All Users" *"LiveBackup Cache" *SMSADMIN *SapWorkDir *SendTo *"Apple Computer" *ajiang *mnelson *pmartin *dkennedy *jpulis *faragon *BOOTCOPYPOOL.BIN *TZMove *sporter *itunes *Application Data\Adobe *Application Data\Java *Application Data\Sun /XF pagefile.sys ntldr mtdetect.com msdos.sys io.sys config.sys boot.ini autoexec.bat *.sqm *.torrent Data ntuser.ini ntuser.pol NTDETECT.COM index.dat desktop.ini thumbs.db *.ost *.mp3 *m4a *.tmp *.temp *.gif *.bmp *.bak *.wma *.m3u *.au *.psm *.stm *.wmf *.mpa *.mp4 *.vob *.png *.lnk *.wav *.sys

echo. > d:\dcheck.txt

IF NOT EXIST D:\dcheck.txt GOTO DDriveSkip

del D:\dcheck.txt /Q

MD "%Usedrive%:\%computername%_%date%\D_Drive"

ROBOCOPY D:\ "%Usedrive%:\%computername%_%date%\D_Drive" /MT:32 /E /Z /R:1 /W:1 /XD *Recent *Config.Msi *"My Music" *LocalService *NetworkService *NetHood *PrintHood *RECYCLER *WINDOWS *Quarantine *DELL *DRIVERS *TEMP *TMP *"TEMPORARY INTERNET FILES" *MSOCache *"System Volume Information" *Cookies *Google *MININT *"Program Files" *SrvcLDAP *SrvcSMSCliConn1 *"Default User" *Administrator *"All Users" *"LiveBackup Cache" *SMSADMIN *SapWorkDir *SendTo *"Apple Computer" *TZMove /XF pagefile.sys ntldr mtdetect.com msdos.sys io.sys config.sys boot.ini autoexec.bat *.sqm *.torrent ntuser.ini ntuser.pol NTDETECT.COM index.dat desktop.ini thumbs.db *.ost

If %errorlevel% geq 16 goto XPD

:XPD

ROBOCOPY D:\ "%Usedrive%:\%computername%_%date%\D_Drive" /E /Z /R:1 /W:1 /XD *Recent *Config.Msi *"My Music" *LocalService *NetworkService *NetHood *PrintHood *RECYCLER *WINDOWS *Quarantine *DELL *DRIVERS *TEMP *TMP *"TEMPORARY INTERNET FILES" *MSOCache *"System Volume Information" *Cookies *Google *MININT *"Program Files" *SrvcLDAP *SrvcSMSCliConn1 *"Default User" *Administrator *"All Users" *"LiveBackup Cache" *SMSADMIN *SapWorkDir *SendTo *"Apple Computer" *TZMove /XF pagefile.sys ntldr mtdetect.com msdos.sys io.sys config.sys boot.ini autoexec.bat *.sqm *.torrent ntuser.ini ntuser.pol NTDETECT.COM index.dat desktop.ini thumbs.db *.ost

:DDriveSkip

SET TIMESTOP=%TIME%

ECHO %TIMESTART%
ECHO %TIMESTOP%

Open in new window

0
 
Bill PrewCommented:
I don't understand these statements:

If %errorlevel% geq 16 goto XP

:XP
Since there is nothing in between then, it will always proceed right into :XP regardless of the error code.  What was the intention here?

I suspect you are trying to use multithreaded mode, and if that failes (non Windows 7 machines) then drop back to unthreaded mode?  If so I can adjust for that.

~bp
0
 
patrickm12981Author Commented:
Thats correct - I noticed that too.
0
 
Bill PrewCommented:
Also, you seemed to indicate just checking the C: drive, but what about the D: drive?  Do we really need to check both of them combined to find a backup drive they will fit on?

~bp
0
 
Bill PrewCommented:
This is coming along well, hope you're okay with me doing a bit of rewrite along the way to try and simplify it a bit?

~bp
0
 
patrickm12981Author Commented:
The D drive is not a necessity. I am ok with a little rewrite/tweaking.
0
 
Bill PrewCommented:
Okay, will try and wrap this up later tonight...

~bp
0
 
Bill PrewCommented:
Getting late, and I haven't tested the BAT file, but the VBS seems to work okay.  I tried to add enough comments to help you get the idea.  Take a look, proof read it all carefully, and let me know what questions you have, or if you get some testing done.  BAT file is first, VBS file is second.

@echo off
setlocal EnableDelayedExpansion

REM Move to root of current disk
cd \

REM Save our start time
set TimeStart=%TIME%

REM Build a date stamp for backup files in YYYY-MM-DD
set Stamp=%date:~10,4%-%date:~4,2%-%date:~7,2%-

REM Define possible backup drives, and the corresponding server and share for each
set Drives=K,L,M,N
set Share[K]=\\server1\PCBackup
set Share[L]=\\server2\PCBackup
set Share[M]=\\server3\PCBackup
set Share[N]=\\server4\PCBackup

REM define robocopy options and exclusions for C: and D: backup
set Options=/XJ /E /Z /R:1 /W:1
set ExcludeDirs[C]=*"Program Files" *Recent *Config.Msi *LocalService *NetworkService *NetHood *PrintHood *RECYCLER *WINDOWS *Quarantine *DELL *DRIVERS *TEMP *TMP *"TEMPORARY INTERNET FILES" *MSOCache *"System Volume Information" *Cookies *Google *MININT *SrvcLDAP *SrvcSMSCliConn1 *"Default User" *Administrator *"All Users" *"LiveBackup Cache" *SMSADMIN *SapWorkDir *SendTo *"Apple Computer" *ajiang *mnelson *pmartin *dkennedy *jpulis *faragon *BOOTCOPYPOOL.BIN *TZMove *sporter *itunes *Application Data\Adobe *Application Data\Java *Application Data\Sun
set ExcludeFiles[C]=pagefile.sys ntldr mtdetect.com msdos.sys io.sys config.sys boot.ini autoexec.bat *.sqm *.torrent Data ntuser.ini ntuser.pol NTDETECT.COM index.dat desktop.ini thumbs.db *.ost *.mp3 *m4a *.tmp *.temp *.gif *.bmp *.bak *.wma *.m3u *.au *.psm *.stm *.wmf *.mpa *.mp4 *.vob *.png *.lnk *.wav *.sys
set ExcludeDirs[D]=*Recent *Config.Msi *"My Music" *LocalService *NetworkService *NetHood *PrintHood *RECYCLER *WINDOWS *Quarantine *DELL *DRIVERS *TEMP *TMP *"TEMPORARY INTERNET FILES" *MSOCache *"System Volume Information" *Cookies *Google *MININT *"Program Files" *SrvcLDAP *SrvcSMSCliConn1 *"Default User" *Administrator *"All Users" *"LiveBackup Cache" *SMSADMIN *SapWorkDir *SendTo *"Apple Computer" *TZMove
set ExcludeFiles[D]=pagefile.sys ntldr mtdetect.com msdos.sys io.sys config.sys boot.ini autoexec.bat *.sqm *.torrent ntuser.ini ntuser.pol NTDETECT.COM index.dat desktop.ini thumbs.db *.ost

REM Disconnect all drives
for %%A in (%Drives%) do net use %%A: /delete

REM Map all drives
for %%A in (%Drives%) do net use %%A: "!Share[%%A]!" /user:**\***********************

REM Check Drive Space, find first drive that we can fit onto
if exist D:\dcheck.txt (set CheckDrives=C:,D:) else (set CheckDrives=C:)
set UseDrive=
for %%A in (%Drives%) do (
  REM Only need to check until we find the first drive with space, and set UseDrive
  if NOT DEFINED UseDrive (
    REM Call VBS to see if used space on first drive will fit in available space of second drive
    cscript EE27872274.vbs "%CheckDrives%" "%%A:"
    REM VBS returns non zero if it will it, zero if it wont
    if %ERRORLEVEL% NEQ 0 Set UseDrive=%%A
  )
)

REM If we couldn't find a drive with enough space, exit now
if NOT DEFINED UseDrive (
  echo No available drive currently has enough space.
  exit /b
)

REM Create a folder to backup to
md "%UseDrive%:\%COMPUTERNAME%_%Stamp%\"

REM Install the resource kit
msiexec /i "\\server\SCRIPTS\rktools.msi" /q

REM Switch to the resource kit folder
cd "C:\Program Files\Windows Resource Kits\Tools"

REM Do the backup
robocopy c:\ "%UseDrive%:\%COMPUTERNAME%_%Stamp%\" /MT:32 %Options% /XD %ExcludeDirs[C]% /XF %ExcludeFiles[C]%
if %errorlevel% geq 16 (
  robocopy c:\ "%UseDrive%:\%COMPUTERNAME%_%Stamp%\" %Options% /XD %ExcludeDirs[C]% /XF %ExcludeFiles[C]%
)
echo. > d:\dcheck.txt

REM See if we need to copy D: drive too
if exist D:\dcheck.txt (
  del D:\dcheck.txt /Q

  REM Create a folder to backup to
  md "%UseDrive%:\%COMPUTERNAME%_%Stamp%\D_Drive"

  REM Do the backup
  robocopy D:\ "%UseDrive%:\%COMPUTERNAME%_%Stamp%\D_Drive" /MT:32 %Options% /XD %ExcludeDirs[D]% /XF %ExcludeFiles[D]%
  if %errorlevel% geq 16 (
    robocopy D:\ "%UseDrive%:\%COMPUTERNAME%_%Stamp%\D_Drive" %Options% /XD %ExcludeDirs[D]% /XF %ExcludeFiles[D]%
  )
)

REM Report start and end time
set TimeStop=%TIME%

echo Start : %TimeStart%
echo Ended : %TimeStop%

Open in new window

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input and output file names from command line parms
If (WScript.Arguments.Count > 0) Then
  sInfile = WScript.Arguments(0)
Else
  WScript.Echo "No input filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
Else
  WScript.Echo "No output filename specified."
  WScript.Quit
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Open input and output files
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)

' Read input file line by line
Do While Not oInfile.AtEndOfStream
   sLine = oInfile.ReadLine
   sVersion = oFSO.GetFileVersion(sLine)
   oOutfile.WriteLine sLine & "," & sVersion
Loop

' Cleanup and end
oInfile.Close
oOutfile.Close
Set oInfile = Nothing
Set oOutfile = Nothing
Set oFSO = Nothing

Open in new window

~bp
0
 
patrickm12981Author Commented:
Thannk you!
0
 
Bill PrewCommented:
Welcome.

~bp
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now