public static List <string> RestoreCleanupShrinkDB(DBRestoreParam dBRestoreParam, string DBName, bool ValidateParam = true) { List <string> RetList = new List <string>(5); string sqlBackupfile = string.Empty; string outMessage = string.Empty; if (ValidateParam) { if ((dBRestoreParam == null) || (!dBRestoreParam.IsValid(out outMessage))) { RetList.Add(outMessage); return(RetList); } } DbConnectionStringBuilder dbConnBuilder = new DbConnectionStringBuilder(); dbConnBuilder.ConnectionString = dBRestoreParam.TargetServer.ConnString; RetList.Add(CopyBackupFromNetwork(dBRestoreParam, DBName, out sqlBackupfile)); RetList.Add(RestoreBackup(sqlBackupfile, dBRestoreParam, DBName)); RetList.Add(CleanupTables(DBName, dBRestoreParam)); RetList.Add(ShrinkDatabase(DBName, dBRestoreParam)); RetList.Add(FixDbUserLogin(DBName, dBRestoreParam)); if (dBRestoreParam.PostRestore.DeleteLocalCopy) { RetList.Add(DeleteBackupFile(sqlBackupfile)); } return(RetList); }
private static string RestoreBackup(string sqlBackupfile, DBRestoreParam dBRestoreParam, string DBName) { DbConnectionStringBuilder dbConnBuilder = new DbConnectionStringBuilder(); dbConnBuilder.ConnectionString = dBRestoreParam.TargetServer.ConnString; return(RestoreBackup(sqlBackupfile, dBRestoreParam.TargetServer.SQLDataPath, dBRestoreParam.TargetServer.SQLLogPath, DBName, dbConnBuilder)); }
private void SubmitBtn_Click(object sender, EventArgs e) { DBRestoreParam dBRestoreParam = null; try { Cursor.Current = Cursors.WaitCursor; if (EnterConfigRadioBtn.Checked) { dBRestoreParam = GetDBRestoreParam(); } else if (ConfigFileRadioBtn.Checked) { dBRestoreParam = DBRestoreParam.NewDBRestoreParam(this.ConfigFileTextBox.Text); } List <string> strList = DBRestoreHelper.RestoreCleanupShrinkDB(dBRestoreParam); AddToResults(strList); } catch (Exception ex) { AddToResults(ex.Message); if (ex.InnerException != null) { AddToResults(ex.InnerException.Message); } } finally { Cursor.Current = Cursors.Default; } }
public static DBRestoreParam NewDBRestoreParam(string jsonFilePath) { DBRestoreParam dBRestoreParam = null; if (File.Exists(jsonFilePath)) { // get info from the file and populate properties dBRestoreParam = JsonConvert.DeserializeObject <DBRestoreParam>(File.ReadAllText(jsonFilePath)); if (dBRestoreParam != null) { dBRestoreParam.RestoreBackupsFromPath = CleanupPath(dBRestoreParam.RestoreBackupsFromPath); dBRestoreParam.TargetServer.SQLDataPath = CleanupPath(dBRestoreParam.TargetServer.SQLDataPath); dBRestoreParam.TargetServer.SQLLogPath = CleanupPath(dBRestoreParam.TargetServer.SQLLogPath); dBRestoreParam.TargetServer.LocalBackupPath = CleanupPath(dBRestoreParam.TargetServer.LocalBackupPath); if (!IsValidDBConnString(dBRestoreParam.TargetServer.ConnString)) { dBRestoreParam.TargetServer.ConnString = string.Empty; } if (dBRestoreParam.PostRestore == null) { dBRestoreParam.PostRestore = new PostRestoreOptions(); } } } return(dBRestoreParam); }
private static string ShrinkDatabase(string DBName, DBRestoreParam dBRestoreParam) { DbConnectionStringBuilder dbConnBuilder = new DbConnectionStringBuilder(); dbConnBuilder.ConnectionString = dBRestoreParam.TargetServer.ConnString; if (dBRestoreParam.PostRestore.ShrinkDB) { return(ShrinkDatabase(DBName, dbConnBuilder)); } return(string.Format("Shrink DB was not run for Database:{0}", DBName)); }
private static string FixDbUserLogin(string DBName, DBRestoreParam dBRestoreParam) { DbConnectionStringBuilder dbConnBuilder = new DbConnectionStringBuilder(); dbConnBuilder.ConnectionString = dBRestoreParam.TargetServer.ConnString; string[] usersToFix = dBRestoreParam.PostRestore.GetUsers(DBName); if (dBRestoreParam.PostRestore.RestoreUserAccess) { return(FixDbUserLogin(DBName, dbConnBuilder, usersToFix)); } return(string.Format("DB User Login was not run for Database:{0}", DBName)); }
private static string CleanupTables(string DBName, DBRestoreParam dBRestoreParam) { string ProcName = dBRestoreParam.PostRestore.CleanupProcName; DbConnectionStringBuilder dbConnBuilder = new DbConnectionStringBuilder(); dbConnBuilder.ConnectionString = dBRestoreParam.TargetServer.ConnString; if (dBRestoreParam.PostRestore.CleanupTables) { return(CleanupTables(DBName, dbConnBuilder, ProcName)); } return(string.Format("Cleanup Tables was not run for Database:{0}", DBName)); }
public static DBRestoreParam NewDBRestoreParam(string networkPath , string dataPath , string logPath , string backupPath , string[] databasesToRestore , string connString) { DBRestoreParam dBRestoreParam = new DBRestoreParam(); dBRestoreParam.RestoreBackupsFromPath = networkPath; dBRestoreParam.TargetServer.SQLDataPath = dataPath; dBRestoreParam.TargetServer.SQLLogPath = logPath; dBRestoreParam.TargetServer.LocalBackupPath = backupPath; dBRestoreParam.DatabasesToRestore = new List <string>(databasesToRestore); dBRestoreParam.TargetServer.ConnString = connString; return(dBRestoreParam); }
private static void RunTool(CmdOptions cmdOptions) { if (!string.IsNullOrEmpty(cmdOptions.InputFile)) { if (!System.IO.File.Exists(cmdOptions.InputFile)) { Console.WriteLine("Input file {0} doesn't exists.", cmdOptions.InputFile); } else { DBRestoreParam dBRestoreParam = DBRestoreParam.NewDBRestoreParam(cmdOptions.InputFile); List <string> strList = DBRestoreHelper.RestoreCleanupShrinkDB(dBRestoreParam); foreach (string str in strList) { Console.WriteLine(str); } } } }
public static List <string> RestoreCleanupShrinkDB(DBRestoreParam dBRestoreParam) { List <string> RetList = new List <string>(5); string outMessage = string.Empty; if ((dBRestoreParam == null) || (!dBRestoreParam.IsValid(out outMessage))) { RetList.Add(outMessage); } else { string[] databasesToRestore = dBRestoreParam.DatabasesToRestore.ToArray(); string sqlBackupfile = string.Empty; foreach (string dbName in databasesToRestore) { RetList.Add("Working on DB:" + dbName); RetList.AddRange(RestoreCleanupShrinkDB(dBRestoreParam, dbName, false)); RetList.Add("Done with DB:" + dbName); } } return(RetList); }
public static List <string> RestoreCleanupShrinkDB(string jsonFilePath) { List <string> RetList = new List <string>(5); DBRestoreParam dbparam = DBRestoreParam.NewDBRestoreParam(jsonFilePath); string outMessage = string.Empty; if ((dbparam != null) && (dbparam.IsValid(out outMessage))) { string sqlBackupfile = string.Empty; foreach (string dbName in dbparam.DatabasesToRestore) { RetList.Add("Working on DB:" + dbName); RetList.AddRange(RestoreCleanupShrinkDB(dbparam, dbName, false)); RetList.Add("Done with DB:" + dbName); } } else { RetList.Add(outMessage); } return(RetList); }
private DBRestoreParam GetDBRestoreParam() { string[] dbstoRestore = this.DBToRestoreTxtBox.Text.Split(','); List <string> usersToRestore = new List <string>(this.UsersToRestoreTxtBox.Text.Split(',')); bool UseCleanupProc = this.CleanupTablesCheckbox.Checked; bool ShrinkDB = this.ShrinkDBCheckBox.Checked; bool DeleteBackups = this.DeleteBackupCheckBox.Checked; DBRestoreParam dBRestoreParam = DBRestoreParam.NewDBRestoreParam(this.NetworkPathTextBox.Text , this.DataPathTextBox.Text , this.LogPathTextBox.Text , this.BackupPathTxtBox.Text , dbstoRestore , this.ServerNameTxtBox.Text); dBRestoreParam.PostRestore.CleanupTables = UseCleanupProc; dBRestoreParam.PostRestore.CleanupProcName = this.CleanUpProcName.Text; dBRestoreParam.PostRestore.UsersToRestore = usersToRestore; dBRestoreParam.PostRestore.ShrinkDB = ShrinkDB; dBRestoreParam.PostRestore.DeleteLocalCopy = DeleteBackups; return(dBRestoreParam); }
private static string CopyBackupFromNetwork(DBRestoreParam dBRestoreParam, string DBName, out string copiedBackupFile) { return(CopyBackupFromNetwork(dBRestoreParam.RestoreBackupsFromPath, dBRestoreParam.TargetServer.LocalBackupPath , DBName, out copiedBackupFile)); }