Пример #1
0
        public override void RunJob()
        {
            Logger.Debug("begin restore portal");

            List <IModuleSpecifics> modulesToProcess = GetModulesToProcess().ToList();

            SetStepsCount(ProcessStorage ? modulesToProcess.Count + 1 : modulesToProcess.Count);

            Logger.Debug("begin restore data");

            using (var dataReader = new ZipReadOperator(BackupFilePath))
            {
                var dbFactory = new DbFactory(ConfigPath);
                foreach (var module in modulesToProcess)
                {
                    var restoreTask = new RestoreDbModuleTask(Logger, module, dataReader, _columnMapper, dbFactory,
                                                              ReplaceDate);
                    restoreTask.ProgressChanged += (sender, args) => SetCurrentStepProgress(args.Progress);
                    foreach (var tableName in IgnoredTables)
                    {
                        restoreTask.IgnoreTable(tableName);
                    }
                    restoreTask.RunJob();
                }

                Logger.Debug("end restore data");

                if (ProcessStorage)
                {
                    DoRestoreStorage(dataReader);
                }
                if (UnblockPortalAfterCompleted)
                {
                    SetTenantActive(dbFactory, _columnMapper.GetTenantMapping());
                }
            }

            if (CoreContext.Configuration.Standalone)
            {
                Logger.Debug("refresh license");
                try
                {
                    LicenseReader.RejectLicense();
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                }

                Logger.Debug("clear cache");
                AscCache.Default.Remove(new Regex(".*"));
            }

            Logger.Debug("end restore portal");
        }
Пример #2
0
        public override void Run()
        {
            InvokeInfo("begin restore portal");

            List <IModuleSpecifics> modulesToProcess = GetModulesToProcess().ToList();

            InitProgress(ProcessStorage ? modulesToProcess.Count + 1 : modulesToProcess.Count);

            using (var dataReader = new ZipReadOperator(BackupFilePath))
            {
                InvokeInfo("begin restore data");

                var dbFactory = new DbFactory(ConfigPath);
                foreach (var module in modulesToProcess)
                {
                    var restoreTask = new RestoreDbModuleTask(module, dataReader, _columnMapper, dbFactory, ReplaceDate);
                    foreach (var tableName in IgnoredTables)
                    {
                        restoreTask.IgnoreTable(tableName);
                    }
                    RunSubtask(restoreTask);
                }

                InvokeInfo("end restore data");

                if (ProcessStorage)
                {
                    DoRestoreStorage(dataReader);
                }

                if (UnblockPortalAfterCompleted)
                {
                    SetTenantActive(dbFactory, _columnMapper.GetTenantMapping());
                }
            }

            InvokeInfo("end restore portal");
        }
Пример #3
0
        private void DoTransferStorage(ColumnMapper columnMapper)
        {
            Logger.Debug("begin transfer storage");
            var fileGroups      = GetFilesToProcess().GroupBy(file => file.Module).ToList();
            int groupsProcessed = 0;

            foreach (var group in fileGroups)
            {
                ICrossModuleTransferUtility transferUtility =
                    StorageFactory.GetCrossModuleTransferUtility(
                        ConfigPath, TenantId, group.Key,
                        ToConfigPath, columnMapper.GetTenantMapping(), group.Key);

                foreach (BackupFileInfo file in group)
                {
                    string adjustedPath = file.Path;

                    IModuleSpecifics module = ModuleProvider.GetByStorageModule(file.Module, file.Domain);
                    if (module == null || module.TryAdjustFilePath(columnMapper, ref adjustedPath))
                    {
                        try
                        {
                            transferUtility.CopyFile(file.Domain, file.Path, file.Domain, adjustedPath);
                        }
                        catch (Exception error)
                        {
                            Logger.Warn("Can't copy file ({0}:{1}): {2}", file.Module, file.Path, error);
                        }
                    }
                    else
                    {
                        Logger.Warn("Can't adjust file path \"{0}\".", file.Path);
                    }
                }
                SetCurrentStepProgress((int)(++groupsProcessed * 100 / (double)fileGroups.Count));
            }

            if (fileGroups.Count == 0)
            {
                SetStepCompleted();
            }

            Logger.Debug("end transfer storage");
        }
Пример #4
0
        private void DoTransferStorage(ColumnMapper columnMapper)
        {
            Logger.Debug("begin transfer storage");
            var fileGroups      = GetFilesToProcess(TenantId).GroupBy(file => file.Module).ToList();
            var groupsProcessed = 0;

            foreach (var group in fileGroups)
            {
                var baseStorage = StorageFactory.GetStorage(ConfigPath, TenantId.ToString(), group.Key);
                var destStorage = StorageFactory.GetStorage(ToConfigPath, columnMapper.GetTenantMapping().ToString(), group.Key);
                var utility     = new CrossModuleTransferUtility(Options, baseStorage, destStorage);

                foreach (var file in group)
                {
                    var adjustedPath = file.Path;

                    var module = ModuleProvider.GetByStorageModule(file.Module, file.Domain);
                    if (module == null || module.TryAdjustFilePath(false, columnMapper, ref adjustedPath))
                    {
                        try
                        {
                            utility.CopyFile(file.Domain, file.Path, file.Domain, adjustedPath);
                        }
                        catch (Exception error)
                        {
                            Logger.WarnFormat("Can't copy file ({0}:{1}): {2}", file.Module, file.Path, error);
                        }
                    }
                    else
                    {
                        Logger.WarnFormat("Can't adjust file path \"{0}\".", file.Path);
                    }
                }
                SetCurrentStepProgress((int)(++groupsProcessed * 100 / (double)fileGroups.Count));
            }

            if (fileGroups.Count == 0)
            {
                SetStepCompleted();
            }

            Logger.Debug("end transfer storage");
        }
Пример #5
0
        private void DoRestoreStorage(IDataReadOperator dataReader)
        {
            InvokeInfo("begin restore storage");
            var fileGroups      = GetFilesToProcess(dataReader).GroupBy(file => file.Module).ToList();
            int groupsProcessed = 0;

            foreach (var group in fileGroups)
            {
                IDataStore storage = StorageFactory.GetStorage(ConfigPath, _columnMapper.GetTenantMapping().ToString(), group.Key, null, null);
                foreach (BackupFileInfo file in group)
                {
                    string adjustedPath = file.Path;

                    IModuleSpecifics module = ModuleProvider.GetByStorageModule(file.Module);
                    if (module == null || module.TryAdjustFilePath(_columnMapper, ref adjustedPath))
                    {
                        Stream stream = dataReader.GetEntry(KeyHelper.GetFileZipKey(file));
                        try
                        {
                            storage.Save(file.Domain, adjustedPath, stream);
                        }
                        catch (Exception error)
                        {
                            InvokeWarning("can't restore file ({0}:{1}): {2}", file.Module, file.Path, error);
                        }
                    }
                }
                SetStepProgress((int)(++groupsProcessed * 100 / (double)fileGroups.Count));
            }

            if (fileGroups.Count == 0)
            {
                SetStepCompleted();
            }

            InvokeInfo("end restore storage");
        }
Пример #6
0
        public override void RunJob()
        {
            Logger.Debug("begin transfer {0}", TenantId);
            var    fromDbFactory  = new DbFactory(ConfigPath);
            var    toDbFactory    = new DbFactory(ToConfigPath);
            string tenantAlias    = GetTenantAlias(fromDbFactory);
            string backupFilePath = GetBackupFilePath(tenantAlias);
            var    columnMapper   = new ColumnMapper();

            try
            {
                //target db can have error tenant from the previous attempts
                SaveTenant(toDbFactory, tenantAlias, TenantStatus.RemovePending, tenantAlias + "_error", "status = " + TenantStatus.Restoring.ToString("d"));

                if (BlockOldPortalAfterStart)
                {
                    SaveTenant(fromDbFactory, tenantAlias, TenantStatus.Transfering);
                }

                SetStepsCount(ProcessStorage ? 3 : 2);

                //save db data to temporary file
                var backupTask = new BackupPortalTask(Logger, TenantId, ConfigPath, backupFilePath, Limit)
                {
                    ProcessStorage = false
                };
                backupTask.ProgressChanged += (sender, args) => SetCurrentStepProgress(args.Progress);
                foreach (var moduleName in IgnoredModules)
                {
                    backupTask.IgnoreModule(moduleName);
                }
                backupTask.RunJob();

                //restore db data from temporary file
                var restoreTask = new RestorePortalTask(Logger, ToConfigPath, backupFilePath, columnMapper)
                {
                    ProcessStorage = false
                };
                restoreTask.ProgressChanged += (sender, args) => SetCurrentStepProgress(args.Progress);
                foreach (var moduleName in IgnoredModules)
                {
                    restoreTask.IgnoreModule(moduleName);
                }
                restoreTask.RunJob();

                //transfer files
                if (ProcessStorage)
                {
                    DoTransferStorage(columnMapper);
                }

                SaveTenant(toDbFactory, tenantAlias, TenantStatus.Active);
                if (DeleteOldPortalAfterCompletion)
                {
                    SaveTenant(fromDbFactory, tenantAlias, TenantStatus.RemovePending, tenantAlias + "_deleted");
                }
                else if (BlockOldPortalAfterStart)
                {
                    SaveTenant(fromDbFactory, tenantAlias, TenantStatus.Active);
                }
            }
            catch
            {
                SaveTenant(fromDbFactory, tenantAlias, TenantStatus.Active);
                if (columnMapper.GetTenantMapping() > 0)
                {
                    SaveTenant(toDbFactory, tenantAlias, TenantStatus.RemovePending, tenantAlias + "_error");
                }
                throw;
            }
            finally
            {
                if (DeleteBackupFileAfterCompletion)
                {
                    File.Delete(backupFilePath);
                }
                Logger.Debug("end transfer {0}", TenantId);
            }
        }
Пример #7
0
        private void RestoreTable(IDbConnection connection, TableInfo tableInfo, ref int transactionsCommited, ref int rowsInserted)
        {
            using (var stream = _reader.GetEntry(KeyHelper.GetTableZipKey(_module, tableInfo.Name)))
            {
                var lowImportanceRelations = _module
                                             .TableRelations
                                             .Where(r => string.Equals(r.ParentTable, tableInfo.Name, StringComparison.InvariantCultureIgnoreCase))
                                             .Where(r => r.Importance == RelationImportance.Low && !r.IsSelfRelation())
                                             .Select(r => Tuple.Create(r, _module.Tables.Single(t => t.Name == r.ChildTable)))
                                             .ToList();

                foreach (IEnumerable <DataRowInfo> rows in GetRows(tableInfo, stream).Skip(transactionsCommited * TransactionLength).MakeParts(TransactionLength))
                {
                    using (var transaction = connection.BeginTransaction())
                    {
                        int rowsSuccess = 0;
                        foreach (DataRowInfo row in rows)
                        {
                            if (_replaceDate)
                            {
                                foreach (var column in tableInfo.DateColumns)
                                {
                                    _columnMapper.SetDateMapping(tableInfo.Name, column, row[column.Key]);
                                }
                            }

                            object oldIdValue = null;
                            object newIdValue = null;

                            if (tableInfo.HasIdColumn())
                            {
                                oldIdValue = row[tableInfo.IdColumn];
                                newIdValue = _columnMapper.GetMapping(tableInfo.Name, tableInfo.IdColumn, oldIdValue);
                                if (newIdValue == null)
                                {
                                    if (tableInfo.IdType == IdType.Guid)
                                    {
                                        newIdValue = Guid.NewGuid().ToString("D");
                                    }
                                    else if (tableInfo.IdType == IdType.Integer)
                                    {
                                        newIdValue = connection
                                                     .CreateCommand(string.Format("select max({0}) from {1};", tableInfo.IdColumn, tableInfo.Name))
                                                     .WithTimeout(120)
                                                     .ExecuteScalar <int>() + 1;
                                    }
                                }
                                if (newIdValue != null)
                                {
                                    _columnMapper.SetMapping(tableInfo.Name, tableInfo.IdColumn, oldIdValue, newIdValue);
                                }
                            }

                            var insertCommand = _module.CreateInsertCommand(connection, _columnMapper, tableInfo, row);
                            if (insertCommand == null)
                            {
                                WarnCantInsertRow(row);
                                _columnMapper.Rollback();
                                continue;
                            }
                            insertCommand.WithTimeout(120).ExecuteNonQuery();
                            rowsSuccess++;

                            if (tableInfo.HasIdColumn() && tableInfo.IdType == IdType.Autoincrement)
                            {
                                var lastIdCommand = _factory.CreateLastInsertIdCommand(_module.ConnectionStringName);
                                lastIdCommand.Connection = connection;
                                newIdValue = Convert.ToInt32(lastIdCommand.ExecuteScalar());
                                _columnMapper.SetMapping(tableInfo.Name, tableInfo.IdColumn, oldIdValue, newIdValue);
                            }

                            _columnMapper.Commit();

                            foreach (Tuple <RelationInfo, TableInfo> relation in lowImportanceRelations)
                            {
                                if (!relation.Item2.HasTenantColumn())
                                {
                                    InvokeWarning("Table {0} does not contain tenant id column. Can't apply low importance relations on such tables.", relation.Item2.Name);
                                    continue;
                                }

                                object oldValue = row[relation.Item1.ParentColumn];
                                object newValue = _columnMapper.GetMapping(relation.Item1.ParentTable, relation.Item1.ParentColumn, oldValue);

                                connection.CreateCommand(string.Format("update {0} set {1} = {2} where {1} = {3} and {4} = {5}",
                                                                       relation.Item1.ChildTable,
                                                                       relation.Item1.ChildColumn,
                                                                       newValue is string? "'" + newValue + "'" : newValue,
                                                                       oldValue is string? "'" + oldValue + "'" : oldValue,
                                                                       relation.Item2.TenantColumn,
                                                                       _columnMapper.GetTenantMapping())).WithTimeout(120).ExecuteNonQuery();
                            }
                        }

                        transaction.Commit();
                        transactionsCommited++;
                        rowsInserted += rowsSuccess;
                    }
                }
            }
        }
        public override void Run()
        {
            InvokeInfo("begin transfer portal ({0})", Tenant.TenantAlias);
            string backupFilePath = GetBackupFilePath();
            var fromDbFactory = new DbFactory(ConfigPath);
            var toDbFactory = new DbFactory(ToConfigPath);
            var columnMapper = new ColumnMapper();
            try
            {
                //target db can have error tenant from the previous attempts
                SaveTenant(toDbFactory, TenantStatus.RemovePending, Tenant.TenantAlias + "_error", "status = " + TenantStatus.Restoring.ToString("d"));

                if (BlockOldPortalAfterStart)
                {
                    SaveTenant(fromDbFactory, TenantStatus.Transfering);
                }

                InitProgress(ProcessStorage ? 3 : 2);

                //save db data to temporary file
                var backupTask = new BackupPortalTask(Tenant, ConfigPath, backupFilePath) {ProcessStorage = false};
                foreach (var moduleName in IgnoredModules)
                {
                    backupTask.IgnoreModule(moduleName);
                }
                RunSubtask(backupTask);
                
                //restore db data from temporary file
                var restoreTask = new RestorePortalTask(ToConfigPath, backupFilePath, columnMapper) {ProcessStorage = false};
                foreach (var moduleName in IgnoredModules)
                {
                    restoreTask.IgnoreModule(moduleName);
                }
                RunSubtask(restoreTask);

                //transfer files
                if (ProcessStorage)
                {
                    DoTransferStorage(columnMapper);
                }

                SaveTenant(toDbFactory, TenantStatus.Active);
                if (DeleteOldPortalAfterCompletion)
                {
                    SaveTenant(fromDbFactory, TenantStatus.RemovePending, Tenant.TenantAlias + "_deleted");
                }
                else if (BlockOldPortalAfterStart)
                {
                    SaveTenant(fromDbFactory, TenantStatus.Active);
                }
            }
            catch
            {
                SaveTenant(fromDbFactory, TenantStatus.Active);
                if (columnMapper.GetTenantMapping() > 0)
                {
                    SaveTenant(toDbFactory, TenantStatus.RemovePending, Tenant.TenantAlias + "_error");
                }
                throw;
            }
            finally
            {
                if (DeleteBackupFileAfterCompletion)
                {
                    File.Delete(backupFilePath);
                }
                InvokeInfo("end transfer portal ({0})", Tenant.TenantAlias);
            }
        }
        private void DoTransferStorage(ColumnMapper columnMapper)
        {
            InvokeInfo("begin transfer storage");
            var fileGroups = GetFilesToProcess().GroupBy(file => file.Module).ToList();
            int groupsProcessed = 0;
            foreach (var group in fileGroups)
            {
                ICrossModuleTransferUtility transferUtility =
                    StorageFactory.GetCrossModuleTransferUtility(
                        ConfigPath, Tenant.TenantId, group.Key,
                        ToConfigPath, columnMapper.GetTenantMapping(), group.Key);

                foreach (BackupFileInfo file in group)
                {
                    string adjustedPath = file.Path;

                    IModuleSpecifics module = ModuleProvider.GetByStorageModule(file.Module, file.Domain);
                    if (module == null || module.TryAdjustFilePath(columnMapper, ref adjustedPath))
                    {
                        try
                        {
                            transferUtility.CopyFile(file.Domain, file.Path, file.Domain, adjustedPath);
                        }
                        catch (Exception error)
                        {
                            InvokeWarning("Can't copy file ({0}:{1}): {2}", file.Module, file.Path, error);
                        }
                    }
                    else
                    {
                        InvokeWarning("Can't adjust file path \"{0}\".", file.Path);
                    }
                }
                SetStepProgress((int)(++groupsProcessed * 100 / (double)fileGroups.Count));
            }

            if (fileGroups.Count == 0)
                SetStepCompleted();

            InvokeInfo("end transfer storage");
        }
Пример #10
0
        private void DoRestoreStorage(IDataReadOperator dataReader)
        {
            Logger.Debug("begin restore storage");

            var fileGroups      = GetFilesToProcess(dataReader).GroupBy(file => file.Module).ToList();
            var groupsProcessed = 0;

            foreach (var group in fileGroups)
            {
                foreach (var file in group)
                {
                    var storage         = StorageFactory.GetStorage(ConfigPath, Dump ? file.Tenant.ToString() : ColumnMapper.GetTenantMapping().ToString(), group.Key);
                    var quotaController = storage.QuotaController;
                    storage.SetQuotaController(null);

                    try
                    {
                        var adjustedPath = file.Path;
                        var module       = ModuleProvider.GetByStorageModule(file.Module, file.Domain);
                        if (module == null || module.TryAdjustFilePath(Dump, ColumnMapper, ref adjustedPath))
                        {
                            var key = file.GetZipKey();
                            if (Dump)
                            {
                                key = CrossPlatform.PathCombine(KeyHelper.GetStorage(), key);
                            }
                            using var stream = dataReader.GetEntry(key);
                            try
                            {
                                storage.Save(file.Domain, adjustedPath, module != null ? module.PrepareData(key, stream, ColumnMapper) : stream);
                            }
                            catch (Exception error)
                            {
                                Logger.WarnFormat("can't restore file ({0}:{1}): {2}", file.Module, file.Path, error);
                            }
                        }
                    }
                    finally
                    {
                        if (quotaController != null)
                        {
                            storage.SetQuotaController(quotaController);
                        }
                    }
                }

                SetCurrentStepProgress((int)(++groupsProcessed * 100 / (double)fileGroups.Count));
            }

            if (fileGroups.Count == 0)
            {
                SetStepCompleted();
            }
            Logger.Debug("end restore storage");
        }
Пример #11
0
        public override void RunJob()
        {
            Logger.Debug("begin restore portal");

            Logger.Debug("begin restore data");

            using (var dataReader = new ZipReadOperator(BackupFilePath))
            {
                using (var entry = dataReader.GetEntry(KeyHelper.GetDumpKey()))
                {
                    Dump = entry != null && CoreBaseSettings.Standalone;
                }

                if (Dump)
                {
                    RestoreFromDump(dataReader);
                }
                else
                {
                    var modulesToProcess = GetModulesToProcess().ToList();
                    SetStepsCount(ProcessStorage ? modulesToProcess.Count + 1 : modulesToProcess.Count);

                    foreach (var module in modulesToProcess)
                    {
                        var restoreTask = new RestoreDbModuleTask(Options, module, dataReader, ColumnMapper, DbFactory, ReplaceDate, Dump, StorageFactory, StorageFactoryConfig, ModuleProvider);
                        restoreTask.ProgressChanged += (sender, args) => SetCurrentStepProgress(args.Progress);
                        foreach (var tableName in IgnoredTables)
                        {
                            restoreTask.IgnoreTable(tableName);
                        }
                        restoreTask.RunJob();
                    }
                }

                Logger.Debug("end restore data");

                if (ProcessStorage)
                {
                    if (CoreBaseSettings.Standalone)
                    {
                        Logger.Debug("clear cache");
                        AscCacheNotify.ClearCache();
                    }

                    DoRestoreStorage(dataReader);
                }
                if (UnblockPortalAfterCompleted)
                {
                    SetTenantActive(ColumnMapper.GetTenantMapping());
                }
            }

            if (CoreBaseSettings.Standalone)
            {
                Logger.Debug("refresh license");
                try
                {
                    LicenseReader.RejectLicense();
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                }

                Logger.Debug("clear cache");
                AscCacheNotify.ClearCache();
            }

            Logger.Debug("end restore portal");
        }
Пример #12
0
        public override void Run()
        {
            InvokeInfo("begin transfer portal ({0})", Tenant.TenantAlias);
            string backupFilePath = GetBackupFilePath();
            var    fromDbFactory  = new DbFactory(ConfigPath);
            var    toDbFactory    = new DbFactory(ToConfigPath);
            var    columnMapper   = new ColumnMapper();

            try
            {
                //target db can have error tenant from the previous attempts
                SaveTenant(toDbFactory, TenantStatus.RemovePending, Tenant.TenantAlias + "_error", "status = " + TenantStatus.Restoring.ToString("d"));

                if (BlockOldPortalAfterStart)
                {
                    SaveTenant(fromDbFactory, TenantStatus.Transfering);
                }

                InitProgress(ProcessStorage ? 3 : 2);

                //save db data to temporary file
                var backupTask = new BackupPortalTask(Tenant, ConfigPath, backupFilePath)
                {
                    ProcessStorage = false
                };
                foreach (var moduleName in IgnoredModules)
                {
                    backupTask.IgnoreModule(moduleName);
                }
                RunSubtask(backupTask);

                //restore db data from temporary file
                var restoreTask = new RestorePortalTask(ToConfigPath, backupFilePath, columnMapper)
                {
                    ProcessStorage = false
                };
                foreach (var moduleName in IgnoredModules)
                {
                    restoreTask.IgnoreModule(moduleName);
                }
                RunSubtask(restoreTask);

                //transfer files
                if (ProcessStorage)
                {
                    DoTransferStorage(columnMapper);
                }

                SaveTenant(toDbFactory, TenantStatus.Active);
                if (DeleteOldPortalAfterCompletion)
                {
                    SaveTenant(fromDbFactory, TenantStatus.RemovePending, Tenant.TenantAlias + "_deleted");
                }
                else if (BlockOldPortalAfterStart)
                {
                    SaveTenant(fromDbFactory, TenantStatus.Active);
                }
            }
            catch
            {
                SaveTenant(fromDbFactory, TenantStatus.Active);
                if (columnMapper.GetTenantMapping() > 0)
                {
                    SaveTenant(toDbFactory, TenantStatus.RemovePending, Tenant.TenantAlias + "_error");
                }
                throw;
            }
            finally
            {
                if (DeleteBackupFileAfterCompletion)
                {
                    File.Delete(backupFilePath);
                }
                InvokeInfo("end transfer portal ({0})", Tenant.TenantAlias);
            }
        }