Skip to content
Snippets Groups Projects
Commit 4c2b7d2a authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Make Create-Backup return True or False.

Instead of terminating the entire script immediately we log a message
and pass on success / fail so the remaining script logic can deal with
the situation.

Refers to #18, #28
parent be0b650a
No related branches found
No related tags found
No related merge requests found
...@@ -169,6 +169,10 @@ function File-IsUpToDate([string]$ExistingFile, [string]$UpdateCandidate) { ...@@ -169,6 +169,10 @@ function File-IsUpToDate([string]$ExistingFile, [string]$UpdateCandidate) {
function Create-Backup { function Create-Backup {
# Rename a file using a time-stamp suffix like "2017-12-04T16.41.35" while
# preserving its original suffix / extension.
#
# Return $True if the backup was created successfully, $False otherwise.
Param ( Param (
[Parameter(Mandatory=$True)] [Parameter(Mandatory=$True)]
[ValidateScript({Test-Path -PathType Leaf $_})] [ValidateScript({Test-Path -PathType Leaf $_})]
...@@ -178,19 +182,20 @@ function Create-Backup { ...@@ -178,19 +182,20 @@ function Create-Backup {
$FileWithoutSuffix = [io.path]::GetFileNameWithoutExtension($FileName) $FileWithoutSuffix = [io.path]::GetFileNameWithoutExtension($FileName)
$FileSuffix = [io.path]::GetExtension($FileName) $FileSuffix = [io.path]::GetExtension($FileName)
$BaseDir = Split-Path -Parent $FileName $BaseDir = Split-Path -Parent $FileName
# assemble a timestamp string like "2017-12-04T16.41.35" # assemble a timestamp string like "2017-12-04T16.41.35"
$BakTimeStamp = Get-Date -Format s | foreach {$_ -replace ":", "."} $BakTimeStamp = Get-Date -Format s | foreach {$_ -replace ":", "."}
$BakName = "$($FileWithoutSuffix)_pre-$($BakTimeStamp)$($FileSuffix)" $BakName = "$($FileWithoutSuffix)_pre-$($BakTimeStamp)$($FileSuffix)"
Log-Info "Creating backup of [$($FileName)] as [$($BaseDir)\$($BakName)]." Log-Info "Creating backup of [$($FileName)] as [$($BaseDir)\$($BakName)]."
try { try {
Rename-Item "$FileName" "$BaseDir\$BakName" -ErrorAction Stop Rename-Item "$FileName" "$BaseDir\$BakName"
} }
catch { catch {
$ex = $_.Exception.Message Log-Error "Backing up [$($DstFile)] FAILED:`n> $($_.Exception.Message)"
Log-Error "Backing up [$($FileName)] as [$($BakName)] FAILED!`n$($ex)" Return $False
Exit
} }
Return $True
} }
...@@ -225,12 +230,8 @@ function Update-File { ...@@ -225,12 +230,8 @@ function Update-File {
Stop-MyService "Found newer file at $($SrcFile), updating..." Stop-MyService "Found newer file at $($SrcFile), updating..."
try { if (-Not (Create-Backup -FileName $DstFile)) {
Create-Backup -FileName $DstFile Return $False
}
catch {
Exit
Log-Error "Backing up [$($DstFile)] FAILED:`n> $($_.Exception.Message)"
} }
try { try {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment