# Date of restore point used by SureBackup Job

PowerShell script exchange

### Date of restore point used by SureBackup Job

Hi All,

I am trying to figure out how to get the date of the restore point that a SureBackup job is using into a script. I have a custom script I'm running in my SureBackup job that reads a txt file from the VM with the date in it. I want to verify that this date matches the date of the restore point being used by SureBackup.

Is there any way to get this using PowerShell? My script is currently VBS and I haven't found a way to get it easily with that so I'm guessing I'll need to have it launch a PowerShell and grab it like that...if that's even possible. I'm pretty new to scripting in general but I can't seem to find any PowerShell commands that would even give me what I'm looking for so I'm hoping someone might be able to give me some guidance. Not sure if it matters but I can post my VBS script if that might help.
bg.ranken
Enthusiast

Posts: 56
Liked: 10 times
Joined: Wed Feb 18, 2015 8:13 pm
Full Name: Randall Kender

### Re: Date of restore point used by SureBackup Job

Hi,

Here is another approach which I think should work. Backup files that are being used by SureBackup session are locked by Veeam.Agent. Verification scripts are executed at VBR so you can use simple one-liner that attempts to write $null into a specific backup file. If it returns error then the file is being used by SureBackup job. Code: Select all echo$null >> .\path\to\backup_file_to_be_checked\backup.vib

Thanks
PTide
Veeam Software

Posts: 3393
Liked: 282 times
Joined: Tue May 19, 2015 1:46 pm

### Re: Date of restore point used by SureBackup Job

This might work, but it would be a lot of work and seemingly prone to errors. Best I can think of is feeding the folder of backup files to the script through arguments, then doing a search of all VIB files in that folder and seeing which ones are locked. The problem I see with that though is that, aren't all the VIB files locked? I guess I could pull the date from all locked VIBs and then sort and find the latest one and hope that it's really what Veeam is using.

Is there no powershell command in Veeam to see job details of a SureBackup job that's running that might have information on which files it's using or which restore point it's testing? Perhaps somewhere in the job log itself I could pull the information from?
bg.ranken
Enthusiast

Posts: 56
Liked: 10 times
Joined: Wed Feb 18, 2015 8:13 pm
Full Name: Randall Kender

### Re: Date of restore point used by SureBackup Job

The problem I see with that though is that, aren't all the VIB files locked?
By default Surebackup should use the most recent restore point. If for some reason SureBackup verifies some other backup than the latest then the latest vib should have no lock on it. So you can run the one-liner against the oldest vib.

Btw I just remembered you other thread. If you want to get the value from a file on a remote machine then you can use this cmdlet:

Code: Select all
Get-Content \\HOSTNAME\Path\date.txt

Example

Get date from date.txt located on surebackup VM:
Code: Select all
$inVM=Get-Content \\SureBackup_VM\date.txt Compare the value with the date extracted from the backup's name Code: Select all $inVM -eq $backupDate The last line should return either "true" or "false" Hope that helps. Thanks PTide Veeam Software Posts: 3393 Liked: 282 times Joined: Tue May 19, 2015 1:46 pm ### Re: Date of restore point used by SureBackup Job By default Surebackup should use the most recent restore point. If for some reason SureBackup verifies some other backup than the latest then the latest vib should have no lock on it. So you can run the one-liner against the oldest vib. So I'm not really understanding this too much. Why would it be better to run the one-liner against the oldest vib? Wouldn't it be better to just find the latest restore point and make sure that it's locked, and then grab the date from that one? Maybe it's just my limit in terms of programming (I've never done serious programming, just copy/paste) but this seems like it would be harder than just a few lines of code. Here's what I've been working on so far, I get the feeling I'm missing some much easier way of doing this. I haven't starting building any of the code for grabbing the backup folder yet from arguments and finding the date, but maybe you can tell me if this is even a good start before I spend too much more time on this. I know it's not powershell and that's where this post is, but I initially started building the scripts based off the ones Veeam included for testing SQL so I assumed VBS was better in some way. SetDate.VBS Code: Select all '=========================================================================='' VBScript Source File -- SetDate.VBS'' NAME: Set Date for Veeam SureBackup Verification''! @AUTHOR: Randall Kender'! DATE : 09/30/2016'!'! @details Overall purpose of the script is to create a file that has'! the current date in it to be verified during the SureBackup job.'!'=========================================================================='! @Version 1.0.1'! @Date 09/30/2016'!'! Changed variable names.'!'=========================================================================='! @Version 1.0.0'! @Date 09/27/2016'!'! Initial inception of scrypt.'!'==========================================================================' The Option Explicit declaration requires all variables used in the script are defined' via a DIM statement prior to their being used within the script. This helps to prevent' undesirable or difficult-to-troubleshoot problems from arising through misspellings or' other typographical errors introduced inadvertantly by the script writer.'==========================================================================Option Explicit'==========================================================================' Define global variables used in the main body of the script'==========================================================================Dim strDataFile '! String variable containing the data file's path and name.Dim objDataFile '! File system object for interacting with data file.Dim strDataWrite '! File handle for writing to the data file.'==========================================================================' Set the name of the data file for script execution'==========================================================================strDataFile = "C:\Windows\Temp\CurrentDate.txt"'Create data FileSet objDataFile = CreateObject("Scripting.FileSystemObject") Set strDataWrite = objDataFile.OpenTextFile(strDataFile, 2, True)'Write current date into data file.strDataWrite.WriteLine DatestrDataWrite.Close ReadDate.VBS Code: Select all '=========================================================================='' VBScript Source File -- ReadDate.VBS'' NAME: Reads Date for Veeam SureBackup Verification''! @AUTHOR: Randall Kender'! DATE : 10/04/2016'!'! @details Overall purpose of the script is to read a file that has'! the current date in it to be verified during the SureBackup job. This '! file is made by the SetDate.vbs script.'!'! Script usage:'! Without Logging: cscript ReadDate.vbs [server_FQDN] [server_IP]'! With Logging: cscript ReadDate.vbs [server_FQDN] [server_IP] [logs folder]'! '! Examples:'! ---------------'! Inside Veeam'! Name: ReadDate'! Path: <Path to script on Veeam server.'! Arguments: %vm_fqdn% %vm_ip% C:\SureBackup_Script_Logs'! ---------------'! Standalone'! Without Logging: cscript ReadDate.vbs server01.contoso.local 10.1.3.248'! With Logging: cscript ReadDate.vbs server01.contoso.local 10.1.3.248 C:\script_logs'!'! 'Return codes:'! 0 - Success'! 1 - Date in data file does not match with current date'! 2 - CurrentDate.txt data file does not exist'! 3 - Wrong command line sintax'! 4 - Unknown error'!'=========================================================================='! @Version 1.0.3'! @Date 10/04/2016'!'! Created functions DataFileDate and MatchingDate and moved all relavent'! commands into the functions.'! '! Modified logging to reflect changes in script.'! '! Added option to test script against local computer.'!'=========================================================================='! @Version 1.0.2'! @Date 10/03/2016'!'! Moved data file close to before any if statements.'!'! Changed strCurrentDate to get date using function instead of just date'! command to fix issues with variable type being date instead of string'! and not matching strDateFromDataFile.'!'! Added check for data file to prevent script from crashing unexpectedly'! when no data file exists.'!'! Incorporated logging and command line parsing functionality from '! Veeam.Backup.SqlChecker.vbs used by SureBackup for SQL testing:'! Created by: Vyacheslav Kuznetsov'!'=========================================================================='! @Version 1.0.1'! @Date 09/30/2016'!'! Added argument capture.'!'! Changed data file target to use UNC path instead of local system path.'!'=========================================================================='! @Version 1.0.0'! @Date 09/27/2016'!'! Initial inception of scrypt.'!'==========================================================================' The Option Explicit declaration requires all variables used in the script are defined' via a DIM statement prior to their being used within the script. This helps to prevent' undesirable or difficult-to-troubleshoot problems from arising through misspellings or' other typographical errors introduced inadvertantly by the script writer.'==========================================================================Option Explicit'==========================================================================' Define global variables and constants used in the main body of the script.'==========================================================================Dim strServerIP '! Server IP address pushed by Veeam.Dim strServerFQDN '! Server FQDN pushed by Veeam.Dim gLog '! For logging current script execution.'Set to 1 if testing script locally, otherwise leave at 0.Const LOCAL_TESTING = 1Const LOG_LEVEL_STANDARD = 1Const EXIT_CODE_SUCCESS = 0Const EXIT_CODE_MISMATCHED_DATES = 1Const EXIT_CODE_MISSING_DATA_FILE = 2Const EXIT_CODE_WRONG_SYNTAX = 3Const EXIT_CODE_ERROR_UNKNOWN = 4'==========================================================================' Main Body.'=========================================================================='Starts logging.Set gLog = New LogWriter'Function to get arguments and enable logging if argument exists.ParseCommandLineOptionsgLog "===================="gLog "Capturing arguments..."gLog "Server FQDN: " & strServerFQDNgLog "Server IP: " & strServerIP'Check if matching date matches date in data file.If DataFileDate(LOCAL_TESTING) = MatchingDate("C:\test",1) ThengLog "Date from data file is a match!"Quit EXIT_CODE_SUCCESSElseQuit EXIT_CODE_MISMATCHED_DATESEnd If'==========================================================================' Define global functions.'==========================================================================Sub ParseCommandLineOptionsDim sLogsFolderSelect Case WScript.Arguments.CountCase 1:gLog.Error "Wrong number of arguments - " & WScript.Arguments.CountPrintUsageQuit EXIT_CODE_WRONG_SYNTAXCase 2:strServerFQDN=WScript.Arguments(0)strServerIP = WScript.Arguments(1)Case 3:strServerFQDN=WScript.Arguments(0)strServerIP = WScript.Arguments(1)sLogsFolder = WScript.Arguments(2)gLog.GenerateLog sLogsFolder,strServerFQDNCase Else:gLog.Error "Wrong number of arguments - " & WScript.Arguments.CountPrintUsageQuit EXIT_CODE_WRONG_SYNTAXEnd SelectEnd Sub'Outputs proper argument usage for script when it detects invalid arguments.Sub PrintUsagegLog.Write "Script usage:"gLog.Write "Without Logging: cscript ReadDate.vbs [server_FQDN] [server_IP]"gLog.Write "With Logging: cscript ReadDate.vbs [server_FQDN] [server_IP] [logs folder]"End Sub'Takes the location of the data file and outputs the first line which contains the date.Function DataFileDate(LocalTesting)'Declaring variablesDim objDataFile '! File system object for interacting with data file.Dim strDataRead '! File handle for reading the data file.Dim strDataFileLocation '! String variable containing the data file's path and name.If LocalTesting = 1 Then'Set the location of the data file locally.strDataFileLocation = "C:\Windows\Temp\currentdate.txt"Else'Set the location of the data file based on arguments.strDataFileLocation = "\\" & strServerIP & "\" & "C$\Windows\Temp\CurrentDate.txt"End IfgLog "Data path used: " & strDataFileLocation'Creates system object for interacting with data file.Set objDataFile = CreateObject("Scripting.FileSystemObject")'Check to see if data file exists and if not, quit with error code.If Not objDataFile.FileExists(strDataFileLocation) ThenQuit EXIT_CODE_MISSING_DATA_FILEEnd IfgLog "Data file exists."'Opens the data file.Set strDataRead = objDataFile.OpenTextFile(strDataFileLocation, 1)'Gets date from data file.DataFileDate = strDataRead.ReadLinegLog "Date from data file: " & DataFileDate'Closes data file.strDataRead.CloseSet strDataRead = NothingEnd FunctionFunction MatchingDate(iBackupFolderLocation,iUseSystemDate)'If function is passed 1 then get current date from system.If iUseSystemDate = 1 Then'This function is necessary to ensure that MatchingDate's variable type is a string and not a date.MatchingDate = month(date) & "/" & day(date) & "/" & year(date)gLog "System date used."gLog "Date from system: " & MatchingDateElseQuit EXIT_CODE_WRONG_SYNTAXEnd IfEnd Function'Descriptions of error codes used when outputing to a log file.Function GetExitCodeDescription(iExitCode)Select Case iExitCodeCase EXIT_CODE_SUCCESS:GetExitCodeDescription = "Success"Case EXIT_CODE_MISMATCHED_DATES:GetExitCodeDescription = "Date in data file does not match with the date obtained from the system."Case EXIT_CODE_MISSING_DATA_FILE:GetExitCodeDescription = "CurrentDate.txt data file does not exist. Please make sure SetDate.vbs has ran correctly."Case EXIT_CODE_WRONG_SYNTAX:GetExitCodeDescription = "Wrong command line syntax."Case EXIT_CODE_ERROR_UNKNOWN:GetExitCodeDescription = "Unknown error."End SelectEnd Function'Exit function that provides error code, logging, and descriptions.Sub Quit(iExitCode)On Error Resume NextIf Not IsNumeric(iExitCode) Then iExitCode = EXIT_CODE_ERROR_UNKNOWNgLog "Exit code: " & iExitCode & ". " & GetExitCodeDescription(iExitCode)Set gLog = NothingWScript.Quit iExitCodeEnd Sub'==========================================================================' Define global Custom Types.'==========================================================================Class LogWriter'Declaring variablesPrivate oFSO,oTextFile,sPath,sCurFolder,iLogLevel,bLoggingEnabledPublic Verbose,FileOpenMethod,FileEncoding,AddDate'Methods Private Sub class_initializeConst FILE_OPEN_WRITE=2Const FILE_OPEN_APPEND=8Const ENCODING_ASCII=0Const ENCODING_UNICODE=-1Const ENCODING_DEFAULT=-2iLogLevel=LOG_LEVEL_STANDARDSet oFSO=WScript.CreateObject("Scripting.FileSystemObject")Verbose=FalseFileOpenMethod=FILE_OPEN_APPENDFileEncoding=ENCODING_DEFAULTAddDate=TruesCurFolder=Left(WScript.ScriptFullName,Len(WScript.ScriptFullName)-Len(WScript.ScriptName))If LCase(Right(WScript.FullName,Len(WScript.FullName)-Len(WScript.Path)-1))="cscript.exe" Then Verbose=TruebLoggingEnabled = FalseEnd SubPrivate Sub class_terminateIf VarType(oTextFile)=9 Then oTextFile.CloseSet oFSO=NothingEnd SubPrivate Sub AssignFileOn Error Resume NextSet oTextFile=oFSO.OpenTextFile(sPath,FileOpenMethod,True,FileEncoding)If Err.Number=0 ThenbLoggingEnabled=TrueEnd IfEnd SubPrivate Sub DoWrite(ByVal sText)sText=Replace(sText,vbCrLf,"")If Verbose And iLogLevel>0 Then WScript.Echo sTextIf iLogLevel=0 Or Not bLoggingEnabled Then Exit SubIf VarType(oTextFile)=0 Then AssignFileIf AddDate ThensText="[" & Now & "] " & sTextEnd IfoTextFile.WriteLine sTextEnd SubPublic Sub GenerateLog(sLogsFolder,ByVal sServer)Dim iPosIf Not oFSO.FolderExists(sLogsFolder) ThenExit SubEnd IfIf InStrRev(sLogsFolder,"\")<Len(sLogsFolder) ThensLogsFolder = sLogsFolder & "\"End IfiPos = InStr(sServer, "\")If iPos>0 ThensServer = Left(sServer,iPos-1)End IfsServer = Replace(sServer,".","_")sServer = Replace(sServer,":","_")sPath = sLogsFolder & "ReadDate_" & sServer & ".log"AssignFileEnd SubPublic Sub Write(sText)DoWrite sTextEnd SubPublic Sub Error(sText)DoWrite "Error " & sTextEnd SubPublic Default Sub Info(sText)DoWrite "Info " & sTextEnd Sub'PropertiesPublic Property Get CurFolderCurFolder=sCurFolderEnd PropertyPublic Property Let LogLevel(iValue)If IsNumeric(iValue) ThenIf iValue>=0 And iValue<=2 And VarType(oTextFile)=0 TheniLogLevel=iValueEnd IfEnd IfEnd PropertyPublic Property Get LogLevelLogLevel=iLogLevelEnd PropertyPublic Property Get PathPath = sPathEnd PropertyEnd Class
bg.ranken
Enthusiast

Posts: 56
Liked: 10 times
Joined: Wed Feb 18, 2015 8:13 pm
Full Name: Randall Kender

### Re: Date of restore point used by SureBackup Job

So I'm not really understanding this too much. Why would it be better to run the one-liner against the oldest vib? Wouldn't it be better to just find the latest restore point and make sure that it's locked, and then grab the date from that one?
Assuming that you're running backups in forever-forward incremental mode the latest .vib contains the latest restore point. So checking if the latest vib is locked and pulling the date from it should be enough.

Maybe it's just my limit in terms of programming (I've never done serious programming, just copy/paste) but this seems like it would be harder than just a few lines of code.
Maybe I'm misunderstanding something, but I really believe that it can be done with a few strings:

Code: Select all
echo "$(Get-Date)" > date.txt Find the oldest file in a folder and get its creation time with: Code: Select all $Item = Get-ChildItem -Path $FolderName | Sort CreationTime | select -First 1$time=$item.creationtime Pull the content from a file Code: Select all $inVM=Get-Content \\SureBackup_VM\date.txt
PTide
Veeam Software

Posts: 3393
Liked: 282 times
Joined: Tue May 19, 2015 1:46 pm

### Re: Date of restore point used by SureBackup Job

Maybe it's just my limit in terms of programming (I've never done serious programming, just copy/paste) but this seems like it would be harder than just a few lines of code.
Maybe I'm misunderstanding something, but I really believe that it can be done with a few strings:

Code: Select all
echo "$(Get-Date)" > date.txt Find the oldest file in a folder and get its creation time with: Code: Select all $Item = Get-ChildItem -Path $FolderName | Sort CreationTime | select -First 1$time=$item.creationtime Pull the content from a file Code: Select all $inVM=Get-Content \\SureBackup_VM\date.txt
[/quote]

So the problem is that many of my jobs are per-vm jobs, which makes things a little more difficult. I also was hoping to get the timestamp on the actual backup file written by Veeam and not the modified date since that could be wrong for a number of reasons, mainly that the vib's modified date is not the same as the date of the snapshot if the job runs past midnight. Also, I'm still not understanding why I might want the oldest time, I would think the latest in the chain of VIB files since that's what's most likely to be in use as it's the latest restore point.

So at the moment, what I'm planning on doing is taking the FQDN that Veeam passes as an argument, cutting off the domain (so I have just the server name), then searching for all files in the folder that include that servername with an extension of .VIB. Then sorting by last modified and taking the latest and testing to see if it's locked like you showed prior. If it is ,then parsing out the date that's listed in the filename and matching that with the date I pulled from the file I had created with my other script. It's a bunch of work but at this point it seems like it would do the trick and be pretty solid.

So at least at this point I think I have an endpoint for where I want to be and it's just a matter of coding it, that's the hard part. Also maybe it would have been easier if I had done the whole thing in powershell, the commands you've presented seem to be a lot easier than what I'm going to have to come up with in VBS, but I already have most of the script working so I'll probably just keep going that route. I was moreso hoping that there would be just a simple command in Veeam like "get-surebackupjob details" that would have the restore point date listed somewhere, but I using the physical files will still work in the end.

Thanks for the help, I'll see how far I can get coding it. I'll probably drop the finalized code in here once I get things working in case anyone else wants to do something similar in the future.
bg.ranken
Enthusiast

Posts: 56
Liked: 10 times
Joined: Wed Feb 18, 2015 8:13 pm
Full Name: Randall Kender

Return to PowerShell

### Who is online

Users browsing this forum: No registered users and 1 guest