From 1f9ac0552a1ca751da015145f8d6fef3d6ed9bc5 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 12 Apr 2023 04:26:00 +0200 Subject: [PATCH] Retarget to .NET 6.0 --- BackupManager/BackupManager.csproj | 2 +- BackupManager/Config.cs | 6 +-- BackupManager/Program.cs | 68 +++++++++++++++--------------- do-backup.sh | 3 -- 4 files changed, 37 insertions(+), 42 deletions(-) diff --git a/BackupManager/BackupManager.csproj b/BackupManager/BackupManager.csproj index 300b3dc..3c0adc5 100644 --- a/BackupManager/BackupManager.csproj +++ b/BackupManager/BackupManager.csproj @@ -2,7 +2,7 @@ Exe - net5.0 + net6.0 diff --git a/BackupManager/Config.cs b/BackupManager/Config.cs index dfd2506..3724bc0 100644 --- a/BackupManager/Config.cs +++ b/BackupManager/Config.cs @@ -1,7 +1,5 @@ -namespace BackupManager -{ - public class Config - { +namespace BackupManager { + public class Config { public string FileSystemPathV2 { get; set; } public string MySqlDumpPathWindows { get; set; } = @"C:\Program Files\MariaDB 10.3\bin\mysqldump.exe"; diff --git a/BackupManager/Program.cs b/BackupManager/Program.cs index 459a977..bcc20f7 100644 --- a/BackupManager/Program.cs +++ b/BackupManager/Program.cs @@ -15,7 +15,7 @@ using System.Xml.Serialization; namespace BackupManager { public static class Program { - public readonly static Stopwatch sw = new Stopwatch(); + public readonly static Stopwatch sw = new(); private const string CONFIG_NAME = @"FlashiiBackupManager.v1.xml"; @@ -39,17 +39,17 @@ namespace BackupManager { ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), @"Backups") : Config.FileSystemPathV2; - public static bool Headless; + private static bool Headless; public static string WindowsToUnixPath(this string path) { return IsWindows ? path.Replace('\\', '/') : path; } public static Stream ToXml(this object obj, bool pretty = false) { - MemoryStream ms = new MemoryStream(); - XmlSerializer xs = new XmlSerializer(obj.GetType()); + MemoryStream ms = new(); + XmlSerializer xs = new(obj.GetType()); - using (XmlWriter xw = XmlWriter.Create(ms, new XmlWriterSettings { Indent = pretty })) + using(XmlWriter xw = XmlWriter.Create(ms, new XmlWriterSettings { Indent = pretty })) xs.Serialize(xw, obj); ms.Seek(0, SeekOrigin.Begin); @@ -57,24 +57,24 @@ namespace BackupManager { } public static T FromXml(Stream xml) { - if (xml.CanSeek) + if(xml.CanSeek) xml.Seek(0, SeekOrigin.Begin); - XmlSerializer xs = new XmlSerializer(typeof(T)); + XmlSerializer xs = new(typeof(T)); return (T)xs.Deserialize(xml); } public static void SaveConfig() { Log(@"Saving configuration..."); - using (FileStream fs = new FileStream(ConfigPath, FileMode.Create, FileAccess.Write)) - using (Stream cs = Config.ToXml(true)) - cs.CopyTo(fs); + using FileStream fs = new(ConfigPath, FileMode.Create, FileAccess.Write); + using Stream cs = Config.ToXml(true); + cs.CopyTo(fs); } public static void LoadConfig() { Log(@"Loading configuration..."); - using (FileStream fs = File.OpenRead(ConfigPath)) - Config = FromXml(fs); + using FileStream fs = File.OpenRead(ConfigPath); + Config = FromXml(fs); } public static void Main(string[] args) { @@ -83,7 +83,7 @@ namespace BackupManager { Log(@"Flashii Backup Manager"); sw.Start(); - if (!File.Exists(ConfigPath)) { + if(!File.Exists(ConfigPath)) { Config = new Config(); SaveConfig(); Error(@"No configuration file exists, created a blank one. Be sure to fill it out properly."); @@ -91,12 +91,12 @@ namespace BackupManager { LoadConfig(); - if (!Directory.Exists(BackupStore)) + if(!Directory.Exists(BackupStore)) Directory.CreateDirectory(BackupStore); Log(@"Fetching database list..."); - MySqlConnectionStringBuilder connStr = new MySqlConnectionStringBuilder { + MySqlConnectionStringBuilder connStr = new() { Server = Config.MySqlHost, UserID = Config.MySqlUser, Password = Config.MySqlPass, @@ -104,16 +104,16 @@ namespace BackupManager { SslMode = MySqlSslMode.None, }; - List databases = new List(); + List databases = new(); string[] exclude = Config.MySqlExcludeDatabases.Split(' '); - using (MySqlConnection conn = new MySqlConnection(connStr.ToString())) { + using(MySqlConnection conn = new(connStr.ToString())) { conn.Open(); using(MySqlCommand comm = new(@"SET NAMES 'utf8mb4';", conn)) comm.ExecuteNonQuery(); - using(MySqlCommand comm = new MySqlCommand(@"SHOW DATABASES;", conn)) + using(MySqlCommand comm = new(@"SHOW DATABASES;", conn)) using(MySqlDataReader read = comm.ExecuteReader()) { while(read.Read()) { string database = read.GetString(0); @@ -130,37 +130,37 @@ namespace BackupManager { string archivePath = Path.GetTempFileName(); - using (FileStream fs = File.OpenWrite(archivePath)) - using (ZipArchive archive = new ZipArchive(fs, ZipArchiveMode.Create)) { + using(FileStream fs = File.OpenWrite(archivePath)) + using(ZipArchive archive = new(fs, ZipArchiveMode.Create)) { Log(@"Database backup..."); string sqldefaults = Path.GetTempFileName(); - using (FileStream sqlConfFs = File.Open(sqldefaults, FileMode.Open, FileAccess.ReadWrite)) - using (StreamWriter sw = new StreamWriter(sqlConfFs)) { + using(FileStream sqlConfFs = File.Open(sqldefaults, FileMode.Open, FileAccess.ReadWrite)) + using(StreamWriter sw = new(sqlConfFs)) { sw.WriteLine(@"[client]"); sw.WriteLine($@"user={Config.MySqlUser}"); sw.WriteLine($@"password={Config.MySqlPass}"); sw.WriteLine(@"default-character-set=utf8mb4"); } - foreach (string database in databases) + foreach(string database in databases) CreateDbDump(archive, sqldefaults, database); Log($@"MariaDB dump done."); File.Delete(sqldefaults); - if (!string.IsNullOrWhiteSpace(Config.MisuzuPath) + if(!string.IsNullOrWhiteSpace(Config.MisuzuPath) && Directory.Exists(Config.MisuzuPath)) { Log(@"Filesystem backup..."); string mszConfig = Path.Combine(Config.MisuzuPath, @"config/config.ini"); - if (!File.Exists(mszConfig)) + if(!File.Exists(mszConfig)) Error(@"Could not find Misuzu config."); string mszStore = Path.Combine(Config.MisuzuPath, @"store"); - if (!Directory.Exists(mszStore)) + if(!Directory.Exists(mszStore)) Error(@"Could not find Misuzu storage directory."); CreateMisuzuDataBackup(archive, mszConfig, mszStore); @@ -181,8 +181,8 @@ namespace BackupManager { } public static void Log(object line, bool forceSatori = false) { - if (!Headless) { - if (sw?.IsRunning == true) { + if(!Headless) { + if(sw?.IsRunning == true) { ConsoleColor fg = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(sw.ElapsedMilliseconds.ToString().PadRight(10)); @@ -192,12 +192,12 @@ namespace BackupManager { Console.WriteLine(line); } - if (forceSatori || (!Headless && !(Config?.SatoriErrorsOnly ?? true))) + if(forceSatori || (!Headless && !(Config?.SatoriErrorsOnly ?? true))) SatoriBroadcast(line.ToString()); } public static void Error(object line, int exit = 0x00DEAD00) { - if (!Headless) { + if(!Headless) { Console.ForegroundColor = ConsoleColor.Red; Log(line); Console.ResetColor(); @@ -219,7 +219,7 @@ namespace BackupManager { string[] storeFiles = Directory.GetFiles(storePath, @"*", SearchOption.AllDirectories); - foreach (string file in storeFiles) + foreach(string file in storeFiles) archive.CreateEntryFromFile( file, @"misuzu/store/" + file.Replace(storePath, string.Empty).WindowsToUnixPath().Trim('/'), @@ -232,7 +232,7 @@ namespace BackupManager { string sqldump = Path.GetTempFileName(); - StringBuilder mysqldumpArgs = new StringBuilder(); + StringBuilder mysqldumpArgs = new(); mysqldumpArgs.AppendFormat(@"--defaults-file={0} ", defaults); mysqldumpArgs.Append(@"--single-transaction "); mysqldumpArgs.Append(@"--tz-utc --triggers "); @@ -262,7 +262,7 @@ namespace BackupManager { } public static void SatoriBroadcast(string text, bool error = false) { - if (string.IsNullOrEmpty(text) + if(string.IsNullOrEmpty(text) || Config == null || string.IsNullOrWhiteSpace(Config.SatoriHost) || string.IsNullOrWhiteSpace(Config.SatoriSecret) @@ -282,7 +282,7 @@ namespace BackupManager { } } - if (ip == null) + if(ip == null) return; EndPoint endPoint = new IPEndPoint(ip, Config.SatoriPort); diff --git a/do-backup.sh b/do-backup.sh index 4af1438..a8c048d 100755 --- a/do-backup.sh +++ b/do-backup.sh @@ -1,5 +1,2 @@ -cwd="$(pwd)" export DOTNET_CLI_TELEMETRY_OPTOUT=1 -cd "$(dirname "$0")" dotnet run --project BackupManager -c Release -f net6.0 -- -cron -cd "$cwd"