From 33b50e93232018988c8f8a492107e1e77dd3fabb Mon Sep 17 00:00:00 2001 From: Julian van de Groep Date: Sat, 17 Aug 2019 01:44:53 +0200 Subject: [PATCH] just store backups locally for download --- BackupManager/BackupManager.csproj | 6 +- BackupManager/Config.cs | 10 --- BackupManager/Program.cs | 132 ++++------------------------- BackupManager/StorageMethod.cs | 9 -- 4 files changed, 16 insertions(+), 141 deletions(-) delete mode 100644 BackupManager/StorageMethod.cs diff --git a/BackupManager/BackupManager.csproj b/BackupManager/BackupManager.csproj index 47c17c2..2eba7d9 100644 --- a/BackupManager/BackupManager.csproj +++ b/BackupManager/BackupManager.csproj @@ -1,12 +1,8 @@ - + Exe netcoreapp2.2 - - - - diff --git a/BackupManager/Config.cs b/BackupManager/Config.cs index c4caeae..669c507 100644 --- a/BackupManager/Config.cs +++ b/BackupManager/Config.cs @@ -2,16 +2,6 @@ { public class Config { - public StorageMethod StorageMethod { get; set; } = StorageMethod.Sftp; - - public string SftpHost { get; set; } - public ushort SftpPort { get; set; } - public string SftpUsername { get; set; } - public string SftpPassphrase { get; set; } - public string SftpPrivateKey { get; set; } - public string SftpBackupDirectoryPath { get; set; } - public string SftpTrustedHost { get; set; } - 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 ea72d22..83b17d5 100644 --- a/BackupManager/Program.cs +++ b/BackupManager/Program.cs @@ -1,6 +1,4 @@ -using Renci.SshNet; -using Renci.SshNet.Common; -using System; +using System; using System.Diagnostics; using System.IO; using System.IO.Compression; @@ -10,7 +8,6 @@ using System.Net.Sockets; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; -using System.Threading; using System.Xml; using System.Xml.Serialization; @@ -44,10 +41,6 @@ namespace BackupManager ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), @"Backups") : Config.FileSystemPathV2; - private static object BackupStorage; - - private static SftpClient SFTP; - public static bool Headless; public static string WindowsToUnixPath(this string path) @@ -107,75 +100,18 @@ namespace BackupManager LoadConfig(); - switch (Config.StorageMethod) - { - case StorageMethod.Sftp: - if (string.IsNullOrWhiteSpace(Config.SftpHost) || string.IsNullOrWhiteSpace(Config.SftpUsername)) - { - sw.Stop(); - Config.SftpHost = Config.SftpHost ?? @""; - Config.SftpPort = Config.SftpPort < 1 ? (ushort)22 : Config.SftpPort; - Config.SftpUsername = Config.SftpUsername ?? @""; - Config.SftpPassphrase = Config.SftpPassphrase ?? @""; - Config.SftpPrivateKey = Config.SftpPrivateKey ?? @""; - Config.SftpTrustedHost = Config.SftpTrustedHost ?? @""; - Config.SftpBackupDirectoryPath = Config.SftpBackupDirectoryPath ?? @""; - SaveConfig(); - Error(@"No Sftp host/auth details found in the configuration."); - } - - if (!string.IsNullOrEmpty(Config.SftpPrivateKey)) - SFTP = new SftpClient(Config.SftpHost, Config.SftpPort, Config.SftpUsername, new PrivateKeyFile(Config.SftpPrivateKey, Config.SftpPassphrase ?? string.Empty)); - else - SFTP = new SftpClient(Config.SftpHost, Config.SftpPort, Config.SftpUsername, Config.SftpPassphrase ?? string.Empty); - - using (ManualResetEvent mre = new ManualResetEvent(false)) - { - if (!string.IsNullOrWhiteSpace(Config.SftpTrustedHost)) - SFTP.HostKeyReceived += (s, e) => - { - string checkString = e.HostKeyName + @"#" + Convert.ToBase64String(e.HostKey) + @"#" + Convert.ToBase64String(e.FingerPrint); - e.CanTrust = Config.SftpTrustedHost.SequenceEqual(checkString); - mre.Set(); - }; - else - mre.Set(); - - try - { - SFTP.Connect(); - } catch (SshConnectionException) - { - Error(@"Error during SFTP connect, it's possible the server key changed."); - } - - mre.WaitOne(); - } - break; - - case StorageMethod.FileSystem: - if (!Directory.Exists(FSPath)) - Directory.CreateDirectory(FSPath); - break; - } - - GetBackupStorage(); + if (!Directory.Exists(FSPath)) + Directory.CreateDirectory(FSPath); Log(@"Database backup..."); - string sqldump = CreateMySqlDump(); + string sqldump = CreateDbDump(); using (Stream s = File.OpenRead(sqldump)) using (Stream g = GZipEncodeStream(s)) { - object f = Upload(DatabaseDumpName, @"application/sql+gzip", g); - - switch (f) - { - default: - Log($@"MySQL dump uploaded."); - break; - } + Upload(DatabaseDumpName, g); + Log($@"MariaDB dump saved."); } File.Delete(sqldump); @@ -197,14 +133,8 @@ namespace BackupManager using (FileStream fs = File.OpenRead(archivePath)) { - object f = Upload(UserDataName, @"application/zip", fs); - - switch (f) - { - default: - Log($@"Misuzu data uploaded."); - break; - } + Upload(UserDataName, fs); + Log($@"Misuzu data saved."); } File.Delete(archivePath); @@ -256,25 +186,14 @@ namespace BackupManager Environment.Exit(exit); } - public static object Upload(string name, string type, Stream stream) + public static void Upload(string name, Stream stream) { - Log($@"Uploading '{name}'..."); + Log($@"Saving '{name}'..."); - switch (Config.StorageMethod) - { - case StorageMethod.Sftp: - SFTP.UploadFile(stream, (BackupStorage as string) + @"/" + name); - break; + string filename = Path.Combine(FSPath, name); - case StorageMethod.FileSystem: - string filename = Path.Combine(BackupStorage as string, name); - - using (FileStream fs = File.OpenWrite(filename)) - stream.CopyTo(fs); - break; - } - - return null; + using (FileStream fs = File.OpenWrite(filename)) + stream.CopyTo(fs); } public static string CreateMisuzuDataBackup(string configPath, string storePath) @@ -301,9 +220,9 @@ namespace BackupManager return tmpName; } - public static string CreateMySqlDump() + public static string CreateDbDump() { - Log(@"Dumping MySQL Databases..."); + Log(@"Dumping MariaDB Databases..."); string sqldefaults = Path.GetTempFileName(); using (FileStream fs = File.Open(sqldefaults, FileMode.Open, FileAccess.ReadWrite)) @@ -358,27 +277,6 @@ namespace BackupManager return output; } - public static void GetBackupStorage(string name = null) - { - switch (Config.StorageMethod) - { - case StorageMethod.Sftp: - string directory = (BackupStorage = name ?? Config.SftpBackupDirectoryPath) as string; - try - { - SFTP.ListDirectory(directory); - } catch (SftpPathNotFoundException) - { - SFTP.CreateDirectory(directory); - } - break; - - case StorageMethod.FileSystem: - BackupStorage = name ?? FSPath; - break; - } - } - public static void SatoriBroadcast(string text, bool error = false) { if (string.IsNullOrEmpty(text) diff --git a/BackupManager/StorageMethod.cs b/BackupManager/StorageMethod.cs deleted file mode 100644 index e83a2c6..0000000 --- a/BackupManager/StorageMethod.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace BackupManager -{ - public enum StorageMethod - { - //GoogleDrive = 1, - Sftp = 2, - FileSystem = 4, - } -}