First commit
This commit is contained in:
commit
cce27559b1
9 changed files with 676 additions and 0 deletions
69
lib/backupscript-dump_mssql.psm1
Normal file
69
lib/backupscript-dump_mssql.psm1
Normal 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
|
Reference in a new issue