Merge pull request #126 from Moonlight-Panel/AddDatabaseCheckup
Implemented database checkup
This commit is contained in:
80
Moonlight/App/Helpers/DatabaseCheckup.cs
Normal file
80
Moonlight/App/Helpers/DatabaseCheckup.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System.Diagnostics;
|
||||
using Logging.Net;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moonlight.App.Database;
|
||||
using Moonlight.App.Services;
|
||||
using Moonlight.App.Services.Files;
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
namespace Moonlight.App.Helpers;
|
||||
|
||||
public class DatabaseCheckup
|
||||
{
|
||||
public static void Perform()
|
||||
{
|
||||
var context = new DataContext(new ConfigService(new StorageService()));
|
||||
|
||||
Logger.Info("Checking database");
|
||||
|
||||
Logger.Info("Checking for pending migrations");
|
||||
|
||||
var migrations = context.Database
|
||||
.GetPendingMigrations()
|
||||
.ToArray();
|
||||
|
||||
if (migrations.Any())
|
||||
{
|
||||
Logger.Info($"{migrations.Length} migrations pending. Updating now");
|
||||
|
||||
BackupDatabase();
|
||||
|
||||
Logger.Info("Applying migrations");
|
||||
|
||||
context.Database.Migrate();
|
||||
|
||||
Logger.Info("Successfully applied migrations");
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Info("Database is up-to-date. No migrations have been performed");
|
||||
}
|
||||
}
|
||||
|
||||
public static void BackupDatabase()
|
||||
{
|
||||
Logger.Info("Creating backup from database");
|
||||
|
||||
var configService = new ConfigService(new StorageService());
|
||||
var dateTimeService = new DateTimeService();
|
||||
|
||||
var config = configService
|
||||
.GetSection("Moonlight")
|
||||
.GetSection("Database");
|
||||
|
||||
var connectionString = $"host={config.GetValue<string>("Host")};" +
|
||||
$"port={config.GetValue<int>("Port")};" +
|
||||
$"database={config.GetValue<string>("Database")};" +
|
||||
$"uid={config.GetValue<string>("Username")};" +
|
||||
$"pwd={config.GetValue<string>("Password")}";
|
||||
|
||||
string file = PathBuilder.File("storage", "backups", $"{dateTimeService.GetCurrentUnix()}-mysql.sql");
|
||||
|
||||
Logger.Info($"Saving it to: {file}");
|
||||
Logger.Info("Starting backup...");
|
||||
|
||||
var sw = new Stopwatch();
|
||||
sw.Start();
|
||||
|
||||
using MySqlConnection conn = new MySqlConnection(connectionString);
|
||||
using MySqlCommand cmd = new MySqlCommand();
|
||||
using MySqlBackup mb = new MySqlBackup(cmd);
|
||||
|
||||
cmd.Connection = conn;
|
||||
conn.Open();
|
||||
mb.ExportToFile(file);
|
||||
conn.Close();
|
||||
|
||||
sw.Stop();
|
||||
Logger.Info($"Done. {sw.Elapsed.TotalSeconds}s");
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ public class StorageService
|
||||
Directory.CreateDirectory(PathBuilder.Dir("storage", "uploads"));
|
||||
Directory.CreateDirectory(PathBuilder.Dir("storage", "configs"));
|
||||
Directory.CreateDirectory(PathBuilder.Dir("storage", "resources"));
|
||||
Directory.CreateDirectory(PathBuilder.Dir("storage", "backups"));
|
||||
|
||||
if(IsEmpty(PathBuilder.Dir("storage", "resources")))
|
||||
{
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="MineStat" Version="3.1.1" />
|
||||
<PackageReference Include="MySqlBackup.NET" Version="2.3.8" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3-beta1" />
|
||||
<PackageReference Include="Otp.NET" Version="1.3.0" />
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
|
||||
@@ -73,6 +74,7 @@
|
||||
<Folder Include="App\ApiClients\CloudPanel\Resources\" />
|
||||
<Folder Include="App\ApiClients\Daemon\Requests\" />
|
||||
<Folder Include="App\Http\Middleware" />
|
||||
<Folder Include="storage\backups\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -77,12 +77,21 @@
|
||||
<img alt="Logo" src="@(moonlightConfig.GetValue<string>("AppUrl"))/api/moonlight/resources/images/logo.svg" class="h-25px"/>
|
||||
|
||||
@{
|
||||
var loadingMessage = LoadingMessageRepository.Get().Random();
|
||||
string loadingMessage;
|
||||
|
||||
try
|
||||
{
|
||||
loadingMessage = LoadingMessageRepository.Get().Random().Message;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
loadingMessage = "";
|
||||
}
|
||||
}
|
||||
|
||||
<div class="d-flex align-items-center mt-5">
|
||||
<span class="spinner-border text-primary" role="status"></span>
|
||||
<span class="text-muted fs-6 fw-semibold ms-5">@(loadingMessage.Message)</span>
|
||||
<span class="text-muted fs-6 fw-semibold ms-5">@(loadingMessage)</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -40,12 +40,14 @@ namespace Moonlight
|
||||
|
||||
Logger.Info($"Working dir: {Directory.GetCurrentDirectory()}");
|
||||
|
||||
DatabaseCheckup.Perform();
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Switch to logging.net injection
|
||||
// TODO: Enable in production
|
||||
//builder.Logging.ClearProviders();
|
||||
//builder.Logging.AddProvider(new LogMigratorProvider());
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddProvider(new LogMigratorProvider());
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddRazorPages();
|
||||
|
||||
Reference in New Issue
Block a user