Exemplo n.º 1
0
        private static async Task RunRepeatAsync(string schedule)
        {
            try
            {
                var factory = new StdSchedulerFactory(new NameValueCollection
                {
                    { "quartz.serializer.type", "binary" }
                });
                var sched = await factory.GetScheduler();

                await sched.Start();

                var job = JobBuilder.Create <SchedJob>()
                          .WithIdentity("job1", "group1")
                          .Build();

                var trigger = TriggerBuilder.Create()
                              .WithIdentity("trigger1", "group1")
                              .StartNow()
                              .WithCronSchedule(schedule)
                              .WithPriority(1)
                              .Build();

                await sched.ScheduleJob(job, trigger);

                await Task.Delay(-1);

                await sched.Shutdown();
            }
            catch (Exception ex)
            {
                await CliUtils.PrintErrorAsync(ex.Message);
            }
        }
Exemplo n.º 2
0
        public static async Task RunExecuteAsync(string commandName, string[] commandArgs, IJobExecutionContext jobContext)
        {
            try
            {
                Func <IJobContext, Task> job;
                if (Jobs.TryGetValue(commandName, out job))
                {
                    if (job != null)
                    {
                        var context = new JobContextImpl(commandName, commandArgs, jobContext);
                        await job(context);
                    }
                }
            }
            catch (Exception ex)
            {
                await CliUtils.PrintErrorAsync(ex.Message);

                var errorLogFilePath = CliUtils.CreateErrorLogFile("siteserver");

                await CliUtils.AppendErrorLogsAsync(errorLogFilePath, new List <TextLogInfo>
                {
                    new TextLogInfo
                    {
                        DateTime  = DateTime.Now,
                        Detail    = "Console Error",
                        Exception = ex
                    }
                });
            }
        }
Exemplo n.º 3
0
        public async Task Execute(IJobContext context)
        {
            if (!CliUtils.ParseArgs(_options, context.Args))
            {
                return;
            }

            if (_isHelp)
            {
                PrintUsage();
                return;
            }

            if (string.IsNullOrEmpty(_directory))
            {
                await CliUtils.PrintErrorAsync("需要指定恢复数据的文件夹名称:directory");

                return;
            }

            var treeInfo = new TreeInfo(_directory);

            if (!DirectoryUtils.IsDirectoryExists(treeInfo.DirectoryPath))
            {
                await CliUtils.PrintErrorAsync($"恢复数据的文件夹 {treeInfo.DirectoryPath} 不存在");

                return;
            }

            var tablesFilePath = treeInfo.TablesFilePath;

            if (!FileUtils.IsFileExists(tablesFilePath))
            {
                await CliUtils.PrintErrorAsync($"恢复文件 {treeInfo.TablesFilePath} 不存在");

                return;
            }

            var webConfigPath = CliUtils.GetWebConfigPath(_configFile);

            if (!FileUtils.IsFileExists(webConfigPath))
            {
                await CliUtils.PrintErrorAsync($"系统配置文件不存在:{webConfigPath}!");

                return;
            }

            WebConfigUtils.Load(CliUtils.PhysicalApplicationPath, webConfigPath);

            if (string.IsNullOrEmpty(WebConfigUtils.ConnectionString))
            {
                await CliUtils.PrintErrorAsync($"{webConfigPath} 中数据库连接字符串 connectionString 未设置");

                return;
            }

            await Console.Out.WriteLineAsync($"数据库类型: {WebConfigUtils.DatabaseType.Value}");

            await Console.Out.WriteLineAsync($"连接字符串: {WebConfigUtils.ConnectionString}");

            await Console.Out.WriteLineAsync($"恢复文件夹: {treeInfo.DirectoryPath}");

            if (!DataProvider.DatabaseDao.IsConnectionStringWork(WebConfigUtils.DatabaseType, WebConfigUtils.ConnectionString))
            {
                await CliUtils.PrintErrorAsync($"系统无法连接到 {webConfigPath} 中设置的数据库");

                return;
            }

            if (!_dataOnly)
            {
                if (!SystemManager.IsNeedInstall())
                {
                    await CliUtils.PrintErrorAsync("数据无法在已安装系统的数据库中恢复,命令执行失败");

                    return;
                }

                // 恢复前先创建表,确保系统在恢复的数据库中能够使用
                SystemManager.CreateSiteServerTables();
            }

            await CliUtils.PrintRowLineAsync();

            await CliUtils.PrintRowAsync("恢复表名称", "总条数");

            await CliUtils.PrintRowLineAsync();

            var errorLogFilePath = CliUtils.CreateErrorLogFile(CommandName);

            await Restore(_includes, _excludes, _dataOnly, tablesFilePath, treeInfo, errorLogFilePath);

            await Console.Out.WriteLineAsync($"恭喜,成功从文件夹:{treeInfo.DirectoryPath} 恢复数据!");
        }
Exemplo n.º 4
0
        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("未指定需要转换至最新版本的备份数据文件夹:directory");

                return;
            }

            var oldTreeInfo = new TreeInfo(_directory);
            var newTreeInfo = new TreeInfo(Folder);

            if (!DirectoryUtils.IsDirectoryExists(oldTreeInfo.DirectoryPath))
            {
                await CliUtils.PrintErrorAsync($"备份数据的文件夹 {oldTreeInfo.DirectoryPath} 不存在");

                return;
            }
            DirectoryUtils.CreateDirectoryIfNotExists(newTreeInfo.DirectoryPath);

            var updater = new UpdaterManager(oldTreeInfo, newTreeInfo);

            var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            await Console.Out.WriteLineAsync($"备份数据文件夹: {oldTreeInfo.DirectoryPath},升级数据文件夹: {newTreeInfo.DirectoryPath},升级版本: {version.Substring(0, version.Length - 2)}");

            var oldTableNames = TranslateUtils.JsonDeserialize <List <string> >(await FileUtils.ReadTextAsync(oldTreeInfo.TablesFilePath, Encoding.UTF8));
            var newTableNames = new List <string>();

            await CliUtils.PrintRowLineAsync();

            await CliUtils.PrintRowAsync("备份表名称", "升级表名称", "总条数");

            await CliUtils.PrintRowLineAsync();

            var siteIdList = new List <int>();
            var tableNameListForContent     = new List <string>();
            var tableNameListForGovPublic   = new List <string>();
            var tableNameListForGovInteract = new List <string>();
            var tableNameListForJob         = new List <string>();

            UpdateUtils.LoadSites(oldTreeInfo, siteIdList,
                                  tableNameListForContent, tableNameListForGovPublic, tableNameListForGovInteract, tableNameListForJob);

            var splitSiteTableDict = new Dictionary <int, TableInfo>();

            if (_contentSplit)
            {
                var converter = ContentConverter.GetSplitConverter();
                foreach (var siteId in siteIdList)
                {
                    splitSiteTableDict.Add(siteId, new TableInfo
                    {
                        Columns    = converter.NewColumns,
                        TotalCount = 0,
                        RowFiles   = new List <string>()
                    });
                }
            }

            foreach (var oldTableName in oldTableNames)
            {
                var oldMetadataFilePath = oldTreeInfo.GetTableMetadataFilePath(oldTableName);

                if (!FileUtils.IsFileExists(oldMetadataFilePath))
                {
                    continue;
                }

                var oldTableInfo = TranslateUtils.JsonDeserialize <TableInfo>(await FileUtils.ReadTextAsync(oldMetadataFilePath, Encoding.UTF8));

                if (StringUtils.ContainsIgnoreCase(tableNameListForContent, oldTableName))
                {
                    if (_contentSplit)
                    {
                        var converter = ContentConverter.GetConverter(oldTableName, oldTableInfo.Columns);

                        await updater.UpdateSplitContentsTableInfoAsync(splitSiteTableDict, siteIdList, oldTableName,
                                                                        oldTableInfo, converter);
                    }
                    else
                    {
                        var converter = ContentConverter.GetConverter(oldTableName, oldTableInfo.Columns);
                        var tuple     = await updater.GetNewTableInfoAsync(oldTableName, oldTableInfo, converter);

                        if (tuple != null)
                        {
                            newTableNames.Add(tuple.Item1);

                            await FileUtils.WriteTextAsync(newTreeInfo.GetTableMetadataFilePath(tuple.Item1), Encoding.UTF8, TranslateUtils.JsonSerialize(tuple.Item2));
                        }
                    }
                }
                else
                {
                    var tuple = await updater.UpdateTableInfoAsync(oldTableName, oldTableInfo, tableNameListForGovPublic, tableNameListForGovInteract, tableNameListForJob);

                    if (tuple != null)
                    {
                        newTableNames.Add(tuple.Item1);

                        await FileUtils.WriteTextAsync(newTreeInfo.GetTableMetadataFilePath(tuple.Item1), Encoding.UTF8, TranslateUtils.JsonSerialize(tuple.Item2));
                    }
                }
            }

            if (_contentSplit)
            {
                foreach (var siteId in siteIdList)
                {
                    var siteTableInfo = splitSiteTableDict[siteId];
                    var siteTableName = ContentDao.GetContentTableName(siteId);
                    newTableNames.Add(siteTableName);

                    await FileUtils.WriteTextAsync(newTreeInfo.GetTableMetadataFilePath(siteTableName), Encoding.UTF8, TranslateUtils.JsonSerialize(siteTableInfo));
                }

                await UpdateUtils.UpdateSitesSplitTableNameAsync(newTreeInfo, splitSiteTableDict);
            }

            await FileUtils.WriteTextAsync(newTreeInfo.TablesFilePath, Encoding.UTF8, TranslateUtils.JsonSerialize(newTableNames));

            await CliUtils.PrintRowLineAsync();

            await Console.Out.WriteLineAsync($"恭喜,成功从备份文件夹:{oldTreeInfo.DirectoryPath} 升级至新版本:{newTreeInfo.DirectoryPath} !");
        }
Exemplo n.º 5
0
        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("需要指定恢复数据的文件夹名称:directory");

                return;
            }

            var treeInfo = new TreeInfo(_directory);

            if (!DirectoryUtils.IsDirectoryExists(treeInfo.DirectoryPath))
            {
                await CliUtils.PrintErrorAsync($"恢复数据的文件夹 {treeInfo.DirectoryPath} 不存在");

                return;
            }

            var tablesFilePath = treeInfo.TablesFilePath;

            if (!FileUtils.IsFileExists(tablesFilePath))
            {
                await CliUtils.PrintErrorAsync($"恢复文件 {treeInfo.TablesFilePath} 不存在");

                return;
            }

            var webConfigPath = PathUtils.Combine(CliUtils.PhysicalApplicationPath, "web.config");

            if (!FileUtils.IsFileExists(webConfigPath))
            {
                await CliUtils.PrintErrorAsync($"系统配置文件不存在:{webConfigPath}!");

                return;
            }

            if (string.IsNullOrEmpty(WebConfigUtils.ConnectionString))
            {
                await CliUtils.PrintErrorAsync("web.config 中数据库连接字符串 connectionString 未设置");

                return;
            }

            WebConfigUtils.Load(CliUtils.PhysicalApplicationPath, "web.config");

            await Console.Out.WriteLineAsync($"数据库类型: {WebConfigUtils.DatabaseType.Value}");

            await Console.Out.WriteLineAsync($"连接字符串: {WebConfigUtils.ConnectionString}");

            await Console.Out.WriteLineAsync($"恢复文件夹: {treeInfo.DirectoryPath}");

            if (!DataProvider.DatabaseDao.IsConnectionStringWork(WebConfigUtils.DatabaseType, WebConfigUtils.ConnectionString))
            {
                await CliUtils.PrintErrorAsync("系统无法连接到 web.config 中设置的数据库");

                return;
            }

            if (!SystemManager.IsNeedInstall())
            {
                await CliUtils.PrintErrorAsync("数据无法在已安装系统的数据库中恢复,命令执行失败");

                return;
            }

            // 恢复前先创建表,确保系统在恢复的数据库中能够使用
            SystemManager.CreateSiteServerTables();

            var tableNames = TranslateUtils.JsonDeserialize <List <string> >(await FileUtils.ReadTextAsync(tablesFilePath, Encoding.UTF8));

            await CliUtils.PrintRowLineAsync();

            await CliUtils.PrintRowAsync("恢复表名称", "总条数");

            await CliUtils.PrintRowLineAsync();

            var errorLogFilePath = CliUtils.CreateErrorLogFile(CommandName);

            foreach (var tableName in tableNames)
            {
                var logs = new List <TextLogInfo>();

                if (_includes != null)
                {
                    if (!StringUtils.ContainsIgnoreCase(_includes, tableName))
                    {
                        continue;
                    }
                }
                if (_excludes != null)
                {
                    if (StringUtils.ContainsIgnoreCase(_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    = $"创建表 {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    = $"插入表 {tableName}, 文件名 {fileName}",
                                Exception = ex
                            });
                        }
                    }
                }

                await CliUtils.AppendErrorLogsAsync(errorLogFilePath, logs);
            }

            await CliUtils.PrintRowLineAsync();

            // 恢复后同步表,确保内容辅助表字段与系统一致
            SystemManager.SyncContentTables();
            SystemManager.UpdateConfigVersion();

            await Console.Out.WriteLineAsync($"恭喜,成功从文件夹:{treeInfo.DirectoryPath} 恢复数据!");
        }
Exemplo n.º 6
0
        public static async Task Execute(IJobContext context)
        {
            if (!CliUtils.ParseArgs(Options, context.Args))
            {
                return;
            }

            if (_isHelp)
            {
                PrintUsage();
                return;
            }

            try
            {
                if (string.IsNullOrEmpty(_userName))
                {
                    await CliUtils.PrintErrorAsync("未设置参数管理员用户名:{userName} !");

                    return;
                }

                if (string.IsNullOrEmpty(_password))
                {
                    await CliUtils.PrintErrorAsync("未设置参数管理员密码:{password} !");

                    return;
                }

                if (_password.Length < 6)
                {
                    await CliUtils.PrintErrorAsync("管理员密码必须大于6位 !");

                    return;
                }

                if (!EUserPasswordRestrictionUtils.IsValid(_password, EUserPasswordRestrictionUtils.GetValue(EUserPasswordRestriction.LetterAndDigit)))
                {
                    await CliUtils.PrintErrorAsync($"管理员密码不符合规则,请包含{EUserPasswordRestrictionUtils.GetText(EUserPasswordRestriction.LetterAndDigit)}");

                    return;
                }

                var webConfigPath = PathUtils.Combine(CliUtils.PhysicalApplicationPath, "web.config");
                if (!FileUtils.IsFileExists(webConfigPath))
                {
                    await CliUtils.PrintErrorAsync($"系统配置文件不存在:{webConfigPath}!");

                    return;
                }

                if (string.IsNullOrEmpty(WebConfigUtils.ConnectionString))
                {
                    await CliUtils.PrintErrorAsync("web.config 中数据库连接字符串 connectionString 未设置");

                    return;
                }

                WebConfigUtils.Load(CliUtils.PhysicalApplicationPath, "web.config");

                await Console.Out.WriteLineAsync($"数据库类型: {WebConfigUtils.DatabaseType.Value}");

                await Console.Out.WriteLineAsync($"连接字符串: {WebConfigUtils.ConnectionString}");

                await Console.Out.WriteLineAsync($"系统文件夹: {CliUtils.PhysicalApplicationPath}");

                if (!DataProvider.DatabaseDao.IsConnectionStringWork(WebConfigUtils.DatabaseType, WebConfigUtils.ConnectionString))
                {
                    await CliUtils.PrintErrorAsync("系统无法连接到 web.config 中设置的数据库");

                    return;
                }

                if (!SystemManager.IsNeedInstall())
                {
                    await CliUtils.PrintErrorAsync("系统已安装在 web.config 指定的数据库中,命令执行失败");

                    return;
                }

                WebConfigUtils.UpdateWebConfig(WebConfigUtils.IsProtectData, WebConfigUtils.DatabaseType, WebConfigUtils.ConnectionString, WebConfigUtils.AdminDirectory, StringUtils.GetShortGuid(), false);

                DataProvider.Reset();

                SystemManager.InstallDatabase(_userName, _password);
            }
            catch (Exception e)
            {
                await CliUtils.PrintErrorAsync(e.Message);

                return;
            }

            await Console.Out.WriteLineAsync("恭喜,系统安装成功!");
        }
Exemplo n.º 7
0
        public static async Task Execute(IJobContext context)
        {
            if (!CliUtils.ParseArgs(Options, context.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 = await CliUtils.LoadConfigByFileAsync(_configFileName);
            }

            if (configInfo == null)
            {
                await CliUtils.PrintErrorAsync("Error, config not exists");

                return;
            }

            if (configInfo.BackupConfig.Excludes == null)
            {
                configInfo.BackupConfig.Excludes = new List <string>();
            }
            configInfo.BackupConfig.Excludes.Add("bairong_ErrorLog");
            configInfo.BackupConfig.Excludes.Add("siteserver_ErrorLog");

            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($"Backup Directory: {treeInfo.DirectoryPath}");

            var tableNames = DataProvider.DatabaseDao.GetTableNameList();

            await FileUtils.WriteTextAsync(treeInfo.TablesFilePath, Encoding.UTF8, TranslateUtils.JsonSerialize(tableNames));

            await CliUtils.PrintRowLineAsync();

            await CliUtils.PrintRowAsync("Backup Table Name", "Total Count");

            await CliUtils.PrintRowLineAsync();

            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.Excludes, tableName))
                    {
                        continue;
                    }
                }

                var tableInfo = new TableInfo
                {
                    Columns    = DataProvider.DatabaseDao.GetTableColumnInfoList(WebConfigUtils.ConnectionString, tableName),
                    TotalCount = DataProvider.DatabaseDao.GetCount(tableName),
                    RowFiles   = new List <string>()
                };

                await CliUtils.PrintRowAsync(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);

                                await FileUtils.WriteTextAsync(treeInfo.GetTableContentFilePath(tableName, fileName), Encoding.UTF8, TranslateUtils.JsonSerialize(rows));
                            }
                        }
                    }
                    else
                    {
                        var fileName = $"{current}.json";
                        tableInfo.RowFiles.Add(fileName);
                        var rows = DataProvider.DatabaseDao.GetObjects(tableName);

                        await FileUtils.WriteTextAsync(treeInfo.GetTableContentFilePath(tableName, fileName), Encoding.UTF8, TranslateUtils.JsonSerialize(rows));
                    }
                }

                await FileUtils.WriteTextAsync(treeInfo.GetTableMetadataFilePath(tableName), Encoding.UTF8, TranslateUtils.JsonSerialize(tableInfo));
            }

            await CliUtils.PrintRowLineAsync();

            await Console.Out.WriteLineAsync("Well done! Thanks for Using SiteServer Cli Tool");
        }
Exemplo n.º 8
0
        public static async Task Execute(IJobContext context)
        {
            if (!CliUtils.ParseArgs(Options, context.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);

            var webConfigPath = CliUtils.GetWebConfigPath(_configFile);

            if (!FileUtils.IsFileExists(webConfigPath))
            {
                await CliUtils.PrintErrorAsync($"系统配置文件不存在:{webConfigPath}!");

                return;
            }

            WebConfigUtils.Load(CliUtils.PhysicalApplicationPath, webConfigPath);

            if (string.IsNullOrEmpty(WebConfigUtils.ConnectionString))
            {
                await CliUtils.PrintErrorAsync($"{webConfigPath} 中数据库连接字符串 connectionString 未设置");

                return;
            }

            await Console.Out.WriteLineAsync($"数据库类型: {WebConfigUtils.DatabaseType.Value}");

            await Console.Out.WriteLineAsync($"连接字符串: {WebConfigUtils.ConnectionString}");

            await Console.Out.WriteLineAsync($"备份文件夹: {treeInfo.DirectoryPath}");

            if (!DataProvider.DatabaseDao.IsConnectionStringWork(WebConfigUtils.DatabaseType, WebConfigUtils.ConnectionString))
            {
                await CliUtils.PrintErrorAsync($"系统无法连接到 {webConfigPath} 中设置的数据库");

                return;
            }

            if (_excludes == null)
            {
                _excludes = new List <string>();
            }
            _excludes.Add("bairong_Log");
            _excludes.Add("bairong_ErrorLog");
            _excludes.Add("siteserver_ErrorLog");
            _excludes.Add("siteserver_Log");
            _excludes.Add("siteserver_Tracking");

            var allTableNames = DataProvider.DatabaseDao.GetTableNameList();
            var tableNames    = new List <string>();

            foreach (var tableName in allTableNames)
            {
                if (_includes != null && !StringUtils.ContainsIgnoreCase(_includes, tableName))
                {
                    continue;
                }
                if (StringUtils.ContainsIgnoreCase(_excludes, tableName))
                {
                    continue;
                }
                if (StringUtils.ContainsIgnoreCase(tableNames, tableName))
                {
                    continue;
                }
                tableNames.Add(tableName);
            }

            await FileUtils.WriteTextAsync(treeInfo.TablesFilePath, Encoding.UTF8, TranslateUtils.JsonSerialize(tableNames));

            await CliUtils.PrintRowLineAsync();

            await CliUtils.PrintRowAsync("备份表名称", "总条数");

            await CliUtils.PrintRowLineAsync();

            foreach (var tableName in tableNames)
            {
                var tableInfo = new TableInfo
                {
                    Columns    = DataProvider.DatabaseDao.GetTableColumnInfoList(WebConfigUtils.ConnectionString, tableName),
                    TotalCount = DataProvider.DatabaseDao.GetCount(tableName),
                    RowFiles   = new List <string>()
                };

                if (_maxRows > 0 && tableInfo.TotalCount > _maxRows)
                {
                    tableInfo.TotalCount = _maxRows;
                }

                await CliUtils.PrintRowAsync(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  = tableInfo.TotalCount - offset < CliUtils.PageSize ? tableInfo.TotalCount - offset : CliUtils.PageSize;

                                var rows = DataProvider.DatabaseDao.GetPageObjects(tableName, identityColumnName, offset, limit);

                                await FileUtils.WriteTextAsync(treeInfo.GetTableContentFilePath(tableName, fileName), Encoding.UTF8, TranslateUtils.JsonSerialize(rows));
                            }
                        }
                    }
                    else
                    {
                        var fileName = $"{current}.json";
                        tableInfo.RowFiles.Add(fileName);
                        var rows = DataProvider.DatabaseDao.GetObjects(tableName);

                        await FileUtils.WriteTextAsync(treeInfo.GetTableContentFilePath(tableName, fileName), Encoding.UTF8, TranslateUtils.JsonSerialize(rows));
                    }
                }

                await FileUtils.WriteTextAsync(treeInfo.GetTableMetadataFilePath(tableName), Encoding.UTF8, TranslateUtils.JsonSerialize(tableInfo));
            }

            await CliUtils.PrintRowLineAsync();

            await Console.Out.WriteLineAsync($"恭喜,成功备份数据库至文件夹:{treeInfo.DirectoryPath}!");
        }
Exemplo n.º 9
0
        public static async Task Execute(IJobContext context)
        {
            if (!CliUtils.ParseArgs(Options, context.Args))
            {
                return;
            }

            if (_isHelp)
            {
                PrintUsage();
                return;
            }

            if (string.IsNullOrEmpty(_version))
            {
                await CliUtils.PrintErrorAsync("Error, the {version} to update is empty");

                return;
            }

            if (string.IsNullOrEmpty(_directory))
            {
                await CliUtils.PrintErrorAsync("Error, the update {directory} name is empty");

                return;
            }

            var oldTreeInfo = new TreeInfo(_directory);
            var newTreeInfo = new TreeInfo(Folder);

            if (!DirectoryUtils.IsDirectoryExists(oldTreeInfo.DirectoryPath))
            {
                await CliUtils.PrintErrorAsync($"Error, directory {oldTreeInfo.DirectoryPath} not exists");

                return;
            }
            DirectoryUtils.CreateDirectoryIfNotExists(newTreeInfo.DirectoryPath);

            UpdaterBase updater = null;

            if (_version == Updater36.Version)
            {
                updater = new Updater36(oldTreeInfo, newTreeInfo);
            }
            else if (_version == Updater40.Version)
            {
                updater = new Updater40(oldTreeInfo, newTreeInfo);
            }
            else if (_version == Updater41.Version)
            {
                updater = new Updater41(oldTreeInfo, newTreeInfo);
            }
            else if (_version == Updater50.Version)
            {
                updater = new Updater50(oldTreeInfo, newTreeInfo);
            }
            if (updater == null)
            {
                await Console.Out.WriteLineAsync($"Error, the currently supported update versions are {Updater36.Version},{Updater40.Version},{Updater41.Version},{Updater50.Version}");

                return;
            }

            var newVersion = "latest";

            await Console.Out.WriteLineAsync($"Old Version: {_version}, Old Directory: {oldTreeInfo.DirectoryPath}");

            await Console.Out.WriteLineAsync($"New Version: {newVersion}, New Directory: {newTreeInfo.DirectoryPath}");

            var oldTableNames = TranslateUtils.JsonDeserialize <List <string> >(await FileUtils.ReadTextAsync(oldTreeInfo.TablesFilePath, Encoding.UTF8));
            var newTableNames = new List <string>();

            await CliUtils.PrintRowLineAsync();

            await CliUtils.PrintRowAsync("Old Table Name", "New Table Name", "Total Count");

            await CliUtils.PrintRowLineAsync();

            var contentTableNameList = new List <string>();

            UpdateUtils.LoadContentTableNameList(oldTreeInfo, "siteserver_PublishmentSystem",
                                                 contentTableNameList);
            UpdateUtils.LoadContentTableNameList(oldTreeInfo, "wcm_PublishmentSystem",
                                                 contentTableNameList);

            foreach (var oldTableName in oldTableNames)
            {
                var oldMetadataFilePath = oldTreeInfo.GetTableMetadataFilePath(oldTableName);

                if (!FileUtils.IsFileExists(oldMetadataFilePath))
                {
                    continue;
                }

                var oldTableInfo = TranslateUtils.JsonDeserialize <TableInfo>(await FileUtils.ReadTextAsync(oldMetadataFilePath, Encoding.UTF8));

                var tuple = await updater.UpdateTableInfoAsync(oldTableName, oldTableInfo, contentTableNameList);

                if (tuple != null)
                {
                    newTableNames.Add(tuple.Item1);

                    await FileUtils.WriteTextAsync(newTreeInfo.GetTableMetadataFilePath(tuple.Item1), Encoding.UTF8, TranslateUtils.JsonSerialize(tuple.Item2));
                }
            }

            await FileUtils.WriteTextAsync(newTreeInfo.TablesFilePath, Encoding.UTF8, TranslateUtils.JsonSerialize(newTableNames));

            await CliUtils.PrintRowLineAsync();

            await Console.Out.WriteLineAsync("Well done! Thanks for Using SiteServer Cli Tool");
        }
Exemplo n.º 10
0
        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");
        }
Exemplo n.º 11
0
        private async Task RunAsync(IJobContext context)
        {
            if (!CliUtils.ParseArgs(_options, context.Args))
            {
                return;
            }

            if (_isHelp)
            {
                PrintUsage();
                return;
            }

            var directory = _directory;

            if (string.IsNullOrEmpty(directory))
            {
                directory = $"backup/{DateTime.Now:yyyy-MM-dd}";
            }

            var treeInfo = new TreeInfo(_settings.ContentRootPath, directory);

            if (!Directory.Exists(treeInfo.DirectoryPath))
            {
                Directory.CreateDirectory(treeInfo.DirectoryPath);
            }

            var(isConnectionWorks, errorMessage) = await CliUtils.CheckSettingsAsync(_settings);

            if (!isConnectionWorks)
            {
                await CliUtils.PrintErrorAsync(errorMessage);

                return;
            }

            await Console.Out.WriteLineAsync($"数据库类型: {_settings.Database.DatabaseType.GetDisplayName()}");

            await Console.Out.WriteLineAsync($"连接字符串: {_settings.Database.ConnectionString}");

            await Console.Out.WriteLineAsync($"备份文件夹: {treeInfo.DirectoryPath}");

            var includes = new List <string>(_settings.Includes);
            var excludes = new List <string>(_settings.Excludes);

            var allTableNames = await _settings.Database.GetTableNamesAsync();

            var tableNames = new List <string>();

            foreach (var tableName in allTableNames)
            {
                if (includes.Count > 0 && !Utilities.ContainsIgnoreCase(includes, tableName))
                {
                    continue;
                }
                if (excludes.Count > 0 && Utilities.ContainsIgnoreCase(excludes, tableName))
                {
                    continue;
                }
                if (Utilities.ContainsIgnoreCase(tableNames, tableName))
                {
                    continue;
                }
                tableNames.Add(tableName);
            }

            await CliUtils.WriteAllTextAsync(treeInfo.TablesFilePath, Utilities.JsonSerialize(tableNames));

            await CliUtils.PrintRowLineAsync();

            await CliUtils.PrintRowAsync("备份表名称", "总条数");

            await CliUtils.PrintRowLineAsync();

            foreach (var tableName in tableNames)
            {
                treeInfo.CreateTableDirectoryPath(tableName);
                var repository = new Repository(_settings.Database, tableName);
                var tableInfo  = new TableInfo
                {
                    Columns    = await _settings.Database.GetTableColumnsAsync(tableName),
                    TotalCount = await repository.CountAsync(),
                    RowFiles   = new List <string>()
                };

                if (_maxRows > 0 && tableInfo.TotalCount > _maxRows)
                {
                    tableInfo.TotalCount = _maxRows;
                }

                await CliUtils.PrintRowAsync(tableName, tableInfo.TotalCount.ToString("#,0"));

                var identityColumnName = await _settings.Database.AddIdentityColumnIdIfNotExistsAsync(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  = tableInfo.TotalCount - offset < CliUtils.PageSize ? tableInfo.TotalCount - offset : CliUtils.PageSize;

                                var rows = GetPageObjects(repository.Database, tableName, identityColumnName, offset, limit);

                                await CliUtils.WriteAllTextAsync(treeInfo.GetTableContentFilePath(tableName, fileName), Utilities.JsonSerialize(rows));
                            }
                        }
                    }
                    else
                    {
                        var fileName = $"{current}.json";
                        tableInfo.RowFiles.Add(fileName);
                        var rows = GetObjects(repository.Database, tableName);

                        await CliUtils.WriteAllTextAsync(treeInfo.GetTableContentFilePath(tableName, fileName), Utilities.JsonSerialize(rows));
                    }
                }

                await CliUtils.WriteAllTextAsync(treeInfo.GetTableMetadataFilePath(tableName), Utilities.JsonSerialize(tableInfo));
            }

            await CliUtils.PrintRowLineAsync();

            await Console.Out.WriteLineAsync($"恭喜,成功备份数据库至文件夹:{treeInfo.DirectoryPath}!");
        }
Exemplo n.º 12
0
        private async Task RunAsync(IJobContext context)
        {
            if (!CliUtils.ParseArgs(_options, context.Args))
            {
                return;
            }

            if (_isHelp)
            {
                PrintUsage();
                return;
            }

            if (string.IsNullOrEmpty(_directory))
            {
                await CliUtils.PrintErrorAsync("需要指定恢复数据的文件夹名称:directory");

                return;
            }

            var treeInfo = new TreeInfo(_settings.ContentRootPath, _directory);

            if (!Directory.Exists(treeInfo.DirectoryPath))
            {
                await CliUtils.PrintErrorAsync($"恢复数据的文件夹 {treeInfo.DirectoryPath} 不存在");

                return;
            }

            var tablesFilePath = treeInfo.TablesFilePath;

            if (!CliUtils.FileExists(tablesFilePath))
            {
                await CliUtils.PrintErrorAsync($"恢复文件 {treeInfo.TablesFilePath} 不存在");

                return;
            }

            var(isConnectionWorks, errorMessage) = await CliUtils.CheckSettingsAsync(_settings);

            if (!isConnectionWorks)
            {
                await CliUtils.PrintErrorAsync(errorMessage);

                return;
            }

            await Console.Out.WriteLineAsync($"数据库类型: {_settings.Database.DatabaseType.GetDisplayName()}");

            await Console.Out.WriteLineAsync($"连接字符串: {_settings.Database.ConnectionString}");

            await Console.Out.WriteLineAsync($"恢复文件夹: {treeInfo.DirectoryPath}");

            var tableNames = Utilities.JsonDeserialize <List <string> >(await CliUtils.ReadAllTextAsync(tablesFilePath));

            await CliUtils.PrintRowLineAsync();

            await CliUtils.PrintRowAsync("恢复表名称", "总条数");

            await CliUtils.PrintRowLineAsync();

            var includes         = new List <string>(_settings.Includes);
            var excludes         = new List <string>(_settings.Excludes);
            var errorLogFilePath = CliUtils.CreateErrorLogFile(_settings.ContentRootPath, CommandName);

            foreach (var tableName in tableNames)
            {
                try
                {
                    if (includes.Count > 0 && !Utilities.ContainsIgnoreCase(includes, tableName))
                    {
                        continue;
                    }
                    if (excludes.Count > 0 && Utilities.ContainsIgnoreCase(excludes, tableName))
                    {
                        continue;
                    }

                    var metadataFilePath = treeInfo.GetTableMetadataFilePath(tableName);

                    if (!CliUtils.FileExists(metadataFilePath))
                    {
                        continue;
                    }

                    var tableInfo = Utilities.JsonDeserialize <TableInfo>(await CliUtils.ReadAllTextAsync(metadataFilePath));

                    var repository = new Repository(_settings.Database, tableName, tableInfo.Columns);

                    await CliUtils.PrintRowAsync(tableName, tableInfo.TotalCount.ToString("#,0"));

                    if (!await _settings.Database.IsTableExistsAsync(tableName))
                    {
                        try
                        {
                            await _settings.Database.CreateTableAsync(tableName, tableInfo.Columns);
                        }
                        catch (Exception ex)
                        {
                            await CliUtils.AppendErrorLogAsync(errorLogFilePath, new TextLogInfo
                            {
                                DateTime  = DateTime.Now,
                                Detail    = $"创建表 {tableName}",
                                Exception = ex
                            });

                            continue;
                        }
                    }
                    else
                    {
                        await _settings.Database.AlterTableAsync(tableName, tableInfo.Columns);
                    }

                    if (tableInfo.RowFiles.Count > 0)
                    {
                        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 = Utilities.JsonDeserialize <List <JObject> >(
                                    await CliUtils.ReadAllTextAsync(treeInfo.GetTableContentFilePath(tableName, fileName)));

                                try
                                {
                                    await repository.BulkInsertAsync(objects);
                                }
                                catch (Exception ex)
                                {
                                    await CliUtils.AppendErrorLogAsync(errorLogFilePath, new TextLogInfo
                                    {
                                        DateTime  = DateTime.Now,
                                        Detail    = $"插入表 {tableName}, 文件名 {fileName}",
                                        Exception = ex
                                    });
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    await CliUtils.AppendErrorLogAsync(errorLogFilePath, new TextLogInfo
                    {
                        DateTime  = DateTime.Now,
                        Detail    = $"插入表 {tableName}",
                        Exception = ex
                    });
                }
            }

            await CliUtils.PrintRowLineAsync();

            await Console.Out.WriteLineAsync($"恭喜,成功从文件夹:{treeInfo.DirectoryPath} 恢复数据!");
        }
Exemplo n.º 13
0
        public static async Task Execute(IJobContext context)
        {
            if (!CliUtils.ParseArgs(Options, context.Args))
            {
                return;
            }

            if (_isHelp)
            {
                PrintUsage();
                return;
            }

            if (string.IsNullOrEmpty(_version))
            {
                await CliUtils.PrintErrorAsync("未指定需要转换的备份数据版本号: version");

                return;
            }

            if (string.IsNullOrEmpty(_directory))
            {
                await CliUtils.PrintErrorAsync("未指定需要转换至最新版本的备份数据文件夹:directory");

                return;
            }

            var oldTreeInfo = new TreeInfo(_directory);
            var newTreeInfo = new TreeInfo(Folder);

            if (!DirectoryUtils.IsDirectoryExists(oldTreeInfo.DirectoryPath))
            {
                await CliUtils.PrintErrorAsync($"备份数据的文件夹 {oldTreeInfo.DirectoryPath} 不存在");

                return;
            }
            DirectoryUtils.CreateDirectoryIfNotExists(newTreeInfo.DirectoryPath);

            UpdaterBase updater = null;

            if (_version == Updater36.Version)
            {
                updater = new Updater36(oldTreeInfo, newTreeInfo);
            }
            else if (_version == Updater40.Version)
            {
                updater = new Updater40(oldTreeInfo, newTreeInfo);
            }
            else if (_version == Updater41.Version)
            {
                updater = new Updater41(oldTreeInfo, newTreeInfo);
            }
            else if (_version == Updater50.Version)
            {
                updater = new Updater50(oldTreeInfo, newTreeInfo);
            }
            if (updater == null)
            {
                await Console.Out.WriteLineAsync($"当前支持的升级版本号必须是 {Updater36.Version},{Updater40.Version},{Updater41.Version},{Updater50.Version} 中的一项");

                return;
            }

            var newVersion = "latest";

            await Console.Out.WriteLineAsync($"备份版本: {_version}, 备份数据文件夹: {oldTreeInfo.DirectoryPath}");

            await Console.Out.WriteLineAsync($"升级版本: {newVersion}, 升级数据文件夹: {newTreeInfo.DirectoryPath}");

            var oldTableNames = TranslateUtils.JsonDeserialize <List <string> >(await FileUtils.ReadTextAsync(oldTreeInfo.TablesFilePath, Encoding.UTF8));
            var newTableNames = new List <string>();

            await CliUtils.PrintRowLineAsync();

            await CliUtils.PrintRowAsync("备份表名称", "升级表名称", "总条数");

            await CliUtils.PrintRowLineAsync();

            var contentTableNameList = new List <string>();

            UpdateUtils.LoadContentTableNameList(oldTreeInfo, "siteserver_PublishmentSystem",
                                                 contentTableNameList);
            UpdateUtils.LoadContentTableNameList(oldTreeInfo, "wcm_PublishmentSystem",
                                                 contentTableNameList);

            foreach (var oldTableName in oldTableNames)
            {
                var oldMetadataFilePath = oldTreeInfo.GetTableMetadataFilePath(oldTableName);

                if (!FileUtils.IsFileExists(oldMetadataFilePath))
                {
                    continue;
                }

                var oldTableInfo = TranslateUtils.JsonDeserialize <TableInfo>(await FileUtils.ReadTextAsync(oldMetadataFilePath, Encoding.UTF8));

                var tuple = await updater.UpdateTableInfoAsync(oldTableName, oldTableInfo, contentTableNameList);

                if (tuple != null)
                {
                    newTableNames.Add(tuple.Item1);

                    await FileUtils.WriteTextAsync(newTreeInfo.GetTableMetadataFilePath(tuple.Item1), Encoding.UTF8, TranslateUtils.JsonSerialize(tuple.Item2));
                }
            }

            await FileUtils.WriteTextAsync(newTreeInfo.TablesFilePath, Encoding.UTF8, TranslateUtils.JsonSerialize(newTableNames));

            await CliUtils.PrintRowLineAsync();

            await Console.Out.WriteLineAsync($"恭喜,成功从备份文件夹:{oldTreeInfo.DirectoryPath} 升级至新版本:{newTreeInfo.DirectoryPath} !");
        }
Exemplo n.º 14
0
        public async Task Execute(IJobContext context)
        {
            if (!CliUtils.ParseArgs(_options, context.Args))
            {
                return;
            }

            if (_isHelp)
            {
                PrintUsage();
                return;
            }

            var directory = _directory;

            if (string.IsNullOrEmpty(directory))
            {
                directory = $"backup/{DateTime.Now:yyyy-MM-dd}";
            }

            var treeInfo = new TreeInfo(directory);

            DirectoryUtils.CreateDirectoryIfNotExists(treeInfo.DirectoryPath);

            var backupWebConfigPath = CliUtils.GetWebConfigPath(_from);

            if (!FileUtils.IsFileExists(backupWebConfigPath))
            {
                await CliUtils.PrintErrorAsync($"系统配置文件不存在:{backupWebConfigPath}!");

                return;
            }
            WebConfigUtils.Load(CliUtils.PhysicalApplicationPath, backupWebConfigPath);
            if (string.IsNullOrEmpty(WebConfigUtils.ConnectionString))
            {
                await CliUtils.PrintErrorAsync($"{backupWebConfigPath} 中数据库连接字符串 connectionString 未设置");

                return;
            }

            await Console.Out.WriteLineAsync($"备份数据库类型: {WebConfigUtils.DatabaseType.Value}");

            await Console.Out.WriteLineAsync($"备份连接字符串: {WebConfigUtils.ConnectionString}");

            await Console.Out.WriteLineAsync($"备份文件夹: {treeInfo.DirectoryPath}");

            if (!DataProvider.DatabaseDao.IsConnectionStringWork(WebConfigUtils.DatabaseType, WebConfigUtils.ConnectionString))
            {
                await CliUtils.PrintErrorAsync($"系统无法连接到 {backupWebConfigPath} 中设置的数据库");

                return;
            }

            if (_excludes == null)
            {
                _excludes = new List <string>();
            }
            _excludes.Add("bairong_Log");
            _excludes.Add("bairong_ErrorLog");
            _excludes.Add("siteserver_ErrorLog");
            _excludes.Add("siteserver_Log");
            _excludes.Add("siteserver_Tracking");

            var errorLogFilePath = CliUtils.CreateErrorLogFile(CommandName);

            await BackupJob.Backup(_includes, _excludes, _maxRows, treeInfo);

            var restoreWebConfigPath = CliUtils.GetWebConfigPath(_to);

            if (!FileUtils.IsFileExists(restoreWebConfigPath))
            {
                await CliUtils.PrintErrorAsync($"系统配置文件不存在:{restoreWebConfigPath}!");

                return;
            }
            WebConfigUtils.Load(CliUtils.PhysicalApplicationPath, restoreWebConfigPath);
            if (string.IsNullOrEmpty(WebConfigUtils.ConnectionString))
            {
                await CliUtils.PrintErrorAsync($"{restoreWebConfigPath} 中数据库连接字符串 connectionString 未设置");

                return;
            }
            await Console.Out.WriteLineAsync($"恢复数据库类型: {WebConfigUtils.DatabaseType.Value}");

            await Console.Out.WriteLineAsync($"恢复连接字符串: {WebConfigUtils.ConnectionString}");

            if (!DataProvider.DatabaseDao.IsConnectionStringWork(WebConfigUtils.DatabaseType, WebConfigUtils.ConnectionString))
            {
                await CliUtils.PrintErrorAsync($"系统无法连接到 {restoreWebConfigPath} 中设置的数据库");

                return;
            }

            await RestoreJob.Restore(_includes, _excludes, true, treeInfo.DirectoryPath, treeInfo, errorLogFilePath);

            await CliUtils.PrintRowLineAsync();

            await Console.Out.WriteLineAsync("恭喜,成功同步数据!");
        }