Compare commits
No commits in common. "60d7e7bf83ded5880525167646b141c76ffd63f1" and "b1ca51aa8f7646c32a65b41e7cebb13a3b294d89" have entirely different histories.
60d7e7bf83
...
b1ca51aa8f
3 changed files with 55 additions and 79 deletions
26
backup.ps1
26
backup.ps1
|
@ -13,8 +13,7 @@ $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 = "Stop"
|
$ErrorActionPreference = "SilentlyContinue"
|
||||||
$DebugPreference = "SilentlyContinue"
|
|
||||||
|
|
||||||
if ( $debug ) {
|
if ( $debug ) {
|
||||||
$DebugPreference = "Continue"
|
$DebugPreference = "Continue"
|
||||||
|
@ -22,7 +21,6 @@ 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(
|
||||||
|
@ -38,7 +36,6 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +63,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 to logfile: $($global:logfile) "
|
write-debug "Write fo Set-Messagefile $($global:logfile) "
|
||||||
|
|
||||||
# Zip old messah=ges (Archives)
|
# Zip old messah=ges (Archives)
|
||||||
# TODO: Test this functionnality
|
# TODO: Test this functionnality
|
||||||
|
@ -84,7 +81,7 @@ if ( $log ) {
|
||||||
"\log\",
|
"\log\",
|
||||||
$logname,
|
$logname,
|
||||||
".archives.",
|
".archives.",
|
||||||
($_.CreationTime).ToString("yyyy.MM"),
|
$_.CreationTime).ToString("yyyy.MM"),
|
||||||
".log.7z`"",
|
".log.7z`"",
|
||||||
" `"",
|
" `"",
|
||||||
$_.FullName,
|
$_.FullName,
|
||||||
|
@ -129,12 +126,9 @@ $backup.actions | % {
|
||||||
$destination = ""
|
$destination = ""
|
||||||
$source = ""
|
$source = ""
|
||||||
|
|
||||||
Set-Message "---"
|
# Test input path
|
||||||
Set-Message "Process action : $($_.name) | type: $($_.type)"
|
Write-Debug "Process action : $($_.name) | type: $($_.type)"
|
||||||
Set-Message "---"
|
if ( $_.psobject.properties.match('source_eval').Count -and $_.source_eval -eq $true ) {
|
||||||
|
|
||||||
# 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)"
|
||||||
}
|
}
|
||||||
|
@ -200,9 +194,8 @@ $backup.actions | % {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Write-Debug "Import module $($_.type)"
|
Import-Module -Name "$PSScriptRoot\lib\backupscript-$($_.type).psm1" -Force
|
||||||
Import-Module -Name "$PSScriptRoot\lib\backupscript-$($_.type).psm1"
|
Backup-Create -source $source -dest $destination -name $_.name -opt $opt #-EA Stop
|
||||||
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
|
||||||
|
@ -217,8 +210,9 @@ $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
|
||||||
Get-Module -Name "backupscript-$($_.type)" | Remove-Module -Force
|
Remove-Module -Name $_.type -Force
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Set-Message "Backup operation done"
|
Set-Message "Backup operation done"
|
||||||
|
|
|
@ -1,64 +1,46 @@
|
||||||
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
|
||||||
|
)
|
||||||
|
|
||||||
function Backup-Create () {
|
$copy_opt = "--stats=0 --log-level INFO --log-format `"shorfile`" "
|
||||||
Param(
|
Set-Message "Sync files from $source to $dest"
|
||||||
[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") ) {
|
if ( -not (Test-Path "$global:bin_path\rclone.exe") ) {
|
||||||
Throw "Rclone executable not found"
|
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]
|
||||||
|
|
||||||
# While I can't process rclone output with | %{ } when script that call this
|
switch ( $type.trim() ){
|
||||||
# module is exetutes by a scheduled task, I log all rclone stuff to a
|
"INFO" {
|
||||||
# temporary file and process it later
|
Set-Message $msg.trim()
|
||||||
|
}
|
||||||
$rclone_logfile = "$env:TEMP\rclone.log"
|
"WARN" {
|
||||||
$clone_options = "--stats=0 --log-level INFO --log-format `"shorfile`" --log-file $rclone_logfile"
|
Set-Warning $msg.trim()
|
||||||
if ( Test-Path $rclone_logfile ) {
|
}
|
||||||
Remove-Item $rclone_logfile -Force
|
"ERROR" {
|
||||||
}
|
Set-Error $msg.trim()
|
||||||
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
|
|
@ -1,4 +1,4 @@
|
||||||
Write-Debug "Utils module loaded"
|
Write-Output "chargement du module"
|
||||||
|
|
||||||
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 ConvertTo-CmdlineOptions with $options, separator: `"$sep`""
|
Write-Debug "Call parse_options with $options, separator: `"$sep`""
|
||||||
$opt = ""
|
$opt = ""
|
||||||
if ($options -eq $false) {
|
if ($options -eq $false) {
|
||||||
return ""
|
return ""
|
||||||
|
|
Reference in a new issue