Retarget to .NET 6.0

This commit is contained in:
flash 2023-04-12 04:26:00 +02:00
parent cbe982ba77
commit 1f9ac0552a
4 changed files with 37 additions and 42 deletions

View file

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View file

@ -1,7 +1,5 @@
namespace BackupManager namespace BackupManager {
{ public class Config {
public class Config
{
public string FileSystemPathV2 { get; set; } public string FileSystemPathV2 { get; set; }
public string MySqlDumpPathWindows { get; set; } = @"C:\Program Files\MariaDB 10.3\bin\mysqldump.exe"; public string MySqlDumpPathWindows { get; set; } = @"C:\Program Files\MariaDB 10.3\bin\mysqldump.exe";

View file

@ -15,7 +15,7 @@ using System.Xml.Serialization;
namespace BackupManager { namespace BackupManager {
public static class Program { 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"; private const string CONFIG_NAME = @"FlashiiBackupManager.v1.xml";
@ -39,17 +39,17 @@ namespace BackupManager {
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), @"Backups") ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), @"Backups")
: Config.FileSystemPathV2; : Config.FileSystemPathV2;
public static bool Headless; private static bool Headless;
public static string WindowsToUnixPath(this string path) { public static string WindowsToUnixPath(this string path) {
return IsWindows ? path.Replace('\\', '/') : path; return IsWindows ? path.Replace('\\', '/') : path;
} }
public static Stream ToXml(this object obj, bool pretty = false) { public static Stream ToXml(this object obj, bool pretty = false) {
MemoryStream ms = new MemoryStream(); MemoryStream ms = new();
XmlSerializer xs = new XmlSerializer(obj.GetType()); 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); xs.Serialize(xw, obj);
ms.Seek(0, SeekOrigin.Begin); ms.Seek(0, SeekOrigin.Begin);
@ -57,23 +57,23 @@ namespace BackupManager {
} }
public static T FromXml<T>(Stream xml) { public static T FromXml<T>(Stream xml) {
if (xml.CanSeek) if(xml.CanSeek)
xml.Seek(0, SeekOrigin.Begin); xml.Seek(0, SeekOrigin.Begin);
XmlSerializer xs = new XmlSerializer(typeof(T)); XmlSerializer xs = new(typeof(T));
return (T)xs.Deserialize(xml); return (T)xs.Deserialize(xml);
} }
public static void SaveConfig() { public static void SaveConfig() {
Log(@"Saving configuration..."); Log(@"Saving configuration...");
using (FileStream fs = new FileStream(ConfigPath, FileMode.Create, FileAccess.Write)) using FileStream fs = new(ConfigPath, FileMode.Create, FileAccess.Write);
using (Stream cs = Config.ToXml(true)) using Stream cs = Config.ToXml(true);
cs.CopyTo(fs); cs.CopyTo(fs);
} }
public static void LoadConfig() { public static void LoadConfig() {
Log(@"Loading configuration..."); Log(@"Loading configuration...");
using (FileStream fs = File.OpenRead(ConfigPath)) using FileStream fs = File.OpenRead(ConfigPath);
Config = FromXml<Config>(fs); Config = FromXml<Config>(fs);
} }
@ -83,7 +83,7 @@ namespace BackupManager {
Log(@"Flashii Backup Manager"); Log(@"Flashii Backup Manager");
sw.Start(); sw.Start();
if (!File.Exists(ConfigPath)) { if(!File.Exists(ConfigPath)) {
Config = new Config(); Config = new Config();
SaveConfig(); SaveConfig();
Error(@"No configuration file exists, created a blank one. Be sure to fill it out properly."); Error(@"No configuration file exists, created a blank one. Be sure to fill it out properly.");
@ -91,12 +91,12 @@ namespace BackupManager {
LoadConfig(); LoadConfig();
if (!Directory.Exists(BackupStore)) if(!Directory.Exists(BackupStore))
Directory.CreateDirectory(BackupStore); Directory.CreateDirectory(BackupStore);
Log(@"Fetching database list..."); Log(@"Fetching database list...");
MySqlConnectionStringBuilder connStr = new MySqlConnectionStringBuilder { MySqlConnectionStringBuilder connStr = new() {
Server = Config.MySqlHost, Server = Config.MySqlHost,
UserID = Config.MySqlUser, UserID = Config.MySqlUser,
Password = Config.MySqlPass, Password = Config.MySqlPass,
@ -104,16 +104,16 @@ namespace BackupManager {
SslMode = MySqlSslMode.None, SslMode = MySqlSslMode.None,
}; };
List<string> databases = new List<string>(); List<string> databases = new();
string[] exclude = Config.MySqlExcludeDatabases.Split(' '); string[] exclude = Config.MySqlExcludeDatabases.Split(' ');
using (MySqlConnection conn = new MySqlConnection(connStr.ToString())) { using(MySqlConnection conn = new(connStr.ToString())) {
conn.Open(); conn.Open();
using(MySqlCommand comm = new(@"SET NAMES 'utf8mb4';", conn)) using(MySqlCommand comm = new(@"SET NAMES 'utf8mb4';", conn))
comm.ExecuteNonQuery(); comm.ExecuteNonQuery();
using(MySqlCommand comm = new MySqlCommand(@"SHOW DATABASES;", conn)) using(MySqlCommand comm = new(@"SHOW DATABASES;", conn))
using(MySqlDataReader read = comm.ExecuteReader()) { using(MySqlDataReader read = comm.ExecuteReader()) {
while(read.Read()) { while(read.Read()) {
string database = read.GetString(0); string database = read.GetString(0);
@ -130,37 +130,37 @@ namespace BackupManager {
string archivePath = Path.GetTempFileName(); string archivePath = Path.GetTempFileName();
using (FileStream fs = File.OpenWrite(archivePath)) using(FileStream fs = File.OpenWrite(archivePath))
using (ZipArchive archive = new ZipArchive(fs, ZipArchiveMode.Create)) { using(ZipArchive archive = new(fs, ZipArchiveMode.Create)) {
Log(@"Database backup..."); Log(@"Database backup...");
string sqldefaults = Path.GetTempFileName(); string sqldefaults = Path.GetTempFileName();
using (FileStream sqlConfFs = File.Open(sqldefaults, FileMode.Open, FileAccess.ReadWrite)) using(FileStream sqlConfFs = File.Open(sqldefaults, FileMode.Open, FileAccess.ReadWrite))
using (StreamWriter sw = new StreamWriter(sqlConfFs)) { using(StreamWriter sw = new(sqlConfFs)) {
sw.WriteLine(@"[client]"); sw.WriteLine(@"[client]");
sw.WriteLine($@"user={Config.MySqlUser}"); sw.WriteLine($@"user={Config.MySqlUser}");
sw.WriteLine($@"password={Config.MySqlPass}"); sw.WriteLine($@"password={Config.MySqlPass}");
sw.WriteLine(@"default-character-set=utf8mb4"); sw.WriteLine(@"default-character-set=utf8mb4");
} }
foreach (string database in databases) foreach(string database in databases)
CreateDbDump(archive, sqldefaults, database); CreateDbDump(archive, sqldefaults, database);
Log($@"MariaDB dump done."); Log($@"MariaDB dump done.");
File.Delete(sqldefaults); File.Delete(sqldefaults);
if (!string.IsNullOrWhiteSpace(Config.MisuzuPath) if(!string.IsNullOrWhiteSpace(Config.MisuzuPath)
&& Directory.Exists(Config.MisuzuPath)) { && Directory.Exists(Config.MisuzuPath)) {
Log(@"Filesystem backup..."); Log(@"Filesystem backup...");
string mszConfig = Path.Combine(Config.MisuzuPath, @"config/config.ini"); string mszConfig = Path.Combine(Config.MisuzuPath, @"config/config.ini");
if (!File.Exists(mszConfig)) if(!File.Exists(mszConfig))
Error(@"Could not find Misuzu config."); Error(@"Could not find Misuzu config.");
string mszStore = Path.Combine(Config.MisuzuPath, @"store"); string mszStore = Path.Combine(Config.MisuzuPath, @"store");
if (!Directory.Exists(mszStore)) if(!Directory.Exists(mszStore))
Error(@"Could not find Misuzu storage directory."); Error(@"Could not find Misuzu storage directory.");
CreateMisuzuDataBackup(archive, mszConfig, mszStore); CreateMisuzuDataBackup(archive, mszConfig, mszStore);
@ -181,8 +181,8 @@ namespace BackupManager {
} }
public static void Log(object line, bool forceSatori = false) { public static void Log(object line, bool forceSatori = false) {
if (!Headless) { if(!Headless) {
if (sw?.IsRunning == true) { if(sw?.IsRunning == true) {
ConsoleColor fg = Console.ForegroundColor; ConsoleColor fg = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(sw.ElapsedMilliseconds.ToString().PadRight(10)); Console.Write(sw.ElapsedMilliseconds.ToString().PadRight(10));
@ -192,12 +192,12 @@ namespace BackupManager {
Console.WriteLine(line); Console.WriteLine(line);
} }
if (forceSatori || (!Headless && !(Config?.SatoriErrorsOnly ?? true))) if(forceSatori || (!Headless && !(Config?.SatoriErrorsOnly ?? true)))
SatoriBroadcast(line.ToString()); SatoriBroadcast(line.ToString());
} }
public static void Error(object line, int exit = 0x00DEAD00) { public static void Error(object line, int exit = 0x00DEAD00) {
if (!Headless) { if(!Headless) {
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
Log(line); Log(line);
Console.ResetColor(); Console.ResetColor();
@ -219,7 +219,7 @@ namespace BackupManager {
string[] storeFiles = Directory.GetFiles(storePath, @"*", SearchOption.AllDirectories); string[] storeFiles = Directory.GetFiles(storePath, @"*", SearchOption.AllDirectories);
foreach (string file in storeFiles) foreach(string file in storeFiles)
archive.CreateEntryFromFile( archive.CreateEntryFromFile(
file, file,
@"misuzu/store/" + file.Replace(storePath, string.Empty).WindowsToUnixPath().Trim('/'), @"misuzu/store/" + file.Replace(storePath, string.Empty).WindowsToUnixPath().Trim('/'),
@ -232,7 +232,7 @@ namespace BackupManager {
string sqldump = Path.GetTempFileName(); string sqldump = Path.GetTempFileName();
StringBuilder mysqldumpArgs = new StringBuilder(); StringBuilder mysqldumpArgs = new();
mysqldumpArgs.AppendFormat(@"--defaults-file={0} ", defaults); mysqldumpArgs.AppendFormat(@"--defaults-file={0} ", defaults);
mysqldumpArgs.Append(@"--single-transaction "); mysqldumpArgs.Append(@"--single-transaction ");
mysqldumpArgs.Append(@"--tz-utc --triggers "); mysqldumpArgs.Append(@"--tz-utc --triggers ");
@ -262,7 +262,7 @@ namespace BackupManager {
} }
public static void SatoriBroadcast(string text, bool error = false) { public static void SatoriBroadcast(string text, bool error = false) {
if (string.IsNullOrEmpty(text) if(string.IsNullOrEmpty(text)
|| Config == null || Config == null
|| string.IsNullOrWhiteSpace(Config.SatoriHost) || string.IsNullOrWhiteSpace(Config.SatoriHost)
|| string.IsNullOrWhiteSpace(Config.SatoriSecret) || string.IsNullOrWhiteSpace(Config.SatoriSecret)
@ -282,7 +282,7 @@ namespace BackupManager {
} }
} }
if (ip == null) if(ip == null)
return; return;
EndPoint endPoint = new IPEndPoint(ip, Config.SatoriPort); EndPoint endPoint = new IPEndPoint(ip, Config.SatoriPort);

View file

@ -1,5 +1,2 @@
cwd="$(pwd)"
export DOTNET_CLI_TELEMETRY_OPTOUT=1 export DOTNET_CLI_TELEMETRY_OPTOUT=1
cd "$(dirname "$0")"
dotnet run --project BackupManager -c Release -f net6.0 -- -cron dotnet run --project BackupManager -c Release -f net6.0 -- -cron
cd "$cwd"