Esempio n. 1
0
        public async Task ExecuteAsync(IPluginJobContext 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(_settingsManager, directory);

            DirectoryUtils.CreateDirectoryIfNotExists(treeInfo.DirectoryPath);

            var configPath = CliUtils.GetConfigPath(_settingsManager);

            if (!FileUtils.IsFileExists(configPath))
            {
                await WriteUtils.PrintErrorAsync($"The sscms.json file does not exist: {configPath}");

                return;
            }

            await Console.Out.WriteLineAsync($"Database type: {_settingsManager.DatabaseType.GetDisplayName()}");

            await Console.Out.WriteLineAsync($"Database connection string: {_settingsManager.DatabaseConnectionString}");

            await Console.Out.WriteLineAsync($"Backup folder: {treeInfo.DirectoryPath}");

            //WebConfigUtils.Load(_settingsManager.ContentRootPath, webConfigPath);

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

            //await Console.Out.WriteLineAsync($"数据库类型: {_settingsManager.Database.DatabaseType.GetValue()}");
            //await Console.Out.WriteLineAsync($"连接字符串: {WebConfigUtils.ConnectionString}");
            //await Console.Out.WriteLineAsync($"备份文件夹: {treeInfo.DirectoryPath}");

            //var (isConnectionWorks, errorMessage) = await _settingsManager.Database.IsConnectionWorksAsync();
            //if (!isConnectionWorks)
            //{
            //    await CliUtils.PrintErrorAsync($"数据库连接错误:{errorMessage}");
            //    return;
            //}

            var(isConnectionWorks, errorMessage) = await _settingsManager.Database.IsConnectionWorksAsync();

            if (!isConnectionWorks)
            {
                await WriteUtils.PrintErrorAsync($"Unable to connect to database, error message:{errorMessage}");

                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.DeleteErrorLogFileIfExists(CommandName, _settingsManager);

            await Backup(_settingsManager, _databaseManager, _includes, _excludes, _maxRows, treeInfo, errorLogFilePath);

            await WriteUtils.PrintRowLineAsync();

            await WriteUtils.PrintSuccessAsync("backup database to folder successfully!");
        }
Esempio n. 2
0
        public async Task ExecuteAsync(IPluginJobContext context)
        {
            if (!CliUtils.ParseArgs(_options, context.Args))
            {
                return;
            }

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

            if (string.IsNullOrEmpty(_directory))
            {
                await WriteUtils.PrintErrorAsync("Restore folder name not specified: --directory");

                return;
            }

            var treeInfo = new TreeInfo(_settingsManager, _directory);

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

                return;
            }

            var tablesFilePath = treeInfo.TablesFilePath;

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

                return;
            }

            var configPath = CliUtils.GetConfigPath(_settingsManager);

            if (!FileUtils.IsFileExists(configPath))
            {
                await WriteUtils.PrintErrorAsync($"The sscms.json file does not exist: {configPath}");

                return;
            }

            //WebConfigUtils.Load(_settingsManager.ContentRootPath, webConfigPath);

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

            //await Console.Out.WriteLineAsync($"数据库类型: {_settingsManager.Database.DatabaseType.GetValue()}");
            //await Console.Out.WriteLineAsync($"连接字符串: {WebConfigUtils.ConnectionString}");
            //await Console.Out.WriteLineAsync($"恢复文件夹: {treeInfo.DirectoryPath}");

            await Console.Out.WriteLineAsync($"Database type: {_settingsManager.Database.DatabaseType.GetDisplayName()}");

            await Console.Out.WriteLineAsync($"Database connection string: {_settingsManager.Database.ConnectionString}");

            await Console.Out.WriteLineAsync($"Restore folder: {treeInfo.DirectoryPath}");

            var(isConnectionWorks, errorMessage) = await _settingsManager.Database.IsConnectionWorksAsync();

            if (!isConnectionWorks)
            {
                await WriteUtils.PrintErrorAsync($"Unable to connect to database, error message:{errorMessage}");

                return;
            }

            //if (!_dataOnly)
            //{
            //    if (!await _configRepository.IsNeedInstallAsync())
            //    {
            //        await WriteUtils.PrintErrorAsync("The data could not be restored on the installed sscms database");
            //        return;
            //    }

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

            //    if (_settingsManager.DatabaseType == DatabaseType.SQLite)
            //    {
            //        var filePath = PathUtils.Combine(_settingsManager.ContentRootPath, Constants.DefaultLocalDbFileName);
            //        if (!FileUtils.IsFileExists(filePath))
            //        {
            //            await FileUtils.WriteTextAsync(filePath, string.Empty);
            //        }
            //    }

            //    await _databaseManager.SyncDatabaseAsync();
            //}

            await WriteUtils.PrintRowLineAsync();

            await WriteUtils.PrintRowAsync("Restore table name", "Count");

            await WriteUtils.PrintRowLineAsync();

            var errorLogFilePath = CliUtils.DeleteErrorLogFileIfExists(_settingsManager);

            var errorTableNames = await _restoreService.RestoreAsync(_includes, _excludes, tablesFilePath, treeInfo, errorLogFilePath);

            if (errorTableNames.Count == 0)
            {
                await WriteUtils.PrintSuccessAsync("restore database successfully!");
            }
            else
            {
                await WriteUtils.PrintErrorAsync($"Database restore failed and the following table was not successfully restored: {ListUtils.ToString(errorTableNames)}");
            }
        }
Esempio n. 3
0
        public async Task ExecuteAsync(IPluginJobContext 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(_settingsManager, directory);

            DirectoryUtils.CreateDirectoryIfNotExists(treeInfo.DirectoryPath);

            var backupConfigPath = PathUtils.Combine(_settingsManager.ContentRootPath, _from);

            if (!FileUtils.IsFileExists(backupConfigPath))
            {
                await WriteUtils.PrintErrorAsync($"The sscms configuration file does not exist: {backupConfigPath}");

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

            //await Console.Out.WriteLineAsync($"备份数据库类型: {_settingsManager.Database.DatabaseType.GetValue()}");
            //await Console.Out.WriteLineAsync($"备份连接字符串: {WebConfigUtils.ConnectionString}");
            //await Console.Out.WriteLineAsync($"备份文件夹: {treeInfo.DirectoryPath}");

            var(isConnectionWorks, errorMessage) = await _settingsManager.Database.IsConnectionWorksAsync();

            if (!isConnectionWorks)
            {
                await WriteUtils.PrintErrorAsync($"Unable to connect to database, error message:{errorMessage}");

                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.DeleteErrorLogFileIfExists(CommandName, _settingsManager);
            await DataBackupJob.Backup(_settingsManager, _databaseManager, _includes, _excludes, _maxRows, treeInfo, errorLogFilePath);

            var restoreConfigPath = PathUtils.Combine(_settingsManager.ContentRootPath, _to);

            if (!FileUtils.IsFileExists(restoreConfigPath))
            {
                await WriteUtils.PrintErrorAsync($"The sscms configuration file does not exist: {restoreConfigPath}");

                return;
            }
            //WebConfigUtils.Load(_settingsManager.ContentRootPath, restoreWebConfigPath);
            //if (string.IsNullOrEmpty(WebConfigUtils.ConnectionString))
            //{
            //    await CliUtils.PrintErrorAsync($"{restoreWebConfigPath} 中数据库连接字符串 connectionString 未设置");
            //    return;
            //}
            //await Console.Out.WriteLineAsync($"恢复数据库类型: {_settingsManager.Database.DatabaseType.GetValue()}");
            //await Console.Out.WriteLineAsync($"恢复连接字符串: {WebConfigUtils.ConnectionString}");
            (isConnectionWorks, errorMessage) = await _settingsManager.Database.IsConnectionWorksAsync();

            if (!isConnectionWorks)
            {
                await WriteUtils.PrintErrorAsync($"Unable to connect to database, error message:{errorMessage}");

                return;
            }

            await _restoreService.RestoreAsync(_includes, _excludes, true, treeInfo.DirectoryPath, treeInfo, errorLogFilePath);

            await WriteUtils.PrintRowLineAsync();

            await WriteUtils.PrintSuccessAsync("恭喜,成功同步数据!");
        }
Esempio n. 4
0
        public async Task ExecuteAsync(IPluginJobContext context)
        {
            if (!CliUtils.ParseArgs(_options, context.Args))
            {
                return;
            }

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

            if (string.IsNullOrEmpty(_directory))
            {
                await WriteUtils.PrintErrorAsync("Backup folder name not specified: --directory");

                return;
            }

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

            if (!DirectoryUtils.IsDirectoryExists(oldTreeInfo.DirectoryPath))
            {
                await WriteUtils.PrintErrorAsync($"The backup folder does not exist: {oldTreeInfo.DirectoryPath}");

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

            _updateService.Load(oldTreeInfo, newTreeInfo);

            await Console.Out.WriteLineAsync($"Backup folder: {oldTreeInfo.DirectoryPath}, Update folder: {newTreeInfo.DirectoryPath}, Update to SSCMS version: {_settingsManager.Version}");

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

            await WriteUtils.PrintRowLineAsync();

            await WriteUtils.PrintRowAsync("Backup table name", "Update table Name", "Count");

            await WriteUtils.PrintRowLineAsync();

            var siteIdList = new List <int>();
            var tableNames = new List <string>();

            UpdateUtils.LoadSites(_settingsManager, oldTreeInfo, siteIdList, tableNames);

            var table = new TableContentConverter(_settingsManager);

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

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

            var errorLogFilePath = CliUtils.DeleteErrorLogFileIfExists(_settingsManager);
            var errorTableNames  = new List <string>();

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

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

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

                    if (ListUtils.ContainsIgnoreCase(tableNames, oldTableName))
                    {
                        if (_splitContents)
                        {
                            var converter = table.GetConverter(oldTableName, oldTableInfo.Columns);

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

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

                                await FileUtils.WriteTextAsync(newTreeInfo.GetTableMetadataFilePath(tuple.Item1), TranslateUtils.JsonSerialize(tuple.Item2));
                            }
                        }
                    }
                    else
                    {
                        var tuple = await _updateService.UpdateTableInfoAsync(oldTableName, oldTableInfo);

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

                            await FileUtils.WriteTextAsync(newTreeInfo.GetTableMetadataFilePath(tuple.Item1), TranslateUtils.JsonSerialize(tuple.Item2));
                        }
                    }
                }
                catch (Exception ex)
                {
                    errorTableNames.Add(oldTableName);
                    await CliUtils.AppendErrorLogAsync(errorLogFilePath, new TextLogInfo
                    {
                        Exception = ex,
                        DateTime  = DateTime.Now,
                        Detail    = oldTableName
                    });
                }
            }

            if (_splitContents)
            {
                foreach (var siteId in siteIdList)
                {
                    var siteTableInfo = splitSiteTableDict[siteId];
                    var siteTableName = UpdateUtils.GetSplitContentTableName(siteId);
                    newTableNames.Add(siteTableName);

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

                await UpdateUtils.UpdateSitesSplitTableNameAsync(_databaseManager, newTreeInfo, splitSiteTableDict);
            }

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

            await WriteUtils.PrintRowLineAsync();

            if (errorTableNames.Count == 0)
            {
                await WriteUtils.PrintSuccessAsync("Update the backup data to the new version successfully!");
            }
            else
            {
                await WriteUtils.PrintErrorAsync($"Database update failed and the following table was not successfully update: {ListUtils.ToString(errorTableNames)}");
            }
        }