Backup Script

Author Message
FatFingerTony

  • Total Posts : 5
  • Scores: 0
  • Reward points : 0
  • Joined: 10/10/2006
  • Location: Grand Rapids, MI
  • Status: offline
Backup Script Tuesday, October 10, 2006 7:06 AM (permalink)
0
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>
 
#1
    Meg

    • Total Posts : 123
    • Scores: 6
    • Reward points : 0
    • Joined: 7/13/2006
    • Location: Australia
    • Status: offline
    RE: Backup Script Wednesday, October 11, 2006 11:33 PM (permalink)
    0
    I imagine the administrators know about it or someone else has mentioned it but I see your point, tabs don't paste across but they show up as a single space
    http://www.visualbasicscript.com/m_38613/mpage_1/key_/tm.htm#38614
    But it looks like previewing a post will remove one leading blank space.
     
    I tend to write flat code anyway so I can live with it.
     
    #2
      yosamit3

      • Total Posts : 1
      • Scores: 0
      • Reward points : 0
      • Joined: 6/12/2007
      • Status: offline
      RE: Backup Script Tuesday, June 12, 2007 4:07 AM (permalink)
      0
      How could this be changed where you could select the user profile as opposed to the one you are logged in with?
       
      #3
        ickleric

        • Total Posts : 92
        • Scores: 0
        • Reward points : 0
        • Joined: 11/15/2006
        • Status: offline
        RE: Backup Script Monday, June 25, 2007 9:14 PM (permalink)
        0
        Just been looking at this, and it looks great. However when i tried it on my PC is comes up with an error message.
         
        PATH NOT FOUND (Line 31, Char2)
         
        Any ideas? Also when it does work, does it just back the current logged on users stuff or does it go through all the profiles on the machine? As i need it to do all profiles.
         
        #4
          Romero775

          • Total Posts : 1
          • Scores: 0
          • Reward points : 0
          • Joined: 10/19/2009
          • Status: offline
          Re: RE: Backup Script Monday, October 19, 2009 9:37 AM (permalink)
          0
          This script works great.

          Is there a way to add a few more items to the script.
          1. A selection to "BackUp" or "Restore" data.
          2. Save "IPConfig /All' settings to a IPConfig text file in a IPConfig folder (not to be restored).
          3. Create a copy of the printers installed - mainly network printers (this info will be used to reinstall the printers after a computer refresh/windows reinstall).
          4. The option to selecting the drive letter you want to use (some computers use card readers and the external drive to store the data would vary) this would be a plus.

          Thank you.
           
          #5

            Online Bookmarks Sharing: Share/Bookmark

            Jump to:

            Current active users

            There are 0 members and 1 guests.

            Icon Legend and Permission

            • New Messages
            • No New Messages
            • Hot Topic w/ New Messages
            • Hot Topic w/o New Messages
            • Locked w/ New Messages
            • Locked w/o New Messages
            • Read Message
            • Post New Thread
            • Reply to message
            • Post New Poll
            • Submit Vote
            • Post reward post
            • Delete my own posts
            • Delete my own threads
            • Rate post

            2000-2012 ASPPlayground.NET Forum Version 3.9