aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services/DatabaseBackupService.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-06-14 22:18:25 +0800
committercrupest <crupest@outlook.com>2020-06-14 22:18:25 +0800
commitcf2cfa4853944ce0af6b6c22a089b937dd59ccaf (patch)
tree7a18386165130e83bc1fa980a7796e2a83e6894a /Timeline/Services/DatabaseBackupService.cs
parentfd4a16df9d67ac017ea5c7f7ca5e4ef45f16c6a2 (diff)
downloadtimeline-cf2cfa4853944ce0af6b6c22a089b937dd59ccaf.tar.gz
timeline-cf2cfa4853944ce0af6b6c22a089b937dd59ccaf.tar.bz2
timeline-cf2cfa4853944ce0af6b6c22a089b937dd59ccaf.zip
feat(back): Add backup service and run it when start.
Diffstat (limited to 'Timeline/Services/DatabaseBackupService.cs')
-rw-r--r--Timeline/Services/DatabaseBackupService.cs35
1 files changed, 35 insertions, 0 deletions
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);
+ }
+ }
+ }
+}