Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BackupConfiguration.cs
1using System;
2using System.Reflection;
3using System.Threading.Tasks;
7using Waher.Content;
8using System.IO;
9
11{
16 {
17 private static BackupConfiguration instance = null;
18
19 private HttpFolderResource exportFolder = null;
20 private HttpFolderResource keyFolder = null;
21 private StartExport startExport = null;
22 private StartAnalyze startAnalyze = null;
23 private DeleteExport deleteExport = null;
24 private UpdateBackupSettings updateBackupSettings = null;
25 private UpdateBackupFolderSettings updateBackupFolderSettings = null;
26
30 public static BackupConfiguration Instance => instance;
31
35 public override string Resource => "/Settings/Backup.md";
36
40 public override int Priority => 175;
41
47 public override Task<string> Title(Language Language)
48 {
49 return Language.GetStringAsync(typeof(Gateway), 7, "Backup");
50 }
51
55 public override async Task ConfigureSystem()
56 {
57 await this.UpdateExportFolder(await Export.GetFullExportFolderAsync());
58 this.UpdateExportKeyFolder(await Export.GetFullKeyExportFolderAsync());
59 }
60
65 public override void SetStaticInstance(ISystemConfiguration Configuration)
66 {
67 instance = Configuration as BackupConfiguration;
68 }
69
74 public override async Task InitSetup(HttpServer WebServer)
75 {
76 HttpAuthenticationScheme Auth = Gateway.LoggedIn(new string[] { this.ConfigPrivilege });
77
78 WebServer.Register(this.exportFolder = new HttpFolderResource("/Export", await Export.GetFullExportFolderAsync(), false, false, false, true, HostDomainOptions.SameForAllDomains, Auth));
79 WebServer.Register(this.keyFolder = new HttpFolderResource("/Key", await Export.GetFullKeyExportFolderAsync(), false, false, false, true, HostDomainOptions.SameForAllDomains, Auth));
80 WebServer.Register(this.startExport = new StartExport());
81 WebServer.Register(this.startAnalyze = new StartAnalyze());
82 WebServer.Register(this.deleteExport = new DeleteExport());
83 WebServer.Register(this.updateBackupSettings = new UpdateBackupSettings());
84 WebServer.Register(this.updateBackupFolderSettings = new UpdateBackupFolderSettings());
85
86 await base.InitSetup(WebServer);
87 }
88
92 protected override string ConfigPrivilege => "Admin.Data.Backup";
93
98 public override Task UnregisterSetup(HttpServer WebServer)
99 {
100 WebServer.Unregister(this.exportFolder);
101 WebServer.Unregister(this.keyFolder);
102 WebServer.Unregister(this.startExport);
103 WebServer.Unregister(this.startAnalyze);
104 WebServer.Unregister(this.deleteExport);
105 WebServer.Unregister(this.updateBackupSettings);
106 WebServer.Unregister(this.updateBackupFolderSettings);
107
108 return base.UnregisterSetup(WebServer);
109 }
110
111 internal async Task UpdateExportFolder(string Folder)
112 {
113 if (!(this.exportFolder is null))
114 this.exportFolder.FolderPath = Folder;
115
116 if (!(Gateway.InternalDatabase is null))
117 {
118 Type T = Gateway.InternalDatabase.GetType();
119 PropertyInfo PI = T.GetProperty("AutoRepairReportFolder");
120
121 if (!(PI is null) && PI.PropertyType == typeof(string))
122 PI.SetValue(Gateway.InternalDatabase, await Export.GetFullExportFolderAsync(), null);
123 }
124 }
125
126 internal void UpdateExportKeyFolder(string Folder)
127 {
128 if (!(this.keyFolder is null))
129 this.keyFolder.FolderPath = Folder;
130 }
131
136 public override Task<bool> SimplifiedConfiguration()
137 {
138 return Task.FromResult(true);
139 }
140
144 public const string GATEWAY_BACKUP = nameof(GATEWAY_BACKUP);
145
149 public const string GATEWAY_BACKUP_TIME = nameof(GATEWAY_BACKUP_TIME);
150
154 public const string GATEWAY_BACKUP_DAYS = nameof(GATEWAY_BACKUP_DAYS);
155
159 public const string GATEWAY_BACKUP_MONTHS = nameof(GATEWAY_BACKUP_MONTHS);
160
164 public const string GATEWAY_BACKUP_YEARS = nameof(GATEWAY_BACKUP_YEARS);
165
169 public const string GATEWAY_BACKUP_FOLDER = nameof(GATEWAY_BACKUP_FOLDER);
170
174 public const string GATEWAY_KEY_FOLDER = nameof(GATEWAY_KEY_FOLDER);
175
179 public const string GATEWAY_BACKUP_HOSTS = nameof(GATEWAY_BACKUP_HOSTS);
180
184 public const string GATEWAY_KEY_HOSTS = nameof(GATEWAY_KEY_HOSTS);
185
190 public override async Task<bool> EnvironmentConfiguration()
191 {
192 if (!this.TryGetEnvironmentVariable(GATEWAY_BACKUP, false, out bool Backup))
193 return false;
194
195 if (!Backup)
196 return true;
197
199
200 string s = Environment.GetEnvironmentVariable(GATEWAY_BACKUP_TIME);
201 if (!string.IsNullOrEmpty(s))
202 {
203 if (!TimeSpan.TryParse(s, out TimeSpan BackupTime) || BackupTime < TimeSpan.Zero || BackupTime.TotalHours >= 24)
204 {
205 this.LogEnvironmentVariableInvalidTimeError(GATEWAY_BACKUP_TIME, s);
206 return false;
207 }
208
209 await Export.SetBackupTimeAsync(BackupTime);
210 }
211
212 s = Environment.GetEnvironmentVariable(GATEWAY_BACKUP_DAYS);
213 if (!string.IsNullOrEmpty(s))
214 {
215 if (!int.TryParse(s, out int i) || i < 0)
216 {
217 this.LogEnvironmentVariableInvalidIntegerError(GATEWAY_BACKUP_DAYS, s);
218 return false;
219 }
220
221 await Export.SetKeepDaysAsync(i);
222 }
223
224 s = Environment.GetEnvironmentVariable(GATEWAY_BACKUP_MONTHS);
225 if (!string.IsNullOrEmpty(s))
226 {
227 if (!int.TryParse(s, out int i) || i < 0)
228 {
229 this.LogEnvironmentVariableInvalidIntegerError(GATEWAY_BACKUP_MONTHS, s);
230 return false;
231 }
232
233 await Export.SetKeepMonthsAsync(i);
234 }
235
236 s = Environment.GetEnvironmentVariable(GATEWAY_BACKUP_YEARS);
237 if (!string.IsNullOrEmpty(s))
238 {
239 if (!int.TryParse(s, out int i) || i < 0)
240 {
241 this.LogEnvironmentVariableInvalidIntegerError(GATEWAY_BACKUP_YEARS, s);
242 return false;
243 }
244
245 await Export.SetKeepYearsAsync(i);
246 }
247
248 if (this.TryGetEnvironmentVariable(GATEWAY_BACKUP_FOLDER, false, out s))
249 {
250 try
251 {
252 if (!Directory.Exists(s))
253 Directory.CreateDirectory(s);
254
256 }
257 catch (Exception ex)
258 {
259 this.LogEnvironmentError(ex.Message, GATEWAY_BACKUP_FOLDER, s);
260 }
261 }
262
263 s = Environment.GetEnvironmentVariable(GATEWAY_KEY_FOLDER);
264 if (!string.IsNullOrEmpty(s))
265 {
266 try
267 {
268 if (!Directory.Exists(s))
269 Directory.CreateDirectory(s);
270
272 }
273 catch (Exception ex)
274 {
275 this.LogEnvironmentError(ex.Message, GATEWAY_KEY_FOLDER, s);
276 }
277 }
278
279 s = Environment.GetEnvironmentVariable(GATEWAY_BACKUP_HOSTS);
280 if (!string.IsNullOrEmpty(s))
281 await Export.SetBackupHostsAsync(s.Split(','));
282
283 s = Environment.GetEnvironmentVariable(GATEWAY_KEY_HOSTS);
284 if (!string.IsNullOrEmpty(s))
285 await Export.SetKeyHostsAsync(s.Split(','));
286
287 return true;
288 }
289
290 }
291}
Static class managing data export.
Definition: Export.cs:19
static async Task< string > GetFullExportFolderAsync()
Full path to export folder.
Definition: Export.cs:23
static async Task< string > GetFullKeyExportFolderAsync()
Full path to key folder.
Definition: Export.cs:36
static async Task SetKeepDaysAsync(long Value)
For how many days backups are kept.
Definition: Export.cs:392
static async Task SetKeepMonthsAsync(long Value)
For how many months the monthly backups are kept.
Definition: Export.cs:418
static async Task SetAutomaticBackupsAsync(bool Value)
If automatic backups are activated
Definition: Export.cs:366
static async Task SetKeyHostsAsync(string[] Value)
Secondary key hosts.
Definition: Export.cs:620
static async Task SetBackupTimeAsync(TimeSpan Value)
Time of day to start performing backups.
Definition: Export.cs:470
static async Task SetBackupHostsAsync(string[] Value)
Secondary backup hosts.
Definition: Export.cs:594
static async Task SetKeepYearsAsync(long Value)
For how many years the yearly backups are kept.
Definition: Export.cs:444
static async Task SetExportFolderAsync(string Value)
Export folder.
Definition: Export.cs:159
static async Task SetExportKeyFolderAsync(string Value)
Key folder
Definition: Export.cs:195
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static RequiredUserPrivileges LoggedIn(string[] Privileges)
Authentication mechanism that makes sure the call is made from a session with a valid authenticated u...
Definition: Gateway.cs:3001
static IDatabaseProvider InternalDatabase
Local Internal Encrypted Object Database provider.
Definition: Gateway.cs:2408
const string GATEWAY_BACKUP
Environment variable name containing a Boolean value if automatic backups should be made or not.
override void SetStaticInstance(ISystemConfiguration Configuration)
Sets the static instance of the configuration.
override string Resource
Resource to be redirected to, to perform the configuration.
const string GATEWAY_BACKUP_FOLDER
Environment variable name containing the backup folder, if different from the default.
override async Task< bool > EnvironmentConfiguration()
Environment configuration by configuring values available in environment variables.
const string GATEWAY_BACKUP_MONTHS
Environment variable name containing the number of months monthly backups are kept.
const string GATEWAY_KEY_HOSTS
Environment variable name containing a comma-delimited list of secondary key hosts for redundant stor...
override int Priority
Priority of the setting. Configurations are sorted in ascending order.
override Task< string > Title(Language Language)
Gets a title for the system configuration.
override async Task InitSetup(HttpServer WebServer)
Initializes the setup object.
override async Task ConfigureSystem()
Is called during startup to configure the system.
const string GATEWAY_KEY_FOLDER
Environment variable name containing the key folder, if different from the default.
static BackupConfiguration Instance
Current instance of configuration.
const string GATEWAY_BACKUP_DAYS
Environment variable name containing the number of days daily backups are kept.
const string GATEWAY_BACKUP_HOSTS
Environment variable name containing a comma-delimited list of secondary backup hosts for redundant s...
override Task UnregisterSetup(HttpServer WebServer)
Unregisters the setup object.
override string ConfigPrivilege
Minimum required privilege for a user to be allowed to change the configuration defined by the class.
const string GATEWAY_BACKUP_YEARS
Environment variable name containing the number of years yearly backups are kept.
const string GATEWAY_BACKUP_TIME
Environment variable name containing a TimeSpan value determining the time of day automatic backups a...
override Task< bool > SimplifiedConfiguration()
Simplified configuration by configuring simple default values.
Abstract base class for system configurations.
void LogEnvironmentVariableInvalidIntegerError(string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value is not a valid int...
void LogEnvironmentError(string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value contains an error.
bool TryGetEnvironmentVariable(string VariableName, bool Required, out string Value)
Tries to get a string-valued environment variable.
void LogEnvironmentVariableInvalidTimeError(string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value is not a valid tim...
Starts analyzing the database
Definition: StartAnalyze.cs:22
Web Resource for updating where backup files are storeed.
Web Resource for updating backup settings.
Base class for all HTTP authentication schemes, as defined in RFC-7235: https://datatracker....
Publishes a folder with all its files and subfolders through HTTP GET, with optional support for PUT,...
Implements an HTTP server.
Definition: HttpServer.cs:36
HttpResource Register(HttpResource Resource)
Registers a resource with the server.
Definition: HttpServer.cs:1287
bool Unregister(HttpResource Resource)
Unregisters a resource from the server.
Definition: HttpServer.cs:1438
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Interface for system configurations. The gateway will scan all module for system configuration classe...
HostDomainOptions
Options on how to handle domain names provided in the Host header.