97 lines
3.4 KiB
C#
97 lines
3.4 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using AyaNova.Models;
|
|
using AyaNova.Util;
|
|
|
|
|
|
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)
|
|
{
|
|
if (BackupIsRunning) return;
|
|
|
|
//get NOW in utc
|
|
DateTime utcNow = DateTime.UtcNow;
|
|
//what time should we backup today?
|
|
DateTime todayBackupTime = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day, ServerGlobalOpsSettings.BackupTime.Hour, ServerGlobalOpsSettings.BackupTime.Minute, 0, DateTimeKind.Utc);//first start with NOW
|
|
//Are we there yet?
|
|
if (utcNow < todayBackupTime) 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 (ServerGlobalOpsSettings.LastBackup > utcNow.AddHours(-24))
|
|
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 JOB RUNNING");
|
|
|
|
log.LogTrace("Backup starting");
|
|
|
|
//*************
|
|
|
|
//DO DATA BACKUP
|
|
log.LogInformation("BACKUP STUB: DATA BACKUP RUNNING NOW - TORA TORA TORA!");
|
|
log.LogInformation($"dbdump path: {ServerBootConfig.AYANOVA_BACKUP_PG_DUMP_PATH}");
|
|
|
|
//DO FILE BACKUP IF ATTACHMENTS BACKED UP
|
|
log.LogInformation("BACKUP STUB: ATTACHMENTS BACKUP RUNNING NOW");
|
|
|
|
//PRUNE BACKUP SETS NOT KEPT
|
|
log.LogInformation("BACKUP STUB: PRUNING RUNNING NOW");
|
|
|
|
|
|
//v.next - COPY TO ONLINE STORAGE
|
|
|
|
//***************
|
|
|
|
//Update last backup
|
|
var biz = GlobalOpsSettingsBiz.GetBiz(ct);
|
|
var OpSet = await biz.GetAsync(false);
|
|
OpSet.LastBackup = utcNow;
|
|
await biz.ReplaceAsync(OpSet);
|
|
|
|
log.LogTrace("Backup completed");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.LogError(ex, "BACKUP FAILED!");
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
apiServerState.ResumePriorState();
|
|
BackupIsRunning = false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
}//eoc
|
|
|
|
|
|
}//eons
|
|
|