141 lines
6.3 KiB
C#
141 lines
6.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using AyaNova.Models;
|
|
using AyaNova.Util;
|
|
using System.IO;
|
|
|
|
|
|
namespace AyaNova.Biz
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
/// Backup
|
|
///
|
|
/// </summary>
|
|
internal static class CoreJobBackup
|
|
{
|
|
private static ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("CoreJobBackup");
|
|
private static bool BackupIsRunning = false;
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
//
|
|
public static async Task DoWorkAsync(AyContext ct, bool OnDemand = false)
|
|
{
|
|
if (BackupIsRunning) return;
|
|
//testing FileUtil.DatabaseBackupCleanUp(ServerGlobalOpsSettingsCache.Backup.BackupSetsToKeep);
|
|
//get NOW in utc
|
|
DateTime utcNow = DateTime.UtcNow;
|
|
if (!OnDemand)
|
|
{
|
|
log.LogTrace("Checking if backup should run");
|
|
|
|
//what time should we backup today?
|
|
// DateTime todayBackupTime = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day, ServerGlobalOpsSettingsCache.Backup.BackupTime.Hour, ServerGlobalOpsSettingsCache.Backup.BackupTime.Minute, 0, DateTimeKind.Utc);//first start with NOW
|
|
//Are we there yet?
|
|
if (utcNow < ServerGlobalOpsSettingsCache.NextBackup)
|
|
{
|
|
log.LogTrace("Not past backup time yet"); return;//nope
|
|
}
|
|
// //Yes, we've passed into the backup window time, but that's also true if we just ran the backup as well so
|
|
// //need to check for that as well...
|
|
// //Has last backup run more than 24 hours ago?
|
|
// if (ServerGlobalOpsSettingsCache.Backup.LastBackup > utcNow.AddHours(-24))
|
|
// {
|
|
// log.LogTrace("Hasn't been 24 hours since last backup yet"); return;//nope, so we have already run today's backup
|
|
// }
|
|
//Ok, we're into backup time and it's been more than 24 hours since it last ran so let's do this...
|
|
}
|
|
AyaNova.Api.ControllerHelpers.ApiServerState apiServerState = null;
|
|
try
|
|
{
|
|
BackupIsRunning = true;
|
|
//LOCK DOWN SERVER
|
|
apiServerState = (AyaNova.Api.ControllerHelpers.ApiServerState)ServiceProviderProvider.Provider.GetService(typeof(AyaNova.Api.ControllerHelpers.ApiServerState));
|
|
apiServerState.SetClosed("BACKUP RUNNING");
|
|
|
|
await JobsBiz.LogJobAsync(Guid.Empty, $"Starting backup job {(OnDemand ? "manual / on demand" : "scheduled") } ", ct);
|
|
|
|
log.LogDebug("Backup starting");
|
|
|
|
|
|
var DemandFileNamePrepend=OnDemand?"manual-":string.Empty;
|
|
|
|
//*************
|
|
//DO DATA BACKUP
|
|
//build command
|
|
//this is valid on windows
|
|
//C:\data\code\PostgreSQLPortable_12.0\App\PgSQL\bin\pg_dump --dbname=postgresql://postgres:raven@127.0.0.1:5432/AyaNova -Fc > huge_new.backup
|
|
|
|
await JobsBiz.LogJobAsync(Guid.Empty, $"Data backup starting", ct);
|
|
Npgsql.NpgsqlConnectionStringBuilder PostgresConnectionString = new Npgsql.NpgsqlConnectionStringBuilder(ServerBootConfig.AYANOVA_DB_CONNECTION);
|
|
var DBNameParameter = $"--dbname=postgresql://{PostgresConnectionString.Username}:{PostgresConnectionString.Password}@{PostgresConnectionString.Host}:{PostgresConnectionString.Port}/{PostgresConnectionString.Database}";
|
|
|
|
var DataBackupFile = $"{DemandFileNamePrepend}db-{FileUtil.GetSafeDateFileName()}.backup";//presentation issue so don't use UTC for this one
|
|
DataBackupFile = FileUtil.GetFullPathForUtilityFile(DataBackupFile);
|
|
|
|
var BackupUtilityCommand = "pg_dump";
|
|
if (!string.IsNullOrWhiteSpace(ServerBootConfig.AYANOVA_BACKUP_PG_DUMP_PATH))
|
|
BackupUtilityCommand = Path.Combine(ServerBootConfig.AYANOVA_BACKUP_PG_DUMP_PATH, BackupUtilityCommand);
|
|
|
|
var Arguments = $"{DBNameParameter} -Fc > {DataBackupFile}";
|
|
|
|
var Result = RunProgram.Run(BackupUtilityCommand, Arguments, log);
|
|
if (string.IsNullOrWhiteSpace(Result))
|
|
{
|
|
|
|
log.LogDebug("BACKUP SUCCESSFUL (NO ERROR)");
|
|
}
|
|
else
|
|
{
|
|
await JobsBiz.LogJobAsync(Guid.Empty, $"Error during data backup \"{Result}\"", ct);
|
|
log.LogError($"BACKUP ERROR: {Result}");
|
|
}
|
|
|
|
|
|
//DO FILE BACKUP IF ATTACHMENTS BACKED UP
|
|
if (ServerGlobalOpsSettingsCache.Backup.BackupAttachments)
|
|
{
|
|
await JobsBiz.LogJobAsync(Guid.Empty, $"Attachments backup starting", ct);
|
|
FileUtil.BackupAttachments(DemandFileNamePrepend);
|
|
|
|
}
|
|
|
|
//PRUNE DATA BACKUP SETS NOT KEPT
|
|
await JobsBiz.LogJobAsync(Guid.Empty, $"Pruning old backup sets", ct);
|
|
FileUtil.DatabaseBackupCleanUp(ServerGlobalOpsSettingsCache.Backup.BackupSetsToKeep);
|
|
|
|
|
|
//v.next - COPY TO ONLINE STORAGE
|
|
|
|
//***************
|
|
|
|
log.LogDebug("Backup completed");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await JobsBiz.LogJobAsync(Guid.Empty, "Backup failed with errors:", ct);
|
|
await JobsBiz.LogJobAsync(Guid.Empty, ExceptionUtil.ExtractAllExceptionMessages(ex), ct);
|
|
log.LogError(ex, "Backup failed");
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
//bump the backup date if automatic backup
|
|
if(!OnDemand)
|
|
ServerGlobalOpsSettingsCache.SetNextBackup();
|
|
apiServerState.ResumePriorState();
|
|
BackupIsRunning = false;
|
|
await JobsBiz.LogJobAsync(Guid.Empty, $"Backup - fully complete, server re-opened", ct);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
}//eoc
|
|
}//eons
|
|
|