Rewrite clone module
Because process rclone output can't be processed with % {} when script that call clone module when executed by scheduled task.
This commit is contained in:
parent
6b7c1aa95e
commit
ba51b397d5
1 changed files with 60 additions and 42 deletions
|
@ -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
|
||||
Export-ModuleMember -Function Backup-Create
|
||||
|
|
Reference in a new issue