-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
139 lines (124 loc) · 6.33 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using backup_manager.BackupWorkers;
using backup_manager.Cypher;
using backup_manager.Interfaces;
using backup_manager.Model;
using backup_manager.Servers;
using backup_manager.Settings;
using backup_manager.Workers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;
using static backup_manager.Model.Enums;
namespace backup_manager
{
internal class Program
{
static string sshClientAddress = "10.10.102.1";
static string backupCmd = "backup startup-configuration to 10.10.200.37 test3.cfg";
static string backupCmd1 = "copy running-configuration 10.10.200.37 run.cfg";
static string ServerDirectory = Environment.CurrentDirectory;
static async Task Main(string[] args)
{
var logger = LogManager.GetCurrentClassLogger();
try
{
var config = new ConfigurationBuilder()
//.SetBasePath(System.IO.Directory.GetCurrentDirectory())
//.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
var servicesProvider = BuildDi(config);
using (servicesProvider as IDisposable)
{
var conf = servicesProvider.GetRequiredService<IConfigurator>();
if(args.Length != 0)
{
foreach (var parameter in args)
{
switch (parameter.ToLower())
{
case "-setreq":
Console.WriteLine($"Set req init.");
Console.WriteLine("Введите id, логин, пароль через пробел:");
while (true)
{
var loginAdnPass = (Console.ReadLine().Split());
if (loginAdnPass.Length != 3 || string.IsNullOrEmpty(loginAdnPass[0])
|| string.IsNullOrEmpty(loginAdnPass[1]) || string.IsNullOrEmpty(loginAdnPass[2]))
{
Console.WriteLine("Ошибка ввода. Введите id, логин, пароль через пробел: ");
continue;
}
else
{
int loginId = 0;
int.TryParse(loginAdnPass[0], out loginId);
conf.SaveLoginSettings(new Login(loginId, loginAdnPass[1], loginAdnPass[2]));
var addedReq = conf.LoadLoginSettings();
Console.WriteLine($"Added reqs: {addedReq[addedReq.Count - 1].AdmLogin} " +
$"{addedReq[addedReq.Count - 1].AdminPass}");
}
Console.WriteLine("Press Enter to exit.");
if (Console.ReadKey().Key == ConsoleKey.Enter)
break;
}
break;
}
}
}
var backupManager = servicesProvider.GetRequiredService<IBackupManager>();
var reqs = conf.LoadLoginSettings();
var deviceConfigs = conf.LoadDeviceSettings(reqs);
var dbTestConfigs = conf.LoadTestDbConfigs(reqs);
var dbConfigs = conf.LoadDbSettings(reqs);
var backupPaths = conf.LoadPathSettings();
var sftpTempPath = conf.LoadSftpTempFolderPath();
var clearAfterInDays = conf.LoadClearAfterInDays();
var dbTempPath = conf.LoadDbTempFolderPath();
var dbRestoreDataFolder = conf.LoadDbRestoreDataFolder();
/*if(deviceConfigs.Any(d => d.BackupCmdType == BackupCmdTypes.HP))
{
var sftpServer = servicesProvider.GetRequiredService<ISftpServer>();
Task.Run(() => sftpServer.RunSftpServer(sftpTempPath));
}*/
await backupManager.Init(deviceConfigs, dbTestConfigs, dbConfigs, backupPaths, clearAfterInDays, sftpTempPath,
dbTempPath, dbRestoreDataFolder);
}
}
catch (Exception ex)
{
// NLog: catch any exception and log it.
logger.Error(ex, "Stopped program because of exception");
throw;
}
finally
{
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
LogManager.Shutdown();
}
}
private static IServiceProvider BuildDi(IConfiguration config)
{
return new ServiceCollection()
// Add DI Classes here
.AddSingleton<ICypher, Encryptor>()
.AddSingleton<IConfigurator, Configurator>()
.AddTransient<ITftpServer, TftpServer>()
.AddTransient<ISftpServer, SftpServer>()
.AddTransient<IBackupManager, BackupManager>()
.AddTransient<ISshWorker, SshWorker>()
.AddTransient<ISshShellWorker, SshShellWorker>()
.AddTransient<IZipWorker, ZipWorker>()
.AddTransient<ISqlWorker, SqlWorker>()
.AddLogging(loggingBuilder =>
{
// configure Logging with NLog
loggingBuilder.ClearProviders();
loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
loggingBuilder.AddNLog(config);
})
.BuildServiceProvider();
}
}
}