This commit is contained in:
2020-05-19 23:54:57 +00:00
parent 337482714d
commit 81270c45de
4 changed files with 104 additions and 47 deletions

View File

@@ -22,45 +22,47 @@ namespace AyaNova.Biz
////////////////////////////////////////////////////////////////////////////////////////////////
//
//
public static async Task DoWorkAsync(AyContext ct)
public static async Task DoWorkAsync(AyContext ct, bool OnDemand = false)
{
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)
if (!OnDemand)
{
log.LogTrace("Not past backup time yet"); return;//nope
log.LogTrace("Checking if backup should run");
//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)
{
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))
{
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...
}
//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))
{
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
{
BackupIsRunning = true;
//LOCK DOWN SERVER
apiServerState = (AyaNova.Api.ControllerHelpers.ApiServerState)ServiceProviderProvider.Provider.GetService(typeof(AyaNova.Api.ControllerHelpers.ApiServerState));
apiServerState.SetClosed("BACKUP JOB RUNNING");
apiServerState.SetClosed("BACKUP RUNNING");
log.LogDebug("Backup starting");
//*************
//DO DATA BACKUP
//build command
//this is valid:
//this is valid on windows
//C:\data\code\PostgreSQLPortable_12.0\App\PgSQL\bin\pg_dump --dbname=postgresql://postgres:raven@127.0.0.1:5432/AyaNova -Fc > huge_new.backup
//"AYANOVA_DB_CONNECTION": "Server=localhost;Username=postgres;Password=raven;Database=AyaNova;",
Npgsql.NpgsqlConnectionStringBuilder PostgresConnectionString = new Npgsql.NpgsqlConnectionStringBuilder(ServerBootConfig.AYANOVA_DB_CONNECTION);
var DBNameParameter = $"--dbname=postgresql://{PostgresConnectionString.Username}:{PostgresConnectionString.Password}@{PostgresConnectionString.Host}:{PostgresConnectionString.Port}/{PostgresConnectionString.Database}";
@@ -80,6 +82,7 @@ namespace AyaNova.Biz
else
log.LogError($"BACKUP ERROR: {Result}");
//PRUNE DATA BACKUP SETS NOT KEPT
FileUtil.DatabaseBackupCleanUp(ServerGlobalOpsSettings.BackupSetsToKeep);
@@ -96,13 +99,14 @@ namespace AyaNova.Biz
//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);
if (!OnDemand)
{
//Update last backup
var biz = GlobalOpsSettingsBiz.GetBiz(ct);
var OpSet = await biz.GetAsync(false);
OpSet.LastBackup = utcNow;
await biz.ReplaceAsync(OpSet);
}
log.LogDebug("Backup completed");
}
catch (Exception ex)
@@ -116,6 +120,9 @@ namespace AyaNova.Biz
BackupIsRunning = false;
}
}
/////////////////////////////////////////////////////////////////////
}//eoc
}//eons