以编程方式备份​​Azure Sql db

时间:2016-09-30 14:03:11

标签: c# sql-server azure

我已经创建了一个脚本来备份和恢复我的sql数据库。我最近刚搬到Azure Sql Server。我的备份策略不再适用。对于我的程序,它通常会保存备份并创建一个带有后缀的新备份,称为training。看来Azure Sql Server的备份策略完全不同。请问如何以编程方式备份​​Azure Sql db并恢复它。

if (!sqlServer.Databases.Contains(databaseTraining))
{
var backupFile = string.Concat(sqlServer.Settings.BackupDirectory, "\\", database, "Training.BAK");

var sourceBackup = new Backup
{
    Database = database,
    Action = BackupActionType.Database
};

var destDevice = new BackupDeviceItem(backupFile, DeviceType.File);
sourceBackup.Devices.Add(destDevice);
sourceBackup.Initialize = true;

sourceBackup.SqlBackup(sqlServer);

var db = new Microsoft.SqlServer.Management.Smo.Database(sqlServer, databaseTraining);
db.Create();

var destinationRestore = new Restore
{
    Database = databaseTraining,
    Action = RestoreActionType.Database
};
destinationRestore.Devices.Add(destDevice);

var dataFile = new RelocateFile
{
    LogicalFileName = destinationRestore.ReadFileList(sqlServer).Rows[0][0].ToString(),
    PhysicalFileName = sqlServer.Databases[databaseTraining].FileGroups[0].Files[0].FileName
};

var logFile = new RelocateFile
{
    LogicalFileName = destinationRestore.ReadFileList(sqlServer).Rows[1][0].ToString(),
    PhysicalFileName = sqlServer.Databases[databaseTraining].LogFiles[0].FileName
};

destinationRestore.RelocateFiles.Add(dataFile);
destinationRestore.RelocateFiles.Add(logFile);

destinationRestore.ReplaceDatabase = true;
destinationRestore.NoRecovery = false;

destinationRestore.SqlRestore(sqlServer);

1 个答案:

答案 0 :(得分:0)

  

我最近刚搬到Azure Sql Server。我的备份策略不再适用

您观察到的是正确的,您的备份策略在Azure中无法运行..

如果需要备份以确保像OnPremises一样的时间点恢复,则不需要备份,因为使用azure可以根据您的等级获得时间点恢复功能..

enter image description here

如果您希望在本地服务器上恢复数据库以进行测试/其他目的..您可以使用以下链接..

1。)How do I copy SQL Azure database to my local development server?

2)https://blogs.msdn.microsoft.com/mast/2013/03/03/different-ways-to-backup-your-windows-azure-sql-database/