C# SQL 无需直接访问远程位置即可将远程数据库服务器备份到远程默认备份位置?

C# SQL Server backup of a remote database to the remote default backup location without direct access to the remote location?

TL;DR - 我希望服务器备份而不是我的应用程序,因为服务器设置为这样做,而我的应用程序无权访问。

背景

我公司在 20 年前用 Delphi 7/Pascal 为客户开发了软件。我正在用 C# 重写软件。作为重写的一部分,我创建了新的 Firebird、Oracle 和 SQL Server 数据库。联邦法规要求维护所有现有数据,因此我创建了一个数据库修改/转换工具,以便从旧数据库结构更改为新数据库结构。

在开始进行更改之前,我需要备份现有数据库。 运行 使用此工具的技术人员无法访问其本地文件结构,也无法手动访问数据库所在的远程服务器。该工具访问本地系统上一个加密的 .ini-like 文件来解析连接字符串的组成部分并创建一个连接对象。然后,我使用该连接对象连接到技术人员计算机设置连接到的同一个数据库。 这部分全部有效

如果我单独保留默认备份路径,它会尝试备份到默认路径,但在本地计算机上(技术人员无权创建,我们也不希望技术人员有权访问 .bak 文件) ) 如果我将默认备份路径修改为从连接字符串中获取的网络路径,我会得到

SmoException: System.Data.SqlClient.SqlError: Cannot open backup device Operating system error 67(The network name cannot be found.).

因为文件路径不是网络共享(将来也不会)并且数据库用户凭据无法从 SQL 服务器外部访问该路径。

所以问题是:如何将备份带到远程默认路径,就好像我在服务器上一样?

这是生成上述错误的代码(它是远程的 null 情况)。

public static void FullSqlBackup (Connection oldProactiveSql)
{
           String sqlServerLogin = oldProactiveSql.UserName;
           String password = oldProactiveSql.PassWord;
           String instanceName = oldProactiveSql.InstanceName;
           String remoteSvrName = oldProactiveSql.Ip + "," + oldProactiveSql.Port;

           Server srv2;
           Server srv3;
           string device;

           switch (oldProactiveSql.InstanceName)
           {
                case null:
                     ServerConnection srvConn2 = new ServerConnection(remoteSvrName);
                     srvConn2.LoginSecure = false;
                     srvConn2.Login = sqlServerLogin;
                     srvConn2.Password = password;
                     srv3 = new Server(srvConn2);
                     srv2 = null;
                     Console.WriteLine(srv3.Information.Version);

                     if (srv3.Settings.DefaultFile is null)
                     {
                          device = srv3.Information.RootDirectory + "\DATA\";
                          device = device.Substring(2);
                          device = oldProactiveSql.Ip + device;
                     }
                     else device = srv3.Settings.DefaultFile;
                     device = device.Substring(2);
                     device = string.Concat("\\", oldProactiveSql.Ip, device);
                     break;

                default:
                     ServerConnection srvConn = new ServerConnection();
                     srvConn.ServerInstance = @".\" + instanceName;
                     srvConn.LoginSecure = false;
                     srvConn.Login = sqlServerLogin;
                     srvConn.Password = password;
                     srv2 = new Server(srvConn);
                     srv3 = null;
                     Console.WriteLine(srv2.Information.Version);

                     if (srv2.Settings.DefaultFile is null)
                     {
                          device = srv2.Information.RootDirectory + "\DATA\";
                     }
                     else device = srv2.Settings.DefaultFile;
                     break;
           }

           Backup bkpDbFull = new Backup();
           bkpDbFull.Action = BackupActionType.Database;
           bkpDbFull.Database = oldProactiveSql.DbName;
           bkpDbFull.Devices.AddDevice(device, DeviceType.File);
           bkpDbFull.BackupSetName = oldProactiveSql.DbName + " database Backup";
           bkpDbFull.BackupSetDescription = oldProactiveSql.DbName + " database - Full Backup";
           bkpDbFull.Initialize = true;
           bkpDbFull.PercentComplete += CompletionStatusInPercent;
           bkpDbFull.Complete += Backup_Completed;

           switch (oldProactiveSql.InstanceName)
           {
                case null:
                     try 
                     {
                         bkpDbFull.SqlBackup(srv3); 
                     }
                     catch (Exception e)
                     {
                          Console.WriteLine (e);
                          Console.WriteLine(e.InnerException.Message);
                          throw;
                     }
                     break;

                default:
                     try 
                     { 
                         bkpDbFull.SqlBackup(srv2); 
                     }
                     catch (Exception e)
                     {
                          Console.WriteLine(e);
                          Console.WriteLine(e.InnerException.Message);
                          throw;
                     }
                     break;
           }
      }

任何帮助将不胜感激,因为我现在只是 运行在兜圈子。

根据下面的评论,我会尝试 - 1. 在数据库上动态创建存储过程 [BackupToDefault] 然后 运行 它。 2. 如果失败 link 数据库自身。 3. 尝试 - 在 [LinkedSelfSynonmym]

处执行 [BackupToDefault]

祝我好运,尽管它看起来很复杂,而且还有很长的路要走,但我希望它能奏效。

灵感...备份分为 3 个文件,每个文件驻留在不同的目录中(sql 实例备份目录和 sql 实例默认目录和数据库主目录)

//// compile with:   
// /r:Microsoft.SqlServer.Smo.dll  
// /r:Microsoft.SqlServer.SmoExtended.dll 
// /r:Microsoft.SqlServer.ConnectionInfo.dll  

using System;
using System.Data;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

namespace SMObackup
{
    class Program
    {
        static void Main()
        {

            // For remote connection, remote server name / ServerInstance needs to be specified  
            ServerConnection srvConn2 = new ServerConnection("machinename"/* <--default sql instance on machinename*/);  // or (@"machinename\sqlinstance") for named instances
            srvConn2.LoginSecure = false;
            srvConn2.Login = "smologin";
            srvConn2.Password = "SmoL@gin11";
            srvConn2.DatabaseName = "msdb";
            Server srv3 = new Server(srvConn2);

            //server info
            Console.WriteLine("servername:{0} ---- version:{1}", srv3.Name, srv3.Information.Version);

            //server root directory
            string serverRootDir = srv3.Information.RootDirectory;
            //server backup directory
            string serverBackupDir = srv3.Settings.BackupDirectory;
            //database primary directory
            string databasePrimaryFilepath = srv3.Databases[srvConn2.DatabaseName].PrimaryFilePath;

            Console.WriteLine("server_root_dir:{0}\nserver_backup_dir:{1}\ndatabase_primary_dir{2}", serverRootDir, serverBackupDir, databasePrimaryFilepath);

            Backup bkpDbFull = new Backup();
            bkpDbFull.Action = BackupActionType.Database;
            //comment out copyonly ....
            bkpDbFull.CopyOnly = true; //copy only, just for testing....avoid messing up with existing backup processes
            bkpDbFull.Database = srvConn2.DatabaseName;

            //backup file name
            string backupfile = $"\backuptest_{DateTime.Now.ToString("dd/MM/yyyy/hh/mm/ss")}.bak";

            //add multiple files, in each location
            bkpDbFull.Devices.AddDevice(serverRootDir + backupfile, DeviceType.File);
            bkpDbFull.Devices.AddDevice(serverBackupDir + backupfile, DeviceType.File);
            bkpDbFull.Devices.AddDevice(databasePrimaryFilepath + backupfile, DeviceType.File);
            bkpDbFull.Initialize = true;

            foreach (BackupDeviceItem backupdevice in bkpDbFull.Devices)
            {
                Console.WriteLine("deviceitem:{0}", backupdevice.Name);
            }

            //backup is split/divided amongst the 3 devices
            bkpDbFull.SqlBackup(srv3);

            Restore restore = new Restore();
            restore.Devices.AddRange(bkpDbFull.Devices);
            DataTable backupHeader = restore.ReadBackupHeader(srv3);


            //IsCopyOnly=True
            for (int r = 0; r < backupHeader.Rows.Count; r++)
            {
                for (int c = 0; c < backupHeader.Columns.Count; c++)
                {
                    Console.Write("{0}={1}\n", backupHeader.Columns[c].ColumnName, (string.IsNullOrEmpty(backupHeader.Rows[r].ItemArray[c].ToString())? "**": backupHeader.Rows[r].ItemArray[c].ToString()) );
                }
            }

            srvConn2.Disconnect(); //redundant

        }
    }
}

谢谢@SeanLange

Pretty sure you would need to use dynamic sql in this case. Then the path will relative to where the sql is executed.

我更改了我的代码以添加:

 private static void WriteBackupSp (Server remoteServer, string dbName, out string storedProcedure)
      {
           var s = "CREATE PROCEDURE [dbo].[ProactiveDBBackup]\n";
           s += "AS\n";
           s += "BEGIN\n";
           s += "SET NOCOUNT ON\n";
           s += "BACKUP DATABASE " + dbName + " TO DISK = \'" + string.Concat (remoteServer.BackupDirectory,@"\", dbName, ".bak") + "\'\n";
           s += "END\n";
           storedProcedure = s;
      }

然后将最后一个开关更改为:

switch (oldProactiveSql.InstanceName)
           {
                case null:
                     try
                     {
                          WriteBackupSp (srv3, oldProactiveSql.DbName, out var storedProcedure);
                          ConnectionToolsUtility.GenerateSqlConnectionString (oldProactiveSql, out var cs);

                          using (SqlConnection connection = new SqlConnection (cs))
                          {
                               using (SqlCommand command = new SqlCommand (storedProcedure, connection))
                               {
                                    connection.Open ();
                                    command.ExecuteNonQuery ();
                                    connection.Close ();
                               }
                          }
                          var execBackup = "EXEC [dbo].[ProactiveDBBackup]\n";
                          using (SqlConnection connection = new SqlConnection (cs))
                          {
                               using (SqlCommand command = new SqlCommand (execBackup, connection))
                               {
                                    connection.Open ();
                                    command.ExecuteNonQuery ();
                                    connection.Close ();
                               }
                          }
                     }
                     catch (Exception e)
                     {
                          Console.WriteLine(e);
                          Console.WriteLine(e.InnerException.Message);
                          throw;
                     }
                     break;
                default:
                     try { bkpDbFull.SqlBackup(srv2); }
                     catch (Exception e)
                     {
                          Console.WriteLine(e);
                          Console.WriteLine(e.InnerException.Message);
                          throw;
                     }
                     break;
           }

这让我可以通过连接字符串将数据库备份到默认备份位置,而无需具有访问网络路径位置的凭据。