just store backups locally for download
This commit is contained in:
parent
9365f34399
commit
33b50e9323
4 changed files with 16 additions and 141 deletions
|
@ -1,12 +1,8 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="SSH.NET" Version="2016.1.0" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -2,16 +2,6 @@
|
||||||
{
|
{
|
||||||
public class Config
|
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 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";
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
using Renci.SshNet;
|
using System;
|
||||||
using Renci.SshNet.Common;
|
|
||||||
using System;
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
|
@ -10,7 +8,6 @@ using System.Net.Sockets;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
|
|
||||||
|
@ -44,10 +41,6 @@ namespace BackupManager
|
||||||
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), @"Backups")
|
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), @"Backups")
|
||||||
: Config.FileSystemPathV2;
|
: Config.FileSystemPathV2;
|
||||||
|
|
||||||
private static object BackupStorage;
|
|
||||||
|
|
||||||
private static SftpClient SFTP;
|
|
||||||
|
|
||||||
public static bool Headless;
|
public static bool Headless;
|
||||||
|
|
||||||
public static string WindowsToUnixPath(this string path)
|
public static string WindowsToUnixPath(this string path)
|
||||||
|
@ -107,75 +100,18 @@ namespace BackupManager
|
||||||
|
|
||||||
LoadConfig();
|
LoadConfig();
|
||||||
|
|
||||||
switch (Config.StorageMethod)
|
if (!Directory.Exists(FSPath))
|
||||||
{
|
Directory.CreateDirectory(FSPath);
|
||||||
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();
|
|
||||||
|
|
||||||
Log(@"Database backup...");
|
Log(@"Database backup...");
|
||||||
|
|
||||||
string sqldump = CreateMySqlDump();
|
string sqldump = CreateDbDump();
|
||||||
|
|
||||||
using (Stream s = File.OpenRead(sqldump))
|
using (Stream s = File.OpenRead(sqldump))
|
||||||
using (Stream g = GZipEncodeStream(s))
|
using (Stream g = GZipEncodeStream(s))
|
||||||
{
|
{
|
||||||
object f = Upload(DatabaseDumpName, @"application/sql+gzip", g);
|
Upload(DatabaseDumpName, g);
|
||||||
|
Log($@"MariaDB dump saved.");
|
||||||
switch (f)
|
|
||||||
{
|
|
||||||
default:
|
|
||||||
Log($@"MySQL dump uploaded.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
File.Delete(sqldump);
|
File.Delete(sqldump);
|
||||||
|
@ -197,14 +133,8 @@ namespace BackupManager
|
||||||
|
|
||||||
using (FileStream fs = File.OpenRead(archivePath))
|
using (FileStream fs = File.OpenRead(archivePath))
|
||||||
{
|
{
|
||||||
object f = Upload(UserDataName, @"application/zip", fs);
|
Upload(UserDataName, fs);
|
||||||
|
Log($@"Misuzu data saved.");
|
||||||
switch (f)
|
|
||||||
{
|
|
||||||
default:
|
|
||||||
Log($@"Misuzu data uploaded.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
File.Delete(archivePath);
|
File.Delete(archivePath);
|
||||||
|
@ -256,25 +186,14 @@ namespace BackupManager
|
||||||
Environment.Exit(exit);
|
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)
|
string filename = Path.Combine(FSPath, name);
|
||||||
{
|
|
||||||
case StorageMethod.Sftp:
|
|
||||||
SFTP.UploadFile(stream, (BackupStorage as string) + @"/" + name);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case StorageMethod.FileSystem:
|
using (FileStream fs = File.OpenWrite(filename))
|
||||||
string filename = Path.Combine(BackupStorage as string, name);
|
stream.CopyTo(fs);
|
||||||
|
|
||||||
using (FileStream fs = File.OpenWrite(filename))
|
|
||||||
stream.CopyTo(fs);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string CreateMisuzuDataBackup(string configPath, string storePath)
|
public static string CreateMisuzuDataBackup(string configPath, string storePath)
|
||||||
|
@ -301,9 +220,9 @@ namespace BackupManager
|
||||||
return tmpName;
|
return tmpName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string CreateMySqlDump()
|
public static string CreateDbDump()
|
||||||
{
|
{
|
||||||
Log(@"Dumping MySQL Databases...");
|
Log(@"Dumping MariaDB Databases...");
|
||||||
string sqldefaults = Path.GetTempFileName();
|
string sqldefaults = Path.GetTempFileName();
|
||||||
|
|
||||||
using (FileStream fs = File.Open(sqldefaults, FileMode.Open, FileAccess.ReadWrite))
|
using (FileStream fs = File.Open(sqldefaults, FileMode.Open, FileAccess.ReadWrite))
|
||||||
|
@ -358,27 +277,6 @@ namespace BackupManager
|
||||||
return output;
|
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)
|
public static void SatoriBroadcast(string text, bool error = false)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(text)
|
if (string.IsNullOrEmpty(text)
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
namespace BackupManager
|
|
||||||
{
|
|
||||||
public enum StorageMethod
|
|
||||||
{
|
|
||||||
//GoogleDrive = 1,
|
|
||||||
Sftp = 2,
|
|
||||||
FileSystem = 4,
|
|
||||||
}
|
|
||||||
}
|
|
Reference in a new issue