From cf2cfa4853944ce0af6b6c22a089b937dd59ccaf Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 14 Jun 2020 22:18:25 +0800 Subject: feat(back): Add backup service and run it when start. --- Timeline/Services/DatabaseBackupService.cs | 35 ++++++++++++++++++++++++++++++ Timeline/Services/PathProvider.cs | 6 +++++ 2 files changed, 41 insertions(+) create mode 100644 Timeline/Services/DatabaseBackupService.cs (limited to 'Timeline/Services') diff --git a/Timeline/Services/DatabaseBackupService.cs b/Timeline/Services/DatabaseBackupService.cs new file mode 100644 index 00000000..a76b2a0d --- /dev/null +++ b/Timeline/Services/DatabaseBackupService.cs @@ -0,0 +1,35 @@ +using System.Globalization; +using System.IO; + +namespace Timeline.Services +{ + public interface IDatabaseBackupService + { + void BackupNow(); + } + + public class DatabaseBackupService : IDatabaseBackupService + { + private readonly IPathProvider _pathProvider; + private readonly IClock _clock; + + public DatabaseBackupService(IPathProvider pathProvider, IClock clock) + { + _pathProvider = pathProvider; + _clock = clock; + } + + public void BackupNow() + { + var databasePath = _pathProvider.GetDatabaseFilePath(); + if (File.Exists(databasePath)) + { + var backupDirPath = _pathProvider.GetDatabaseBackupDirectory(); + Directory.CreateDirectory(backupDirPath); + var fileName = _clock.GetCurrentTime().ToString("yyyy-MM-ddTHH-mm-ss", CultureInfo.InvariantCulture); + var path = Path.Combine(backupDirPath, fileName); + File.Copy(databasePath, path); + } + } + } +} diff --git a/Timeline/Services/PathProvider.cs b/Timeline/Services/PathProvider.cs index ac7cd800..1baba5c0 100644 --- a/Timeline/Services/PathProvider.cs +++ b/Timeline/Services/PathProvider.cs @@ -8,6 +8,7 @@ namespace Timeline.Services { public string GetWorkingDirectory(); public string GetDatabaseFilePath(); + public string GetDatabaseBackupDirectory(); } public class PathProvider : IPathProvider @@ -32,5 +33,10 @@ namespace Timeline.Services { return Path.Combine(_workingDirectory, ApplicationConfiguration.DatabaseFileName); } + + public string GetDatabaseBackupDirectory() + { + return Path.Combine(_workingDirectory, ApplicationConfiguration.DatabaseBackupDirectoryName); + } } } -- cgit v1.2.3