aboutsummaryrefslogtreecommitdiff
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
parentfd4a16df9d67ac017ea5c7f7ca5e4ef45f16c6a2 (diff)
downloadtimeline-cf2cfa4853944ce0af6b6c22a089b937dd59ccaf.tar.gz
timeline-cf2cfa4853944ce0af6b6c22a089b937dd59ccaf.tar.bz2
timeline-cf2cfa4853944ce0af6b6c22a089b937dd59ccaf.zip
feat(back): Add backup service and run it when start.
-rw-r--r--Timeline/Configs/ApplicationConfiguration.cs1
-rw-r--r--Timeline/Program.cs5
-rw-r--r--Timeline/Services/DatabaseBackupService.cs35
-rw-r--r--Timeline/Services/PathProvider.cs6
-rw-r--r--Timeline/Startup.cs2
5 files changed, 49 insertions, 0 deletions
diff --git a/Timeline/Configs/ApplicationConfiguration.cs b/Timeline/Configs/ApplicationConfiguration.cs
index c84327d7..64ab6f6c 100644
--- a/Timeline/Configs/ApplicationConfiguration.cs
+++ b/Timeline/Configs/ApplicationConfiguration.cs
@@ -5,6 +5,7 @@
public const string WorkDirKey = "WorkDir";
public const string DefaultWorkDir = "/timeline";
public const string DatabaseFileName = "timeline.db";
+ public const string DatabaseBackupDirectoryName = "backup";
public const string DisableFrontEndKey = "DisableFrontEnd";
public const string FrontEndProxyOnlyKey = "FrontEndProxyOnly";
public const string UseMockFrontEndKey = "UseMockFrontEnd";
diff --git a/Timeline/Program.cs b/Timeline/Program.cs
index c49f74b0..0540fbd1 100644
--- a/Timeline/Program.cs
+++ b/Timeline/Program.cs
@@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Resources;
using Timeline.Entities;
+using Timeline.Services;
[assembly: NeutralResourcesLanguage("en")]
@@ -17,6 +18,10 @@ namespace Timeline
var host = CreateWebHostBuilder(args).Build();
var env = host.Services.GetRequiredService<IWebHostEnvironment>();
+
+ var databaseBackupService = host.Services.GetRequiredService<IDatabaseBackupService>();
+ databaseBackupService.BackupNow();
+
if (env.IsProduction())
{
using (var scope = host.Services.CreateScope())
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);
+ }
}
}
diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs
index 597fa4c3..a5eab4ad 100644
--- a/Timeline/Startup.cs
+++ b/Timeline/Startup.cs
@@ -64,6 +64,8 @@ namespace Timeline
services.AddSingleton<IPathProvider, PathProvider>();
+ services.AddSingleton<IDatabaseBackupService, DatabaseBackupService>();
+
services.AddAutoMapper(GetType().Assembly);
services.AddTransient<IClock, Clock>();