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; if (BackupIsRunning) return;
log.LogTrace("Checking if backup should run");
//get NOW in utc //get NOW in utc
DateTime utcNow = DateTime.UtcNow; DateTime utcNow = DateTime.UtcNow;
//what time should we backup today? //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 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? //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 //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... //need to check for that as well...
//Has last backup run more than 24 hours ago? //Has last backup run more than 24 hours ago?
if (ServerGlobalOpsSettings.LastBackup > utcNow.AddHours(-24)) 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... //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; AyaNova.Api.ControllerHelpers.ApiServerState apiServerState = null;
try try
@@ -48,7 +52,7 @@ namespace AyaNova.Biz
apiServerState = (AyaNova.Api.ControllerHelpers.ApiServerState)ServiceProviderProvider.Provider.GetService(typeof(AyaNova.Api.ControllerHelpers.ApiServerState)); apiServerState = (AyaNova.Api.ControllerHelpers.ApiServerState)ServiceProviderProvider.Provider.GetService(typeof(AyaNova.Api.ControllerHelpers.ApiServerState));
apiServerState.SetClosed("BACKUP JOB RUNNING"); apiServerState.SetClosed("BACKUP JOB RUNNING");
log.LogTrace("Backup starting"); log.LogDebug("Backup starting");
//************* //*************
@@ -74,14 +78,19 @@ namespace AyaNova.Biz
if (string.IsNullOrWhiteSpace(Result)) if (string.IsNullOrWhiteSpace(Result))
log.LogDebug("BACKUP SUCCESSFUL (NO ERROR)"); log.LogDebug("BACKUP SUCCESSFUL (NO ERROR)");
else 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 //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 //v.next - COPY TO ONLINE STORAGE
@@ -94,26 +103,20 @@ namespace AyaNova.Biz
OpSet.LastBackup = utcNow; OpSet.LastBackup = utcNow;
await biz.ReplaceAsync(OpSet); await biz.ReplaceAsync(OpSet);
log.LogTrace("Backup completed"); log.LogDebug("Backup completed");
} }
catch (Exception ex) catch (Exception ex)
{ {
log.LogError(ex, "BACKUP FAILED!"); log.LogError(ex, "Backup failed");
throw ex; throw ex;
} }
finally finally
{ {
apiServerState.ResumePriorState(); apiServerState.ResumePriorState();
BackupIsRunning = false; BackupIsRunning = false;
log.LogInformation("BACKUP STUB: COMPLETELY FINISHED BACKING UP");
} }
} }
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
}//eoc }//eoc

View File

@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using AyaNova.Models; 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 #endregion attachment stuff
#region General utilities #region General utilities