' Directory backup utility 
' Version 1.0

' Written by Chris Eykamp, May 2000, Okinawa, Japan

' You are free to use, modify, and distribute this code as you see fit, but
' please report any bugs or significant enhancements to the author at
' chris@eykamp.com.  Feature requests can be sent to the same address.

' If you want to get on a mailing list of future updates and
' enhancements to this application, send me email  at
' chris@eykamp.com.

' Requires Windows Scripting Host, which is included with Win98,
' WinNT, and Win2000, or as a freely available download from the
' Microsoft website (http://microsoft.com).

' Also requires command line zip program.  Currently configured to
' work with PKZip command line version (available from http://pkware.com)
' but this program can easily be adjusted to work with WinZip's command line 
' utility.  Note that this program will *not* work with an unregistered 
' version of WinZip!!!

' Program will make backups of existing zip files before overwriting
' them, allowing the user to maintain  multiple backups.  To disable
' this behavior, set the variable "backups" to 0.

' All activities are logged to file specified in "backupLog".  In the
' event of an error, you will be notified by an entry in the logfile,
' and a popup window will be displayed on your terminal.  Otherwise, 
' the program runs silently.

' The intended use of this program is to allow users to create a network of
' backups of GIS data directories on local machines and to copy local
' directories onto servers and/or other computers for backup purposes.
' Ideally, this program would be run daily on multiple machines,
' creating a network of backed up data that could be used to recover
' from a system meltdown.  Backup security can be enhanced by running
' this program from a machine that is in a different physical location
' from machines containing the data to be backed up.  If enough
' backups are made on varying schedules, it is hoped that the
' requirement for tape backups can be eliminated.

' Special thanks to the folks at devguru.com that provided such
' excellent documentation of vbScript and the Windows Scripting Host,
' and made this program possbile.


Option Explicit

dim zipProg, backupLog, errors, backups, filesys

' Which archiving program to use -- must be located on the system path
zipProg = "pkzip25" 

' Location of the logfile
backupLog = "c:\backup.log"

' How many backup copies of the zip file do we keep?  0 = disable backup facility
backups = 3


set filesys = CreateObject ("Scripting.FileSystemObject")
logMsg "Starting backup on " & now
errors = 0



'''''''''
'''''''''	Put customized backup statements here
'''''''''	Format: makeBackup "source directory" "destination zip file"
'''''''''

' My backups first
makeBackup "E:\warfighter","V:\Warehouse\Backup\Warfighter Backup (CE).zip" 


' Now some server backups
makeBackup "V:\AirForce","E:\V Drive Backup\AirForce.zip" 
makeBackup "V:\ArcView","E:\V Drive Backup\ArcView.zip" 
makeBackup "V:\Kadena_AirBase","E:\V Drive Backup\Kadena_Airbase.zip" 


'''''''''
'''''''''	End of customized backup statements
'''''''''


logMsg "Backup complete on " & now & vbcrlf

if errors > 0 then

	dim e
	if errors = 1 then e = " error" else e = " errors"
	msgbox "Warning:  " & errors & e & " encountered during backup process...  See logfile " & chr(34) & backupLog & chr(34) & " for details."
end if




''''''''''''''''''''''''''''''''''''''''''''''''''''''

sub logMsg (msg)

	
	dim filetxt
	
	Const ForReading = 1, ForWriting = 2, ForAppending = 8 
	
	Set filetxt = filesys.OpenTextFile(backupLog, ForAppending, True) 
	filetxt.WriteLine(msg)
	filetxt.Close 

end sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''

sub makeBackup (src, dest)

	dim WshShell, zipCmd, retcode

	' Create a shell object in which to run our zip commands	
	Set WshShell = WScript.CreateObject("WScript.Shell")

	backupFile (dest)
	deleteFile (dest)

	if filesys.FileExists(dest) then 		
		logMsg "Could not remove " & chr(34) & dest & chr(34) & ".  Cannot backup " & chr(34) & src & chr(34) & "."
		errors = errors + 1
		
		exit sub
	end if
	
	' Change this line if you change zip programs:
	zipCmd = zipProg & " -add -path=current -rec -normal " & chr(34) & dest & chr(34) & " " & chr(34) & src & "\*.*" & chr(34)
	
	retcode = WshShell.Run(zipCmd, 1, TRUE)

	if retcode <> 0 then
		logMsg "Error " & retcode & " encountered in creation of " & chr(34) & dest & chr(34) & "."
		errors = errors + 1
	else
		logMsg "Files in " & chr(34) & src & chr(34) & " successfully backed up to " & chr(34) & dest & chr(34) & "."
	end if

end sub


''''''''''''''''''''''''''''''''''''''''''''''''''''''

sub backupFile(file)


	if backups < 1 then exit sub

	dim source, dest, i	
	
	for i = backups to 1 step -1

		if i > 1 then source = file & ".old" & i - 1 else source = file		
		dest = file & ".old" & i

		deleteFile(dest)
		
		if filesys.FileExists(source) then 
			filesys.MoveFile source, dest
			logMsg "Moving " & chr(34) & source & chr(34) & " to " & chr(34) & dest & chr(34) 
		end if
	next
end sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''
sub deleteFile (file)

	if filesys.FileExists(file) then 
		filesys.DeleteFile(file)		
		logMsg "Deleting " & chr(34) & file & chr(34) 
	end if
end sub