Files
raven/server/AyaNova/generator/CoreJobBackup.cs

162 lines
7.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using AyaNova.Models;
using AyaNova.Util;
using System.IO;
using System.Runtime.InteropServices;
namespace AyaNova.Biz
{
/// <summary>
/// Backup
///
/// </summary>
internal static class CoreJobBackup
{
private static ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("CoreJobBackup");
private static bool BackupIsRunning = false;
private const int MAXIMUM_MS_ALLOWED_FOR_PROCESSING = 10 * 60 * 1000;//wild assed guess 10 minutes maximum to run backup command, ever
////////////////////////////////////////////////////////////////////////////////////////////////
// BACK-THE-FUCK-UP
//
public static async Task DoWorkAsync(bool OnDemand = false)
{
if (BackupIsRunning) return;
if (!OnDemand)
{
log.LogTrace("Checking if backup should run");
if (!ServerGlobalOpsSettingsCache.Backup.Active)
{
log.LogDebug("Automatic backup is set to INACTIVE - not backing up");
return;
}
if (DateTime.UtcNow < ServerGlobalOpsSettingsCache.NextBackup)
{
log.LogTrace("Not past backup time yet");
return;
}
}
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 RUNNING");
var jobstartmessage = $"LT:Backup LT:StartJob {(OnDemand ? "manual / on demand" : "scheduled")} ";
await JobsBiz.LogJobAsync(Guid.Empty, jobstartmessage);
DateTime dtStartBackup = DateTime.Now;
log.LogDebug("Backup starting");
// var DemandFileNamePrepend = OnDemand ? "manual-" : string.Empty;
//*************
//DO DATA BACKUP
//run pg_dump directly with no shell: each connection detail is its own argument and the password goes in PGPASSWORD,
//so a user name or password containing spaces or @ : / ? # % can't break the command (it used to be a postgresql:// URI
//with the raw password, run through cmd.exe or /bin/sh for a > file redirect), and the password is never on a command line
//equivalent to: pg_dump --host=localhost --port=5432 --username=postgres --dbname=AyaNova --format=custom --file=db-xxx.backup
// await JobsBiz.LogJobAsync(Guid.Empty, $"Data backup starting");
Npgsql.NpgsqlConnectionStringBuilder PostgresConnectionString = new Npgsql.NpgsqlConnectionStringBuilder(ServerBootConfig.AYANOVA_DB_CONNECTION);
//var DataBackupFile = $"{DemandFileNamePrepend}db-{FileUtil.GetSafeDateFileName()}.backup";//presentation issue so don't use UTC for this one
var DataBackupFile = $"db-{FileUtil.GetSafeDateFileName()}.backup";//presentation issue so don't use UTC for this one
DataBackupFile = FileUtil.GetFullPathForBackupFile(DataBackupFile);
var BackupUtilityCommand = "pg_dump";
if (!string.IsNullOrWhiteSpace(ServerBootConfig.AYANOVA_BACKUP_PG_DUMP_PATH))
BackupUtilityCommand = Path.Combine(ServerBootConfig.AYANOVA_BACKUP_PG_DUMP_PATH, BackupUtilityCommand);
var Arguments = new List<string>();
//host and user only when set, otherwise pg_dump's own defaults apply as they would for any omitted option
if (!string.IsNullOrWhiteSpace(PostgresConnectionString.Host))
Arguments.AddRange(new[] { "--host", PostgresConnectionString.Host });
Arguments.AddRange(new[] { "--port", PostgresConnectionString.Port.ToString() });
if (!string.IsNullOrWhiteSpace(PostgresConnectionString.Username))
Arguments.AddRange(new[] { "--username", PostgresConnectionString.Username });
Arguments.AddRange(new[] {
"--dbname", PostgresConnectionString.Database,
"--format=custom",
"--file", DataBackupFile,
"--no-password"//never prompt, a missing or wrong password must fail rather than hang
});
var PgEnvironment = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(PostgresConnectionString.Password))
PgEnvironment["PGPASSWORD"] = PostgresConnectionString.Password;
var Result = RunProgram.RunNoShell(BackupUtilityCommand, Arguments, PgEnvironment, log, MAXIMUM_MS_ALLOWED_FOR_PROCESSING);
if (!string.IsNullOrWhiteSpace(Result))
{
var msg = $"Error during data backup \"{Result}\"";
await JobsBiz.LogJobAsync(Guid.Empty, msg);
log.LogError($"BACKUP ERROR: {Result}");
await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.BackupStatus, msg, "Backup");
}
else
{
log.LogDebug("Backup of database completed OK");
//DO FILE BACKUP IF ATTACHMENTS BACKED UP
if (ServerGlobalOpsSettingsCache.Backup.BackupAttachments)
{
await JobsBiz.LogJobAsync(Guid.Empty, $"LT:Backup LT:Attachments");
FileUtil.BackupAttachments();
log.LogDebug("Backup of file attachments completed OK");
}
//PRUNE DATA BACKUP SETS NOT KEPT
await JobsBiz.LogJobAsync(Guid.Empty, $"LT:BackupDeleteOld");
//case 4203
#if (SUBSCRIPTION_BUILD)
FileUtil.DatabaseBackupCleanUp(1);
#else
FileUtil.DatabaseBackupCleanUp(ServerGlobalOpsSettingsCache.Backup.BackupSetsToKeep);
#endif
//v.next - COPY TO ONLINE STORAGE
//***************
log.LogDebug("Backup completed");
var duration = DateTime.Now - dtStartBackup;
await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.BackupStatus, $"Backup ({(OnDemand ? "manual / on demand" : "scheduled")}) completed successfully, duration (h:m:s.ms): {duration.ToString()}, server re-opened", "Backup");
}
}
catch (Exception ex)
{
await JobsBiz.LogJobAsync(Guid.Empty, "LT:JobFailed");
await JobsBiz.LogJobAsync(Guid.Empty, ExceptionUtil.ExtractAllExceptionMessages(ex));
log.LogError(ex, "Backup failed");
await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.BackupStatus, "Backup failed", "Backup", ex);
throw;
}
finally
{
//bump the backup date if automatic backup
if (!OnDemand)
ServerGlobalOpsSettingsCache.SetNextBackup();
apiServerState.ResumePriorState();
BackupIsRunning = false;
await JobsBiz.LogJobAsync(Guid.Empty, "LT:JobCompleted");
}
}
/////////////////////////////////////////////////////////////////////
}//eoc
}//eons