From ff3946b2b0c88dd8402592d518f9a9cbf13007c9 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Sat, 23 May 2020 13:51:58 +0000 Subject: [PATCH] --- .vscode/launch.json | 2 +- server/AyaNova/generator/CoreJobBackup.cs | 28 +++++++++++-------- server/AyaNova/util/FileUtil.cs | 12 ++++---- .../util/ServerGlobalOpsSettingsCache.cs | 25 +++++++++++++++-- 4 files changed, 46 insertions(+), 21 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 259171c9..e3a31ed6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -50,7 +50,7 @@ "AYANOVA_FOLDER_USER_FILES": "c:\\temp\\RavenTestData\\userfiles", "AYANOVA_FOLDER_BACKUP_FILES": "c:\\temp\\RavenTestData\\backupfiles", "AYANOVA_METRICS_USE_INFLUXDB": "false", - "AYANOVA_SERVER_TEST_MODE":"true", + "AYANOVA_SERVER_TEST_MODE":"false", "AYANOVA_SERVER_TEST_MODE_SEEDLEVEL":"huge", "AYANOVA_SERVER_TEST_MODE_TZ_OFFSET":"-7", "AYANOVA_BACKUP_PG_DUMP_PATH":"C:\\data\\code\\PostgreSQLPortable_12.0\\App\\PgSQL\\bin\\" diff --git a/server/AyaNova/generator/CoreJobBackup.cs b/server/AyaNova/generator/CoreJobBackup.cs index fd311aae..471c6ce5 100644 --- a/server/AyaNova/generator/CoreJobBackup.cs +++ b/server/AyaNova/generator/CoreJobBackup.cs @@ -33,19 +33,19 @@ namespace AyaNova.Biz 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 + // 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 < todayBackupTime) + 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 - } + // //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; @@ -60,6 +60,9 @@ namespace AyaNova.Biz log.LogDebug("Backup starting"); + + var DemandFileNamePrepend=OnDemand?"manual-":string.Empty; + //************* //DO DATA BACKUP //build command @@ -70,7 +73,7 @@ namespace AyaNova.Biz 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 = $"db-{FileUtil.GetSafeDateFileName()}.backup";//presentation issue so don't use UTC for this one + 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"; @@ -96,7 +99,7 @@ namespace AyaNova.Biz if (ServerGlobalOpsSettingsCache.Backup.BackupAttachments) { await JobsBiz.LogJobAsync(Guid.Empty, $"Attachments backup starting", ct); - FileUtil.BackupAttachments(); + FileUtil.BackupAttachments(DemandFileNamePrepend); } @@ -127,6 +130,9 @@ namespace AyaNova.Biz } 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); diff --git a/server/AyaNova/util/FileUtil.cs b/server/AyaNova/util/FileUtil.cs index cd86b3bd..244a0964 100644 --- a/server/AyaNova/util/FileUtil.cs +++ b/server/AyaNova/util/FileUtil.cs @@ -171,16 +171,16 @@ namespace AyaNova.Util /// - /// Get date of newest backup file or minvalue if not found + /// Get date of newest automatic backup file or minvalue if not found /// /// /// - internal static DateTime MostRecentBackupFileDate() + internal static DateTime MostRecentAutomatedBackupFileDate() { DateTime LastBackup = DateTime.MinValue; var BackupPath = UtilityFilesFolder; - foreach (string file in Directory.EnumerateFiles(UtilityFilesFolder, "*.backup")) - { + foreach (string file in Directory.EnumerateFiles(UtilityFilesFolder, "db-*.backup")) + { var ThisFileTime = File.GetCreationTimeUtc(file); if (ThisFileTime > LastBackup) { @@ -505,11 +505,11 @@ namespace AyaNova.Util } - internal static void BackupAttachments(ILogger log = null) + internal static void BackupAttachments(string demandFileNamePrepend,ILogger log = null) { try { - var AttachmentsBackupFile = $"at-{FileUtil.GetSafeDateFileName()}.zip";//presentation issue so don't use UTC for this one + var AttachmentsBackupFile = $"{demandFileNamePrepend}at-{FileUtil.GetSafeDateFileName()}.zip";//presentation issue so don't use UTC for this one AttachmentsBackupFile = GetFullPathForUtilityFile(AttachmentsBackupFile); System.IO.Compression.ZipFile.CreateFromDirectory(UserFilesFolder, AttachmentsBackupFile); diff --git a/server/AyaNova/util/ServerGlobalOpsSettingsCache.cs b/server/AyaNova/util/ServerGlobalOpsSettingsCache.cs index dfab2458..b83c432b 100644 --- a/server/AyaNova/util/ServerGlobalOpsSettingsCache.cs +++ b/server/AyaNova/util/ServerGlobalOpsSettingsCache.cs @@ -13,7 +13,7 @@ namespace AyaNova.Util { internal static GlobalOpsBackupSettings Backup { get; set; } - // internal static DateTime LastBackup { get; set; } + internal static DateTime NextBackup { get; set; } /// @@ -28,8 +28,27 @@ namespace AyaNova.Util Backup = new GlobalOpsBackupSettings(); ct.GlobalOpsBackupSettings.Add(Backup); ct.SaveChanges(); - } - Backup.LastBackup=FileUtil.MostRecentBackupFileDate(); + } + Backup.LastBackup=FileUtil.MostRecentAutomatedBackupFileDate(); + SetNextBackup(); + + } + + internal static void SetNextBackup() + { + //If backup at 6pm and come here need to check if should add day + DateTime utcNow = DateTime.UtcNow; + //Has last backup run more than 24 hours ago? + if (NextBackup < utcNow.AddHours(-24)) + { + //set it to today then + NextBackup = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day, Backup.BackupTime.Hour, Backup.BackupTime.Minute, 0, DateTimeKind.Utc); + } + else + { + //less than 24 hours, set it to tomorrow + NextBackup = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day+1, Backup.BackupTime.Hour, Backup.BackupTime.Minute, 0, DateTimeKind.Utc); + } }