internal void Clean(string dbName, RestoreOption opt) { string dbUse = $"USE [{dbName}]\n"; string connectionString = $"Data Source={_sqlAccess.name};Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; if ((opt & RestoreOption.CleanTable) == RestoreOption.CleanTable) { ExecuteSql(dbUse + SqlQuery.CleanTable, connectionString, "Clean Table"); } }
private async Task Clean(string db) { SqlServerAccess sa = null; RestoreOption opt = RestoreOption.None; await Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)(() => { opt = (CleanTable ? RestoreOption.CleanTable : 0); sa = SqlSourcesCombo.SelectedItem as SqlServerAccess; })); if (sa != null) { await Task.Run(() => CleanExecute(sa, opt, db)); } }
public override void Visit(RestoreOption node) { this.action(node); }
public override void ExplicitVisit(RestoreOption fragment) { _fragments.Add(fragment); }
private void RestoreBackup(string path, string restore_name, RestoreOption restoreOption) { try { _restoreOption = restoreOption; InitConnexion(); if (!_smoServer.Databases.Contains(restore_name)) { var database = new Database(_smoServer, restore_name); database.Create(); } Logger.Success("SQL connected"); _targetDatabase = _smoServer.Databases[restore_name]; _targetDatabase.RecoveryModel = RecoveryModel.Simple; _targetDatabase.Alter(TerminationClause.RollbackTransactionsImmediately); Restore restore = new Restore(); var backupDeviceItem = new BackupDeviceItem(path, DeviceType.File); restore.Devices.Add(backupDeviceItem); restore.Database = restore_name; restore.ReplaceDatabase = true; restore.NoRecovery = false; if (_mode == RestoreMode.AtTime) { restore.NoRecovery = true; } var fileList = restore.ReadFileList(_smoServer); var dataFile = new RelocateFile(); dataFile.LogicalFileName = fileList.Rows[0][0].ToString(); dataFile.PhysicalFileName = _smoServer.Databases[restore_name].FileGroups[0].Files[0].FileName; var logFile = new RelocateFile(); logFile.LogicalFileName = fileList.Rows[1][0].ToString(); logFile.PhysicalFileName = _smoServer.Databases[restore_name].LogFiles[0].FileName; restore.RelocateFiles.Add(dataFile); restore.RelocateFiles.Add(logFile); _smoServer.KillAllProcesses(restore_name); restore.PercentComplete += Restore_PercentComplete; restore.SqlRestoreAsync(_smoServer); restore.Information += Restore_Information; restore.Wait(); Restore_Complete(); } catch (SmoException ex) { Logger.Error("SMO Message : " + ex.Message); Logger.Error("SMO Exception : " + ex.InnerException); } catch (IOException ex) { Logger.Error("IO Message : " + ex.Message); Logger.Error("IO Exception : " + ex.InnerException); } catch (Exception ex) { Logger.Error("Message : " + ex.Message); Logger.Error("Exception : " + ex.InnerException); } }
public void RestoreAtTime(string path, List <string> transactionFilesDestPath, DateTime time, string restore_name, RestoreOption restoreOption) { _transactions = transactionFilesDestPath; _time = time; _mode = RestoreMode.AtTime; RestoreBackup(path, restore_name, restoreOption); }
public void Restore(string path, string restore_name, RestoreOption restoreOption) { _mode = RestoreMode.AtDate; RestoreBackup(path, restore_name, restoreOption); }
private void RestoreAtTimeExecute(ServerAccess ba, SqlServerAccess sa, string c, string dbName, DateTime time, RestoreOption opt) { try { using (new NetworkConnection(ba.uri, new NetworkCredential(ba.username, ba.password))) { string path = Path.Combine(ba.uri, ba.dailyfolder, c); DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] files = dir.GetFiles("*.bak").OrderByDescending(p => p.CreationTime).ToArray(); var f = files.FirstOrDefault(); var filePath = f.FullName; var creationDate = f.CreationTime; FileInfo[] transactionFiles = dir.GetFiles("*.trn").OrderByDescending(p => p.CreationTime).ToArray(); using (new NetworkConnection(sa.uri, new NetworkCredential(sa.username, sa.password))) { string destPath = Path.Combine(sa.uri, sa.folder, f.Name); if (File.Exists(destPath)) { File.Delete(destPath); } File.Copy(filePath, destPath); var bakFilePath = destPath; var transactionFilesPath = CopyAllTransactionFiles(transactionFiles, sa, creationDate, time); destPath = destPath.Replace(sa.uri, sa.disk); var transactionFilesDestPath = new List <string>(); foreach (var tf in transactionFilesPath) { transactionFilesDestPath.Add(tf.Replace(sa.uri, sa.disk)); } SMOHelper restoreInstance = new SMOHelper(sa); restoreInstance.RestoreAtTime(destPath, transactionFilesDestPath, time, dbName, opt); if (File.Exists(bakFilePath)) { File.Delete(bakFilePath); } foreach (var fileToDel in transactionFilesPath) { if (File.Exists(fileToDel)) { File.Delete(bakFilePath); } } } } } catch (Exception ex) { Logger.Error(ex.Message); } }
private async Task CleanExecute(SqlServerAccess sa, RestoreOption opt, string db) { SMOHelper instance = new SMOHelper(sa); instance.Clean(db, opt); }
private void RestoreAtDateExecute(ServerAccess ba, SqlServerAccess sa, FileInfo f, string dbName, RestoreOption opt) { try { using (new NetworkConnection(ba.uri, new NetworkCredential(ba.username, ba.password))) { var filePath = f.FullName; using (new NetworkConnection(sa.uri, new NetworkCredential(sa.username, sa.password))) { string destPath = Path.Combine(sa.uri, sa.folder, f.Name); if (File.Exists(destPath)) { File.Delete(destPath); } File.Copy(filePath, destPath); var bakFilePath = destPath; destPath = destPath.Replace(sa.uri, sa.disk); SMOHelper restoreInstance = new SMOHelper(sa); restoreInstance.Restore(destPath, dbName, opt); if (File.Exists(bakFilePath)) { File.Delete(bakFilePath); } } } } catch (Exception ex) { Logger.Error(ex.Message); } }