Skip to content

Commit 493d62f

Browse files
committed
Feature: Create a daily settings backup
1 parent a4c7122 commit 493d62f

File tree

3 files changed

+109
-6
lines changed

3 files changed

+109
-6
lines changed

Source/NETworkManager.Settings/GlobalStaticConfiguration.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public static class GlobalStaticConfiguration
4646
public static string ZipFileExtensionFilter => "ZIP Archive (*.zip)|*.zip";
4747
public static string XmlFileExtensionFilter => "XML-File (*.xml)|*.xml";
4848

49+
// Backup settings
50+
public static int Backup_MaximumNumberOfBackups => 10;
51+
4952
#endregion
5053

5154
#region Default settings

Source/NETworkManager.Settings/SettingsInfo.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,27 @@ public string Version
109109
}
110110
}
111111

112+
/// <summary>
113+
/// Private field for the <see cref="LastBackup" /> property.
114+
/// </summary>
115+
private DateTime _lastBackup = DateTime.Now.Date;
116+
117+
/// <summary>
118+
/// Store the date and time of the last backup of the settings file.
119+
/// </summary>
120+
public DateTime LastBackup
121+
{
122+
get => _lastBackup;
123+
set
124+
{
125+
if (value == _lastBackup)
126+
return;
127+
128+
_lastBackup = value;
129+
OnPropertyChanged();
130+
}
131+
}
132+
112133
#region General
113134

114135
// General
@@ -1400,7 +1421,7 @@ public ExportFileType PortScanner_ExportFileType
14001421

14011422
#region Ping Monitor
14021423

1403-
private ObservableCollection<string> _pingMonitor_HostHistory = new();
1424+
private ObservableCollection<string> _pingMonitor_HostHistory = [];
14041425

14051426
public ObservableCollection<string> PingMonitor_HostHistory
14061427
{

Source/NETworkManager.Settings/SettingsManager.cs

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ public static void Save()
237237
// Create the directory if it does not exist
238238
Directory.CreateDirectory(GetSettingsFolderLocation());
239239

240+
// Create backup before modifying
241+
CreateDailyBackupIfNeeded();
242+
240243
// Serialize the settings to a file
241244
SerializeToFile(GetSettingsFilePath());
242245

@@ -258,15 +261,91 @@ private static void SerializeToFile(string filePath)
258261
#endregion
259262

260263
#region Backup
261-
/*
262-
private static void Backup()
264+
/// <summary>
265+
/// Creates a backup of the settings file if a backup has not already been created for the current day.
266+
/// </summary>
267+
/// <remarks>This method checks whether a backup for the current date exists and, if not, creates a new
268+
/// backup of the settings file. It also removes old backups according to the configured maximum number of backups.
269+
/// If the settings file does not exist, no backup is created and a warning is logged. This method is intended to be
270+
/// called as part of a daily maintenance routine.</remarks>
271+
private static void CreateDailyBackupIfNeeded()
272+
{
273+
var currentDate = DateTime.Now.Date;
274+
275+
if (Current.LastBackup < currentDate)
276+
{
277+
// Check if settings file exists
278+
if (!File.Exists(GetSettingsFilePath()))
279+
{
280+
Log.Warn("Settings file does not exist yet. Skipping backup creation...");
281+
return;
282+
}
283+
284+
// Create backup
285+
Backup(GetSettingsFilePath(),
286+
GetSettingsBackupFolderLocation(),
287+
$"{TimestampHelper.GetTimestamp()}_{GetSettingsFileName()}");
288+
289+
// Cleanup old backups
290+
CleanupBackups(GetSettingsBackupFolderLocation(),
291+
GetSettingsFileName(),
292+
GlobalStaticConfiguration.Backup_MaximumNumberOfBackups);
293+
294+
Current.LastBackup = currentDate;
295+
}
296+
}
297+
298+
/// <summary>
299+
/// Deletes older backup files in the specified folder to ensure that only the most recent backups, up to the
300+
/// specified maximum, are retained.
301+
/// </summary>
302+
/// <remarks>This method removes the oldest backup files first, keeping only the most recent backups as
303+
/// determined by file creation time. It is intended to prevent excessive accumulation of backup files and manage
304+
/// disk space usage.</remarks>
305+
/// <param name="backupFolderPath">The full path to the directory containing the backup files to be managed. Cannot be null or empty.</param>
306+
/// <param name="settingsFileName">The file name pattern used to identify backup files for cleanup.</param>
307+
/// <param name="maxBackupFiles">The maximum number of backup files to retain. Must be greater than zero.</param>
308+
private static void CleanupBackups(string backupFolderPath, string settingsFileName, int maxBackupFiles)
263309
{
264-
Log.Info("Creating settings backup...");
310+
var backupFiles = Directory.GetFiles(backupFolderPath)
311+
.Where(f => f.EndsWith(settingsFileName) || f.EndsWith(Path.Combine(GetLegacySettingsFileName())))
312+
.OrderByDescending(f => File.GetCreationTime(f))
313+
.ToList();
314+
315+
if (backupFiles.Count > maxBackupFiles)
316+
Log.Info($"Cleaning up old backup files... Found {backupFiles.Count} backups, keeping the most recent {maxBackupFiles}.");
317+
318+
while (backupFiles.Count > maxBackupFiles)
319+
{
320+
var fileToDelete = backupFiles.Last();
265321

322+
File.Delete(fileToDelete);
323+
324+
backupFiles.RemoveAt(backupFiles.Count - 1);
325+
326+
Log.Info($"Backup deleted: {fileToDelete}");
327+
}
328+
}
329+
330+
/// <summary>
331+
/// Creates a backup of the specified settings file in the given backup folder with the provided backup file name.
332+
/// </summary>
333+
/// <param name="setingsFilePath">The full path to the settings file to back up. Cannot be null or empty.</param>
334+
/// <param name="backupFolderPath">The directory path where the backup file will be stored. If the directory does not exist, it will be created.</param>
335+
/// <param name="backupFileName">The name to use for the backup file within the backup folder. Cannot be null or empty.</param>
336+
private static void Backup(string setingsFilePath, string backupFolderPath, string backupFileName)
337+
{
266338
// Create the backup directory if it does not exist
267-
Directory.CreateDirectory(GetSettingsBackupFolderLocation());
339+
Directory.CreateDirectory(backupFolderPath);
340+
341+
// Create the backup file path
342+
var backupFilePath = Path.Combine(backupFolderPath, backupFileName);
343+
344+
// Copy the current settings file to the backup location
345+
File.Copy(setingsFilePath, backupFilePath, true);
346+
347+
Log.Info($"Backup created: {backupFilePath}");
268348
}
269-
*/
270349

271350
#endregion
272351

0 commit comments

Comments
 (0)