Here's a backup script that I wrote that we use to replace the software that comes with Maxtor 100GB OneTouch software. We bought a bunch of these external USB hard drives for laptop user backup solutions, but the software that comes with them... well it sucks. It won't even configure on our new Acer laptops.
Anyway, this script backs up a user's data to an external drive:
1) Checks the existence of a drive E to be used as the backup drive
2) Checks how much space is required for a backup and compares it to the free space on the backup drive
3) If there is enough space, it'll backup the users
Desktop, My Documents, Favorites & Outlook folders
4) If there ISN'T enough space, it finds the oldest backup folder and deletes it to make room.
Enjoy!
'Backup script 1.0
'Backup's select components of the user's profile to a dated folder on an external hard drive
'Created 10.20.06
'Declarations
Set fso = WScript.CreateObject("Scripting.FileSystemObject") 'Standard File System Object
Set WshNetwork = WScript.CreateObject("WScript.Network") 'Standard Network Object
strUser = WshNetwork.UserName 'Pulls User Name, used to find user's profile folder
BackupDriveLetter = "E" 'The Drive Letter the Backup Hard Drive is set to.
'Asks to start the backup
X=MsgBox("Are you ready to create Backup on " & Date & " ?",36,"Computer Backup 1.0")
If X = 6 Then 'If MsgBox is answered YES
X=MsgBox("Make sure the External Hard Drive is plugged in and set to Drive Letter " & BackupDriveLetter & ":",48,"Computer Backup 1.0")
StartBackup() 'Start main Subroutine
Else
WScript.Quit 'If MsgBox is answered NO
End If
'Main Program
Sub StartBackup()
'Check for Backup Drive
If (Not fso.DriveExists(BackupDriveLetter)) Then
X=MsgBox("Can't find External Hard Drive on " & BackupDriveLetter & ":!" & vbCrLf & "Make sure it's plugged in and is set to " & BackupDriveLetter & ":!",16,"Backup Error")
WScript.Quit
End If
'Check free space on Backup Drive
Set BackupDrive = fso.GetDrive(BackupDriveLetter)
EFreeSpace = CDbl(FormatNumber(BackupDrive.FreeSpace/1024/1024,2)) 'Polls for free space on BackupDrive
'Check how much room backup will need
Set objFolder = FSO.GetFolder("C:\Documents and Settings\" & strUser & "\Desktop")
SpaceNeeded = SpaceNeeded + CDbl(FormatNumber((objFolder.Size/1024/1024),2)) 'Polls folder size, then adds it to total SpaceNeeded
Set objFolder = FSO.GetFolder("C:\Documents and Settings\" & strUser & "\My Documents")
SpaceNeeded = SpaceNeeded + CDbl(FormatNumber((objFolder.Size/1024/1024),2)) 'Polls folder size, then adds it to total SpaceNeeded
Set objFolder = FSO.GetFolder("C:\Documents and Settings\" & strUser & "\Favorites")
SpaceNeeded = SpaceNeeded + CDbl(FormatNumber((objFolder.Size/1024/1024),2)) 'Polls folder size, then adds it to total SpaceNeeded
Set objFolder = FSO.GetFolder("C:\Documents and Settings\" & strUser & "\Local Settings\Application Data\Microsoft\Outlook")
SpaceNeeded = SpaceNeeded + CDbl(FormatNumber((objFolder.Size/1024/1024),2)) 'Polls folder size, then adds it to total SpaceNeeded
Set objFolder = FSO.GetFolder("C:\Documents and Settings\" & strUser & "\Application Data\Microsoft\Outlook")
SpaceNeeded = SpaceNeeded + CDbl(FormatNumber((objFolder.Size/1024/1024),2)) 'Polls folder size, then adds it to total SpaceNeeded
'See if there is enough free space on Backup Drive for the backup.
If SpaceNeeded >= EFreeSpace Then 'If there isn't enough free space then...
Set objFolder = FSO.GetFolder(BackupDriveLetter & ":\") 'Get the BackupDrive
Set colFolders = objFolder.SubFolders 'Get the Subfolders on BackupDrive
For Each objSubFolder In colFolders 'For each subfolder.....
If Left(objSubFolder.Name,1) = "B" Then 'Check if the folder starts with a "B", this is to prevent deletion of non-backup folders.
If FTDName = "" Then FTDName = objSubFolder.Name 'Get Subfolder name if variables empty (first run)
If FTDDate = "" Then FTDDate = objSubFolder.DateCreated 'Get subfolder date if variables empty (first run)
If FTDDateDiff = "" Then FTDDateDiff = DateDiff("s", Now, objSubFolder.DateCreated) 'Gets the difference in seconds between NOW and the folder's timestamp
If FTDDateDiff > DateDiff("s", Now, objSubFolder.DateCreated) Then 'If the DateDifference for the previous folder is greater (newer) than the current folder...
FTDName = objSubFolder.Name 'Record a new folder name
FTDDate = objSubFolder.DateCreated 'Record a new folder Date
FTDDateDiff = DateDiff("s", Now, objSubFolder.DateCreated) 'Record a new folder DateDifference
End If
End If
Next
X=MsgBox("There is not enough room on the External Hard Drive!" & VbCrLf & "Backup Size: " & SpaceNeeded & "MB Free Space Available: " & EFreeSpace & "MB" & VbCrLf &"The oldest backup found is * " & FTDName & " * created on " & FTDDate & VbCrLf & "Delete this backup and start new backup?",36,"Backup Error")
If X = 6 Then 'If MsgBox is answered YES
FSO.DeleteFolder(BackupDriveLetter & ":\" & FTDName)
StartBackup()
WScript.quit
Else 'If MsgBox is answered NO
X=MsgBox("Free up space manually and run Backup again",16,"Backup Error")
WScript.quit
End If
End If
X=MsgBox("Ready to Backup " & SpaceNeeded & "MB of data." & VbCrLf & "Please click OK then WAIT until you get a Backup Complete message.",48,"Backup Starting")
'If there's enough free space, backup files
TodaysDate = ("B" & Month(Now) & Day(Now) & Year(Now) & "." & Hour(Now) & Minute(Now) & Second(Now)) 'Get Unique folder name for backup with a timestamp
FSO.CreateFolder(BackupDriveLetter & ":\" & TodaysDate) 'Create Unique folder
FSO.CopyFolder "C:\Documents and Settings\" & strUser & "\Desktop" , BackupDriveLetter & ":\" & TodaysDate & "\Desktop" , True 'Copy user's Desktop folder
FSO.CopyFolder "C:\Documents and Settings\" & strUser & "\My Documents" , BackupDriveLetter & ":\" & TodaysDate & "\My Documents" , True 'Copy user's My Documents folder
FSO.CopyFolder "C:\Documents and Settings\" & strUser & "\Favorites" , BackupDriveLetter & ":\" & TodaysDate & "\Favorites" , True 'Copy user's Favorites folder
FSO.CopyFolder "C:\Documents and Settings\" & strUser & "\Local Settings\Application Data\Microsoft\Outlook" , BackupDriveLetter & ":\" & TodaysDate & "\Outlook", True 'Copy user's Outlook folder
FSO.CopyFolder "C:\Documents and Settings\" & strUser & "\Application Data\Microsoft\Outlook" , BackupDriveLetter & ":\" & TodaysDate & "\Outlook", True 'Copy another user's Outlook folder
X=MsgBox("Done backing up "& SpaceNeeded & "MB of data to folder "& BackupDriveLetter & ":\"& TodaysDate,64,"Backup Complete")
End Sub
BTW, I just noticed that all of my indentation is gone from my script. Is there a way to post a script with indents in place?
EDIT(EBGREEN): I don't know why it lost your indentation, but I put it back in for you.
Thanks, Ebgreen, for added the tabs. I'll try several different ways to copy / paste my next script to maintain indents, its so much easier to read IF and For/Next statements.
<message edited by FatFingerTony on Friday, October 13, 2006 5:28 AM>