This commit is contained in:
2020-05-23 13:51:58 +00:00
parent 6f7f162de4
commit ff3946b2b0
4 changed files with 46 additions and 21 deletions

2
.vscode/launch.json vendored
View File

@@ -50,7 +50,7 @@
"AYANOVA_FOLDER_USER_FILES": "c:\\temp\\RavenTestData\\userfiles", "AYANOVA_FOLDER_USER_FILES": "c:\\temp\\RavenTestData\\userfiles",
"AYANOVA_FOLDER_BACKUP_FILES": "c:\\temp\\RavenTestData\\backupfiles", "AYANOVA_FOLDER_BACKUP_FILES": "c:\\temp\\RavenTestData\\backupfiles",
"AYANOVA_METRICS_USE_INFLUXDB": "false", "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_SEEDLEVEL":"huge",
"AYANOVA_SERVER_TEST_MODE_TZ_OFFSET":"-7", "AYANOVA_SERVER_TEST_MODE_TZ_OFFSET":"-7",
"AYANOVA_BACKUP_PG_DUMP_PATH":"C:\\data\\code\\PostgreSQLPortable_12.0\\App\\PgSQL\\bin\\" "AYANOVA_BACKUP_PG_DUMP_PATH":"C:\\data\\code\\PostgreSQLPortable_12.0\\App\\PgSQL\\bin\\"

View File

@@ -33,19 +33,19 @@ namespace AyaNova.Biz
log.LogTrace("Checking if backup should run"); log.LogTrace("Checking if backup should run");
//what time should we backup today? //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? //Are we there yet?
if (utcNow < todayBackupTime) if (utcNow < ServerGlobalOpsSettingsCache.NextBackup)
{ {
log.LogTrace("Not past backup time yet"); return;//nope 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 (ServerGlobalOpsSettingsCache.Backup.LastBackup > utcNow.AddHours(-24)) // 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 // 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... //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;
@@ -60,6 +60,9 @@ namespace AyaNova.Biz
log.LogDebug("Backup starting"); log.LogDebug("Backup starting");
var DemandFileNamePrepend=OnDemand?"manual-":string.Empty;
//************* //*************
//DO DATA BACKUP //DO DATA BACKUP
//build command //build command
@@ -70,7 +73,7 @@ namespace AyaNova.Biz
Npgsql.NpgsqlConnectionStringBuilder PostgresConnectionString = new Npgsql.NpgsqlConnectionStringBuilder(ServerBootConfig.AYANOVA_DB_CONNECTION); 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 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); DataBackupFile = FileUtil.GetFullPathForUtilityFile(DataBackupFile);
var BackupUtilityCommand = "pg_dump"; var BackupUtilityCommand = "pg_dump";
@@ -96,7 +99,7 @@ namespace AyaNova.Biz
if (ServerGlobalOpsSettingsCache.Backup.BackupAttachments) if (ServerGlobalOpsSettingsCache.Backup.BackupAttachments)
{ {
await JobsBiz.LogJobAsync(Guid.Empty, $"Attachments backup starting", ct); await JobsBiz.LogJobAsync(Guid.Empty, $"Attachments backup starting", ct);
FileUtil.BackupAttachments(); FileUtil.BackupAttachments(DemandFileNamePrepend);
} }
@@ -127,6 +130,9 @@ namespace AyaNova.Biz
} }
finally finally
{ {
//bump the backup date if automatic backup
if(!OnDemand)
ServerGlobalOpsSettingsCache.SetNextBackup();
apiServerState.ResumePriorState(); apiServerState.ResumePriorState();
BackupIsRunning = false; BackupIsRunning = false;
await JobsBiz.LogJobAsync(Guid.Empty, $"Backup - fully complete, server re-opened", ct); await JobsBiz.LogJobAsync(Guid.Empty, $"Backup - fully complete, server re-opened", ct);

View File

@@ -171,16 +171,16 @@ namespace AyaNova.Util
/// <summary> /// <summary>
/// Get date of newest backup file or minvalue if not found /// Get date of newest automatic backup file or minvalue if not found
/// ///
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
internal static DateTime MostRecentBackupFileDate() internal static DateTime MostRecentAutomatedBackupFileDate()
{ {
DateTime LastBackup = DateTime.MinValue; DateTime LastBackup = DateTime.MinValue;
var BackupPath = UtilityFilesFolder; 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); var ThisFileTime = File.GetCreationTimeUtc(file);
if (ThisFileTime > LastBackup) 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 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); AttachmentsBackupFile = GetFullPathForUtilityFile(AttachmentsBackupFile);
System.IO.Compression.ZipFile.CreateFromDirectory(UserFilesFolder, AttachmentsBackupFile); System.IO.Compression.ZipFile.CreateFromDirectory(UserFilesFolder, AttachmentsBackupFile);

View File

@@ -13,7 +13,7 @@ namespace AyaNova.Util
{ {
internal static GlobalOpsBackupSettings Backup { get; set; } internal static GlobalOpsBackupSettings Backup { get; set; }
// internal static DateTime LastBackup { get; set; } internal static DateTime NextBackup { get; set; }
/// <summary> /// <summary>
@@ -28,8 +28,27 @@ namespace AyaNova.Util
Backup = new GlobalOpsBackupSettings(); Backup = new GlobalOpsBackupSettings();
ct.GlobalOpsBackupSettings.Add(Backup); ct.GlobalOpsBackupSettings.Add(Backup);
ct.SaveChanges(); 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);
}
} }