This commit is contained in:
2020-05-19 23:28:31 +00:00
parent 2abb64de82
commit dea4369501
2 changed files with 41 additions and 17 deletions

View File

@@ -26,19 +26,23 @@ namespace AyaNova.Biz
{
if (BackupIsRunning) return;
log.LogTrace("Checking if backup should run");
//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
if (utcNow < todayBackupTime)
{
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 (ServerGlobalOpsSettings.LastBackup > utcNow.AddHours(-24))
return;//nope, so we have already run today's backup
{
log.LogTrace("Hasn't been 24 hours since last backup yet"); return;//nope//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
@@ -48,7 +52,7 @@ namespace AyaNova.Biz
apiServerState = (AyaNova.Api.ControllerHelpers.ApiServerState)ServiceProviderProvider.Provider.GetService(typeof(AyaNova.Api.ControllerHelpers.ApiServerState));
apiServerState.SetClosed("BACKUP JOB RUNNING");
log.LogTrace("Backup starting");
log.LogDebug("Backup starting");
//*************
@@ -74,14 +78,19 @@ namespace AyaNova.Biz
if (string.IsNullOrWhiteSpace(Result))
log.LogDebug("BACKUP SUCCESSFUL (NO ERROR)");
else
log.LogInformation($"BACKUP ERROR: {Result}");
log.LogError($"BACKUP ERROR: {Result}");
//PRUNE DATA BACKUP SETS NOT KEPT
FileUtil.DatabaseBackupCleanUp(ServerGlobalOpsSettings.BackupSetsToKeep);
//DO FILE BACKUP IF ATTACHMENTS BACKED UP
log.LogInformation("BACKUP STUB: ATTACHMENTS BACKUP RUNNING NOW");
if (ServerGlobalOpsSettings.BackupAttachments)
FileUtil.BackupAttachments();
//PRUNE DATA BACKUP SETS NOT KEPT
FileUtil.AttachmentBackupCleanUp(ServerGlobalOpsSettings.BackupSetsToKeep);
//PRUNE BACKUP SETS NOT KEPT
FileUtil.DatabaseBackupCleanUp(ServerGlobalOpsSettings.BackupSetsToKeep);
//v.next - COPY TO ONLINE STORAGE
@@ -94,26 +103,20 @@ namespace AyaNova.Biz
OpSet.LastBackup = utcNow;
await biz.ReplaceAsync(OpSet);
log.LogTrace("Backup completed");
log.LogDebug("Backup completed");
}
catch (Exception ex)
{
log.LogError(ex, "BACKUP FAILED!");
log.LogError(ex, "Backup failed");
throw ex;
}
finally
{
apiServerState.ResumePriorState();
BackupIsRunning = false;
log.LogInformation("BACKUP STUB: COMPLETELY FINISHED BACKING UP");
}
}
/////////////////////////////////////////////////////////////////////
}//eoc

View File

@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using AyaNova.Models;
@@ -440,6 +441,26 @@ namespace AyaNova.Util
}
internal static void BackupAttachments(ILogger log = null)
{
try
{
var AttachmentsBackupFile = $"at-{FileUtil.GetSafeDateFileName()}.zip";//presentation issue so don't use UTC for this one
AttachmentsBackupFile = GetFullPathForUtilityFile(AttachmentsBackupFile);
System.IO.Compression.ZipFile.CreateFromDirectory(UserFilesFolder, AttachmentsBackupFile);
}
catch (Exception ex)
{
if (log != null)
{
log.LogError(ex, $"FileUtil::BackupAttachments");
}
throw ex;
}
}
#endregion attachment stuff
#region General utilities