SQL Server SMO take back up locally from remote server

纵然是瞬间 提交于 2019-12-10 14:56:33

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3334238/sql-server-smo-take-back-up-locally-from-remote-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!