Restore filegroup to different database - sql-server

I have a SQL server database with 5 filegroups i want to backup 2 filegroups(one contains filestream)only and restore them to different existing database.

What you are asking about is Piecemeal Restores (SQL Server)
You can restore primary + all readwrite fg + any of readonly filegroups in you are in simple recovery model or you can restore primary + any other filegroup(s) if you are in full recovery model.
As the first filegroup to restore is always primary filegroup, it will replace your "different existing database" primary data file and log (in your restore command you should use with move that points to your existing mdf + log files) and every subsequent restore will overwrite other files, there will remain nothing from your "existing" database, so there is no sense to restore to a "different existing database": you will be able to bring online only the filegroups you are restored and the database will know nothing about remaining files of "existing database". So it's just the same as to restore to just another(non existing) database.
Here is the restore sequence example for primary + readonly fg in simple recovery model Example: Piecemeal Restore of Only Some Filegroups (Simple Recovery Model)
And here is the example for full recovery model Example: Piecemeal Restore of Only Some Filegroups (Full Recovery Model)

Related

SQL Server 2019 - Take backup of a single table from a secondary filegroup using SSMS [migrated]

This question was migrated from Stack Overflow because it can be answered on Database Administrators Stack Exchange.
Migrated 10 days ago.
I am working a database with a size of 325 GB total. Out of this a single table Documents is occupying 321 GB storage and the rest of the 84 table occupy the remaining space. As the database is growing rapidly, taking backups is consuming a lot of space. In most of the scenarios Documents table is not required to be backed up.
So we created a new filegroup and moved the Clustered Index with primary key of the Documents Table only to the secondary filegroup. Despite this when we take the PRIMARY filegroup backup with Backup Type set as FULL, we are also getting all the SECONDARY filegroup records.
How can we take backups of the PRIMARY filegroup without including the Document table?
Also I noticed that despite creating the new index in SECONDARY filegroup, it's file size is still the same when it was created, and it is not reflecting the size of the Documents table.
The backup command to backup single filegroup is this one:
--Back up the files in Myfilegroup1.
BACKUP DATABASE MyDB
FILEGROUP = 'Myfilegroup1'
TO DISK = 'C:\MySQLServer\Backups\MyDB\Myfilegroup1.bck'
Add this for differential backup:
WITH
DIFFERENTIAL;
GO
To have a good segregation of the contents be sure to have each table (clustered index or heap) in the same Filegroup of the secondaries indexes.
Also consider that the restore in quite complicated as you have to:
After you restore the filegroup, you need to take a tail log backup of the
restored database.
Restore the lastest differential you may have to
with norecovery.
Restore all the tlog backups to with norecovery.
Restore the tail log backup from step 1 to with recovery.

SQL Server--piecemeal restore of filegroups from complete backup in Simple Recovery Mode

We have a large database in MS SQL in which one of the tables is partitioned by a date column. The Primary key index is also partitioned using the same partition function. The database is kept in Simple Recovery model, since data is added to it in batches every 3 months.
DBCC checkfilegroup found consistency errors, so we needed to bring back just one filegroup from a complete backup.
Restore did not allow me to run a restore of a filegroup in Simple Mode, so I changed to full recovery mode, then ran the following, with no errors.
restore database aricases filegroup='2003'
from disk=N'backupfile-name.bak'
with recovery
I expected the "with recovery" clause to bring this back to working order, but the process ended with a note saying
The roll forward start point is now at log sequence number (LSN) 511972000001350200037. Additional roll forward past LSN 549061000001370900001 is required to complete the restore sequence.
When I query the database table that includes this filegroup I get a message saying that the primary key cannot be accessed because one of the partitions for the table cannot be access because it is offline, restoring, or defunct.
Why didn't "with recovery" clause leave this filegroup fully restored. Now what?
The entire database is very large (1.5TB). I can't backup the log file, because I'd first need to create a backup in full model mode. The filegroup itself is only 300gb.
I can do the restore again-- but would like to know the correct way of performing this.
Is there a way of staying in complete recovery mode and performing a piecemeal filegroup backup from a complete database backup?
I found the answer. Bottom line is that Simple Recovery Model is very limited. You must restore ALL read/write filegroups together from the same backup. Individual read/only filegroups CAN be restored separately, as long as they became read/only (no more changes) BEFORE the last backup of the read/write filegroups.
Bottom line-- only Full or Bulk-Logged models let you restore single read/write filegroups.
Bulk-Logged model is what a datawarehouse with batch loading should be using, not Simple Model. My error in design.
see from Microsoft
http://msdn.microsoft.com/en-us/library/ms191253.aspx
then look at piecemeal restores for Simple Model
http://msdn.microsoft.com/en-us/library/ms190984%28v=sql.100%29.aspx
very limited

Restore DB in Full recovery model when there is no backup

Suppose I want to restore a database in such circumstances: from one side there is no backup,
from the other side - all the changes are stored in the log file.
Can I restore the database from the log file to a specific date & time?
You are talking about point in time recovery
for that you first need a full backup at least then if you take t-log backup you can restore. without full backup nothing possible

SQL Server Partial Database Backup (excluding some tables)

I'm managing a reasonably large SQL Server database. Some tables contain data that are business-critical and must be backed up offsite daily. But we also have other (read-write) tables that take up about half the size of the database that aren't business-critical. What I would like to do is something like this:
Primary filegroup: Tables A, B, C --> daily backup
Secondary filegroup: Tables D, E, F --> monthly (or occasional manual) backup
When I tried to test this, I got errors while trying to restore the filegroups. It looks like I can't restore a single filegroup alone or different file groups from different points in time. Ideally, I'd like to be able to just restore the primary filegroup (the most important one) first, and then restore the secondary one. I'm willing to accept some data loss on the secondary filegroup.
Can this be done?
In order to succeed with a partial or piecemeal restore strategy, you first need to adopt a Filegroup backup strategy. You can still backup your whole database at one time if you wish, but the backup needs to be at the filegroup level.
Details of how to perform filegroup backups can be found at the following link: http://msdn.microsoft.com/en-us/library/ms179401(v=sql.105).aspx
Details of how to perform a piecemeal restore can be found here http://msdn.microsoft.com/en-us/library/ms177425(v=sql.100).aspx

Is it possible to copy a filegroup from one database to another?

I want to extract a subset of a database and copy to another server/database. Is it possible to copy/backup a single filegroup from one database and attach/restore to another?
You cannot restore a filegroup into a different database. Any restore operation must go through the recovery phase when the log is replayed against the restored data files and a new database would have a log that does not match the restored filgroup, so the engine will reject the backup.
You cannot detach/attach filegroups either, detach and attach are not working at filegroup level, only at database level.
Nor can you cannot do a file copy/overwrite, when the database comes online the recovery process will detect the mismatch and declare the database corrupt.
Use the faithful "SELECT * FROM old_db.old_tbl INTO new_db.new_tbl" for each table.

Resources