Example #1
0
        public static void Execute(string[] args)
        {
            if (!CliUtils.ParseArgs(Options, args))
            {
                return;
            }

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

            if (string.IsNullOrEmpty(_directory))
            {
                CliUtils.PrintError("Error, the restore {directory} name is empty");
                return;
            }

            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;
            }

            ConfigInfo configInfo;

            if (!string.IsNullOrEmpty(_databaseType) && !string.IsNullOrEmpty(_connectionString))
            {
                configInfo = CliUtils.LoadConfigByArgs(_databaseType, _connectionString);
            }
            else
            {
                configInfo = CliUtils.LoadConfigByFile(_configFileName);
            }

            if (configInfo == null)
            {
                CliUtils.PrintError("Error, config not exists");
                return;
            }

            if (string.IsNullOrEmpty(WebConfigUtils.ConnectionString))
            {
                CliUtils.PrintError("Error, connection string is empty");
                return;
            }

            if (!DataProvider.DatabaseDao.IsConnectionStringWork(WebConfigUtils.DatabaseType, WebConfigUtils.ConnectionString))
            {
                CliUtils.PrintError("Error, can not connect to the database");
                return;
            }

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

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

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

            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>(FileUtils.ReadText(metadataFilePath, Encoding.UTF8));

                CliUtils.PrintRow(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> >(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    = $"insert table {tableName}, fileName {fileName}",
                                Exception = ex
                            });
                        }
                    }
                }

                CliUtils.AppendErrorLogs(errorLogFilePath, logs);
            }

            CliUtils.PrintLine();

            if (configInfo.RestoreConfig.SyncDatabase)
            {
                SystemManager.SyncDatabase();
            }

            Console.WriteLine("Well done! Thanks for Using SiteServer Cli Tool");
        }
Example #2
0
        private static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;

            var commandName = string.Empty;
            var commandArgs = new List <string>();

            if (args.Length >= 1)
            {
                for (var i = 0; i < args.Length; i++)
                {
                    if (i == 0)
                    {
                        commandName = args[i];
                    }
                    else
                    {
                        commandArgs.Add(args[i]);
                    }
                }
            }

            Console.WriteLine("Welcome to SiteServer Cli Tool");
            Console.WriteLine();

            try
            {
                if (StringUtils.EqualsIgnoreCase(commandName, BackupManager.CommandName))
                {
                    BackupManager.Execute(commandArgs.ToArray());
                }
                else if (StringUtils.EqualsIgnoreCase(commandName, RestoreManager.CommandName))
                {
                    RestoreManager.Execute(commandArgs.ToArray());
                }
                else if (StringUtils.EqualsIgnoreCase(commandName, UpdateManager.CommandName))
                {
                    UpdateManager.Execute(commandArgs.ToArray());
                }
                else if (StringUtils.EqualsIgnoreCase(commandName, VersionManager.CommandName))
                {
                    VersionManager.Execute(commandArgs.ToArray());
                }
                else if (StringUtils.EqualsIgnoreCase(commandName, TestManager.CommandName))
                {
                    TestManager.Execute(commandArgs.ToArray());
                }
                else
                {
                    CliUtils.PrintLine();
                    CliUtils.PrintRow("Usage");
                    CliUtils.PrintLine();
                    BackupManager.PrintUsage();
                    RestoreManager.PrintUsage();
                    UpdateManager.PrintUsage();
                    VersionManager.PrintUsage();
                    CliUtils.PrintLine();
                    CliUtils.PrintRow("http://www.siteserver.cn/docs/cli");
                    CliUtils.PrintLine();
                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                CliUtils.PrintError(ex.Message);

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

                CliUtils.AppendErrorLogs(errorLogFilePath, new List <TextLogInfo> {
                    new TextLogInfo
                    {
                        DateTime  = DateTime.Now,
                        Detail    = "Console Error",
                        Exception = ex
                    }
                });
            }
        }