开发者

SQL Server SMO take back up locally from remote server

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 开发者_C百科save backups locally.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜