views:

139

answers:

2

My question is straight forward, how can I program SQL SMO to take local backup, by connecting to a remote server. I want to save the bak file to a local machine which is connecting to the remote server. Also I want users with only certain privilege to be able to save backups locally.

+3  A: 

You cannot - period. Backups of SQL Server can only be saved to a local disk - local to the SQL Server itself.

You cannot with any tricks or tools backup a remote SQL Server to your local harddisk. Just can't do it.

marc_s
+1  A: 

EDIT: re-reading your question and marc_s answer my answer will only work if by remote server you're talking about a server on your network somewhere. If you're talking about a hosted SQL Server on another domain somewhere marc_s is right and my answer is of no use. I'll leave it here anyway in case you are talking about a server in your domain. Edit Ends

After setting a share on my local C:\tmp directory running this bit of Powershell does the backup.


[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')  | out-null
# may need this instead [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SmoExtended') | out-null

$datePart = Get-Date -Format "yyyyMMdd_hhmm" 
$targetDir = '\\LocalMachineName\tmp\'  # change to fit your specs
$dbname = "DatabaseNameToBackUp"

$s = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'remoteSqlServer'
$bckfile = $targetDir + $dbname + "_" + $datePart + ".bak"
$dbbk = new-object ('Microsoft.SqlServer.Management.Smo.Backup')
$bdi = new-object ('Microsoft.SqlServer.Management.Smo.BackupDeviceItem') ($bckfile, 'File')
$dbbk.Action = 'Database'
$dbbk.BackupSetDescription = "Full backup of " + $dbname
$dbbk.BackupSetName = $dbname + " Backup"
$dbbk.Database = $dbname
$dbbk.MediaDescription = "Disk"
$dbbk.Devices.Add($bdi)
$dbbk.SqlBackup($s)
$dbbk.Devices.Remove($bdi) |out-null
$bckfile = $null

I know you didn't mention Powershell but since I saw the .net tag I thought it might still be some help. Shouldn't be too much effort to rewrite to your .net flavor of choice.

Bruce