' Comet Backup Script ' Justin Guerber ' 2/18/08 ' ' ' This script takes a specified backup file (zipped archive) and ' transfers it to the specified directory with a unique (per second) ' timestamp. ' Ideally, an automated winzip job file (.wjf) is specified, which ' gives us the name of the archive file to transfer (NOTE: if the job ' file appends a string to the filename, this script will NOT work). ' Alternatively, the archive file can be specified manually. Option Explicit ' ----- Constants ----- ' Comet Anywhere program (on CC) that performs backup and unpacking Const CA_BACKUP_PROGRAM = "backup" ' Address to Comet Anywhere host (CC machine) Const CA_REMOTE_ADDRESS = "cc.signature.net" ' Temporary file name for "keyfile=" parameter for CA backup program Const KEYFILE_NAME = "catmpfile.txt" ' Location of comet executable file Const DEFAULT_COMET_EXE = "comet.exe" ' Parameters Dim argZipFile, argCometExe, argTempFolder, argSaveArchives, argFullBackup ' File/Folder/File system objects (fo) and streams (fs) Dim FSO, foTempFolder, foCometExe, foZipFile, foArchiveDir, fsKeyFile ' Strings Dim keyfile_path, exec_str ' Return value for program execution through shell Dim exec_result ' ----- Initialization ----- ' Initialize the File System Object Set FSO = CreateObject("Scripting.FileSystemObject") If( IsNull(FSO) ) Then Wscript.echo( "Could not initialize the File System Object." ) WScript.quit End If ' Init file system object ' Populate the parameter variables argZipFile = Wscript.arguments.named( "zip_file" ) argCometExe = Wscript.arguments.named( "comet_exe" ) argTempFolder = Wscript.arguments.named( "temp_folder" ) argSaveArchives = Wscript.arguments.named( "save_archives" ) argFullBackup = WScript.arguments.named.exists( "full_backup" ) ' Set the temp folder to the system default Set foTempFolder = FSO.GetSpecialFolder(2) ' ----- Validate Parameters ----- ' If a temporary folder argument was specified, use that If Not( IsEmpty(argTempFolder) ) Then If( FSO.FolderExists(argTempFolder) ) Then Set foTempFolder = FSO.GetFolder( argTempFolder ) Else Error( "Could not locate temporary folder: " & argTempFolder ) End If ' Temp Folder exists End If ' argTempFolder specified ' If the comet executable was specified, locate it If Not( IsEmpty(argCometExe) ) Then If( FSO.FileExists(argCometExe) ) Then Set foCometExe = FSO.GetFile( argCometExe ) Else Error( "Could not locate comet executable: " & argCometExe ) End If ' Specified Comet Exe exists ' Otherwise look in the local directory for the comet executable Else ' Comet Exe specified ' Get the path to the current directory (of the vbscript) + comet executable ' Note: The current working directory is the "start in" folder specified by the shortcut ' This gets the directory the vbscript was run from (where comet.exe should be) Dim comet_exe_path comet_exe_path = Left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName))) + DEFAULT_COMET_EXE If( FSO.FileExists(comet_exe_path) ) Then Set foCometExe = FSO.GetFile( comet_exe_path ) Else Error( "This script must be located in the same directory as the Comet executable (" & DEFAULT_COMET_EXE & ")." ) End If ' Default Comet Exe exists End If ' Default Comet Exe ' If a Winzip archive was specified, locate it If Not( IsEmpty(argZipFile) ) Then If( FSO.FileExists(argZipFile) ) Then Set foZipFile = FSO.GetFile( argZipFile ) Else Error( "Could not locate Winzip archive: " & argZipFile ) End If ' Winzip archive exists End If ' Zip File specified ' If an archive directory was specified, locate it If Not( IsEmpty(argSaveArchives) ) Then If( FSO.FolderExists(argSaveArchives) ) Then Set foArchiveDir = FSO.GetFolder( argSaveArchives ) Else Error( "Could not locate archive directory: " & argSaveArchives ) End If ' archive directory exists End If ' Archive directory specified ' ----- Create and Write Keyfile ----- ' Create the keyfile for parameters, overwriting if it already exists keyfile_path = foTempFolder.path & "\" & KEYFILE_NAME Set fsKeyFile = FSO.CreateTextFile( keyfile_path, True ) ' Write parameters in keyfile fsKeyFile.WriteLine( CA_BACKUP_PROGRAM ) ' Program to launch fsKeyFile.WriteLine( "tempfolder:" & foTempFolder.path ) ' Temporary folder ' If a Winzip archive was specified, write the parameter If Not( IsEmpty(foZipFile) OR IsNull(foZipFile) ) Then fsKeyFile.WriteLine( "zipfile:" & foZipFile.path ) End If ' If we should save the generated archives to a separate directory, write the parameter If Not( IsEmpty(foArchiveDir) OR IsNull(foArchiveDir) ) Then fsKeyFile.WriteLine( "archivedir:" & foArchiveDir.path ) End If ' If we should perform a full backup (regardless of timestamps), write the param If( argFullBackup ) Then fsKeyFile.WriteLine( "fullbackup" ) End If ' Indicate there are no more parameters fsKeyFile.WriteLine( "LAST.PARAMETER" ) ' Close the keyfile fsKeyFile.Close() ' ----- Run CA Backup program ----- ' Run the Comet Anywhere CC backup program exec_str = Chr(34) & foCometExe.path & Chr(34) & " /net:" & CA_REMOTE_ADDRESS & " /keyfile:" & Chr(34) & keyfile_path & Chr(34) & " /run:CAID" exec_result = Run( exec_str ) ' Report if there was an error If( exec_result <> 0 ) Then Error( "There was a problem launching Comet: " & exec_str ) End If ' ----- Cleanup ----- Set fsKeyFile = nothing Set foZipFile = nothing Set foCometExe = nothing Set foTempFolder = nothing Set FSO = nothing WScript.Quit '----- Sub Routines ----- ' Logs the message and Pops up a message box if not running silently then quits Sub Error( message ) ' Pop up an error message WScript.echo( message ) ' End script processing WScript.Quit End Sub ' Execute a .exe file, or open up a file with a recognized ' file extension by executing the correct program. e.g. by ' specifying a *.doc file, this subroutine will boot up Word ' or open office, etc, as long as it is registered with windows. Function Run(ByVal sFile) Dim shell, ret_val ' Add "" (quotes) around the filename (if not already present) If( Left(sFile, 1) <> Chr(34) ) Then sFile = Chr(34) & sFile & Chr(34) End If Set shell = CreateObject("WScript.Shell") ret_val = shell.Run( sFile, 1, true) Set shell = Nothing ' Return the value returned by the external program Run = ret_val End Function