This repository has been archived on 2024-09-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
powershell_backupscript/lib/backupscript-dump_mssql.psm1
2021-01-15 12:19:10 +01:00

69 lines
No EOL
2.1 KiB
PowerShell

$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