public static void ShowPauseAndExit(bool noPrompt = true) { Consoler.Pause(noPrompt); if (noPrompt) { Environment.Exit(0); } }
public static void ShowCompletedAndExit(bool noPrompt = true) { Consoler.Success(noPrompt); if (noPrompt) { Environment.Exit(0); } }
public static void ShowErrorAndExit(Exception e, bool noPrompt = true) { Consoler.ShowError(e, noPrompt); if (noPrompt) { Environment.Exit(0); } }
public static void ShowHeader() { var sb = new StringBuilder(); sb.AppendLine("This utility performs actions such as."); sb.AppendLine("- database backup"); sb.AppendLine("- database restore"); sb.AppendLine("- zip .bak files"); Consoler.ShowHeader("DatabaseName Minder", sb.ToString()); }
private static void DoRestore() { if (!_command.Restore) { return; } Consoler.Write("Restore requested..."); RestoreDatabase.Execute(_server, _command.Folder, _command.DatabaseName); }
public static void Execute(Server server, string folder, string databaseName) { var bak = Directory.EnumerateFiles(folder, "*.bak").First(x => x.Contains(databaseName)); //If the database doesn't exist, create it so that we have something //to overwrite. if (!server.Databases.Contains(databaseName)) { Consoler.Information($"Database does not exist... Creating {databaseName}"); var database = new Database(server, databaseName); database.Create(); } var targetDatabase = server.Databases[databaseName]; targetDatabase.RecoveryModel = RecoveryModel.Simple; targetDatabase.Alter(); Restore restore = new Restore { Database = databaseName, ReplaceDatabase = true }; restore.Devices.AddDevice(bak, DeviceType.File); restore.Information += Restore_Information; var fileList = restore.ReadFileList(server); // restore to new location var dataFile = new RelocateFile { LogicalFileName = fileList.Rows[0][0].ToString(), PhysicalFileName = Path.Combine(folder, databaseName.WithoutExtension() + ".mdf") }; var logFile = new RelocateFile { LogicalFileName = fileList.Rows[1][0].ToString(), PhysicalFileName = Path.Combine(folder, databaseName.WithoutExtension() + "_log.log") }; restore.RelocateFiles.Add(dataFile); Consoler.Information($"dataFile {dataFile.LogicalFileName}"); restore.RelocateFiles.Add(logFile); Consoler.Information($"logFile {logFile.LogicalFileName}"); server.KillAllProcesses(databaseName); Consoler.Information($"KillAllProcesses"); restore.SqlRestore(server); Consoler.Information($"Database restored "); }
public static void Execute(Server server, string databaseName, string nameOfCredentials, string fullSavePath) { var backup = new Backup { CredentialName = nameOfCredentials, Database = databaseName //EncryptionOption = new BackupEncryptionOptions(BackupEncryptionAlgorithm.Aes128,BackupEncryptorType.ServerCertificate, "AutoBackup_Certificate") }; backup.Devices.AddDevice(fullSavePath, DeviceType.File); backup.SqlBackup(server); Consoler.Information($"Database backed up and saved to {fullSavePath}"); }
public static void RestoreDatabaseFromFolder(Server server, string databaseName, string backupPath) { Consoler.Write(backupPath); foreach (var file in Directory.EnumerateFiles(backupPath, "*.bak").OrderBy(x => x)) { try { Consoler.Write(file); Execute(server, databaseName, file); } catch (Exception ex) { Consoler.Warn("bak restore failed", backupPath); Consoler.Error(ex.ToString()); } } }
private static void DoBackup() { if (!_command.Backup) { return; } Consoler.Write("Backup requested..."); BackupDatabase.Execute(_server, _command.DatabaseName, _command.NameOfCredentials, _command.BackFullPath); if (_command.ZipBackup) { var dir = $"{Path.GetDirectoryName(_command.BackFullPath)}\\backup_{_command.DatabaseName}_{DateTime.Now.ToString("yyyyMMdd")}"; Directory.CreateDirectory(dir); File.Move(_command.BackFullPath, Path.Combine(dir, Path.GetFileName(_command.BackFullPath))); // add extract folder name in for zipping, move file into it then zip it ZipFile.CreateFromDirectory(dir, _command.BackFullPath.Replace(".bak", ".zip"), CompressionLevel.Optimal, false); Directory.Delete(dir, true); } }
public static void ShowHelpAndExit(bool noPrompt = false) { Consoler.Title("Usage:"); Consoler.Write("/help\t\t\tShow help"); Consoler.Write(""); Consoler.Write("Example:"); Consoler.Write(""); Consoler.Write(@"DatabaseMinder.Runner.exe /e /p /b /z /d DatabaseMinder /f c:\temp"); Consoler.Write(""); Consoler.Write("Arguments:"); Consoler.Write(""); Consoler.Write("\t/e EnableArgsModeViaConfig - true or false --> use config args or command line args"); Consoler.Write("\t/p PromptsEnabled - true or false --> pause and show prompts on the console (or skip)"); Consoler.Write("\t/r Restore - true or false --> Restore database"); Consoler.Write("\t/b Backup - true or false --> Backup database"); Consoler.Write("\t/d DatabaseName - the database name to back or restore"); Consoler.Write("\t/s ServerName - leave blank for localhost"); Consoler.Write("\t/f Folder - folder to use for backup,restore,zip"); //Consoler.Write("\t/c ConnectionString - for backups / restore"); Consoler.Write("\t/z ZipBackup - true or false --> after backup zip the .bak file"); ShowPauseAndExit(noPrompt); }
private static void EnsureDirectoriesExist(params string[] paths) { string lastPath = null; try { foreach (var path in paths.Where(x => !string.IsNullOrEmpty(x))) { lastPath = path; if (!Directory.Exists(path)) { Consoler.Information("creating directory " + path); Directory.CreateDirectory(path); } } } catch (Exception ex) { Consoler.Error($"Cannot create directories {lastPath} "); Consoler.Warn(ex.Message); Consoler.Pause(true); } }
private static void Restore_Information(object sender, Microsoft.SqlServer.Management.Common.ServerMessageEventArgs e) { Consoler.Information($"restore info {e.Error}"); }