12

I have an application which performs backups and restores of SQL databases, this works fine on the local machine, however if I run this against a SQL server hosted on another machine I get the following error

Microsoft.SqlServer.Management.Smo.FailedOperationException: Backup failed for Server '25.98.30.79'.
Microsoft.SqlServer.Management.Common.ExecutionFailureException: An exception occurred while executing a Transact-SQL statement or batch.
System.Data.SqlClient.SqlException: Cannot open backup device 'C:\Program Files\State Manager\Archive\Capture\20100217152147\*product*\databases\*database*\*database*.bak'. Operating system error 3(The system cannot find the path specified.).

This appears to be being caused by the SQL server attempting to write this file to its local drive. I cannot setup a shared area into which the backup can be placed due to security restrictions.

Does anyone know how I can move this data back to the machine the code is being called from?

My code is below.

private string Name;
private string Server;
private string dbName;
private string user;
private string password;

public Boolean performCapture(String archiveDir)
{
    String destination = archiveDir + "\\" + Name;
    if (!System.IO.Directory.Exists(destination))
    {
        System.IO.Directory.CreateDirectory(destination);
    }

    Server sqlServer = connect();
    if (sqlServer != null)
    {
        DatabaseCollection dbc = sqlServer.Databases;
        if (dbc.Contains(dbName))
        {
            Backup bkpDatabase = new Backup();
            bkpDatabase.Action = BackupActionType.Database;
            bkpDatabase.Database = dbName;
            BackupDeviceItem bkpDevice = new BackupDeviceItem(destination + "\\" + dbName + ".bak", DeviceType.File);

            bkpDatabase.Devices.Add(bkpDevice);
            bkpDatabase.Incremental = false;
            bkpDatabase.Initialize = true;
            // Perform the backup
            bkpDatabase.SqlBackup(sqlServer);

            if (System.IO.File.Exists(destination + "\\" + dbName + ".bak"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}
4

4 回答 4

18

No, this won't ever work - SQL Server can only ever back up to a drive physically attached to the actual SQL Server machine. You cannot under any circumstances back up a remote SQL Server to your local harddisk - just not possible (neither in SMO, or in SQL Server Management Studio).

于 2010-02-17T16:35:58.520 回答
1

您可以将远程数据库复制到本地。要复制使用:右键单击 SSMS 中的远程数据库:任务 -> 导出数据。在打开的窗口中选择源和目标数据库,它将所有数据从源(远程)复制到本地数据库。

于 2012-11-17T16:54:46.377 回答
1

正如 Marc_s 所说,它无法制造。作为一种解决方法,您可以让您的数据库调用主机中的命令行程序,该程序通过 ftp 发送文件,将其复制到某个共享文件夹或其他东西。

祝你好运

于 2010-02-17T16:53:32.020 回答
0

我知道这是一篇旧帖子,但是是的,您可以备份远程 sqlserver 的数据库

您只需在远程计算机和本地计算机中创建相同的文件夹名称和驱动器

例子:

d:\sql_backup 在本地计算机上 d:\sql_backup 在远程计算机上

它会做到的......当然备份文件将在远程计算机上

如果要获取备份文件,只需共享远程计算机的文件夹

萨鲁多斯·鲁本克

于 2010-09-26T00:59:37.917 回答