简体   繁体   English

备份远程SQL Server数据库

[英]Backup a remote SQL Server database

I want to backup my remote SQL Server database onto the local computer. 我想将远程SQL Server数据库备份到本地计算机上。

So far I've tried: 到目前为止,我已经尝试过:

using (SqlConnection defaultSqlConnection = new SqlConnection(Constants.Database_Constants.GetConnectionStringFromProfile(Constants.Profiles.Staging)))
{
    string pathDatabase = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\backup_production_database"+DateTime.UtcNow.ToString("d_MMM_yyyy_HH_mm_ss", CultureInfo.InvariantCulture)+".bak";
    WriteLine("The backup will be stored in " + pathDatabase);

    string backupDb = "BACKUP DATABASE DB_Staging TO DISK = '" + pathDatabase+ "' WITH INIT, COMPRESSION";

    File.Create(pathDatabase);

    using (SqlCommand backupCommand = new SqlCommand(backupDb, defaultSqlConnection))
    {
        defaultSqlConnection.Open();
        backupCommand.ExecuteNonQuery();
    }
}

But I get this error : 但是我得到这个错误:

Cannot open backup device 'XXXX\\bin\\Debug\\backup_production_database22_Mar_2017_08_04_42.bak'. 无法打开备份设备'XXXX \\ bin \\ Debug \\ backup_production_database22_Mar_2017_08_04_42.bak'。 Operating system error 3(The system cannot find the path specified.). 操作系统错误3(系统找不到指定的路径。)。 BACKUP DATABASE is terminating abnormally. BACKUP DATABASE异常终止。

I know that I could generate a script using SQL Server Management Studio but it doesn't help me. 我知道我可以使用SQL Server Management Studio生成脚本,但这对我没有帮助。 I want to backup my database in a powershell script automatically without having to go manually into SQL Server Management Studio 我想自动以Powershell脚本备份数据库,而不必手动进入SQL Server Management Studio

You cannot create a directly backup from a remote server to a local disk. 您不能从远程服务器直接创建备份到本地磁盘。

Please look this link . 请查看此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM