Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Backup script

 
Logged in as: Guest
arrSession:exec spGetSession 2,16,31942
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Post a VBScript >> Backup script
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Backup script - 3/7/2006 1:33:09 AM   
  mcds99


Posts: 424
Score: 4
Joined: 2/28/2006
Status: offline
This script is one I did last year. It backs up the local profile (c:\documents and settings\"username") locally and copies it to the home drive.
Be sure to look at this script in detail, it does some date manipulation and deletes old files in specific directories.

Sam.

'### Written by: Sam MacDonald
'### 12/29/05
'### This script backs up the logged on users Profile
'### It can be altered to backup only specific data
'### It copies the backup to the users H: home drive
'### Dependencies, Backup Operators Group membership
'### Only Pre-Defined Variables and Constructs can be used.
Option Explicit
'### Define variables
'### For Date and Time
Dim xDay
Dim xMonth
Dim xYear
Dim bDate
Dim xHour
Dim xMinute
Dim xSecond
Dim bTime
Dim xDateTime
Dim xfilename
Dim fso1
Dim wrfile
Dim strFile
Dim strFolder
'### For Backup
Dim objShell
Dim strADAMInstanceName
Dim strADAMInstancePath
Dim strBackupPath
Dim strCommandLine
Dim strDescription
'### For User Name and Folder
Dim newfolder
Dim uname
Dim nfile
Dim nfolder
Dim lfolder
Dim rfolder
Dim strComputer
Dim objWMIService
Dim colComputer
Dim objComputer
Dim objFolder
Dim objFSO1
Dim objFSO2
Dim CleanPath
'### Define Constructs
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const conWindowStyle  =    1
Const conWaitOnReturn = True
'### File System Objects
Set objFSO1 = CreateObject("Scripting.FileSystemObject")
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
'### Remove old backup file from the Home drive
Set objFSO1 = CreateObject("Scripting.FileSystemObject")
objFSO1.DeleteFile("H:\*.bkf")
'### Get Username
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
   uname = objComputer.UserName
   newfolder = uname
   wscript.echo newfolder
Next
    nfolder = (replace(newfolder,"<dom>\","")) '### put your domain in <dom>
'     wscript.echo nfolder
'### Get Path Name
Set objFolder = objFSO1.GetFolder (("C:\Documents and Settings\") & (nfolder))
'wscript.echo objFolder
'### Aquire month day year
xDay = Day(Date)
xMonth = Month(Date)
xYear = Year(Date)
bDate = xMonth & xDay & xYear
'### Aquire hour minute second
xHour = (Hour(Now))
xMinute = (Minute(Now))
xSecond = (Second(Now))
bTime = xHour & xMinute & xSecond
'### Create File Name using date and time
xDateTime = bDate & bTime
strFile = "backup" & xDateTime
strFolder = objFolder
'### Backup Files
'strADAMInstanceName = strFile
strADAMInstancePath  = strFolder
strBackupPath  = "C:\backup\" & strFile & ".bkf"
strDescription = "Backup of User Files: " & strFile
strCommandLine = "cmd /c ""ntbackup backup """ & strADAMInstancePath & """ /snap:off /d """ & strDescription & """ /n
""User Backup"" /m Normal /l:f /f """ & strBackupPath & """"""
'WScript.Echo "Execute: " & vbNewLine & strCommandLine
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run strCommandLine, conWindowStyle, conWaitOnReturn
wScript.Echo "Backup Complete"
'### Copy to the network
lfolder = "C:\backup\" & strFile & ".bkf"
rfolder = "H:\"
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
objFSO2.CopyFile lfolder , rfolder
wScript.Echo strFile & ".bkf File Copied"
'### Delete the files in C:\backup Folder
Set objFSO1=CreateObject("Scripting.FileSystemObject")
   CleanPath = "C:\backup\"
For Each nFile In objFSO1.GetFolder(CleanPath).Files
   nFile.delete
Next
 
 
Post #: 1
 
 RE: Backup script - 3/7/2006 6:51:49 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Some comments:

1) The use of Option Explicit is a good thing and I applaud you for it.
2) Proper block indentation makes your code much easier to read.
3) You do not need to use 2 FileSystemObjects, just use the same one over and over again.
4) Commenting code is good and I applaud you for the comments that you have included. If you are putting code out for the world to use, it makes things a little less confusing if you remove comments that do not need to be there (i.e. code lines that you have commented out to prevent them from running.
5) In addition to not needing objFSO2 at all, you certainly do not need to instantiate it twice (you don't need to instantiate objFSO1 twice either).

As always, these observations are purely my opinion. You have a script that does what you want so in the spirit of pragmatism, it is by definition coded correctly. Here is the code with the chages that I suggested:


      

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to mcds99)
 
 
Post #: 2
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Post a VBScript >> Backup script Page: [1]
Jump to:





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
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts