Compare commits

..

8 commits

Author SHA1 Message Date
60d7e7bf83 Modify debug messages 2021-01-20 22:14:39 +01:00
ba51b397d5 Rewrite clone module
Because process rclone output can't be processed with % {} when script
that call clone module when executed by scheduled task.
2021-01-20 22:08:06 +01:00
6b7c1aa95e Code Indentation fix 2021-01-20 21:49:07 +01:00
74eae2fd11 Remove module gracefully 2021-01-20 21:44:46 +01:00
990c9eb249 Update error and debug actions 2021-01-20 17:35:45 +01:00
1397b77f49 Add some output messages 2021-01-19 16:49:28 +01:00
2a40ac1377 Log message correction in log() 2021-01-19 13:56:28 +01:00
d6c94a35c4 Missing ( in log() 2021-01-19 13:55:12 +01:00
3 changed files with 79 additions and 55 deletions

View file

@ -13,7 +13,8 @@ $global:logfile = $false
$tmp_path = "$PSScriptRoot\tmp" $tmp_path = "$PSScriptRoot\tmp"
$global:bin_path = "$PSScriptRoot\bin" $global:bin_path = "$PSScriptRoot\bin"
$ProgressPreference = "SilentlyContinue" $ProgressPreference = "SilentlyContinue"
$ErrorActionPreference = "SilentlyContinue" $ErrorActionPreference = "Stop"
$DebugPreference = "SilentlyContinue"
if ( $debug ) { if ( $debug ) {
$DebugPreference = "Continue" $DebugPreference = "Continue"
@ -21,6 +22,7 @@ if ( $debug ) {
Import-Module -Name "$PSScriptRoot\lib\backupscript-global_utils.psm1" -Scope Global -Force Import-Module -Name "$PSScriptRoot\lib\backupscript-global_utils.psm1" -Scope Global -Force
function json_to_hash () { function json_to_hash () {
[CmdletBinding()] [CmdletBinding()]
Param( Param(
@ -36,6 +38,7 @@ function json_to_hash () {
Write-Debug "Found value $_" Write-Debug "Found value $_"
$r[$_.Name] = $_.Value $r[$_.Name] = $_.Value
} }
Write-debug "End json_to_hash"
return $r return $r
} }
@ -63,7 +66,7 @@ if ( -not $conf ) {
if ( $log ) { if ( $log ) {
$logname = (Get-Item $conf ).Basename $logname = (Get-Item $conf ).Basename
$global:logfile = "$PSScriptRoot\log\$((Get-Date).ToString("yyyy.MM.dd"))_$($logname).log" $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) # Zip old messah=ges (Archives)
# TODO: Test this functionnality # TODO: Test this functionnality
@ -81,7 +84,7 @@ if ( $log ) {
"\log\", "\log\",
$logname, $logname,
".archives.", ".archives.",
$_.CreationTime).ToString("yyyy.MM"), ($_.CreationTime).ToString("yyyy.MM"),
".log.7z`"", ".log.7z`"",
" `"", " `"",
$_.FullName, $_.FullName,
@ -126,9 +129,12 @@ $backup.actions | % {
$destination = "" $destination = ""
$source = "" $source = ""
# Test input path Set-Message "---"
Write-Debug "Process action : $($_.name) | type: $($_.type)" Set-Message "Process action : $($_.name) | type: $($_.type)"
if ( $_.psobject.properties.match('source_eval').Count -and $_.source_eval -eq $true ) { Set-Message "---"
# Process source
if ( $_.psobject.properties.match('source_eval').Count -and $_.source_eval -eq $true ) {
$_.source = eval_var $($_.source) $_.source = eval_var $($_.source)
Write-Debug "Result $($_.source)" Write-Debug "Result $($_.source)"
} }
@ -194,8 +200,9 @@ $backup.actions | % {
} }
try { try {
Import-Module -Name "$PSScriptRoot\lib\backupscript-$($_.type).psm1" -Force Write-Debug "Import module $($_.type)"
Backup-Create -source $source -dest $destination -name $_.name -opt $opt #-EA Stop 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 "" ) { if ( $_.psobject.properties.match('hook_postexec').Count -and $_.hook_postexec -ne "" ) {
Set-Message "Postexec hook : execute $($_.hook_postexec)" Set-Message "Postexec hook : execute $($_.hook_postexec)"
Invoke-Expression $_.hook_postexec Invoke-Expression $_.hook_postexec
@ -210,9 +217,8 @@ $backup.actions | % {
if ( Test-Path "$($net_default_letter):" ){ if ( Test-Path "$($net_default_letter):" ){
Remove-PSDrive -Name $net_default_letter Remove-PSDrive -Name $net_default_letter
} }
# Remove Module # Remove Module
Remove-Module -Name $_.type -Force Get-Module -Name "backupscript-$($_.type)" | Remove-Module -Force
} }
} }
Set-Message "Backup operation done" Set-Message "Backup operation done"

View file

@ -1,46 +1,64 @@
function Backup-Create () { Write-Debug "Module Clone loaded"
Param(
[Parameter(Mandatory=$True)]
[string]
$source,
[Parameter(Mandatory=$True)]
[string]
$dest,
[Parameter(Mandatory=$false)]
[string]
$name,
[Parameter(Mandatory=$false)]
[object]
$options
)
$copy_opt = "--stats=0 --log-level INFO --log-format `"shorfile`" " function Backup-Create () {
Set-Message "Sync files from $source to $dest" Param(
[Parameter(Mandatory=$True)][string]$source,
if ( -not (Test-Path "$global:bin_path\rclone.exe") ) { [Parameter(Mandatory=$True)][string]$dest,
Throw "Rclone executable not found" [Parameter(Mandatory=$false)][string]$name,
} [Parameter(Mandatory=$false)][object]$options
$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]
switch ( $type.trim() ){ if ( -not (Test-Path "$global:bin_path\rclone.exe") ) {
"INFO" { Throw "Rclone executable not found"
Set-Message $msg.trim() }
}
"WARN" { # While I can't process rclone output with | %{ } when script that call this
Set-Warning $msg.trim() # module is exetutes by a scheduled task, I log all rclone stuff to a
} # temporary file and process it later
"ERROR" {
Set-Error $msg.trim() $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 Export-ModuleMember -Function Backup-Create

View file

@ -1,4 +1,4 @@
Write-Output "chargement du module" Write-Debug "Utils module loaded"
function Get-LogTimeStamp { function Get-LogTimeStamp {
return (Get-Date).toString("yyyy.MM.dd HH:mm:ss") return (Get-Date).toString("yyyy.MM.dd HH:mm:ss")
@ -16,7 +16,7 @@ function ConvertTo-CmdlineOptions {
$sep=" " $sep=" "
) )
Write-Debug "Call parse_options with $options, separator: `"$sep`"" Write-Debug "Call ConvertTo-CmdlineOptions with $options, separator: `"$sep`""
$opt = "" $opt = ""
if ($options -eq $false) { if ($options -eq $false) {
return "" return ""
@ -81,4 +81,4 @@ function Set-Message {
} }
} }
Export-ModuleMember -Function ConvertTo-CmdlineOptions, Set-Message, Set-Warning, Set-Error Export-ModuleMember -Function ConvertTo-CmdlineOptions, Set-Message, Set-Warning, Set-Error