First commit

This commit is contained in:
Yorick Barbanneau 2021-01-15 12:19:10 +01:00
commit cce27559b1
9 changed files with 676 additions and 0 deletions

View file

@ -0,0 +1,46 @@
function Backup-Create () {
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`" "
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]
switch ( $type.trim() ){
"INFO" {
Set-Message $msg.trim()
}
"WARN" {
Set-Warning $msg.trim()
}
"ERROR" {
Set-Error $msg.trim()
}
}
}
}
}
Export-ModuleMember -Function Backup-Create

View file

@ -0,0 +1,50 @@
function Backup-GetModuleInfo {
$local:version = "0.1a"
$local:name = "backup_module-7zip"
Write-Log "INFO" "$name v.$version"
}
function Backup-Create {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]
$source,
[Parameter(Mandatory=$True)]
[string]
$dest,
[Parameter(Mandatory=$True)]
[string]
$name,
[Parameter(Mandatory=$false)]
[object]
$options
)
Set-Message "Compression of $source to $dest"
if ( -not (Test-Path "$global:bin_path\7za.exe") ) {
Throw [System.IO.FileNotFoundException]::new("7zip executable not found")
return
}
$zip_opt = ConvertTo-CmdlineOptions $options ""
$zip_opt = -join($zip_opt, " -bb3")
$command = -join($global:bin_path, "/7za.exe", " u"," $zip_opt", " `"$dest", "\", "$name", ".7z`"", " `"$source`"")
Write-Debug "commande : $command"
Invoke-Expression $command | % {
if ($_ -ne "" ){ Set-Message $_ }
}
Set-Message "Compressing $source done"
}
function backup-verify {
Set-Message "Verify zip archive"
}
Export-ModuleMember -Function Backup-Create

View file

@ -0,0 +1,69 @@
$ProgressPreference = "SilentlyContinue"
function Backup-Create () {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]
$source,
[Parameter(Mandatory=$True)]
[string]
$dest,
[Parameter(Mandatory=$True)]
[string]
$name,
[Parameter(Mandatory=$True)]
[object]
$options
)
if ( -not (Get-Module -ListAvailable -Name sqlserver) ) {
Throw "There is not sqlserver Powershell module"
}
if ( $options -eq $false ){
Throw "you must define options for MSSQL action"
}
if ( -not ($options.ContainsKey("instance")) -or -not ($options.ContainsKey("server")) ) {
Throw "Instance and server option needed"
}
if ( -not ( Get-SqlAgent -ServerInstance "$($options.server)\$($options.instance)" -EA SilentlyContinue ) ) {
Throw "Can't connect to $($options.server)\$($options.instance)"
}
if ( $options.ContainsKey("compression") -and $options.compression -eq $true ) {
Write-Debug "Compression output activated"
$comp = "on"
$ext = "zip"
}
else {
Write-Debug "Compression disable"
$comp = "off"
$ext = "bak"
}
# Get databases on instance
$dblist = Get-SqlDatabase -ServerInstance "$($options.server)\$($options.instance)" | Select Name
#
if (-not ( -not ($options.ContainsKey("databases")) -or $options.databases -eq "" -or $options.databases -eq "all" )) {
Write-Debug "Backup some databases : $($options.databases)"
$dblist = $dblist | Where-Object { $_.Name -in ($options.databases).split(" ") }
}
$dblist | % {
Set-Message "Backup database $($_.Name)"
$db = $($_.Name)
try {
Backup-SqlDatabase -Database "$db" -ServerInstance "$($options.server)\$($options.instance)" -CompressionOption $comp -BackupFile $dest\$db.$ext -EA SilentlyContinue
}
catch {
Set-Warning "Can't backup $db"
}
}
}
Export-ModuleMember -Function Backup-Create

View file

@ -0,0 +1,84 @@
Write-Output "chargement du module"
function Get-LogTimeStamp {
return (Get-Date).toString("yyyy.MM.dd HH:mm:ss")
}
function ConvertTo-CmdlineOptions {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[object]
$options,
[Parameter(Mandatory=$False)]
[string]
$sep=" "
)
Write-Debug "Call parse_options with $options, separator: `"$sep`""
$opt = ""
if ($options -eq $false) {
return ""
}
$options.Keys | foreach {
if ( -not $($options.item($_)) -eq "" ) {
$opt = -join($opt, " $($_)", $sep, "$($options.item($_))")
}
else {
$opt = -join($opt, " $($_)")
}
}
return $opt
}
function Set-Error {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]
$message
)
if ( $global:logfile ) {
Add-Content $global:logfile -Value "$(Get-LogTimeStamp)`tERROR`t$message"
}
else {
Write-Host -ForegroundColor Red $message
}
}
function Set-Warning {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]
$message
)
if ( $global:logfile ) {
Add-Content $global:logfile -Value "$(Get-LogTimeStamp)`tWARN`t$message"
}
else {
Write-Host -ForegroundColor Yellow $message
}
}
function Set-Message {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]
$message
)
if ( $global:logfile ) {
Add-Content $global:logfile -Value "$(Get-LogTimeStamp)`tINFO`t$message"
}
else {
Write-Host "$message"
}
}
Export-ModuleMember -Function ConvertTo-CmdlineOptions, Set-Message, Set-Warning, Set-Error