Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup-DbaDatabase - fixes piping issue #5041

Merged
merged 1 commit into from
Feb 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions functions/Backup-DbaDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -287,44 +287,43 @@ function Backup-DbaDatabase {

Write-Message -Level Verbose -Message "$($InputObject.Count) database to backup"

foreach ($Database in $InputObject) {
foreach ($db in $InputObject) {
$ProgressId = Get-Random
$failures = @()
$dbname = $Database.Name
$dbname = $db.Name
$server = $db.Parent

if ($dbname -eq "tempdb") {
Stop-Function -Message "Backing up tempdb not supported" -Continue
}

if ('Normal' -notin ($Database.Status -split ',')) {
if ('Normal' -notin ($db.Status -split ',')) {
Stop-Function -Message "Database status not Normal. $dbname skipped." -Continue
}

if ($Database.DatabaseSnapshotBaseName) {
if ($db.DatabaseSnapshotBaseName) {
Stop-Function -Message "Backing up snapshots not supported. $dbname skipped." -Continue
}

if ($null -eq $server) { $server = $Database.Parent }
Write-Message -Level Verbose -Message "Backup database $db"

Write-Message -Level Verbose -Message "Backup database $database"

if ($null -eq $Database.RecoveryModel) {
$Database.RecoveryModel = $server.Databases[$Database.Name].RecoveryModel
Write-Message -Level Verbose -Message "$dbname is in $($Database.RecoveryModel) recovery model"
if ($null -eq $db.RecoveryModel) {
$db.RecoveryModel = $server.Databases[$db.Name].RecoveryModel
Write-Message -Level Verbose -Message "$dbname is in $($db.RecoveryModel) recovery model"
}

# Fixes one-off cases of StackOverflowException crashes, see issue 1481
$dbRecovery = $Database.RecoveryModel.ToString()
$dbRecovery = $db.RecoveryModel.ToString()
if ($dbRecovery -eq 'Simple' -and $Type -eq 'Log') {
$failreason = "$database is in simple recovery mode, cannot take log backup"
$failreason = "$db is in simple recovery mode, cannot take log backup"
$failures += $failreason
Write-Message -Level Warning -Message "$failreason"
}

$lastfull = $database.Refresh().LastBackupDate.Year
$lastfull = $db.Refresh().LastBackupDate.Year

if ($Type -notin @("Database", "Full") -and $lastfull -eq 1) {
$failreason = "$database does not have an existing full backup, cannot take log or differentialbackup"
$failreason = "$db does not have an existing full backup, cannot take log or differentialbackup"
$failures += $failreason
Write-Message -Level Warning -Message "$failreason"
}
Expand All @@ -335,11 +334,11 @@ function Backup-DbaDatabase {

$server.ConnectionContext.StatementTimeout = 0
$backup = New-Object Microsoft.SqlServer.Management.Smo.Backup
$backup.Database = $Database.Name
$backup.Database = $db.Name
$Suffix = "bak"

if ($CompressBackup) {
if ($database.EncryptionEnabled) {
if ($db.EncryptionEnabled) {
Write-Message -Level Warning -Message "$dbname is enabled for encryption, will not compress"
$backup.CompressionOption = 2
} elseif ($server.Edition -like 'Express*' -or ($server.VersionMajor -eq 10 -and $server.VersionMinor -eq 0 -and $server.Edition -notlike '*enterprise*') -or $server.VersionMajor -lt 10) {
Expand Down Expand Up @@ -530,12 +529,14 @@ function Backup-DbaDatabase {
if ($server.VersionMajor -eq '8') {
$HeaderInfo = Get-BackupAncientHistory -SqlInstance $server -Database $dbname
} else {
$HeaderInfo = Get-DbaBackupHistory -SqlInstance $server -Database $dbname @gbhSwitch -IncludeCopyOnly -RecoveryFork $database.RecoveryForkGuid | Sort-Object -Property End -Descending | Select-Object -First 1
$HeaderInfo = Get-DbaBackupHistory -SqlInstance $server -Database $dbname @gbhSwitch -IncludeCopyOnly -RecoveryFork $db.RecoveryForkGuid | Sort-Object -Property End -Descending | Select-Object -First 1
}
$Verified = $false
if ($Verify) {
$verifiedresult = [PSCustomObject]@{
SqlInstance = $server.name
ComputerName = $server.ComputerName
InstanceName = $server.ServiceName
SqlInstance = $server.DomainInstanceName
DatabaseName = $dbname
BackupComplete = $BackupComplete
BackupFilesCount = $FinalBackupPath.Count
Expand Down