Sunday, March 25, 2012

Backing up database file groups with SMO

Hi,

I am trying to backup file groups with Backup class:

Server svr = new Server();
Backup bkp = new Backup();
bkp.Database = "AdventureWorks";
bkp.Action = BackupActionType.Files;
bkp.DatabaseFileGroups.Add("WorkOrderGroup");
bkp.DeviceType = DeviceType.File;
bkp.Devices.Add(@."c:\SMOTest_FG.bak");
bkp.SqlBackup(svr);

Unfortunately, BackupRestoreBase.DatabaseFileGroups property does not have set method (i.e. it is read-only). How can I perform a database file group backup?

Thanks in advance,

Mark

Hi Mark,

BackupRestoreBase.DatabaseFileGroups does not have a set method, but you can add DatabaseFileGroups using the Add method and that seems to work. Here is a sample application you can use (I am creating a new database, you can use an existing one) -

Server svr = new Server(".");
Database db = new Database(svr, "sampleDB");
db.Create();
FileGroup fg = new FileGroup(db, "fg1");
fg.Create();
Backup bkp = new Backup();
bkp.Database = db.Name;
bkp.Action = BackupActionType.Files;
bkp.DatabaseFileGroups.Add(db.FileGroups[0].Name);
bkp.DatabaseFileGroups.Add(db.FileGroups[1].Name);
bkp.Devices.Add(new BackupDeviceItem(@."c:\testbackup\sampleDb.bak", DeviceType.File));
bkp.SqlBackup(svr);

Thanks,
Kuntal

|||

Thanks! It worked.

In C++, I had to do the following:

Backup ^ bkpH = gcnew Backup();

bkpH->Database = gcnew String(dbName);

bkpH->Action = BackupActionType::Files;

StringCollection^ strCollH = bkpH->DatabaseFileGroups;

strCollH->Add(gcnew String(groupName));

...

No comments:

Post a Comment