Fixed the cleanup of the User config backup files.

Fixed the cleanup of the profile/world backup file in CSM.
This commit is contained in:
Brett Hewitson 2021-12-18 23:57:11 +10:00
parent 3512e36bac
commit 3b2bad7607
20 changed files with 221 additions and 141 deletions

View file

@ -35,8 +35,7 @@ namespace ServerManagerTool.Lib
public const int MUTEX_TIMEOUT = 5; // 5 minutes
public const int MUTEX_ATTEMPTDELAY = 5000; // 5 seconds
private const int WRITELOG_ERRORRETRYDELAY = 2000; // 2 seconds
private const int BACKUP_DELETEINTERVAL = 7; // 7 days
public const int BACKUP_DELETEINTERVAL = 7; // 7 days
private const int STEAM_MAXRETRIES = 10;
private const int RCON_MAXRETRIES = 3;
@ -100,7 +99,7 @@ namespace ServerManagerTool.Lib
public bool BackupWorldFile = Config.Default.BackupWorldFile;
public bool CheckForOnlinePlayers = Config.Default.ServerShutdown_CheckForOnlinePlayers;
public bool SendMessages = Config.Default.ServerShutdown_SendShutdownMessages;
public bool DeleteOldServerBackupFiles = false;
public bool DeleteOldBackupFiles = Config.Default.AutoBackup_DeleteOldFiles;
public int ExitCode = EXITCODE_NORMALEXIT;
public bool OutputLogs = false;
public bool SendAlerts = false;
@ -187,7 +186,7 @@ namespace ServerManagerTool.Lib
}
// make a backup of the current profile and config files.
CreateProfileBackupArchiveFile();
CreateProfileBackupArchiveFile(_profile);
if (ExitCode != EXITCODE_NORMALEXIT)
return;
@ -198,7 +197,7 @@ namespace ServerManagerTool.Lib
}
// make a backup of the current world file.
CreateServerBackupArchiveFile(emailMessage);
CreateServerBackupArchiveFile(emailMessage, _profile);
if (ExitCode != EXITCODE_NORMALEXIT)
return;
@ -256,7 +255,7 @@ namespace ServerManagerTool.Lib
}
// make a backup of the current profile and config files.
CreateProfileBackupArchiveFile();
CreateProfileBackupArchiveFile(_profile);
if (ExitCode != EXITCODE_NORMALEXIT)
return;
@ -264,7 +263,7 @@ namespace ServerManagerTool.Lib
if (BackupWorldFile)
{
// make a backup of the current world file.
CreateServerBackupArchiveFile(null);
CreateServerBackupArchiveFile(null, _profile);
if (ExitCode != EXITCODE_NORMALEXIT)
return;
@ -1161,7 +1160,7 @@ namespace ServerManagerTool.Lib
emailMessage.AppendLine($"Server Manager version: {App.Instance.Version}");
// make a backup of the current profile and config files.
CreateProfileBackupArchiveFile();
CreateProfileBackupArchiveFile(_profile);
if (ExitCode != EXITCODE_NORMALEXIT)
return;
@ -1169,7 +1168,7 @@ namespace ServerManagerTool.Lib
if (BackupWorldFile)
{
// make a backup of the current world file.
CreateServerBackupArchiveFile(emailMessage);
CreateServerBackupArchiveFile(emailMessage, _profile);
if (ExitCode != EXITCODE_NORMALEXIT)
return;
@ -1728,7 +1727,7 @@ namespace ServerManagerTool.Lib
ExitCode = EXITCODE_NORMALEXIT;
}
public void CreateProfileBackupArchiveFile(ServerProfileSnapshot profile = null)
public void CreateProfileBackupArchiveFile(ServerProfileSnapshot profile)
{
// do nothing if profile is null
if (profile == null)
@ -1802,51 +1801,56 @@ namespace ServerManagerTool.Lib
}
// delete the old backup files
try
if (DeleteOldBackupFiles)
{
LogProfileMessage("Delete old profile backup files started...");
var backupFolder = GetProfileBackupFolder(_profile);
var backupFileFilter = $"*{Config.Default.BackupExtension}";
var backupDateFilter = DateTime.Now.AddDays(-BACKUP_DELETEINTERVAL);
var backupFiles = new DirectoryInfo(backupFolder).GetFiles(backupFileFilter).Where(f => f.LastWriteTime < backupDateFilter);
foreach (var backupFile in backupFiles)
try
{
try
var deleteInterval = Config.Default.AutoBackup_EnableBackup ? Config.Default.AutoBackup_DeleteInterval : BACKUP_DELETEINTERVAL;
LogProfileMessage("Delete old profile backup files started...");
var backupFolder = GetProfileBackupFolder(_profile);
var backupFileFilter = $"*{Config.Default.BackupExtension}";
var backupDateFilter = DateTime.Now.AddDays(-deleteInterval);
var backupFiles = new DirectoryInfo(backupFolder).GetFiles(backupFileFilter).Where(f => f.LastWriteTime < backupDateFilter);
foreach (var backupFile in backupFiles)
{
LogProfileMessage($"{backupFile.Name} was deleted, last updated {backupFile.CreationTime}.");
backupFile.Delete();
}
catch
{
// if unable to delete, do not bother
try
{
LogProfileMessage($"{backupFile.Name} was deleted, last updated {backupFile.CreationTime}.");
backupFile.Delete();
}
catch
{
// if unable to delete, do not bother
}
}
}
}
catch (Exception ex)
{
LogProfileError($"Error deleting old profile backup files.\r\n{ex.Message}", false);
}
finally
{
LogProfileMessage("Delete old profile backup files finished.");
}
// cleanup any backup folders from old backup process
try
{
var backupFolder = GetProfileBackupFolder(_profile);
var oldBackupFolders = new DirectoryInfo(backupFolder).GetDirectories();
foreach (var oldBackupFolder in oldBackupFolders)
catch (Exception ex)
{
oldBackupFolder.Delete(true);
LogProfileError($"Error deleting old profile backup files.\r\n{ex.Message}", false);
}
finally
{
LogProfileMessage("Delete old profile backup files finished.");
}
// cleanup any backup folders from old backup process
try
{
var backupFolder = GetProfileBackupFolder(_profile);
var oldBackupFolders = new DirectoryInfo(backupFolder).GetDirectories();
foreach (var oldBackupFolder in oldBackupFolders)
{
oldBackupFolder.Delete(true);
}
}
catch
{
// if unable to delete, do not bother
}
}
catch
{
// if unable to delete, do not bother
}
}
finally
@ -1855,7 +1859,7 @@ namespace ServerManagerTool.Lib
}
}
public void CreateServerBackupArchiveFile(StringBuilder emailMessage, ServerProfileSnapshot profile = null)
public void CreateServerBackupArchiveFile(StringBuilder emailMessage, ServerProfileSnapshot profile)
{
// do nothing if profile is null
if (profile == null)
@ -1958,7 +1962,7 @@ namespace ServerManagerTool.Lib
}
// delete the old backup files
if (DeleteOldServerBackupFiles)
if (DeleteOldBackupFiles)
{
try
{
@ -2939,7 +2943,7 @@ namespace ServerManagerTool.Lib
Parallel.ForEach(_profiles.Keys.Where(p => p.EnableAutoBackup), profile => {
var app = new ServerApp
{
DeleteOldServerBackupFiles = Config.Default.AutoBackup_DeleteOldFiles,
DeleteOldBackupFiles = Config.Default.AutoBackup_DeleteOldFiles,
OutputLogs = true,
SendAlerts = true,
SendEmails = true,