Because process rclone output can't be processed with % {} when script that call clone module when executed by scheduled task.
64 lines
1.8 KiB
PowerShell
64 lines
1.8 KiB
PowerShell
Write-Debug "Module Clone loaded"
|
|
|
|
function Backup-Create () {
|
|
Param(
|
|
[Parameter(Mandatory=$True)][string]$source,
|
|
[Parameter(Mandatory=$True)][string]$dest,
|
|
[Parameter(Mandatory=$false)][string]$name,
|
|
[Parameter(Mandatory=$false)][object]$options
|
|
)
|
|
|
|
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
|