Commits

Nathan Durnan committed 6a52409 Merge

Merge with Release 0.1.1

Comments (0)

Files changed (6)

 .hgsub,1325791137.19
 .hgsubstate,1325791876.03
 .hgtags,1326752830.7
-_HgPreBackup.au3,1326752711.52
+_HgPreBackup.au3,1326761799.77
 HgPreBackup.ico,1325031982.39
 HgPreBackup.svg,1325032006.66
-HgPreBackup_x64.exe,1326752710.89
-HgPreBackup_x86.exe,1326752712.03
-HgPreBackupSettings.ini,1326749875.39
-README.rst,1326752112.33
+HgPreBackup_x64.exe,1326761796.98
+HgPreBackup_x86.exe,1326761791.0
+HgPreBackupSettings.ini,1326759741.89
+README.rst,1326760580.3

HgPreBackupSettings.ini

 ;       e.g. at least 30 days worth of logs, AND at least 10 logs.
 ;       The more generous condition will always take precedence to 
 ;       preserve the maximum amount of historical log information.
+;  PurgeBackups ..: Remove backup repositories when they no longer have
+;                   matching source repositories.
+;                   Expected Values: 'True' or 'False'
+;                       True  -> Send orphaned backups to Recycle Bin.
+;                       False -> Leave orphaned backups in BackupPath.
 ;----------------------------------------------------------------------
 SubFolderLimit = 10
 Report = False
 KeepLogCount = 10
 KeepLogDays = 30
+PurgeBackups = False
 ;End of Settings
 [email]
 ;----------------------------------------------------------------------

HgPreBackup_x64.exe

Binary file modified.

HgPreBackup_x86.exe

Binary file modified.
   - 'KeepLogDays = -1' will keep ALL log files (regardless of KeepLogCount setting)
   
 **NOTE:** The KeepLog... settings work in combinaton with each other:
-    e.g. at least 30 days worth of logs, AND at least 10 logs.
-    The more generous condition will always take precedence to 
-    preserve the maximum amount of historical log information.
+  e.g. at least 30 days worth of logs, AND at least 10 logs.
+  The more generous condition will always take precedence to 
+  preserve the maximum amount of historical log information.
+
+* **PurgeBackups** - Remove backup repositories when they no longer have
+  matching source repositories.  This typically is the result of a source
+  repository being deleted or renamed.  "Orphaned" repositories will likely
+  be taking up unnecessary space in the backup project.
+
+  - Expected Values: 'True' or 'False'
+  
+    + True = Send orphaned backups to Recycle Bin.
+    + False = Leave orphaned backups in BackupPath.
+
 
 [Email] Settings
 ~~~~~~~~~~~~~~~~
 
 Dependencies
 ~~~~~~~~~~~~
-* Microsoft Script Runtime (scrrun.dll)
+* Microsoft Script Runtime (scrrun.dll)
 
   - Developed using Version 5.7.0.18066
 
-* Windows NT BASE API Client DLL (kernel32.dll)
+* Windows NT BASE API Client DLL (kernel32.dll)
 
   - Developed using Version 5.1.2600.5781
 
-* Microsoft CDO for Windows Library (cdosys.dll)
+* Microsoft CDO for Windows Library (cdosys.dll)
 
   - Developed using Version 6.2.4.0
 
 #AutoIt3Wrapper_Compression=4
 #AutoIt3Wrapper_Res_Comment=Automated tool to create/update Mercurial Repository clones for backups.
 #AutoIt3Wrapper_Res_Description=Mercurial Repository Pre-Backup Tool
-#AutoIt3Wrapper_Res_Fileversion=0.1.0.9
-#AutoIt3Wrapper_Res_Field=FileVersion Notes|Beta Test
+#AutoIt3Wrapper_Res_Fileversion=0.1.1.1
+#AutoIt3Wrapper_Res_Field=FileVersion Notes|Beta Test - Purge Orphaned Backups
 #AutoIt3Wrapper_Res_Fileversion_AutoIncrement=p
 #AutoIt3Wrapper_Res_Language=1033
 #AutoIt3Wrapper_Res_LegalCopyright=Open Source
 Global $Gh_LogFile 			;Handle for log file
 Global $Gi_KeepLogCount		;Number of old logs to keep in log path.
 Global $Gi_KeepLogDays		;Days of old logs to keep in log path.
+Global $Gb_PurgeBackups		;Purge Orphaned Backups that no longer have a source repository.
 
 Global $Ga_RepoList[1]				;Array containing paths of repositories to be backed up.
 Global $Ga_RepoValidErr[1]			;Array containing list of source repositories that failed validation.
 Global $Gi_CloneCount = 0			;Number of Clone commands issued.
 Global $Ga_BackupRepoCloneErr[1]	;Array to keep track of any repositories with errors durring Clone
 Global $Ga_BackupRepoList[1]		;Array containing paths of repositories in the backup directory.
-Global $Ga_BackupRepoValidErr[1]	;;Array containing list of backup repositories that failed validation.
+Global $Ga_BackupRepoValidErr[1]	;Array containing list of backup repositories that failed validation.
+Global $Ga_PurgedBackupList[1]		;Array containing list of orphaned backups that have been purged.
 
 ;email settings
 Global $Gb_ReportErrors		;True = email log if errors encountered.  False = no email.
 ;------------------------------------------------------------
 #Region Local Variables and Constants
 ;***** Declare Variables *****
-Local $iResult
+Local $iResult, $lError
+Local $ii, $jj
+Local $sBackupPath
+Local $sRelativePath
+Local $bFoundMatch
 
 #EndRegion Local Variables and Constants
 
 		$G_objFSO.CreateFolder($Gs_BackupDir)
 	EndIf ;Check for backup directory.
 	;Check source repositories.
-	Local $ii, $jj, $sBackupPath
-	Local $sRelativePath
 	For $ii = 1 To $Ga_RepoList[0]
 		$sRelativePath = _PathGetRelative($Gs_RepoHomeDir, $Ga_RepoList[$ii])
 		If @error Then ;NOTE: check @error IMMEDIATELY after function call!!!
 	ConsoleWrite("========== Post-Backup ==========" & @CRLF)
 	_LogFileAddMessage($Gh_LogFile, "Searching for Backup Repositories at " & $Gs_BackupDir)
 	_SearchFolderForRepositories($Gs_BackupDir, $Ga_BackupRepoList, $Ga_BackupRepoValidErr)
+	;Purge Orphaned Backups (backups that no longer have a source).
+	ConsoleWrite("Purge Orphaned Backups..." & @CRLF)
+	$ii = 1 ;Initialize Loop Variable
+	Do ;Loop through backup repo list.
+		$sBackupPath = _PathGetRelative($Gs_BackupDir, $Ga_BackupRepoList[$ii])
+		ConsoleWrite(@TAB & "$Ga_BackupRepoList[" & $ii & "] = " & $sBackupPath & @CRLF)
+		$bFoundMatch = False ;Initialize flag.
+		For $jj = 1 to $Ga_RepoList[0]
+			$sRelativePath = _PathGetRelative($Gs_RepoHomeDir, $Ga_RepoList[$jj])
+			$iResult = StringCompare($sBackupPath, $sRelativePath)
+			If ($iResult = 0) Then
+				ConsoleWrite(@TAB & "$Ga_RepoList[" & $jj & "] = " & $sRelativePath & @CRLF)
+				$bFoundMatch = True
+				ExitLoop ;($jj) ;Found a matching source!
+			EndIf ;check for match found.
+		Next ;loop through source repositories.
+		If Not $bFoundMatch Then
+			;Backup not found in Source.
+			ConsoleWrite(@TAB & "Backup no longer in Source!" & @CRLF)
+			$iResult = _ArrayAdd($Ga_PurgedBackupList, $Ga_BackupRepoList[$ii])
+			If ($iResult > 0) Then
+				$Ga_PurgedBackupList[0] = $iResult
+			EndIf ;Check results of adding item to array.
+			If Not $Gb_PurgeBackups Then
+				;Just report orphans found.
+				_LogFileAddMessage($Gh_LogFile, "Found Orphaned Backup: " & $Ga_BackupRepoList[$ii])
+			Else ;Purging Orphaned Backups.
+				_LogFileAddMessage($Gh_LogFile, "Purging Orphaned Backup: " & $Ga_BackupRepoList[$ii])
+				$iResult = FileRecycle($Ga_BackupRepoList[$ii])
+				If ($iResult > 0) Then
+					$jj = _ArrayDelete($Ga_BackupRepoList, $ii)
+					If ($jj >= 0) Then
+						$Ga_BackupRepoList[0] = ($jj - 1) ;don't count the first item - is is placeholder for count only!
+						$ii -= 1 ;Jump back one index to make up for removed item on next loop.
+					EndIf ;Check for successful array item deletion.
+				EndIf ;Check for successful repository deletion.
+			EndIf ;Check for PurgeBackups setting.
+		EndIf ;Check for backup not found in source.
+		$ii += 1 ;Increment Loop Variable.
+	Until ($ii > $Ga_BackupRepoList[0]) ;Loop through backup repositories.
 	#EndRegion - Evaluate Backup Repositories
 EndIf ;Check for valid Mercurial installation.
 _LogFileAddMessage($Gh_LogFile, "End of Operations")
 	$myResult = $myResult And FileWriteLine($hFile, ";       e.g. at least 30 days worth of logs, AND at least 10 logs.")
 	$myResult = $myResult And FileWriteLine($hFile, ";       The more generous condition will always take precedence to ")
 	$myResult = $myResult And FileWriteLine($hFile, ";       preserve the maximum amount of historical log information.")
+	$myResult = $myResult And FileWriteLine($hFile, ";  PurgeBackups ..: Remove backup repositories when they no longer have")
+	$myResult = $myResult And FileWriteLine($hFile, ";                   matching source repositories.")
+	$myResult = $myResult And FileWriteLine($hFile, ";                   Expected Values: 'True' or 'False'")
+	$myResult = $myResult And FileWriteLine($hFile, ";                       True  -> Send orphaned backups to Recycle Bin.")
+	$myResult = $myResult And FileWriteLine($hFile, ";                       False -> Leave orphaned backups in BackupPath.")
 	$myResult = $myResult And FileWriteLine($hFile, ";----------------------------------------------------------------------")
 	$myResult = $myResult And FileWriteLine($hFile, "SubFolderLimit = " & $Gi_RecurseLimit)
 	$myResult = $myResult And FileWriteLine($hFile, "Report = " & $Gb_ReportErrors)
 	$myResult = $myResult And FileWriteLine($hFile, "KeepLogCount = " & $Gi_KeepLogCount)
 	$myResult = $myResult And FileWriteLine($hFile, "KeepLogDays = " & $Gi_KeepLogDays)
+	$myResult = $myResult And FileWriteLine($hFile, "PurgeBackups = " & $Gb_PurgeBackups)
 	$myResult = $myResult And FileWriteLine($hFile, ";End of Settings")
 	$myResult = $myResult And FileWriteLine($hFile, "[email]")
 	$myResult = $myResult And FileWriteLine($hFile, ";----------------------------------------------------------------------")
 	Else ;use default value
 		$Gi_KeepLogDays = -1
 	EndIf ;check for valid value in setting
+	$bValue = IniRead($IN_sSettingsFilePath, 'settings', 'PurgeBackups', False)
+	;Validate Setting Value
+	If IsBool ($bValue) Then
+		$Gb_PurgeBackups = $bValue
+	ElseIf IsString($bValue) Or IsNumber($bValue) Then
+		Switch $bValue
+		Case 't', 'true', 1, -1
+			$Gb_PurgeBackups = True
+		Case Else
+			$Gb_PurgeBackups = False
+		EndSwitch
+	Else
+		$Gb_PurgeBackups = False
+	EndIf
 	#EndRegion Read Settings
 	#Region Read Email Configuration:
 	;NOTE: The default settings will probably not work if the IniRead command fails to retrieve data from the file.
 				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       > " & $Ga_BackupRepoPullErr[$ii])
 			Next ;loop through list of repos
 		EndIf ;Check for Pull Errors
-		If ($Ga_RepoList[0] <= 0) Then ;check for any repositories found.
-			$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Total Found.........: none (huh?)")
+		If ($Ga_BackupRepoList[0] <= 0) Then ;check for any repositories found.
+			$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Total Found.............: none (huh?)")
 		Else ;At least one repository was found.
-			$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Total Found.........: " & $Ga_BackupRepoList[0])
+			$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Total Found.............: " & $Ga_BackupRepoList[0])
 			If ($Ga_BackupRepoValidErr[0] > 0) Then
-				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Validation Errors...: " & $Ga_BackupRepoValidErr[0])
+				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Validation Errors.......: " & $Ga_BackupRepoValidErr[0])
 				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       Review the following repositories:")
 				For $ii = 1 To $Ga_BackupRepoValidErr[0]
 					$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       > " & $Ga_BackupRepoValidErr[$ii])
 				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Validation Errors.......: none (yaay!)")
 			EndIf ;Check for Validation Errors
 		EndIf ;check for any repositories found.
+		If ($Ga_PurgedBackupList[0] > 0) Then
+			$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Orphaned Backups Found..: " & $Ga_PurgedBackupList[0])
+			If Not $Gb_PurgeBackups Then
+				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       The following no longer have matching source repositories")
+				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       and should be evaluated as to their usefulness: ")
+			Else ;Orphans have been deleted.
+				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       The following no longer have matching source repositories")
+				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       and have been moved to the Recycle Bin: ")
+			EndIf ;Check for PurgeBackups setting.
+			For $ii = 1 to $Ga_PurgedBackupList[0]
+					$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       > " & $Ga_PurgedBackupList[$ii])
+			Next ;Loop through list of repos
+		EndIf ;Check for any purged orphans.
 	Endif ;Check for mercurial installation/path errors.
 	$myResult = $myResult And FileWriteLine($IN_hLogFile, "+---------------------------------------------------------------------")
 	Return $myResult