How to conditionally prevent execution of a backup?


Author
Message
Patrick O'Keefe
Patrick O'Keefe
Expert
Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)
Group: Forum Members
Posts: 604, Visits: 4.1K
I don't know whether this question belongs in the Scheduling or Scripting sub-forum. Sorry if I've put it in the wrong place.

I'm starting to switch (as of yesterday) to Macrium Reflect after about 13 year using Acronis True Image and am fumbling with some of the differences. Acronis has the facility to execute a user-supplied command that can cancel the backup if an error return code is given. I use that function and am trying to achieve the same result in MR.

I see that MR has the ability to create a backup script (either VBS or PowerShell) that can be modified, but I don't know either VBS or PowerShell so I'm reluctant to go that route. I tried using MR's capability of automatically including a user-supplied command, but I don't see that a failure of the command will kill execution of the backup script.  Assuming I would use PowerShell, is there an easy way I can make a failure of a .bat file terminate execution of the backup script?

I also see that MR uses Windows' Task Scheduler. (Acronis TI has its own internal scheduler.) This means I could schedule a small .bat file that executes my command and then, if all is well, executes the MR backup command.  I suspect MR would rebuild the schedule entry if I change the backup definition (and I'll probably be doing that a lot as I get familiar with MR), but is that a better way to acheive my goal?

jphughan
jphughan
Macrium Evangelist
Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)
Group: Forum Members
Posts: 14K, Visits: 85K
As far as I can tell, Reflect's interface itself doesn't have a way to configure this.  But either of the options you describe will work.  Also note that you can have Reflect generate a batch file, not just a PS or VBS file.  You don't get as many options that way (and you have to create separate batch files for separate backup types, i.e. Full/Diff/Inc), but if you're more comfortable with batch files and it does what you need, then it may be the best option for you -- or it might at least give you a baseline for you to expand on.

If on the other hand you make some other batch file the "master" script that just calls Reflect, you're correct that managing your schedule within Reflect would be more difficult, if not impossible.  Reflect only shows scheduled tasks that it creates, and I don't think the ones it creates are editable by users within Task Scheduler, at least not without jumping through hoops.  And if memory serves, some people who have jumped through those hoops have found that while the tasks still work, they're no longer displayed within Reflect.

If you can't get the batch file option to work, I would be willing to help with the PowerShell piece.  You'd use the Start-Process cmdlet to invoke an external batch file, and as long as your batch file returns a non-zero exit code when it encounters an error, you'd be able to capture that within PowerShell and then have a simple If/Else statement to handle whether to proceed with the rest of the Reflect-generated script after that batch file completes.  In fact there's even an example of this within the PowerShell script that Reflect generates.  But here's basically what you could write:

$MyBatchFile = Start-Process -FilePath C:\MyScript.bat -ArgumentList "-arg1 value1 -arg2 value2" -PassThru -Wait
if ($MyBatchFile.exitcode -ne 0)
{
Write-Output "Batch file failed, skipping backup"
Exit 1
}


In English, the above means, "Call MyScript.bat using these parameters and values, and wait for the batch file to complete before proceeding with the rest of the PowerShell script. (The PassThru argument causes the process execution to return an object with some information about the execution, which is important because that's what you're capturing in the $MyBatchFile variable that you'll be checking next.)  When the process completes, if the exit code of that process execution is not equal to 0 (success), then exit the PowerShell script with exit code 1, i.e. Error."  If your batch file returns multiple possible error codes and you want to take different actions based on that, you can use something called a switch loop to efficiently express that.

Macrium is working on implementing their own scheduling engine in order to get away from Windows Task Scheduler in order to deal with some strange issues it seems to have, which aren't technically Macrium's fault but which impact Reflect's operation since Reflect currently relies on WTS.  No ETA on that yet though.

Edited 7 June 2020 11:39 PM by jphughan
Patrick O'Keefe
Patrick O'Keefe
Expert
Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)
Group: Forum Members
Posts: 604, Visits: 4.1K
Thank you. I'll try the PS modification you suggested.
Patrick O'Keefe
Patrick O'Keefe
Expert
Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)
Group: Forum Members
Posts: 604, Visits: 4.1K
jphughan - 7 June 2020 11:30 PM
As far as I can tell, Reflect's interface itself doesn't have a way to configure this.  But either of the options you describe will work.  Also note that you can have Reflect generate a batch file, not just a PS or VBS file.  You don't get as many options that way (and you have to create separate batch files for separate backup types, i.e. Full/Diff/Inc), but if you're more comfortable with batch files and it does what you need, then it may be the best option for you -- or it might at least give you a baseline for you to expand on.

If on the other hand you make some other batch file the "master" script that just calls Reflect, you're correct that managing your schedule within Reflect would be more difficult, if not impossible.  Reflect only shows scheduled tasks that it creates, and I don't think the ones it creates are editable by users within Task Scheduler, at least not without jumping through hoops.  And if memory serves, some people who have jumped through those hoops have found that while the tasks still work, they're no longer displayed within Reflect.

If you can't get the batch file option to work, I would be willing to help with the PowerShell piece.  You'd use the Start-Process cmdlet to invoke an external batch file, and as long as your batch file returns a non-zero exit code when it encounters an error, you'd be able to capture that within PowerShell and then have a simple If/Else statement to handle whether to proceed with the rest of the Reflect-generated script after that batch file completes.  In fact there's even an example of this within the PowerShell script that Reflect generates.  But here's basically what you could write:

$MyBatchFile = Start-Process -FilePath C:\MyScript.bat -ArgumentList "-arg1 value1 -arg2 value2" -PassThru -Wait
if ($MyBatchFile.exitcode -ne 0)
{
Write-Output "Batch file failed, skipping backup"
Exit 1
}


In English, the above means, "Call MyScript.bat using these parameters and values, and wait for the batch file to complete before proceeding with the rest of the PowerShell script. (The PassThru argument causes the process execution to return an object with some information about the execution, which is important because that's what you're capturing in the $MyBatchFile variable that you'll be checking next.)  When the process completes, if the exit code of that process execution is not equal to 0 (success), then exit the PowerShell script with exit code 1, i.e. Error."  If your batch file returns multiple possible error codes and you want to take different actions based on that, you can use something called a switch loop to efficiently express that.

Macrium is working on implementing their own scheduling engine in order to get away from Windows Task Scheduler in order to deal with some strange issues it seems to have, which aren't technically Macrium's fault but which impact Reflect's operation since Reflect currently relies on WTS.  No ETA on that yet though.

I misunderstood the purpose of the VBS, PS, and .bat scripts that MR could generate.  I though they contained steps that had to be performed when invoking MR. I could not understand why the .bat script was so different than the other two.  I now understand that these are just model or template scripts.  I can probably write a single small .bat file that will work with all my backups.  At worst, I'll have one script for disk/partition backups and one for file backups.  (I include a simple "Has this source been a target of ransomware?" test in each backup and kill the backup if the answer is "Yes".)

Patrick O'Keefe
Patrick O'Keefe
Expert
Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)
Group: Forum Members
Posts: 604, Visits: 4.1K
After futzing around with some simple .bat files for a few weeks I've decided to try my hand a PowerShell.  There's a lot in the Powershell (and VBS) sample scripts that are not in the .bat script (such as elevating a manual invocation), and those bits might be important.  But I've got some questions - PS rather than MR questions, but if I put off the programming until I'm proficient at PS it will never happen.

The MR-generated code uses Write-Host and Write-Warning.  Your sample uses Write-Output. I read that output from Write-Host does not go into the pipeline, but I think I don't care about that in this case.  I also read that output from Write-Output is logged but not displayed, but that seems not to be the case.  How do I decide which to use?

My script (which is really the MR-generated script with a modification of your script inserted) seems to be working, but it would make me more comfortable to know what I'm doing ... and why.



jphughan
jphughan
Macrium Evangelist
Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)
Group: Forum Members
Posts: 14K, Visits: 85K
Write-Host will only ever show in the console window, and it has some options for colorizing the text and the background color that can be handy in scenarios where that's useful and where it's expected that somebody will be watching the output.

Write-Output does not offer those types of options.

If you just run a script normally, output from either cmdlet will be shown in the console window.

However, a difference comes into play if for example you call a script and choose to have its output directed to a text file, e.g. ".\MyScript.ps1 > C:\Logs\MyscriptLog.txt"  In that case, output generated by Write-Host will continue to appear in the console, but won't be captured in the text file that you're outputting to.  Conversely, output generated by Write-Output will NOT be shown in the console window, but WILL be captured into the text file that you specify.

I write a fair number of scripts that are NOT designed to be invoked manually by some user that will be sitting there watching it run, but where someone may well want to review a log of the script results after the fact and might therefore configure a script to direct its output to a text file.  So I default to using Write-Output.  However, there are times where I'll generate output that would be nice for a user who MIGHT be watching the script to help reassure them that something is in fact happening, but would be completely irrelevant to somebody who just wanted to review the log after the fact.  In cases like that, I'll use Write-Host in order to reassure that possible user without cluttering up an "action report" in the log with messages that were only relevant while the script was running.

Patrick O'Keefe
Patrick O'Keefe
Expert
Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)
Group: Forum Members
Posts: 604, Visits: 4.1K
jphughan - 25 August 2020 10:51 PM
Write-Host will only ever show in the console window, and it has some options for colorizing the text and the background color that can be handy in scenarios where that's useful and where it's expected that somebody will be watching the output.

Write-Output does not offer those types of options.

If you just run a script normally, output from either cmdlet will be shown in the console window.

However, a difference comes into play if for example you call a script and choose to have its output directed to a text file, e.g. ".\MyScript.ps1 > C:\Logs\MyscriptLog.txt"  In that case, output generated by Write-Host will continue to appear in the console, but won't be captured in the text file that you're outputting to.  Conversely, output generated by Write-Output will NOT be shown in the console window, but WILL be captured into the text file that you specify.

I write a fair number of scripts that are NOT designed to be invoked manually by some user that will be sitting there watching it run, but where someone may well want to review a log of the script results after the fact and might therefore configure a script to direct its output to a text file.  So I default to using Write-Output.  However, there are times where I'll generate output that would be nice for a user who MIGHT be watching the script to help reassure them that something is in fact happening, but would be completely irrelevant to somebody who just wanted to review the log after the fact.  In cases like that, I'll use Write-Host in order to reassure that possible user without cluttering up an "action report" in the log with messages that were only relevant while the script was running.

Thank you!  Very informative.  I can definitely see the need for both progress information (that would rarely be useful in a log) and status information (that definitely could be useful in a log).

But speaking of logging, ...  The HTML logs created by MR (understandably) contain just output from MR itself, not from the scripts.  There is an option to enable script logging, but it apparently applies to just VBS scripts.  And I've read that VBS is effectively dead (except, I guess, for use in products like MR that provide their own VBS execution environment).   Is there an equivalent way to collect PS output?  I guess I could write an outer script that just invokes and redirects the output from the real work-horse script, but that seems a bit messy.


jphughan
jphughan
Macrium Evangelist
Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)
Group: Forum Members
Posts: 14K, Visits: 85K
Reflect doesn't provide its own VBS execution environment.  VBS is still supported at an OS level, but I don't think it's being developed anymore.  Even "Windows PowerShell" is now on course for retirement in favor of "PowerShell", which is a multi-OS incarnation.  In theory you'll be able to run the exact same PowerShell on Windows, Linux, and even macOS.

In terms of your question, Macrium relatively recently added a feature to Reflect to enable just what you're asking about.  Someone asked for it, and it wasn't there, but a week or two later a Reflect update arrived to add it. Smile  Here is the documentation.

Patrick O'Keefe
Patrick O'Keefe
Expert
Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)Expert (879 reputation)
Group: Forum Members
Posts: 604, Visits: 4.1K
Thanks.  I'll give that a try.  The example modification uses just a generic name for the log file: "-RedirectStandardOutput c:\backup_log.txt", but I think it would be better to have one log file for each backup definition (if there is more that one definition using a script).  The log file would be of use only if it did not immediately get overwritten by the new backup.

jphughan
jphughan
Macrium Evangelist
Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)Macrium Evangelist (22K reputation)
Group: Forum Members
Posts: 14K, Visits: 85K
You can create a simple test job to see if the default behavior is to overwrite an existing log file or append to it.  If it overwrites, then you could incorporate a variable into the name of the log file, such as to include the date and time of the job.  Example:

$Date = Get-Date
$DateFormatted = $Date.ToString('yyyy-MM-dd_HHmm')
$FilePath = "C:\Logs\MyBackupLog-$DateFormatted.txt"


That could in theory be condensed down into a one-liner, but if you're getting used to PowerShell, I figured I'd optimize for readability. Smile

Edited 26 August 2020 12:32 AM by jphughan
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Reading This Topic

Login

Explore
Messages
Mentions
Search