Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# SMO backup of remote database to local machine

Tags:

c#

sql

smo

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;
    }
}
like image 811
Andy March Avatar asked Feb 17 '10 16:02

Andy March


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.


3 Answers

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).

like image 183
marc_s Avatar answered Oct 23 '22 15:10

marc_s


As Marc_s said, It can't be made. As a workarround you make that your database call a command line program in the host, that send the file by ftp, copy it to some shared folder, or something else.

Good Luck

like image 20
Jonathan Avatar answered Oct 23 '22 15:10

Jonathan


You can copy remote database to local. To copy use this: Right click on remote database in SSMS: Tasks -> Export data. In opened window choose source and destination database, it will be copy all data from source (remote) to your local database.

like image 1
Dmitriy Avatar answered Oct 23 '22 17:10

Dmitriy