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!
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
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: