/// <summary> /// lists the dates of available backups and returns selected DateTime ojbect /// </summary> /// <param name="restore"></param> /// <param name="maintPlan"></param> /// <returns></returns> public static DateTime GetDate(string dB, MaintenancePlan maintPlan) { DateTime date = new DateTime(); string strIndex = ""; int intIndex = 0; do { do { strIndex = ""; Console.WriteLine(dB); Console.WriteLine(CreateIndexedString(maintPlan.Dates())); strIndex = Console.ReadLine(); if (strIndex == "") { intIndex = maintPlan.Dates().Count - 1; strIndex = intIndex.ToString(); intIndex = 0; } } while (!Int32.TryParse(strIndex, out intIndex)); intIndex = Convert.ToInt32(strIndex); } while (intIndex > maintPlan.Dates().Count()); date = maintPlan.Dates()[intIndex]; return date; }
public string CompiledScript(MaintenancePlan maintPlan) { StringBuilder sbString = new StringBuilder(); string script; foreach (RestoreDB database in DBs) { sbString.AppendLine(GenerateBackupCommand(maintPlan,database)); sbString.AppendLine(""); } script = sbString.ToString(); return script; }
public string GenerateBackupCommand(MaintenancePlan maintPlan,RestoreDB genDatabase) { string command; StringBuilder sbString = new StringBuilder(); command = string.Format("RESTORE DATABASE '{0}' FROM DISK = '{1}\\{2}' WITH FILE = 1 NORECOVERY;" , genDatabase.Database , maintPlan.FullBKFolder.Path , maintPlan.FullBKFolder.Files .Where(x => x.LastModified.Date.Equals(genDatabase.Date) && x.DatabaseName.Equals(genDatabase.Database)) .Select(x => x.Name) .First() .ToString()); return command; }
public void PopulateTreeview(MaintenancePlan maintPlan) { int db = 0; int dateindex = 0; treeView1.Nodes.Clear(); foreach (string dB in maintPlan.DBs()) { treeView1.Nodes.Add(dB); foreach (DateTime date in maintPlan.GetDates(dB)) { treeView1.Nodes[db].Nodes.Add(date.ToString("d")); treeView1.Nodes[db].LastNode.Tag = maintPlan.GetFullBackupFile(dB, date); if (maintPlan.LogBKFolder != null) { foreach (DateTime time in maintPlan.GetLogTimes(dB,date)) { treeView1.Nodes[db].Nodes[dateindex].Nodes.Add(time.ToString("hh:mm tt")); treeView1.Nodes[db].Nodes[dateindex].LastNode.Tag = maintPlan.GetLogBackupFile(dB, time); } dateindex++; } } dateindex = 0; db++; } }
/// <summary> /// Accepts a MaintenancePlan and returns a list of strings containing The users selected databases /// </summary> /// <param name="maintPlan"></param> /// <returns></returns> public static List<string> GetDB(MaintenancePlan maintPlan) { List<string> databases = new List<string>(); string strIndex = ""; int intIndex = 0; do { do { strIndex = ""; Console.Clear(); Console.WriteLine("Choose databases from below list\n"); Console.WriteLine(CreateIndexedString(maintPlan.DBs())); Console.Write("[All]: "); strIndex = Console.ReadLine(); if (strIndex == "") strIndex = maintPlan.DBs().Count.ToString(); } while (!Int32.TryParse(strIndex, out intIndex)); intIndex = Convert.ToInt32(strIndex); } while (maintPlan.DBs().Count < intIndex); if (intIndex == maintPlan.DBs().Count) databases.AddRange(maintPlan.DBs()); else databases.Add(maintPlan.DBs()[intIndex]); return databases; }
static void Main(string[] args) { MaintenancePlan maintPlan = new MaintenancePlan(); Restore restore = new Restore(); maintPlan = GetPaths(); restore = new Restore(GetDB(maintPlan)); if (restore.DBs.Count != 1) Console.WriteLine("You have selected all databases for restore"); else Console.WriteLine("\nYou have selected: \"{0}\"", restore.DBs.Last().Database); string answer = ""; Console.Clear(); Console.WriteLine("Restore all databases to latest state?"); answer = Console.ReadLine(); if (answer.ToLower() == "y") { restore.SetDate(maintPlan); Console.WriteLine("All DBs have been set to the latest recovery points."); } else { foreach (RestoreDB dB in restore.DBs) { restore.SetDate(dB.Database, GetDate(dB.Database, maintPlan)); } } do { Console.WriteLine("please enter the database directory."); restore.RestoreTarget = Console.ReadLine(); }while(restore.RestoreTarget == null); Program.SaveFile(restore.CompiledScript(maintPlan)); Console.WriteLine("The restore command is:\n"); Console.WriteLine(restore.CompiledScript(maintPlan)); Console.ReadKey(); }
public static MaintenancePlan OldGetPath() { MaintenancePlan MaintPlan = new MaintenancePlan(); do { Console.WriteLine("Enter path to full backups (c:\\FullBackups)"); Console.Write("Required: "); MaintPlan.FullBKPath = Console.ReadLine(); if (MaintPlan.FullBKPath != null) break; Console.Clear(); Console.WriteLine("Supplied path does not exist!\n"); } while (MaintPlan.FullBKPath == null); Console.Clear(); Console.WriteLine("Enter path to full backups (c:\\FullBackups)"); Console.WriteLine("Required: {0}\n", MaintPlan.FullBKPath); do { Console.WriteLine("Enter path to Differential backups (c:\\DiffBackups)"); Console.Write("Optional: "); MaintPlan.DiffBKPath = Console.ReadLine(); if (MaintPlan.DiffBKPath != null) break; Console.Clear(); Console.WriteLine("Enter path to full backups (c:\\DiffBackups)"); Console.WriteLine("Required: {0}", MaintPlan.FullBKPath); Console.WriteLine("\nSupplied path does not exist!\n"); } while (MaintPlan.DiffBKPath == null); Console.Clear(); Console.WriteLine("Enter path to full backups (c:\\FullBackups)"); Console.WriteLine("Required: {0}\n", MaintPlan.FullBKPath); Console.WriteLine("Enter path to Differential backups (c:\\DiffBackups)"); Console.WriteLine("Optional: {0}\n", MaintPlan.DiffBKPath); do { Console.WriteLine("Enter path to Log backups (c:\\LogBackups)"); Console.Write("Optional: "); MaintPlan.LogBKPath = Console.ReadLine(); if (MaintPlan.LogBKPath != null) break; Console.Clear(); Console.WriteLine("Enter path to full backups (c:\\FullBackups)"); Console.WriteLine("Required: {0}\n", MaintPlan.FullBKPath); Console.WriteLine("Enter path to Differential backups (c:\\DiffBackups)"); Console.WriteLine("Optional: {0}\n", MaintPlan.DiffBKPath); Console.WriteLine("\nSupplied path does not exist!\n"); } while (MaintPlan.LogBKPath == null); return MaintPlan; }
/// <summary> /// Collects the locations of full, diff, and log backup files. /// </summary> /// <returns></returns> public static MaintenancePlan GetPaths() { MaintenancePlan maintPlan = new MaintenancePlan(); string answer = ""; do { Console.Clear(); if(maintPlan.FullBKPath == null) { Console.WriteLine("Enter path to Full backups (c:\\FullBackups)"); Console.Write("Required: "); maintPlan.FullBKPath = Console.ReadLine(); if (maintPlan.FullBKPath == null) { Console.Clear(); continue; } Console.WriteLine(); } else { Console.WriteLine("Enter path to Full backups (c:\\FullBackups)"); Console.WriteLine("Required: {0}\n", maintPlan.FullBKPath); } if (maintPlan.DiffBKPath == null) { Console.WriteLine("Enter path to Differential backups (c:\\DiffBackups)"); Console.Write("Optional [N\\A]: "); maintPlan.DiffBKPath = Console.ReadLine(); if (maintPlan.DiffBKPath == null) { Console.Clear(); continue; } Console.WriteLine(); } else { Console.WriteLine("Enter path to Differential backups (c:\\DiffBackups)"); Console.WriteLine("Optional [N\\A]: {0}\n", maintPlan.DiffBKPath); } if (maintPlan.LogBKPath == null) { Console.WriteLine("Enter path to Log backups (c:\\LogBackups)"); Console.Write("Optional [N\\A]: "); maintPlan.LogBKPath = Console.ReadLine(); if (maintPlan.LogBKPath == null) { Console.Clear(); continue; } Console.WriteLine(); } else { Console.WriteLine("Enter path to Log backups (c:\\LogBackups)"); Console.WriteLine("Optional [N\\A]: {0}\n", maintPlan.LogBKPath); } Console.WriteLine("{0}", maintPlan.ToString()); Console.Write("\nIs this correct?[N] "); answer = Console.ReadLine(); if(answer.ToLower() != "y") { maintPlan = new MaintenancePlan(); } }while (answer.ToLower() != "y"); return maintPlan; }
public void SetDate(MaintenancePlan maintPlan) { foreach (RestoreDB dB in DBs) { foreach (BackupFile file in maintPlan.FullBKFolder.Files) { if(file.DatabaseName == dB.Database) { int result = dB.Date.CompareTo(file.LastModified.Date); if (result < 0) dB.Date = file.LastModified.Date; } } } }