public Form1() { InitializeComponent(); xmlSerializeManager = new XmlSerializeManager(); smoManager = new SmoManager(); callBackDel = callBackResultMethod; backupPercentComplete = backup_PercentComplete; backupComplete = backup_Complete; backupInformation = backup_Information; // usupełnianie bazy danych testowymi rekordami //initializeDatabaseData(300); }
public void CreateBackup(BackupConfigurationData configurationData, PercentCompleteEventHandler backup_PercentComplete = null, ServerMessageEventHandler backup_Complete = null, ServerMessageEventHandler backup_Information = null) { Backup backup = new Backup(); backup.Action = BackupActionType.Database; backup.Database = configurationData.DatabaseName; string backupFileFullPath = getBackupFileFullPath(configurationData.FileName, configurationData.LocalDirectory); backup.Incremental = backupIncremental(configurationData.Id, backupFileFullPath); backup.Devices.AddDevice(backupFileFullPath, DeviceType.File); backup.Initialize = false; try { backup.PercentCompleteNotification = 5; if (backup_PercentComplete != null) { backup.PercentComplete += new PercentCompleteEventHandler(backup_PercentComplete); } if (backup_Complete != null) { backup.Complete += new ServerMessageEventHandler(backup_Complete); } if (backup_Information != null) { backup.Information += new ServerMessageEventHandler(backup_Information); } backup.SqlBackupAsync(server); } catch (Exception ex) { LogInfo.LogErrorWrite($"Nie można utworzyć backapu bazy danych {configurationData.DatabaseName}", ex); } }
public void Restore(DatabaseRestoreRequest request, PercentCompleteEventHandler percentCompleteDelegate) { var server = new Server(request.TargetInstance); Database database = server.Databases[request.TargetDatabase]; // Start by locking down the database database.DatabaseOptions.UserAccess = DatabaseUserAccess.Single; database.Alter(TerminationClause.RollbackTransactionsImmediately); var restoreDb = new Restore { Database = database.Name, Action = RestoreActionType.Database }; //Specify whether you want to restore database or files or log etc restoreDb.Devices.AddDevice(request.FullBackupFile, DeviceType.File); // For now we only support database replacement. restoreDb.ReplaceDatabase = true; // For full backup no recovery is not usefull. Will need to to change this if support for restoring transactional backup file happens. restoreDb.NoRecovery = false; // Associate the correct physical path for each file to be restored foreach (FileToRestore fileToRestore in request.FilesLists) { restoreDb.RelocateFiles.Add(new RelocateFile(fileToRestore.BackupLogicalName, fileToRestore.TargetPhysicalPath)); } if (percentCompleteDelegate != null) { restoreDb.PercentComplete += percentCompleteDelegate; } // Magic! restoreDb.SqlRestore(server); restoreDb.SqlRestoreAsync(server); // After the restore, ensure the recovery model is set to simple. // Since we only support DEV/TEST/DEMO, we dont want the overhead of the other recovery models. database.RecoveryModel = RecoveryModel.Simple; database.Alter(); string sqlConnectionString = string.Format("Integrated Security=SSPI;Persist Security Info=True;Initial Catalog={1};Data Source={0}", server.Name, database.Name); foreach (var script in request.ScriptsToExecute) { var fileInfo = new FileInfo(script); string sql; using (var text = fileInfo.OpenText()) { sql = text.ReadToEnd(); text.Close(); } SqlConnection connection = new SqlConnection(sqlConnectionString); Server srv = new Server(new ServerConnection(connection)); srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.ExecuteAndCaptureSql; srv.ConnectionContext.ExecuteNonQuery(sql); } }
public Task<string> RestoreAsync(DatabaseRestoreRequest request, PercentCompleteEventHandler percentCompleteDelegate) { var server = new Server(request.TargetInstance); Database database = server.Databases[request.TargetDatabase]; // Start by locking down the database database.DatabaseOptions.UserAccess = DatabaseUserAccess.Single; database.Alter(TerminationClause.RollbackTransactionsImmediately); var restoreDb = new Restore { Database = database.Name, Action = RestoreActionType.Database }; //Specify whether you want to restore database or files or log etc restoreDb.Devices.AddDevice(request.FullBackupFile, DeviceType.File); // For now we only support database replacement. restoreDb.ReplaceDatabase = true; // For full backup no recovery is not usefull. Will need to to change this if support for restoring transactional backup file happens. restoreDb.NoRecovery = false; // Associate the correct physical path for each file to be restored foreach (FileToRestore fileToRestore in request.FilesLists) { restoreDb.RelocateFiles.Add(new RelocateFile(fileToRestore.BackupLogicalName, fileToRestore.TargetPhysicalPath)); } if (percentCompleteDelegate != null) { restoreDb.PercentComplete += percentCompleteDelegate; } var tcs = new TaskCompletionSource<string>(); //Handle the Complete event with a TaskCompletionSource that way when can control when the task is completed. restoreDb.Complete += (sender, e) => RestoreWasCompleted(tcs, e, request, database, server); try { // Start asynchrounos restore. That way the ThreadPool is not blocked, restoreDb.SqlRestoreAsync(server); } catch (Exception ex) { // An exception occured. notify the task tcs.TrySetException(ex); } return tcs.Task; }