Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C# & SMO, how do I add a backup device to a SQL Server?

Tags:

c#

sql-server

smo

I know how to use C# and SMO to create a file backup on a SQL Server:

public void BackupDatabase(Microsoft.SqlServer.Management.Smo.Server server, 
                               string databaseName, string backupFilename,
                               string backupName, string backupDescription) {

 Backup backup = new Backup();
 backup.Devices.AddDevice(backupFilename, DeviceType.File);
 backup.Database = databaseName;
 backup.Action = BackupActionType.Database;
 backup.BackupSetDescription = backupDescription;
 backup.BackupSetName = backupName;
 backup.Incremental = false;
 backup.LogTruncation = BackupTruncateLogType.Truncate;
 backup.Initialize = true; // supposed to overwrite
 backup.SqlBackup(server);
}

What I don't know (and can't find) is how to add the file BackupDevice to the SQL server.

like image 387
C-Pound Guru Avatar asked Mar 18 '10 18:03

C-Pound Guru


1 Answers

Use BackupDevice.Create:

Server server = new Server("localhost");
BackupDevice newDevice = new BackupDevice();
newDevice.Parent = server;
newDevice.Name = "newDevice";
newDevice.BackupDeviceType = BackupDeviceType.Disk;
newDevice.PhysicalLocation = "c:\temp\newdevice.bak";
newDevice.Create();
like image 169
Remus Rusanu Avatar answered Oct 11 '22 02:10

Remus Rusanu