我们有一个天蓝色的存储容器,每天我们都在其中保存数据库备份文件。现在,我们正在开发使用SMO(C#)从Azure存储容器还原数据库的应用程序。从本地设备还原可以正常工作,但是在使用c#从azure容器中还原时,我没有得到任何帮助。
当我使用azure blob还原备份时,出现以下错误
无法打开备份设备 'https://myaccount.blob.core.windows.net/backupcontainer/Database_e95aec6ab14a43a3a14a62f82b2c7208_20180718022621-04.bak'。 操作系统错误3(系统找不到指定的路径。)。 RESTORE FILELIST异常终止
我的代码是
Restore restoreDB = new Restore();
restoreDB.Action = RestoreActionType.Database;
restoreDB.Database = databaseName;
BackupDeviceItem deviceItem = new BackupDeviceItem(backUpFile, DeviceType.Url, CredentialName);
restoreDB.Devices.Add(deviceItem);
restoreDB.ReplaceDatabase = true;
RelocateFile DataFile = new RelocateFile();
string MDF = restoreDB.ReadFileList(smoServer).Rows[0][1].ToString();
DataFile.LogicalFileName = restoreDB.ReadFileList(smoServer).Rows[0][0].ToString();
DataFile.PhysicalFileName = smoServer.Databases[databaseName].FileGroups[0].Files[0].FileName;
RelocateFile LogFile = new RelocateFile();
string LDF = restoreDB.ReadFileList(smoServer).Rows[1][1].ToString();
LogFile.LogicalFileName = restoreDB.ReadFileList(smoServer).Rows[1][0].ToString();
LogFile.PhysicalFileName = smoServer.Databases[databaseName].LogFiles[0].FileName;
restoreDB.RelocateFiles.Add(DataFile);
restoreDB.RelocateFiles.Add(LogFile);
restoreDB.NoRecovery = false;
restoreDB.PercentComplete += new PercentCompleteEventHandler(rstDatabase_PercentComplete);
restoreDB.Complete += new ServerMessageEventHandler(rstDatabase_Complete);
restoreDB.SqlRestore(smoServer);
有人可以帮助我如何使用c#从azure存储容器中还原备份文件,或者我的代码有什么问题?
答案 0 :(得分:0)
我发现了问题。 SQL Server 2012或更早版本不支持从块Blob还原数据库。因此,我将SQL Server更新为SQL Server 2017,还更改了代码。您需要在SQL Server中创建凭据,以便从天蓝色还原时使用该凭据。
if (!smoServer.Credentials.Contains(CredentialName))
{
Credential cr = new Credential(smoServer, CredentialName);
cr.Create("SHARED ACCESS SIGNATURE", AzureDataModel.SharedAccessKey);
}
Restore restoreDB = new Restore();
restoreDB.Action = RestoreActionType.Database;
restoreDB.Database = databaseName;
BackupDeviceItem deviceItem = new BackupDeviceItem(backUpFile, DeviceType.Url);
restoreDB.Devices.Add(deviceItem);
restoreDB.ReplaceDatabase = true;
if (!smoServer.Databases.Contains(databaseName)) {
Database db = new Database(smoServer, databaseName);
db.Create();
}
RelocateFile DataFile = new RelocateFile();
string MDF = restoreDB.ReadFileList(smoServer).Rows[0][1].ToString();
DataFile.LogicalFileName = restoreDB.ReadFileList(smoServer).Rows[0][0].ToString();
DataFile.PhysicalFileName = smoServer.Databases[databaseName].FileGroups[0].Files[0].FileName;
RelocateFile LogFile = new RelocateFile();
string LDF = restoreDB.ReadFileList(smoServer).Rows[1][1].ToString();
LogFile.LogicalFileName = restoreDB.ReadFileList(smoServer).Rows[1][0].ToString();
LogFile.PhysicalFileName = smoServer.Databases[databaseName].LogFiles[0].FileName;
restoreDB.RelocateFiles.Add(DataFile);
restoreDB.RelocateFiles.Add(LogFile);
restoreDB.NoRecovery = false;
restoreDB.PercentComplete += new PercentCompleteEventHandler(rstDatabase_PercentComplete);
restoreDB.Complete += new ServerMessageEventHandler(rstDatabase_Complete);
restoreDB.SqlRestore(smoServer);