public static void Execute(string[] args) { if (!CliUtils.ParseArgs(Options, args)) { return; } if (_isHelp) { PrintUsage(); return; } if (string.IsNullOrEmpty(_directory)) { _directory = $"backup/{DateTime.Now:yyyy-MM-dd}"; } var treeInfo = new TreeInfo(_directory); DirectoryUtils.CreateDirectoryIfNotExists(treeInfo.DirectoryPath); ConfigInfo configInfo; if (!string.IsNullOrEmpty(_databaseType) && !string.IsNullOrEmpty(_connectionString)) { configInfo = CliUtils.LoadConfigByArgs(_databaseType, _connectionString); } else { configInfo = CliUtils.LoadConfigByFile(_configFileName); } if (configInfo == null) { CliUtils.PrintError("Error, config not exists"); return; } if (string.IsNullOrEmpty(WebConfigUtils.ConnectionString)) { CliUtils.PrintError("Error, connection string is empty"); return; } if (!DataProvider.DatabaseDao.IsConnectionStringWork(WebConfigUtils.DatabaseType, WebConfigUtils.ConnectionString)) { CliUtils.PrintError("Error, can not connect to the database"); return; } Console.WriteLine($"Database Type: {WebConfigUtils.DatabaseType.Value}"); Console.WriteLine($"Connection String: {WebConfigUtils.ConnectionString}"); Console.WriteLine($"Backup Directory: {treeInfo.DirectoryPath}"); var tableNames = DataProvider.DatabaseDao.GetTableNameList(); FileUtils.WriteText(treeInfo.TablesFilePath, Encoding.UTF8, TranslateUtils.JsonSerialize(tableNames)); CliUtils.PrintLine(); CliUtils.PrintRow("Backup Table Name", "Total Count"); CliUtils.PrintLine(); foreach (var tableName in tableNames) { if (configInfo.BackupConfig.Includes != null) { if (!StringUtils.ContainsIgnoreCase(configInfo.BackupConfig.Includes, tableName)) { continue; } } if (configInfo.BackupConfig.Excludes != null) { if (StringUtils.ContainsIgnoreCase(configInfo.BackupConfig.Includes, tableName)) { continue; } } var tableInfo = new TableInfo { Columns = DataProvider.DatabaseDao.GetTableColumnInfoList(WebConfigUtils.ConnectionString, tableName), TotalCount = DataProvider.DatabaseDao.GetCount(tableName), RowFiles = new List <string>() }; CliUtils.PrintRow(tableName, tableInfo.TotalCount.ToString("#,0")); var identityColumnName = DataProvider.DatabaseDao.AddIdentityColumnIdIfNotExists(tableName, tableInfo.Columns); if (tableInfo.TotalCount > 0) { var current = 1; if (tableInfo.TotalCount > CliUtils.PageSize) { var pageCount = (int)Math.Ceiling((double)tableInfo.TotalCount / CliUtils.PageSize); using (var progress = new ProgressBar()) { for (; current <= pageCount; current++) { progress.Report((double)(current - 1) / pageCount); var fileName = $"{current}.json"; tableInfo.RowFiles.Add(fileName); var offset = (current - 1) * CliUtils.PageSize; var limit = CliUtils.PageSize; var rows = DataProvider.DatabaseDao.GetPageObjects(tableName, identityColumnName, offset, limit); FileUtils.WriteText(treeInfo.GetTableContentFilePath(tableName, fileName), Encoding.UTF8, TranslateUtils.JsonSerialize(rows)); } } } else { var fileName = $"{current}.json"; tableInfo.RowFiles.Add(fileName); var rows = DataProvider.DatabaseDao.GetObjects(tableName); FileUtils.WriteText(treeInfo.GetTableContentFilePath(tableName, fileName), Encoding.UTF8, TranslateUtils.JsonSerialize(rows)); } } FileUtils.WriteText(treeInfo.GetTableMetadataFilePath(tableName), Encoding.UTF8, TranslateUtils.JsonSerialize(tableInfo)); } CliUtils.PrintLine(); Console.WriteLine("Well done! Thanks for Using SiteServer Cli Tool"); }
public static async Task Execute(IJobContext context) { if (!CliUtils.ParseArgs(Options, context.Args)) { return; } if (_isHelp) { PrintUsage(); return; } if (string.IsNullOrEmpty(_directory)) { await CliUtils.PrintErrorAsync("Error, the restore {directory} name is empty"); return; } var treeInfo = new TreeInfo(_directory); if (!DirectoryUtils.IsDirectoryExists(treeInfo.DirectoryPath)) { await CliUtils.PrintErrorAsync($"Error, directory {treeInfo.DirectoryPath} not exists"); return; } var tablesFilePath = treeInfo.TablesFilePath; if (!FileUtils.IsFileExists(tablesFilePath)) { await CliUtils.PrintErrorAsync($"Error, file {treeInfo.TablesFilePath} not exists"); return; } ConfigInfo configInfo; if (!string.IsNullOrEmpty(_databaseType) && !string.IsNullOrEmpty(_connectionString)) { configInfo = CliUtils.LoadConfigByArgs(_databaseType, _connectionString); } else { configInfo = await CliUtils.LoadConfigByFileAsync(_configFileName); } if (configInfo == null) { await CliUtils.PrintErrorAsync("Error, config not exists"); return; } if (string.IsNullOrEmpty(WebConfigUtils.ConnectionString)) { await CliUtils.PrintErrorAsync("Error, connection string is empty"); return; } if (!DataProvider.DatabaseDao.IsConnectionStringWork(WebConfigUtils.DatabaseType, WebConfigUtils.ConnectionString)) { await CliUtils.PrintErrorAsync("Error, can not connect to the database"); return; } await Console.Out.WriteLineAsync($"Database Type: {WebConfigUtils.DatabaseType.Value}"); await Console.Out.WriteLineAsync($"Connection String: {WebConfigUtils.ConnectionString}"); await Console.Out.WriteLineAsync($"Restore Directory: {treeInfo.DirectoryPath}"); var tableNames = TranslateUtils.JsonDeserialize <List <string> >(await FileUtils.ReadTextAsync(tablesFilePath, Encoding.UTF8)); await CliUtils.PrintRowLineAsync(); await CliUtils.PrintRowAsync("Import Table Name", "Total Count"); await CliUtils.PrintRowLineAsync(); var errorLogFilePath = CliUtils.CreateErrorLogFile(CommandName); foreach (var tableName in tableNames) { var logs = new List <TextLogInfo>(); if (configInfo.RestoreConfig.Includes != null) { if (!StringUtils.ContainsIgnoreCase(configInfo.RestoreConfig.Includes, tableName)) { continue; } } if (configInfo.RestoreConfig.Excludes != null) { if (StringUtils.ContainsIgnoreCase(configInfo.RestoreConfig.Excludes, tableName)) { continue; } } var metadataFilePath = treeInfo.GetTableMetadataFilePath(tableName); if (!FileUtils.IsFileExists(metadataFilePath)) { continue; } var tableInfo = TranslateUtils.JsonDeserialize <TableInfo>(await FileUtils.ReadTextAsync(metadataFilePath, Encoding.UTF8)); await CliUtils.PrintRowAsync(tableName, tableInfo.TotalCount.ToString("#,0")); if (!DataProvider.DatabaseDao.IsTableExists(tableName)) { if (!DataProvider.DatabaseDao.CreateSystemTable(tableName, tableInfo.Columns, out var ex, out var sqlString)) { logs.Add(new TextLogInfo { DateTime = DateTime.Now, Detail = $"create table {tableName}: {sqlString}", Exception = ex }); continue; } } else { DataProvider.DatabaseDao.AlterSystemTable(tableName, tableInfo.Columns); } using (var progress = new ProgressBar()) { for (var i = 0; i < tableInfo.RowFiles.Count; i++) { progress.Report((double)i / tableInfo.RowFiles.Count); var fileName = tableInfo.RowFiles[i]; var objects = TranslateUtils.JsonDeserialize <List <JObject> >( await FileUtils.ReadTextAsync(treeInfo.GetTableContentFilePath(tableName, fileName), Encoding.UTF8)); try { DataProvider.DatabaseDao.InsertMultiple(tableName, objects, tableInfo.Columns); } catch (Exception ex) { logs.Add(new TextLogInfo { DateTime = DateTime.Now, Detail = $"insert table {tableName}, fileName {fileName}", Exception = ex }); } } } await CliUtils.AppendErrorLogsAsync(errorLogFilePath, logs); } await CliUtils.PrintRowLineAsync(); if (configInfo.RestoreConfig.SyncDatabase) { SystemManager.SyncDatabase(); } await Console.Out.WriteLineAsync("Well done! Thanks for Using SiteServer Cli Tool"); }