Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Automated Backups With the Help of Windows...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Automated Backups With the Help of Windows Scripting
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 17
    2004-08-10

    Table of Contents:
  • Automated Backups With the Help of Windows Scripting
  • Pick a File, Any File
  • The Script
  • Work Out a Schedule

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Automated Backups With the Help of Windows Scripting - The Script


    (Page 3 of 4 )

    You're ready to write the script. Create a VBScript file in c:backup, or whatever you called the folder. If you're not sure how to do this, it's as simple as creating a text file, and changing its extension to '.vbs' instead of '.txt'. You can call it whatever you want, but backup.vbs will suffice. Here are the lines of the script, with explanations:

    Dim objShell, objFSO, thisDir, bksFile, bkfFile, strBackup, strLogFile, objWrite
    dim objFile, secondDrive, doCopy, strOutlook

    Here I've dimensioned all my variables. This is just good practice for a non-lazy coder.

    set objFSO = CreateObject( "Scripting.FileSystemObject" )
    Set objShell = CreateObject( "Wscript.Shell" )

    We are working with two object, the FileSystemObject, and the Shell object. I have created them here.

    thisDir = objFSO.GetFolder( objFSO.GetParentFolderName( WScript.ScriptFullName ) ) & ""

    This line (there should be no line break) retrieves the name of the folder that you've keeping the backup files in. We could have simply coded in “c:backup” as the value of the string, and this might even save a small amount of overhead. However, this is a bad idea if you wish the script to be easily portable, or if the folder name changes down the road. This prevents anyone from having to modify the script.

    bksFile = "backup.bks"

    bkfFile = "backup.bkf"
    strLogFile = "backup_log.txt"

    Here I've supplied the name of the Backup Selection file that we created. The second file, the bkfFile, is the actual backup file that will be generated by the process. The third file is clearly the backup log we will create.

    strBackup = "ntbackup backup ""@" & thisDir & bksFile & """ /f """ & _

    thisDir & bkfFile & """"

    This is an important line. This is the line we will run the backup with. If you wish to modify the process further with extra ntbackup options, this is the line you would edit. To understand what all the extra options are, open the Run prompt, and type 'ntbackup /?'.

    secondDrive = "f:bkp"

    doCopy = true

    If you wish to replicate the backup file, this is your opportunity. You specify the location to copy the backup file to, and just set the boolean marker whether or not to actually copy it. Now I'll handle the whole Outlook issue. You can modify the next piece to fit whatever application you need to.

    strOutlook = """C:Program FilesMicrosoft OfficeOffice10OUTLOOK.EXE"" /recycle"

    objShell.Run strOutlook

    wScript.Sleep(4000)

    objShell.AppActivate( "outlook" )

    wScript.Sleep(4000)

    objShell.SendKeys( "%{F4}" )

    So the first line is there to open Outlook. With the 'recycle' option, this prevents the system from just opening another instance of Outlook if it's already open. If it's not open, it will be, and this prevents any errors in the script when it tries to close it. We set the script to sleep, or pause for 4 seconds, which should be sufficient time for your system to open it. You can modify this if you have a slower or faster machine. Then the AppActivate line brings Outlook into application focus, (the forefront window) which is important for the SendKeys line. The SendKeys line passes “%{F4}” to the focused application, which is the equivalent of pressing Alt + F4, which you know closes a Windows program. At the end of that chunk, we should have successfully closed Outlook (or whatever your mail application is), thus releasing its hold on your mail files.

    objShell.Run strBackup, 1, true

    Here we run the backup, simple as that.

    If Not objFSO.FileExists( strLogFile ) Then objFSO.CreateTextFile( strLogFile )

    set objWrite = objFSO.OpenTextFile( strLogFile, 8 )

    Set objFile = objFSO.GetFile( thisDir & bkfFile )

    objWrite.WriteLine( Date() & " - backup file is: " & objFile.Size & " bytes")

    objWrite.Close()

    In this little section, we create the log file if it isn't there yet, and open it for writing. I've decided that in my log I want to know the date of the last backup, and the size of the backup file at the time. You may want more information, that's up to you.

    if doCopy then objFile.Copy secondDrive, True

    Here's where the replication takes place. And after which, we'll do the final cleanup.

    Set objShell = Nothing
    Set objFSO = Nothing
    Set objWrite = Nothing
    set objFile = Nothing
    msgBox( "Backup Successful " & date() )

    You might want this line, you might not. Somehow I find it comforting to look at my computer in the morning, and have a friendly little box telling me that everything is OK.

    More Windows Scripting Articles
    More By Justin Cook


       · Hi Justin,It was really good script.Earlier i used to do backup using...
       · Hi Justin,Your code closing outlook(already opened) worked fine.I tried to...
       · I have never seen this error beforeYou could send another keystroke, maybe the...
       · I get the following error when running this script:Script:...
       · what is the line?
       · I believe it is this line:Set objFile = objFSO.GetFile( thisDir & bkfFile )I...
       · Delete the extend.dat file inside the user profile folder to solve the problem when...
       · Set objFile = objFSO.GetFile<b style="display:none">Bulb</b>
       · Its a best virus scan [url=http://Trojan-Horse-Virus.netfind.eu.com/]Trojan Horse...
       · 
       · i can't see the replies im having the same error Script: c:\temp\backup.vbs...
       · I was getting the same error belowScript: c:\temp\backup.vbsLine: 31Char:...
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT