From d6c94a35c4e924a33f066b2f50e2553cfec968b9 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Tue, 19 Jan 2021 13:55:12 +0100 Subject: [PATCH 1/8] Missing ( in log() --- backup.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backup.ps1 b/backup.ps1 index 15d6b7e..29be3d3 100644 --- a/backup.ps1 +++ b/backup.ps1 @@ -81,7 +81,7 @@ if ( $log ) { "\log\", $logname, ".archives.", - $_.CreationTime).ToString("yyyy.MM"), + ($_.CreationTime).ToString("yyyy.MM"), ".log.7z`"", " `"", $_.FullName, From 2a40ac1377eabfc60211c2e7a289236780c248fd Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Tue, 19 Jan 2021 13:56:28 +0100 Subject: [PATCH 2/8] Log message correction in log() --- backup.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backup.ps1 b/backup.ps1 index 29be3d3..412ce3d 100644 --- a/backup.ps1 +++ b/backup.ps1 @@ -63,7 +63,7 @@ if ( -not $conf ) { if ( $log ) { $logname = (Get-Item $conf ).Basename $global:logfile = "$PSScriptRoot\log\$((Get-Date).ToString("yyyy.MM.dd"))_$($logname).log" - write-debug "Write fo Set-Messagefile $($global:logfile) " + write-debug "Write to logfile: $($global:logfile) " # Zip old messah=ges (Archives) # TODO: Test this functionnality From 1397b77f4959d3bc2a3f20471153eb2d230b00b7 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Tue, 19 Jan 2021 16:49:28 +0100 Subject: [PATCH 3/8] Add some output messages --- backup.ps1 | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/backup.ps1 b/backup.ps1 index 412ce3d..209ae81 100644 --- a/backup.ps1 +++ b/backup.ps1 @@ -126,9 +126,12 @@ $backup.actions | % { $destination = "" $source = "" - # Test input path - Write-Debug "Process action : $($_.name) | type: $($_.type)" - if ( $_.psobject.properties.match('source_eval').Count -and $_.source_eval -eq $true ) { + Set-Message "---" + Set-Message "Process action : $($_.name) | type: $($_.type)" + Set-Message "---" + + # Process source + if ( $_.psobject.properties.match('source_eval').Count -and $_.source_eval -eq $true ) { $_.source = eval_var $($_.source) Write-Debug "Result $($_.source)" } From 990c9eb249c35a8e3b2e252fd66be252aa2918a5 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 20 Jan 2021 17:35:45 +0100 Subject: [PATCH 4/8] Update error and debug actions --- backup.ps1 | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/backup.ps1 b/backup.ps1 index 209ae81..3fcd3ae 100644 --- a/backup.ps1 +++ b/backup.ps1 @@ -13,7 +13,8 @@ $global:logfile = $false $tmp_path = "$PSScriptRoot\tmp" $global:bin_path = "$PSScriptRoot\bin" $ProgressPreference = "SilentlyContinue" -$ErrorActionPreference = "SilentlyContinue" +$ErrorActionPreference = "Stop" +$DebugPreference = "SilentlyContinue" if ( $debug ) { $DebugPreference = "Continue" @@ -36,6 +37,7 @@ function json_to_hash () { Write-Debug "Found value $_" $r[$_.Name] = $_.Value } + Write-debug "End json_to_hash" return $r } @@ -197,8 +199,9 @@ $backup.actions | % { } try { - Import-Module -Name "$PSScriptRoot\lib\backupscript-$($_.type).psm1" -Force - Backup-Create -source $source -dest $destination -name $_.name -opt $opt #-EA Stop + Write-Debug "Import module $($_.type)" + Import-Module -Name "$PSScriptRoot\lib\backupscript-$($_.type).psm1" + Backup-Create -source $source -dest $destination -name $_.name -opt $opt if ( $_.psobject.properties.match('hook_postexec').Count -and $_.hook_postexec -ne "" ) { Set-Message "Postexec hook : execute $($_.hook_postexec)" Invoke-Expression $_.hook_postexec From 74eae2fd117ad857ebf0c02bedc91da7f3dd987f Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 20 Jan 2021 21:44:46 +0100 Subject: [PATCH 5/8] Remove module gracefully --- backup.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backup.ps1 b/backup.ps1 index 3fcd3ae..3d7adf8 100644 --- a/backup.ps1 +++ b/backup.ps1 @@ -216,9 +216,8 @@ $backup.actions | % { if ( Test-Path "$($net_default_letter):" ){ Remove-PSDrive -Name $net_default_letter } - # Remove Module - Remove-Module -Name $_.type -Force + Get-Module -Name "backupscript-$($_.type)" | Remove-Module -Force } } Set-Message "Backup operation done" From 6b7c1aa95eec587b579935cd91cd3d978f6dbc3a Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 20 Jan 2021 21:49:07 +0100 Subject: [PATCH 6/8] Code Indentation fix --- backup.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backup.ps1 b/backup.ps1 index 3d7adf8..dbd8b71 100644 --- a/backup.ps1 +++ b/backup.ps1 @@ -22,6 +22,7 @@ if ( $debug ) { Import-Module -Name "$PSScriptRoot\lib\backupscript-global_utils.psm1" -Scope Global -Force + function json_to_hash () { [CmdletBinding()] Param( @@ -200,7 +201,7 @@ $backup.actions | % { try { Write-Debug "Import module $($_.type)" - Import-Module -Name "$PSScriptRoot\lib\backupscript-$($_.type).psm1" + Import-Module -Name "$PSScriptRoot\lib\backupscript-$($_.type).psm1" Backup-Create -source $source -dest $destination -name $_.name -opt $opt if ( $_.psobject.properties.match('hook_postexec').Count -and $_.hook_postexec -ne "" ) { Set-Message "Postexec hook : execute $($_.hook_postexec)" From ba51b397d5bc9a914253e74db53217552b05b09b Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 20 Jan 2021 22:08:06 +0100 Subject: [PATCH 7/8] Rewrite clone module Because process rclone output can't be processed with % {} when script that call clone module when executed by scheduled task. --- lib/backupscript-clone.psm1 | 102 +++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 42 deletions(-) diff --git a/lib/backupscript-clone.psm1 b/lib/backupscript-clone.psm1 index 985d4fb..49ec070 100644 --- a/lib/backupscript-clone.psm1 +++ b/lib/backupscript-clone.psm1 @@ -1,46 +1,64 @@ -function Backup-Create () { - Param( - [Parameter(Mandatory=$True)] - [string] - $source, - [Parameter(Mandatory=$True)] - [string] - $dest, - [Parameter(Mandatory=$false)] - [string] - $name, - [Parameter(Mandatory=$false)] - [object] - $options - ) +Write-Debug "Module Clone loaded" - $copy_opt = "--stats=0 --log-level INFO --log-format `"shorfile`" " - Set-Message "Sync files from $source to $dest" - - if ( -not (Test-Path "$global:bin_path\rclone.exe") ) { - Throw "Rclone executable not found" - } - $copy_opt = -join($copy_opt, $(ConvertTo-CmdlineOptions $options)) - $command = -join($global:bin_path, "\rclone.exe", " sync", " `"$source`"", " `"$dest`"", " $copy_opt", " 2>&1") - Set-Message "$command" - Invoke-Expression $command -ErrorAction Ignore | % { - if ($_ -ne ""){ - $type, $mdg = ($_ -split ':')[0] - $msg = -join ($_ -split ':')[1..2] +function Backup-Create () { + Param( + [Parameter(Mandatory=$True)][string]$source, + [Parameter(Mandatory=$True)][string]$dest, + [Parameter(Mandatory=$false)][string]$name, + [Parameter(Mandatory=$false)][object]$options + ) - switch ( $type.trim() ){ - "INFO" { - Set-Message $msg.trim() - } - "WARN" { - Set-Warning $msg.trim() - } - "ERROR" { - Set-Error $msg.trim() - } - } - } - } + if ( -not (Test-Path "$global:bin_path\rclone.exe") ) { + Throw "Rclone executable not found" + } + + # While I can't process rclone output with | %{ } when script that call this + # module is exetutes by a scheduled task, I log all rclone stuff to a + # temporary file and process it later + + $rclone_logfile = "$env:TEMP\rclone.log" + $clone_options = "--stats=0 --log-level INFO --log-format `"shorfile`" --log-file $rclone_logfile" + if ( Test-Path $rclone_logfile ) { + Remove-Item $rclone_logfile -Force + } + Set-Message "Sync files from $source to $dest encule ta mere" + $clone_options = -join($clone_options, $(ConvertTo-CmdlineOptions $options)) + $command = -join($global:bin_path, "\rclone.exe", " sync", " `"$source`"", " `"$dest`"", $clone_options, " 2>&1") + Set-Message "execute: $command" + try { + Invoke-Expression $command -ErrorAction Ignore | Out-Null + } + catch { + Throw "Error on rclone command execution" + } + Set-Message "Process rclone log file" + Get-Content "$rclone_logfile" | % { + if ($_ -ne ""){ + $type, $mdg = ($_ -split ':')[0] + $msg = -join ($_ -split ':')[1..2] + + switch ( $type.trim() ){ + "INFO" { + Set-Message $msg.trim() + } + "NOTICE" { + Set-Warning $msg.trim() + } + "ERROR" { + Set-Error $msg.trim() + } + } + } + } + try { + Remove-Item $rclone_logfile -Force -EA Stop | Out-Null + } + catch [System.Management.Automation.ItemNotFoundException] { + Throw "$rclone_logfile not found, can't delete" + } + catch { + Throw "Can't delete $rclone_logfile, reason unknown" + } } -Export-ModuleMember -Function Backup-Create \ No newline at end of file +Export-ModuleMember -Function Backup-Create From 60d7e7bf83ded5880525167646b141c76ffd63f1 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 20 Jan 2021 22:14:39 +0100 Subject: [PATCH 8/8] Modify debug messages --- lib/backupscript-global_utils.psm1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/backupscript-global_utils.psm1 b/lib/backupscript-global_utils.psm1 index c8d47d6..04a3200 100644 --- a/lib/backupscript-global_utils.psm1 +++ b/lib/backupscript-global_utils.psm1 @@ -1,4 +1,4 @@ -Write-Output "chargement du module" +Write-Debug "Utils module loaded" function Get-LogTimeStamp { return (Get-Date).toString("yyyy.MM.dd HH:mm:ss") @@ -16,7 +16,7 @@ function ConvertTo-CmdlineOptions { $sep=" " ) - Write-Debug "Call parse_options with $options, separator: `"$sep`"" + Write-Debug "Call ConvertTo-CmdlineOptions with $options, separator: `"$sep`"" $opt = "" if ($options -eq $false) { return "" @@ -81,4 +81,4 @@ function Set-Message { } } -Export-ModuleMember -Function ConvertTo-CmdlineOptions, Set-Message, Set-Warning, Set-Error \ No newline at end of file +Export-ModuleMember -Function ConvertTo-CmdlineOptions, Set-Message, Set-Warning, Set-Error