Copy-DbaDatabase -SetSourceOffline sets source offline even if the copy wasn't successfull.
Verified issue does not already exist?
I have searched and found no existing issue
What error did you receive?
Using Cop-DBaDatabase to Move a database to another server using the "-SetSourceOffline" option, it will still set the source offline even if the copy DIDN'T Go through. You can see it even tries to continue the process in the script without noticing the database isn't there.
Also I think this should throw an error instead of a warning so it can be picked up by a try/catch.
WARNING: [08:33:17][Copy-DbaDatabase] Failure attempting to restore <MYDATABASE> to <MYSERVER> | Microsoft.Data.SqlClient.SqlError: There is insufficient free space on disk volume 'E:' to create the database. The database requires 677288869888 additional free bytes, while only 515491758080 bytes are available. WARNING: [08:33:19][Copy-DbaDatabase] Failure setting <MYDATABASE> to read-write on destination server | An exception occurred while executing a Transact-SQL statement or batch.User does not have permission to alter database <MYDATABASE>, the database does not exist, or the database is not in a state that allows access checks. ALTER DATABASE statement failed. WARNING: [08:33:19][Copy-DbaDatabase] Failed to update DatabaseOwnershipChaining for False on <MYDATABASE> on <MYSERVER>. WARNING: [08:33:19][Copy-DbaDatabase] Failed to update Trustworthy to False for <MYDATABASE> on <MYSERVER>. WARNING: [08:33:19][Copy-DbaDatabase] Failed to update BrokerEnabled to False for <MYDATABASE> on <MYSERVER>.
Steps to Reproduce
PrePReq: Target server must have lack of diskspace to contain the whole database.
Copy-DbaDatabase -Source <MYSOURCE> -Destination <MYDESTINATIN> -BackupRestore -SharedPath \
Please confirm that you are running the most recent version of dbatools
2.1.26
Other details or mentions
No response
What PowerShell host was used when producing this error
Windows PowerShell (powershell.exe)
PowerShell Host Version
Name Value
PSVersion 5.1.19041.5007
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.5007
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
SQL Server Edition and Build number
SQL2022
.NET Framework Version
PSChildName Version
Client 4.8.09037 Full 4.8.09037 Client 4.0.0.0
GitHub removes the servernames since It looked like HTML-tags. Here is the error message again
WARNING: [08:33:17][Copy-DbaDatabase] Failure attempting to restore MYDATABASE to MYSERVER | Microsoft.Data.SqlClient.SqlError: There is insufficient free space on disk volume 'E:' to create the database. The database requires 677288869888 additional free bytes, while only 515491758080 bytes are available. WARNING: [08:33:19][Copy-DbaDatabase] Failure setting MYDATABASE to read-write on destination server | An exception occurred while executing a Transact-SQL statement or batch.User does not have permission to alter database 'MYDATABASE', the database does not exist, or the database is not in a state that allows access checks. ALTER DATABASE statement failed. WARNING: [08:33:19][Copy-DbaDatabase] Failed to update DatabaseOwnershipChaining for False on MYDATABASE on MYSERVER. WARNING: [08:33:19][Copy-DbaDatabase] Failed to update Trustworthy to False for MYDATABASE on MYSERVER. WARNING: [08:33:19][Copy-DbaDatabase] Failed to update BrokerEnabled to False for MYDATABASE on MYSERVER.
STEPS TO REPRODUCE:
Copy-DbaDatabase -Source MYSOURCE -Destination MYDESTINATION -BackupRestore -SharedPath \MYSHARESERVER -SetSourceOffline -Database MYDATABASE
Thank you for the report @frodr1k. @claude please review, fix and create a PR.
Claude finished @potatoqualitee's task —— View job • claude/issue-9546-20251028-0038 • Create PR ➔
Issue #9546 - Fixed
Task: Fix Copy-DbaDatabase -SetSourceOffline to only set source offline when the copy succeeds
Changes Made
Modified /home/runner/work/dbatools/dbatools/public/Copy-DbaDatabase.ps1 (line 1452) to include a success check before setting the source database offline:
if ($SetSourceOffline -and $copyDatabaseStatus.Status -eq "Successful" -and $sourceServer.databases[$dbName].status -notlike '*offline*') {
Before: The source database would be set offline even if the copy/restore operation failed.
After: The source database is only set offline when $copyDatabaseStatus.Status equals "Successful", ensuring the database remains online if the operation fails.
Analysis
The bug occurred because the -SetSourceOffline logic didn't check whether the copy operation completed successfully. It only verified:
- The
-SetSourceOfflineparameter was specified - The database wasn't already offline
This meant that when a restore failed (e.g., due to insufficient disk space), the source database would still be taken offline, causing data unavailability.
The fix adds a check for $copyDatabaseStatus.Status -eq "Successful" to ensure the operation completed successfully before taking the source offline.
|