Piecemeal Restore: Backup Set Holds a Backup of a Database Other than the Existing Database (Error)
When attempting to do a piecemeal restore of a database, using the ‘REPLACE’ option to replace the existing database, you may see the following error message:
Msg 3154, Level 16, State 4, Line 63 The backup set holds a backup of a database other than the existing '<database name>' database. Msg 3013, Level 16, State 1, Line 63 RESTORE DATABASE is terminating abnormally.
This error indicates that you cannot do a piecemeal restore of one database onto an existing copy of a different database. You must drop the existing database before you can do the piecemeal restore. As always, code illustrates the problem quite well.
The first piece of code creates two databases, fgRestoreTest_src
and fgRestoreTest_dest
. Each database consists of the primary filegroup, plus two custom filegroups, fg1
and fg2
. This allows us to do a piecemeal restore of a single filegroup.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
USE master; IF DB_ID(N'fgRestoreTest_src') IS NOT NULL BEGIN DROP DATABASE fgRestoreTest_src; END CREATE DATABASE fgRestoreTest_src ON (NAME = N'PRIMARY', FILENAME = N'G:\Database\Data\fgRestoreTest_src_primary.mdf') LOG ON (NAME = N'LOG', FILENAME = N'F:\Database\Data\fgRestoreTest_src.ldf'); GO ALTER DATABASE fgRestoreTest_src SET RECOVERY FULL; BACKUP DATABASE fgRestoreTest_src TO DISK = N'NUL:'; ALTER DATABASE fgRestoreTest_src ADD FILEGROUP fg1; ALTER DATABASE fgRestoreTest_src ADD FILE ( NAME = N'fg1_file1' , FILENAME = N'G:\DATABASE\Data\fgRestoreTest_src_fg1_file1.mdf' ) TO FILEGROUP fg1; ALTER DATABASE fgRestoreTest_src ADD FILEGROUP fg2; ALTER DATABASE fgRestoreTest_src ADD FILE ( NAME = N'fg2_file1' , FILENAME = N'G:\DATABASE\Data\fgRestoreTest_src_fg2_file1.mdf' ) TO FILEGROUP fg2; GO IF DB_ID(N'fgRestoreTest_dest') IS NOT NULL BEGIN DROP DATABASE fgRestoreTest_dest; END CREATE DATABASE fgRestoreTest_dest ON (NAME = N'PRIMARY', FILENAME = N'G:\Database\Data\fgRestoreTest_dest_primary.mdf') LOG ON (NAME = N'LOG', FILENAME = N'F:\Database\Data\fgRestoreTest_dest.ldf'); GO ALTER DATABASE fgRestoreTest_dest SET RECOVERY FULL; BACKUP DATABASE fgRestoreTest_dest TO DISK = N'NUL:'; ALTER DATABASE fgRestoreTest_dest ADD FILEGROUP fg1; ALTER DATABASE fgRestoreTest_dest ADD FILE ( NAME = N'fg1_file1' , FILENAME = N'G:\DATABASE\Data\fgRestoreTest_dest_fg1_file1.mdf' ) TO FILEGROUP fg1; ALTER DATABASE fgRestoreTest_dest ADD FILEGROUP fg2; ALTER DATABASE fgRestoreTest_dest ADD FILE ( NAME = N'fg2_file1' , FILENAME = N'G:\DATABASE\Data\fgRestoreTest_dest_fg2_file1.mdf' ) TO FILEGROUP fg2; GO |
This next piece of code is not really necessary, but it creates two tables in the fgRestoreTest_src
database, one on each filegroup. These tables allow us to prove which database is which because only the fgRestoreTest_src
database contains these tables. The fgRestoreTest_dest
database has no tables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
USE fgRestoreTest_src; CREATE TABLE dbo.RestoreTestTable_fg1 ( RestoreTestTable_ID int NOT NULL CONSTRAINT RestoreTestTable_fg1_pk PRIMARY KEY CLUSTERED IDENTITY(1,1) ) ON fg1; INSERT INTO dbo.RestoreTestTable_fg1 DEFAULT VALUES; CREATE TABLE dbo.RestoreTestTable_fg2 ( RestoreTestTable_ID int NOT NULL CONSTRAINT RestoreTestTable_fg2_pk PRIMARY KEY CLUSTERED IDENTITY(1,1) ) ON fg2; INSERT INTO dbo.RestoreTestTable_fg2 DEFAULT VALUES; GO |
Next, we’ll backup the fgRestoreTest_src
database, and attempt to restore it over top of the fgRestoreTest_dest
database using piecemeal restore1.
The backup:
1 2 3 4 |
USE master; GO BACKUP DATABASE fgRestoreTest_src TO DISK = 'F:\Database\Backups\fgRestoreTest_src.bak' WITH INIT; GO |
The restore:
1 2 3 4 5 6 7 8 9 10 |
RESTORE DATABASE fgRestoreTest_dest FILEGROUP = 'fg1' , FILEGROUP = 'PRIMARY' FROM DISK = 'F:\Database\Backups\fgRestoreTest_src.bak' WITH MOVE 'fg1_file1' TO 'G:\Database\Data\fgRestoreTest_dest_fg1_file1.mdf' , MOVE 'PRIMARY' TO 'G:\Database\Data\fgRestoreTest_dest_primary.mdf' , MOVE 'LOG' TO 'F:\Database\Data\fgRestoreTest_dest.ldf' , RECOVERY , REPLACE; GO |
The restore results in the following error message:
Msg 3154, Level 16, State 4, Line 63 The backup set holds a backup of a database other than the existing 'fgRestoreTest_dest' database. Msg 3013, Level 16, State 1, Line 63 RESTORE DATABASE is terminating abnormally.
If we drop the target database, fgRestoreTest_dest
, first, then attempt the restore, it succeeds. So, let’s drop the fgRestoreTest_dest
database:
1 2 3 |
USE master; DROP DATABASE fgRestoreTest_dest; GO |
Now, we’ll do the restore, without even attempting to use the REPLACE
option:
1 2 3 4 5 6 7 8 9 |
RESTORE DATABASE fgRestoreTest_dest FILEGROUP = 'fg1' , FILEGROUP = 'PRIMARY' FROM DISK = 'F:\Database\Backups\fgRestoreTest_src.bak' WITH MOVE 'fg1_file1' TO 'G:\Database\Data\fgRestoreTest_dest_fg1_file1.mdf' , MOVE 'PRIMARY' TO 'G:\Database\Data\fgRestoreTest_dest_primary.mdf' , MOVE 'LOG' TO 'F:\Database\Data\fgRestoreTest_dest.ldf' , RECOVERY; GO |
As you can see, the restore succeeded.
I hope this post helps us! If you have any questions, please don’t hesitate to leave a comment below, or send us a tweet.
This post is part of our series on Database Recovery.
Here’s a great painting by Vincent Van Gogh of a wheat field. Because, wheat field.
1 – https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/piecemeal-restores-sql-server