Delete differential images based on available free space |
Applies to: Standard, Professional and Server Editions of Macrium Reflect only. |
The instructions presented in this topic demonstrate how you would use VBScript to delete the oldest differential images in a backup set based on the amount free space available on the backup drive.
In the following example we generate a VBScript source file that, if necessary, creates 5 GB of free space on the target drive by deleting the oldest differential images before running the current differential.
Caution Only run this script on a differential backup set. Running the script on incremental backup sets leads to loss of backup integrity. |
In the Backup Definition Files tab, right-click
a backup definition file and choose Generate
a VB Script File from the shortcut menu.
The VBScript generation dialog box appears.
Leave the options as defaults but change the file name for clarity.
Select the VBScript
Files tab, right-click the new file and select Edit
from the shortcut menu.
The VBScript file opens in the system default text editor, generally
this is Notepad.
For clarity, switch off word wrap. In notepad,
choose Edit > Word Wrap.
Edit the file in Notepad as follows:
Before the line:
' Do the backup
Copy and paste the following:
' Create 5GB free space
CreateSpace "N:\Backups", 5 * 1024
Note The number 5 represents the number of GB of free space you want to create to ensure you have enough space before running the backup. Change 'N:\Backups' to the path of your image. |
At
the end of the file copy and paste the following code:
Function CreateSpace(ByVal drvPath, MB)
While GetFreeSpaceMB(drvPath) < MB
If DeleteOldestFile(drvPath) = false Then
Exit
Function
End If
Wend
End Function
Function GetFreeSpaceMB(ByVal drvPath)
Dim objFSo, d
Set objFSo =
CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(drvPath) Then
' return a big number if the folder doesn't exist
GetFreeSpaceMB = 1000 * 1024 * 1024
Exit Function
End If
Set d = objFSo.GetDrive(objFSo.GetDriveName(drvPath))
GetFreeSpaceMB = d.FreeSpace/ (1024 * 1024)
End Function
Function DeleteOldestFile(ByVal drvPath)
Dim objFSo, colFiles , dtmOldestDate, strOldestFile, objFolder, objFile
Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(drvPath)
Set colFiles = objFolder.Files
dtmOldestDate = Now
For Each objFile in colFiles
If objFile.DateLastModified < dtmOldestDate _
AND Left(Right(objFile.Path, 11), 2) <> "00" _
AND Right(objFile.Path, 6) = ".mrimg" Then
dtmOldestDate
= objFile.DateCreated
strOldestFile
= objFile.Path
End If
Next
If strOldestFile <> "" Then
objFSO.DeleteFile strOldestFile
DeleteOldestFile = true
Else
DeleteOldestFile = false
End If
End Function
Save the file.
Execute or schedule the VBScript source file in exactly the same way as a backup definition file. See Schedule backups.