• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

Backup Script

I need 2 scripts.

1.  I plug in my external drive. Double Click on something(like a .vbs). And automatically backup everything that user needs before i wipe it.(I believe that would be the documents and settings folder)
2. I plug in my external drive. Double Click something. And automatically everything is restored.(My documents,- word docs, ppts, pics,etc, that was on there desktop-,IE Favorites, Firefox Favorites)
  • 3
  • 3
  • 2
  • +1
1 Solution
You'd be surprised how good the files and settigns transfer wizard in Windows it:
(info here:)

Other option is maybe disk imaging software:
Paid, but good:
Acronis Trueimage

Free, and apparently okay:
DriveImage XML: http://www.runtime.org/driveimage-xml.htm
iRestorer: http://irestorer.parsaspace.ir/

Hope this is of some help.
So who's to stop the client from saving important personal data in C:\Joe's Cool Stuff?

Or what if they downloaded a cd ripping program that stores the ripped files in C:\Program Files\AwesomeRipperPro\output ?

Restoring a user's personal files after wiping their computer is a manual task. Typically the My Documents, Favourites, and a couple other minor things cna be done automatically, but everything else has to be done by hand.

For that reason, it is crucially important you have a FULL bare metal backup of the computer before you start reformatting it. That way, when the client comes back two weeks later in a panic because they just realized half their music collection was actually not in the My Music folder, or that their entire company financial records was stored in the \Program Files\Quickbooks folder, you have the data and don't get into a mess.

That said, I do two. First I use driveimage xml to make an image-based backup. DriveImageXML is great if you need to restore windows in it's full glory - boot record and all.

Then I use robocopy to make a file-level backup of every file. I just do the latter so that it is fairly painless to copy the files back later. Again, with robocopy.
I use the attached code to set-up backup for clients - a script I wrote.

If you put it on a USB drive, you need two other files:

cscript backup.vbs

action="Backup Documents and Settings"

Save the files above (they are just normal text files just rename as shown), then when you insert the USB drive you are offered the option to backup.  By default it copies everything in the Documents and Settings folder to a backup folder on the USB drive.

So that is part 1 of your question.  Part 2 should actually be another question.  However, the code below can easily be modified to do part 2 too.

The advantage of using the script below (in my opinion) is that it doesn't crash (any errors it will just carry on), it doesn't copy files that don't need copying (so runs much quicker after the first copy), and saves the files in a way easily recoverable (i.e. doesn't need third party software to read an image file).
' Backup Script
'on error resume next
' Logging levels:
' 0 = no logging
' 1 = log failures only
' 2 = log everything
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const vbReadOnly = 1, vbHidden = 2, vbSystem = 4, vbVolume = 8
Const vbDirectory = 16, vbArchive = 32, vbAlias = 64, vbCompressed = 128
dim loggingLevel
dim sourceFolder, targetFolder
dim logFile
dim filesChecked
dim filesCopied
dim foldersChecked
dim filesFailed
' Read backup parameters
set fso=CreateObject("Scripting.FileSystemObject")
if WScript.Arguments.Count=0 then
	' No arguments - assume running on USB drive and set defaults
	WScript.echo sourceFolder
	WScript.echo targetFolder
	if WScript.Arguments.Count>3 then
	end if
end if
' Create/open log file
if loggingLevel>0 then
   if not fso.FileExists(logFile) then
      set logFileTS=fso.CreateTextFile(logFile,false)
      if err.number<>0 then
      end if
      set logFileTS=fso.OpenTextFile(logFile,ForAppending)
      if err.number<>0 then
      end if
   end if
end if
if loggingLevel>0 then
	logFileTS.WriteLine "Backup started: " & Now
	logFileTS.WriteLine "Source: " & sourceFolder
	logFileTS.WriteLine "Target: " & targetFolder
	logFileTS.WriteLine "Logging Level: " & loggingLevel
end if
CopyFolder sourceFolder, targetFolder
if loggingLevel>0 then
	logFileTS.WriteLine "Backup finished: " & Now
	logFileTS.WriteLine "Total folders backed up: " & foldersChecked
	logFileTS.WriteLine "Total files checked: " & filesChecked
	logFileTS.WriteLine "Total files copied: " & filesCopied
	logFileTS.WriteLine "Total files failed: " & filesFailed
end if
set fso=nothing
Sub CopyFolder(sourceFolder, targetFolder)
    ' Copies the source folder to the destination folder...
	dim dirList()
	dim sourceFolderObject
	dim f
    dim firstRedim
    On Error Resume Next
    If InStr(1, targetFolder, sourceFolder, vbTextCompare) > 0 Then
	    if not (mid(sourceFolder, 2,1)=":" and mid(targetFolder,2,1)=":" and Left(sourceFolder,1)<>left(targetFolder,1)) then
           ' Source folder is the target folder... don't copy! (or recursion will kick in badly...)
		   AddLog "Target folder same as source folder... process halted", 1
           Exit Sub
		end if
    End If
    firstRedim = True
    If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
    If Right(targetFolder, 1) <> "\" Then targetFolder = targetFolder & "\"
    If Not fso.FolderExists(Left(targetFolder, Len(targetFolder) - 1)) Then
        BuildPath targetFolder
    End If
    ' Get list of directories
    set sourceFolderObject=fso.GetFolder(Left(sourceFolder, Len(SourceFolder)-1))
	for each f in sourceFolderObject.SubFolders
        If f.Name <> "." And f.Name <> ".." Then
			' Add directory to dirList
			If firstRedim Then
				ReDim dirList(0)
				firstRedim = False
				ReDim Preserve dirList(UBound(dirList) + 1)
			End If
			dirList(UBound(dirList)) = f.Name
		End If
    ' Now copy files
	if sourceFolderObject.Files.Count>0 then
		for each f in sourceFolderObject.Files
	            ' Copy this file (if different from target folder)!
	        If FilesDiffer(sourceFolder & f.Name, targetFolder & f.Name) Then
	            ' Files don't match, so copy
	            If Not fso.FolderExists(targetFolder) Then BuildPath targetFolder
	            fso.CopyFile sourceFolder & f.Name, targetFolder & f.Name, True
	            If Err.Number = 0 Then
	                FilesCopied = FilesCopied + 1
	                AddLog "Copied " & sourceFolder & f.Name, 2
	                AddLog "FAILED TO COPY (" & Err.Description & ") " & sourceFolder & f.Name, 1
	            End If
	            AddLog "Skipped " & sourceFolder & f.Name, 2
	        End If
	end if
    ' Now recurse directories
    If Not firstRedim Then
        ' Directories need copying
        For i = 0 To UBound(dirList)
            CopyFolder sourceFolder & dirList(i), targetFolder & dirList(i)
        End If
    If Err.Number <> 0 Then Err.Clear
End Sub
Private Function FilesDiffer(sourceFile, targetFile)
    ' Returns true is the given files are different
	dim same, sf, tf
    On Error Resume Next
	same = True
    Set sf = fso.getfile(sourceFile)
    Set tf = fso.getfile(targetFile)
    If Err.Number <> 0 Then
        ' File not present, so need to copy!
        same = False
        If sf.Size <> tf.Size Then same = False
        If sf.DateLastModified > tf.DateLastModified Then same = False
    End If
	set sf=nothing
	set tf=nothing
    FilesDiffer = Not same
    Err.Clear ' Just to make sure we're not passing any error messages back
End Function
Public Sub BuildPath(path)
    ' Builds the specified path.  Starts from the lowest level, creating each
    ' directory in turn.
    On Error Resume Next
    i = 1
    If Left(chkPath, 2) = "\\" Then i = 2
        chkPath = PathDepth(path, i)
        If Not fso.FolderExists(chkPath) Then
            ' Folder needs creating
            fso.CreateFolder chkPath
            End If
        i = i + 1
    Loop Until fso.FolderExists(path) Or Err.Number <> 0
    If Err.Number <> 0 Then Err.Clear
End Sub
Private Function PathDepth(FullPath, RequiredDepth)
    ' Returns the path to the specified depth
    ' (e.g. c:\1\2\3\4\, 1 will return c:\1 )
    On Error Resume Next
    If Left(FullPath, 2) = "\\" Then CutOffPoint = 2
    For i = 0 To RequiredDepth
        CutOffPoint = InStr(CutOffPoint + 1, FullPath, "\")
        If CutOffPoint = 0 Then Exit For
    If (CutOffPoint = 0) Or (Err.Number <> 0) Then
        PathDepth = FullPath
        PathDepth = Left(FullPath, CutOffPoint - 1)
        End If
End Function
Sub AddLog(LogText, LogLevel)
    ' Adds a log entry
    ' Check we should be logging this entry...
    wscript.echo LogText
    if loggingLevel<=LogLevel then exit sub
    ' Add to log file
    On Error Resume Next
    logfileTS.Writeline LogText
    If Err.Number <> 0 Then Err.Clear
End Sub
sub USBAutoConfigure
	' Determine backup location based on script location
	strDrive=Left(WScript.ScriptFullName,InStr(1, WScript.ScriptFullName, ":"))
	targetFolder=strDrive & "\Backup\Documents and Settings"
	' Determine source location based on Docs & Settings folder - doesn't support redirection yet
	set WshShell = WScript.CreateObject("WScript.Shell")
	set oWSH    = CreateObject("WScript.Shell")
	sAllUsersProfile    = oWSH.ExpandEnvironmentStrings("%ALLUSERSPROFILE%")
	set oWSH=nothing
	Set oAllUsersFolder = fso.GetFolder(sAllUsersProfile)
	sProfilesRoot       = oAllUsersFolder.ParentFolder
	' Set logging
	logFile=strDrive & "\backup log.txt"
end sub

Open in new window

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Though it must be said, I agree with Frosty555's comments in this case.  If you are wanting to use this kind of thing before wiping a computer, you need a full image - you cannot presume that everything you need is under Documents and Settings.  Or anywhere, in fact.  I'm often amazed where people put things, and also where little known programs or such store their configuration data (and of course, it's too late to check these things once the machine is wiped...).

While the above script could also be reconfigured to backup the whole drive, it would fail at any files that are locked (where imaging software wouldn't).  So the backup couldn't be considered complete.
Yeah, the script above can be considered a time-saving utility, but not a rigorous backup by any means.

Still though, I just use robocopy to copy the whole hard drive. It's real easy:

ROBOCOPY C:\ E:\Backups\SomeClient\CDrive /e /zb /r:3 /w:5 /v /eta /XD "Temporary Internet Files" "C:\Recycler" /XF "pagefile.sys" "hiberfil.sys"

That copies everything from the C drive, retries up to three times per file but persists through if it fails, and doesn't copy the Temporary Internet Files, Recycling Bin, or pagefile/hibernation files.
Though Robocopy also fails on locked files, which can be a major issue when doing a full-system backup.  For example, database files will often be locked if a program is accessing them... yet they contain a lot of data, and are definitely files that need to be copied.  So for a true backup, I would say you need a utility that backs up with shadow copy, or force a reboot to ensure it can back up the files e.g. TrueImage or similar.
zboxAuthor Commented:
Hey thanks so much for all the great responses. Please allow me sometime to review the different solutions and ill respond asap.
zboxAuthor Commented:
Hey great advice all.  I now have acronis true image, which I can use to backup.  I don't to image though. If someone had a virus i wouldn't want to image a partition with a virus on it(callmecheez)

good point frosty, I figured I'd end up checking the root no matter what :-\   but yea robocopy should work pretty well and if not ill definately end up using it for something.

still figuring that script out pomegranite. looks pretty cool and thanks for the heads up on the database thing, im working on my access skills now.

but great point callmecheez  i had no idea the file and transfer wizard was that robust.

zboxAuthor Commented:
the file and transfer wizard is pretty nice.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now