public override string Query() { LogOptions.GetLogger().Info("Starting to run mysqldump of database"); var settings = BackupSettings.GetInstance(); var executable = settings.Executable; var arguments = $"-u {settings.User} -p{settings.Password} {settings.Database}"; LogOptions.GetLogger().Info("Starting dump {executable} {arguments}", executable, arguments); try { var processStartInfo = new ProcessStartInfo { FileName = executable, Arguments = arguments, RedirectStandardError = true, RedirectStandardOutput = true, CreateNoWindow = true, UseShellExecute = false }; var p = Process.Start(processStartInfo); var standardOutput = p?.StandardOutput.ReadToEnd(); p?.WaitForExit(); return(standardOutput); } catch (Exception e) { LogOptions.GetLogger().Fatal(e, "Exception in dump mysql database {message}", e.Message); return(string.Empty); } }
protected override void OnExecuting() { //Abort backup if only when skywin is running and not currently running if (BackupSettings.GetInstance().OnlyBackupWhenSkyWinIsRunning && !new IsSkywinRunningQuery().Query()) { LogOptions.GetLogger().Info("Backup only when skywin is running - it's not right now - aborting"); return; } var sqlData = new DumpMySqlDatabaseQuery().Query(); if (string.IsNullOrWhiteSpace(sqlData)) { LogOptions.GetLogger().Warn("Unable to dump sql database into string {output}", sqlData); return; } var dateTime = DateTime.Now; var subPath = Path.Combine(dateTime.Year.ToString(), dateTime.Month.ToString(), dateTime.Day.ToString(), DateTime.Now.ToString("s").Replace(":", "-") + ".sql"); var initialFile = Path.Combine(BackupSettings.GetInstance().SaveBackupPath, subPath); LogOptions.GetLogger().Info("Initial backup will be saved into {filepath}", initialFile); try { var fi = new FileInfo(initialFile); if (!fi?.Directory?.Exists ?? true) { LogOptions.GetLogger().Info("Directory path for file doesnt exists creating it"); fi?.Directory?.Create(); } LogOptions.GetLogger().Info("Saving backup file"); File.WriteAllText(initialFile, sqlData); LogOptions.GetLogger().Info("Saved backup file"); if (BackupSettings.GetInstance().CopyBackupsTo?.Count > 0) new CopyBackupFileIntoExtraLocationsCommand(initialFile, subPath).Execute(); } catch (Exception e) { LogOptions.GetLogger().Fatal(e, "Exception in save initial file {message}", e.Message); } }
private void CopyFile(string destinationPath) { try { var fi = new FileInfo(destinationPath); if (!fi?.Directory?.Exists ?? true) { LogOptions.GetLogger().Info("Directory {path} for copying destination not existing", destinationPath); fi?.Directory?.Create(); } LogOptions.GetLogger().Info("Copying {src} to {dst}", _fileLocation, destinationPath); File.Copy(_fileLocation, destinationPath); LogOptions.GetLogger().Info("Copied successful"); } catch (Exception e) { LogOptions.GetLogger().Warn(e, "Exception in copy file {message}", e.Message); } }
protected QueryBase() { LogOptions.GetLogger().Info("Running Query of {type}", GetType()); }
private static void Main(string[] args) { LogOptions.GetLogger().Info("Starting windows program"); new BackupDatabaseIntoFileCommand().Execute(); LogOptions.GetLogger().Info("Finished executing"); }
//Constructor public BackupSettings() { LogOptions.GetLogger().Info("Starting to read configuration"); var temporayConfigRead = ConfigurationManager.AppSettings["Host"]; if (temporayConfigRead != null) { Host = temporayConfigRead; } temporayConfigRead = ConfigurationManager.AppSettings["Database"]; if (temporayConfigRead != null) { Database = temporayConfigRead; } temporayConfigRead = ConfigurationManager.AppSettings["User"]; if (temporayConfigRead != null) { User = temporayConfigRead; } temporayConfigRead = ConfigurationManager.AppSettings["Password"]; if (temporayConfigRead != null) { Password = temporayConfigRead; } temporayConfigRead = ConfigurationManager.AppSettings["SaveBackupPath"]; if (temporayConfigRead != null) { SaveBackupPath = temporayConfigRead; } bool tmp; temporayConfigRead = ConfigurationManager.AppSettings["OnlyBackupWhenSkyWinIsRunning"]; if (temporayConfigRead != null && bool.TryParse(temporayConfigRead, out tmp)) { OnlyBackupWhenSkyWinIsRunning = tmp; } temporayConfigRead = ConfigurationManager.AppSettings["CopyBackupsTo"]; if (temporayConfigRead != null) { CopyBackupsTo = temporayConfigRead.Split('|').ToList(); } temporayConfigRead = ConfigurationManager.AppSettings["Executable"]; if (temporayConfigRead != null) { Executable = temporayConfigRead; } LogOptions.GetLogger().Info("Loaded settings are as follows"); LogOptions.GetLogger().Info("User:{User}", User); LogOptions.GetLogger().Info("Password:{Password}", Password); LogOptions.GetLogger().Info("Database:{Database}", Database); LogOptions.GetLogger().Info("SaveBackupPath:{SaveBackupPath}", SaveBackupPath); LogOptions.GetLogger().Info("OnlyBackupWhenSkyWinIsRunning:{OnlyBackupWhenSkyWinIsRunning}", OnlyBackupWhenSkyWinIsRunning); LogOptions.GetLogger().Info("CopyBackupsTo:{CopyBackupsTo}", CopyBackupsTo); LogOptions.GetLogger().Info("Executable:{Executable}", Executable); }