コード例 #1
0
        public async Task Execute(IJobContext context)
        {
            if (!CliUtils.ParseArgs(_options, context.Args))
            {
                return;
            }

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

            var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            await Console.Out.WriteLineAsync($"SiteServer CLI 版本号: {version.Substring(0, version.Length - 2)}");

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

            await Console.Out.WriteLineAsync();

            var webConfigPath = CliUtils.GetWebConfigPath(_configFile);

            if (FileUtils.IsFileExists(webConfigPath))
            {
                WebConfigUtils.Load(CliUtils.PhysicalApplicationPath, webConfigPath);

                try
                {
                    var cmsVersion = FileVersionInfo.GetVersionInfo(PathUtils.Combine(CliUtils.PhysicalApplicationPath, "Bin", "SiteServer.CMS.dll")).ProductVersion;
                    await Console.Out.WriteLineAsync($"SitServer CMS Version: {cmsVersion}");
                }
                catch
                {
                    // ignored
                }

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

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

                await Console.Out.WriteLineAsync($"连接字符串(加密): {TranslateUtils.EncryptStringBySecretKey(WebConfigUtils.ConnectionString, WebConfigUtils.SecretKey)}");
            }
        }
コード例 #2
0
ファイル: CliUtils.cs プロジェクト: zr53722/cms
        public static ConfigInfo LoadConfigByFile(string configFileName)
        {
            ConfigInfo configInfo = null;

            if (string.IsNullOrEmpty(configFileName))
            {
                configFileName = "cli.json";
            }

            if (FileUtils.IsFileExists(PathUtils.Combine(PhysicalApplicationPath, configFileName)))
            {
                configInfo = TranslateUtils.JsonDeserialize <ConfigInfo>(
                    FileUtils.ReadText(PathUtils.Combine(PhysicalApplicationPath, configFileName), Encoding.UTF8));

                if (configInfo != null)
                {
                    WebConfigUtils.Load(PhysicalApplicationPath, configInfo.DatabaseType, configInfo.ConnectionString);

                    if (configInfo.BackupConfig == null)
                    {
                        configInfo.BackupConfig = new BackupConfigInfo();
                    }
                    if (configInfo.RestoreConfig == null)
                    {
                        configInfo.RestoreConfig = new RestoreConfigInfo();
                    }
                }
            }
            else if (FileUtils.IsFileExists(PathUtils.Combine(PhysicalApplicationPath, "web.config")))
            {
                WebConfigUtils.Load(PhysicalApplicationPath, "web.config");

                configInfo = new ConfigInfo
                {
                    DatabaseType     = WebConfigUtils.DatabaseType.Value,
                    ConnectionString = WebConfigUtils.ConnectionString,
                    BackupConfig     = new BackupConfigInfo(),
                    RestoreConfig    = new RestoreConfigInfo(),
                };
            }

            return(configInfo);
        }
コード例 #3
0
        //private static List<PluginInstance> _pluginInfoListRunnable;

        public static void LoadPlugins(string applicationPhysicalPath)
        {
            WebConfigUtils.Load(applicationPhysicalPath, PathUtils.Combine(applicationPhysicalPath, WebConfigUtils.WebConfigFileName));

            Context.Initialize(new EnvironmentImpl(WebConfigUtils.DatabaseType, WebConfigUtils.ConnectionString, WebConfigUtils.HomeDirectory, WebConfigUtils.AdminDirectory, WebConfigUtils.PhysicalApplicationPath, ApiManager.ApiUrl), new ApiCollectionImpl
            {
                AdminApi   = AdminApi.Instance,
                ConfigApi  = ConfigApi.Instance,
                ContentApi = ContentApi.Instance,
                ChannelApi = ChannelApi.Instance,
                ParseApi   = ParseApi.Instance,
                PluginApi  = PluginApi.Instance,
                SiteApi    = SiteApi.Instance,
                UserApi    = UserApi.Instance,
                UtilsApi   = UtilsApi.Instance
            });

            //_pluginInfoListRunnable = PluginInfoListRunnable;
        }
コード例 #4
0
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();

            var config = GlobalConfiguration.Configuration;

            var corsAttr = new EnableCorsAttribute("*", "*", "*")
            {
                SupportsCredentials = true
            };

            config.EnableCors(corsAttr);
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                "DefaultApi",
                "api/{controller}/{id}",
                new { id = RouteParameter.Optional }
                );

            //config.Routes.Add("name", new HttpRoute());

            RouteTable.Routes.Ignore(""); //Allow index.html to load

            var jsonFormatter = config.Formatters.JsonFormatter;
            var settings      = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };
            var timeFormat = new IsoDateTimeConverter
            {
                DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
            };

            settings.Converters.Add(timeFormat);
            jsonFormatter.SerializerSettings = settings;
            jsonFormatter.Indent             = true;

            config.EnsureInitialized();

            WebConfigUtils.Load(HostingEnvironment.ApplicationPhysicalPath);
            var c = PluginManager.PluginInfoListRunnable;
        }
コード例 #5
0
        public static void LoadPlugins(string applicationPhysicalPath)
        {
            WebConfigUtils.Load(applicationPhysicalPath);
            _pluginInfoListRunnable = PluginInfoListRunnable;

            Context.Initialize(new EnvironmentImpl(WebConfigUtils.DatabaseType, WebConfigUtils.ConnectionString, WebConfigUtils.AdminDirectory, WebConfigUtils.PhysicalApplicationPath), new ApiCollectionImpl
            {
                AdminApi    = AdminApi.Instance,
                ConfigApi   = ConfigApi.Instance,
                ContentApi  = ContentApi.Instance,
                DatabaseApi = DataProvider.DatabaseApi,
                ChannelApi  = ChannelApi.Instance,
                ParseApi    = ParseApi.Instance,
                PluginApi   = PluginApi.Instance,
                SiteApi     = SiteApi.Instance,
                UserApi     = UserApi.Instance,
                UtilsApi    = UtilsApi.Instance
            });
        }
コード例 #6
0
        protected override void OnStart(string[] args)
        {
            try
            {
                //配置的时间间隔,单位秒
                var interval = ConfigHelper.GetConfigInt("Interval");
                interval = interval == 0 ? 10 : interval;

                //读取配置项并设置连接字符串
                var applicationPhysicalPath = AppDomain.CurrentDomain.BaseDirectory;
                WebConfigUtils.Load(applicationPhysicalPath, PathUtils.Combine(applicationPhysicalPath, "SiteServer.Service.exe.config"));

                timer1 = new System.Timers.Timer {
                    Interval = interval * 1000
                };
                //设置计时器事件间隔执行时间
                timer1.Elapsed += timer1_Elapsed;
                timer1.Enabled  = true;
            }
            catch (Exception e)
            {
                WriteLog(e.Message);
            }
        }
コード例 #7
0
ファイル: BackupManager.cs プロジェクト: justgohead/cms-1
        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}";
            }
            if (string.IsNullOrEmpty(_webConfigFileName))
            {
                _webConfigFileName = "web.config";
            }

            var treeInfo = new TreeInfo(_directory);

            DirectoryUtils.CreateDirectoryIfNotExists(treeInfo.DirectoryPath);

            WebConfigUtils.Load(CliUtils.PhysicalApplicationPath, _webConfigFileName);

            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)
            {
                var tableInfo = new TableInfo
                {
                    Columns    = DataProvider.DatabaseDao.GetTableColumnInfoListLowercase(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);

                        for (; current <= pageCount; current++)
                        {
                            CliUtils.PrintProgressBar(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));
                        }

                        CliUtils.PrintProgressBarEnd();
                    }
                    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");
        }
コード例 #8
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} 恢复数据!");
        }
コード例 #9
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} 恢复数据!");
        }
コード例 #10
0
ファイル: RestoreManager.cs プロジェクト: yuchueh/cms-1
        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}";
            }
            if (string.IsNullOrEmpty(_webConfigFileName))
            {
                _webConfigFileName = "web.config";
            }

            var treeInfo = new TreeInfo(_directory);

            if (!DirectoryUtils.IsDirectoryExists(treeInfo.DirectoryPath))
            {
                CliUtils.PrintError($"Error, Directory {treeInfo.DirectoryPath} Not Exists");
                return;
            }

            var tablesFilePath = treeInfo.TablesFilePath;

            if (!FileUtils.IsFileExists(tablesFilePath))
            {
                CliUtils.PrintError($"Error, File {treeInfo.TablesFilePath} Not Exists");
                return;
            }

            WebConfigUtils.Load(CliUtils.PhysicalApplicationPath, _webConfigFileName);

            Console.WriteLine($"Database Type: {WebConfigUtils.DatabaseType.Value}");
            Console.WriteLine($"Connection String: {WebConfigUtils.ConnectionString}");
            Console.WriteLine($"Restore Directory: {treeInfo.DirectoryPath}");

            if (string.IsNullOrEmpty(WebConfigUtils.ConnectionString))
            {
                CliUtils.PrintError("Error, Connection String Is Empty");
                return;
            }

            List <string> databaseNameList;
            string        errorMessage;
            var           isConnectValid = DataProvider.DatabaseDao.ConnectToServer(WebConfigUtils.DatabaseType, WebConfigUtils.ConnectionString, out databaseNameList, out errorMessage);

            if (!isConnectValid)
            {
                CliUtils.PrintError("Error, Connection String Not Correct");
                return;
            }

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

            CliUtils.PrintLine();
            CliUtils.PrintRow("Import Table Name", "Total Count");
            CliUtils.PrintLine();

            var logs = new List <TextLogInfo>();

            foreach (var tableName in tableNames)
            {
                var metadataFilePath = treeInfo.GetTableMetadataFilePath(tableName);

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

                var tableInfo = TranslateUtils.JsonDeserialize <TableInfo>(FileUtils.ReadText(metadataFilePath, Encoding.UTF8));

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

                if (!DataProvider.DatabaseDao.IsTableExists(tableName))
                {
                    DataProvider.DatabaseDao.CreateSystemTable(tableName, tableInfo.Columns);
                }
                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> >(FileUtils.ReadText(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 {tableName}, fileName {fileName}",
                                Exception = ex
                            });
                        }
                    }
                }
            }

            CliUtils.PrintLine();

            SystemManager.SyncDatabase();

            CliUtils.LogErrors(CommandName, logs);

            Console.WriteLine("Well done! Thanks for Using SiteServer Cli Tool");
        }
コード例 #11
0
ファイル: InstallJob.cs プロジェクト: zhouwybetter/cms
        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("恭喜,系统安装成功!");
        }
コード例 #12
0
ファイル: BackupJob.cs プロジェクト: ym1100/siteserver-cms
        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}!");
        }
コード例 #13
0
ファイル: SyncJob.cs プロジェクト: xueyu19870128/MyProject
        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("恭喜,成功同步数据!");
        }
コード例 #14
0
ファイル: PluginManager.cs プロジェクト: zxbe/cms
 public static void LoadPlugins(string applicationPhysicalPath)
 {
     WebConfigUtils.Load(applicationPhysicalPath);
     _pluginInfoListRunnable = PluginInfoListRunnable;
 }