问题
I'm trying to make a little app that would help me making my server backup. That app would run on my home PC so the main goal is to be able to connect to the external server, backup the selected database, dump the backup content to a string or something so I could write it on my PC disk and not the server's disk.
I did that which works to write on the server's disk, but I'd like to be able to write on my PC's disk the backup's result.
private bool BackupDatabase()
{
try
{
// Filename
string sFileName = string.Format("{0}\\{1}.bak", _sWhereToBackup, DatabaseName);
// Connection
string sConnectionString = String.Format(
"Data Source=tcp:{0};Initial Catalog={1};User ID={2};Pwd={3};",
DatabaseHost, DatabaseName, DatabaseUsername, DatabasePassword);
SqlConnection oSqlConnection = new SqlConnection(sConnectionString);
Server oServer = new Server(new ServerConnection(oSqlConnection));
// Backup
Backup backup = new Backup();
backup.Action = BackupActionType.Database;
backup.Database = DatabaseName;
backup.Incremental = false;
backup.Initialize = true;
backup.LogTruncation = BackupTruncateLogType.Truncate;
// Backup Device
BackupDeviceItem backupItemDevice = new BackupDeviceItem(sFileName, DeviceType.File);
backup.Devices.Add(backupItemDevice);
// Start Backup
backup.SqlBackup(oServer);
}
catch (Exception ex)
{
throw ex;
}
return false;
}
Thanks so much!
回答1:
This is going to get a bit hacky because you need to either
- call sql functions to read the file on the server and return as a binary array to you and then convert back to a file. this will require proper permissions as the account you are running under to access the file on the server's drive.
you can use t-sql or a bit more 'advanced' .net code t-sql can be seen in this great sql injection guide http://www.blackhat.com/presentations/bh-europe-09/Guimaraes/Blackhat-europe-09-Damele-SQLInjection-slides.pdf
or .net: http://www.mssqltips.com/sqlservertip/2349/read-and-write-binary-files-with-the-sql-server-clr/
map a file location (i.e. access a share/drive/ipc connection) over the network
have the server ftp the files to a location
Which sounds likely in your scenario?
回答2:
I take it that you're not networked to the server? Ideally you could back it up directly to your machine using a network address but I'm guessing you would have already thought of that.
Going the route you're thinking of is going to require you to have permissions that you would not normally want to have opened up to the someone over the internet, and will be much more trouble to program than simply setting up a second process to move the file somewhere accessible to you.
Adams suggestion to have the server ftp the files is a good one. Alternatively, you may find that using DropBox (www.dropbox.com) might be the path of least resistance. Then you could just back the database up to a folder setup with dropbox and it will automatically be copied to any folder you setup on your machine to be synchronized with it.
回答3:
You should look into DAC, it is mainly made for the SQL Server in Azure, but will work with a SQL Server 2008 R2 too.
http://sqldacexamples.codeplex.com/
来源:https://stackoverflow.com/questions/8199972/using-smo-backup-to-backup-sql-server-database-to-string