Compare commits
7 commits
Author | SHA1 | Date | |
---|---|---|---|
9178cd5e14 | |||
866bd93afa | |||
b6656506ee | |||
b793af9157 | |||
dd7b63fc24 | |||
1f9ac0552a | |||
cbe982ba77 |
4 changed files with 43 additions and 158 deletions
|
@ -2,11 +2,11 @@
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MySql.Data" Version="8.0.26" />
|
<PackageReference Include="MySql.Data" Version="8.1.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,21 +1,11 @@
|
||||||
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 MySqlDumpPath { get; set; } = @"mysqldump";
|
public string MySqlDumpPath { get; set; } = @"mysqldump";
|
||||||
public string MySqlHost { get; set; } = @"localhost";
|
public string MySqlHost { get; set; } = @"localhost";
|
||||||
public string MySqlUser { get; set; }
|
public string MySqlUser { get; set; }
|
||||||
public string MySqlPass { get; set; }
|
public string MySqlPass { get; set; }
|
||||||
public string MySqlExcludeDatabases { get; set; } = @"mysql information_schema performance_schema";
|
public string MySqlExcludeDatabases { get; set; } = @"mysql information_schema performance_schema";
|
||||||
|
|
||||||
public string MisuzuPath { get; set; }
|
|
||||||
|
|
||||||
public string SatoriHost { get; set; }
|
|
||||||
public ushort SatoriPort { get; set; }
|
|
||||||
public string SatoriSecret { get; set; }
|
|
||||||
public bool SatoriErrorsOnly { get; set; } = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,23 +5,17 @@ using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
|
||||||
using System.Net.Sockets;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Security.Cryptography;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
using System.Xml.Serialization;
|
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";
|
||||||
|
|
||||||
public static bool IsWindows
|
|
||||||
=> RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
|
||||||
|
|
||||||
public readonly static DateTimeOffset Startup = DateTimeOffset.UtcNow;
|
public readonly static DateTimeOffset Startup = DateTimeOffset.UtcNow;
|
||||||
|
|
||||||
public static string Basename
|
public static string Basename
|
||||||
|
@ -39,17 +33,13 @@ 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) {
|
|
||||||
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,24 +47,24 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Main(string[] args) {
|
public static void Main(string[] args) {
|
||||||
|
@ -83,7 +73,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,29 +81,29 @@ 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,
|
||||||
CharacterSet = @"utf8mb4",
|
CharacterSet = @"utf8mb4",
|
||||||
SslMode = MySqlSslMode.None,
|
SslMode = MySqlSslMode.Disabled,
|
||||||
};
|
};
|
||||||
|
|
||||||
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,41 +120,25 @@ 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)
|
|
||||||
&& Directory.Exists(Config.MisuzuPath)) {
|
|
||||||
Log(@"Filesystem backup...");
|
|
||||||
string mszConfig = Path.Combine(Config.MisuzuPath, @"config/config.ini");
|
|
||||||
|
|
||||||
if (!File.Exists(mszConfig))
|
|
||||||
Error(@"Could not find Misuzu config.");
|
|
||||||
|
|
||||||
string mszStore = Path.Combine(Config.MisuzuPath, @"store");
|
|
||||||
|
|
||||||
if (!Directory.Exists(mszStore))
|
|
||||||
Error(@"Could not find Misuzu storage directory.");
|
|
||||||
|
|
||||||
CreateMisuzuDataBackup(archive, mszConfig, mszStore);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string targetPath = Path.Combine(BackupStore, BackupName);
|
string targetPath = Path.Combine(BackupStore, BackupName);
|
||||||
|
@ -173,16 +147,16 @@ namespace BackupManager {
|
||||||
|
|
||||||
SaveConfig();
|
SaveConfig();
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
Log($@"Done! Took {sw.Elapsed}.", true);
|
Log($@"Done! Took {sw.Elapsed}.");
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Log(object line, bool forceSatori = false) {
|
public static void Log(object line) {
|
||||||
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));
|
||||||
|
@ -191,20 +165,15 @@ namespace BackupManager {
|
||||||
|
|
||||||
Console.WriteLine(line);
|
Console.WriteLine(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (forceSatori || (!Headless && !(Config?.SatoriErrorsOnly ?? true)))
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
SatoriBroadcast(line.ToString(), true);
|
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
#endif
|
#endif
|
||||||
|
@ -212,27 +181,12 @@ namespace BackupManager {
|
||||||
Environment.Exit(exit);
|
Environment.Exit(exit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void CreateMisuzuDataBackup(ZipArchive archive, string configPath, string storePath) {
|
|
||||||
Log(@"Storing non-volatile Misuzu data...");
|
|
||||||
|
|
||||||
archive.CreateEntryFromFile(configPath, @"misuzu/config/config.ini", CompressionLevel.Optimal);
|
|
||||||
|
|
||||||
string[] storeFiles = Directory.GetFiles(storePath, @"*", SearchOption.AllDirectories);
|
|
||||||
|
|
||||||
foreach (string file in storeFiles)
|
|
||||||
archive.CreateEntryFromFile(
|
|
||||||
file,
|
|
||||||
@"misuzu/store/" + file.Replace(storePath, string.Empty).WindowsToUnixPath().Trim('/'),
|
|
||||||
CompressionLevel.Optimal
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void CreateDbDump(ZipArchive archive, string defaults, string database) {
|
public static void CreateDbDump(ZipArchive archive, string defaults, string database) {
|
||||||
Log($@"Dumping {database}...");
|
Log($@"Dumping {database}...");
|
||||||
|
|
||||||
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 ");
|
||||||
|
@ -248,7 +202,7 @@ namespace BackupManager {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Process p = Process.Start(new ProcessStartInfo {
|
Process p = Process.Start(new ProcessStartInfo {
|
||||||
FileName = IsWindows ? Config.MySqlDumpPathWindows : Config.MySqlDumpPath,
|
FileName = Config.MySqlDumpPath,
|
||||||
Arguments = mysqldumpArgs.ToString(),
|
Arguments = mysqldumpArgs.ToString(),
|
||||||
UseShellExecute = false,
|
UseShellExecute = false,
|
||||||
CreateNoWindow = true,
|
CreateNoWindow = true,
|
||||||
|
@ -260,66 +214,5 @@ namespace BackupManager {
|
||||||
|
|
||||||
File.Delete(sqldump);
|
File.Delete(sqldump);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SatoriBroadcast(string text, bool error = false) {
|
|
||||||
if (string.IsNullOrEmpty(text)
|
|
||||||
|| Config == null
|
|
||||||
|| string.IsNullOrWhiteSpace(Config.SatoriHost)
|
|
||||||
|| string.IsNullOrWhiteSpace(Config.SatoriSecret)
|
|
||||||
|| Config.SatoriPort < 1)
|
|
||||||
return;
|
|
||||||
|
|
||||||
IPAddress ip = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
ip = IPAddress.Parse(Config.SatoriHost);
|
|
||||||
} catch {
|
|
||||||
try {
|
|
||||||
// forcing IPv4 here, it seems to explode with IPv6 and i don't really want to figure out why
|
|
||||||
ip = Dns.GetHostAddresses(Config.SatoriHost).FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);
|
|
||||||
} catch {
|
|
||||||
ip = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ip == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
EndPoint endPoint = new IPEndPoint(ip, Config.SatoriPort);
|
|
||||||
|
|
||||||
StringBuilder textBuilder = new StringBuilder();
|
|
||||||
textBuilder.AppendFormat(@"[b]Backup System[/b] [{0}]: ", Environment.MachineName);
|
|
||||||
|
|
||||||
if (error)
|
|
||||||
textBuilder.Append(@"[color=red]");
|
|
||||||
|
|
||||||
textBuilder.Append(text);
|
|
||||||
|
|
||||||
if (error)
|
|
||||||
textBuilder.Append(@"[/color]");
|
|
||||||
|
|
||||||
text = textBuilder.ToString();
|
|
||||||
|
|
||||||
StringBuilder messageBuilder = new StringBuilder();
|
|
||||||
|
|
||||||
using (HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(Config.SatoriSecret))) {
|
|
||||||
byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(text));
|
|
||||||
|
|
||||||
foreach (byte b in hash)
|
|
||||||
messageBuilder.AppendFormat(@"{0:x2}", b);
|
|
||||||
}
|
|
||||||
|
|
||||||
messageBuilder.Append(text);
|
|
||||||
string message = messageBuilder.ToString();
|
|
||||||
byte[] messageBytes = new byte[Encoding.UTF8.GetByteCount(message) + 2];
|
|
||||||
messageBytes[0] = messageBytes[messageBytes.Length - 1] = 0x0F;
|
|
||||||
Encoding.UTF8.GetBytes(message).CopyTo(messageBytes, 1);
|
|
||||||
|
|
||||||
using (Socket sock = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) {
|
|
||||||
sock.NoDelay = sock.Blocking = true;
|
|
||||||
sock.Connect(endPoint);
|
|
||||||
sock.Send(messageBytes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
do-backup.sh
10
do-backup.sh
|
@ -1,5 +1,7 @@
|
||||||
cwd="$(pwd)"
|
cwd="$(pwd)"
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
|
||||||
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 net5.0 -- -cron
|
|
||||||
cd "$cwd"
|
cd "$cwd"
|
||||||
|
|
Reference in a new issue