Exemple #1
0
        public void LoadFrom(IEnumerable <XElement> elements, int tenant, string[] configs, IDataReadOperator dataOperator)
        {
            InvokeProgressChanged("Restoring files...", 0);

            var config = GetWebConfig(configs);

            AppDomain.CurrentDomain.SetData(Constants.CustomDataDirectory, Path.GetDirectoryName(config));
            var    files   = elements.Where(e => e.Name == "file");
            double current = 0;

            foreach (var file in files)
            {
                var backupInfo = new FileBackupInfo(file);
                if (allowedModules.Contains(backupInfo.Module))
                {
                    var entry   = dataOperator.GetEntry(GetBackupPath(backupInfo));
                    var storage = StorageFactory.GetStorage(config, tenant.ToString(), backupInfo.Module, null, null);
                    try
                    {
                        storage.Save(backupInfo.Domain, backupInfo.Path, entry);
                    }
                    catch (Exception error)
                    {
                        log.ErrorFormat("Can not restore file {0}: {1}", file, error);
                    }

                    InvokeProgressChanged("Restoring file " + backupInfo.Path, current++ / files.Count() * 100);
                }
            }
        }
        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 = Path.Combine(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");
        }
 private async Task RestoreFromDumpFile(IDataReadOperator dataReader, string fileName)
 {
     Logger.DebugFormat("Restore from {0}", fileName);
     using (var stream = dataReader.GetEntry(fileName))
     {
         await RunMysqlFile(stream);
     }
     SetStepCompleted();
 }
        private IEnumerable <BackupFileInfo> GetFilesToProcess(IDataReadOperator dataReader)
        {
            using var stream = dataReader.GetEntry(KeyHelper.GetStorageRestoreInfoZipKey());
            if (stream == null)
            {
                return(Enumerable.Empty <BackupFileInfo>());
            }
            var restoreInfo = XElement.Load(new StreamReader(stream));

            return(restoreInfo.Elements("file").Select(BackupFileInfo.FromXElement).ToList());
        }
        private async Task RestoreFromDumpFile(IDataReadOperator dataReader, string fileName1, string fileName2 = null, string db = "default")
        {
            Logger.DebugFormat("Restore from {0}", fileName1);
            using (var stream = dataReader.GetEntry(fileName1))
            {
                await RunMysqlFile(stream, db);
            }
            SetStepCompleted();

            Logger.DebugFormat("Restore from {0}", fileName2);
            if (fileName2 != null)
            {
                using (var stream = dataReader.GetEntry(fileName2))
                {
                    await RunMysqlFile(stream, db);
                }

                SetStepCompleted();
            }
        }
        private void RestoreDatabase(ConnectionStringSettings connectionString, IEnumerable <XElement> elements, IDataReadOperator reader)
        {
            var dbName    = connectionString.Name;
            var dbElement = elements.SingleOrDefault(e => string.Compare(e.Name.LocalName, connectionString.Name, true) == 0);

            if (dbElement != null && dbElement.Attribute("ref") != null)
            {
                dbName    = dbElement.Attribute("ref").Value;
                dbElement = elements.Single(e => string.Compare(e.Name.LocalName, dbElement.Attribute("ref").Value, true) == 0);
            }
            if (dbElement == null)
            {
                return;
            }

            using (var dbHelper = new DbHelper(connectionString))
            {
                var tables = dbHelper.GetTables();
                for (int i = 0; i < tables.Count; i++)
                {
                    var table = tables[i];
                    OnProgressChanged(table, (int)(i / (double)tables.Count * 100));

                    if (processedTables.Contains(table, StringComparer.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    if (dbElement.Element(table) != null)
                    {
                        using (var stream = reader.GetEntry(string.Format("{0}\\{1}\\{2}", Name, dbName, table).ToLower()))
                        {
                            var data = new DataTable();
                            data.ReadXml(stream);
                            dbHelper.SetTable(data);
                        }
                        processedTables.Add(table);
                    }
                }
            }
        }
        public void LoadFrom(IEnumerable <XElement> elements, int tenant, string[] configs, IDataReadOperator dataOperator)
        {
            InvokeProgressChanged("Restoring files started", -1);

            var config = GetWebConfig(configs);

            AppDomain.CurrentDomain.SetData(Constants.CustomDataDirectory, Path.GetDirectoryName(config));
            var    files   = elements.Where(e => e.Name == "file");
            double current = 0;

            foreach (var file in files)
            {
                var backupInfo = new FileBackupInfo(file);
                var entry      = dataOperator.GetEntry(GetBackupPath(backupInfo));
                var storage    = StorageFactory.GetStorage(config, tenant.ToString(), backupInfo.Module, null, null);
                storage.Save(backupInfo.Domain, backupInfo.Path, entry);

                InvokeProgressChanged("Restoring files", current / files.Count() * 100);
                current++;
            }

            InvokeProgressChanged("OK", 100);
        }
        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");
        }
        private void RestoreDatabase(int tenant, ConnectionStringSettings connectionString, IEnumerable<XElement> elements, IDataReadOperator reader)
        {
            var dbName = connectionString.Name;
            var dbElement = elements.SingleOrDefault(e => string.Compare(e.Name.LocalName, connectionString.Name, true) == 0);
            if (dbElement != null && dbElement.Attribute("ref") != null)
            {
                dbName = dbElement.Attribute("ref").Value;
                dbElement = elements.Single(e => string.Compare(e.Name.LocalName, dbElement.Attribute("ref").Value, true) == 0);
            }
            if (dbElement == null) return;

            using (var dbHelper = new DbHelper(tenant, connectionString))
            {
                var tables = dbHelper.GetTables();
                for (int i = 0; i < tables.Count; i++)
                {
                    var table = tables[i];
                    OnProgressChanged(table, (int)(i / (double)tables.Count * 100));

                    if (dbElement.Element(table) == null) continue;

                    var stream = reader.GetEntry(string.Format("{0}\\{1}\\{2}", Name, dbName, table).ToLower());
                    var data = new DataTable();
                    data.ReadXml(stream);

                    dbHelper.SetTable(data);
                }
            }
        }
Exemple #10
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;
                    }
                }
            }
        }
        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");
        }
        private IEnumerable<BackupFileInfo> GetFilesToProcess(IDataReadOperator dataReader)
        {
            using (Stream stream = dataReader.GetEntry(KeyHelper.GetStorageRestoreInfoZipKey()))
            {
                if (stream == null)
                    return Enumerable.Empty<BackupFileInfo>();

                var restoreInfo = XElement.Load(new StreamReader(stream));
                return restoreInfo.Elements("file").Select(BackupFileInfo.FromXElement).ToList();
            }
        }
        public void LoadFrom(IEnumerable<XElement> elements, int tenant, string[] configs, IDataReadOperator dataOperator)
        {
            InvokeProgressChanged("Restoring files...", 0);

            var config = GetWebConfig(configs);
            AppDomain.CurrentDomain.SetData(Constants.CustomDataDirectory, Path.GetDirectoryName(config));
            var files = elements.Where(e => e.Name == "file");
            double current = 0;
            foreach (var file in files)
            {
                var backupInfo = new FileBackupInfo(file);
                if (allowedModules.Contains(backupInfo.Module))
                {
                    var entry = dataOperator.GetEntry(GetBackupPath(backupInfo));
                    var storage = StorageFactory.GetStorage(config, tenant.ToString(), backupInfo.Module, null, null);
                    try
                    {
                        storage.Save(backupInfo.Domain, backupInfo.Path, entry);
                    }
                    catch (Exception error)
                    {
                        log.ErrorFormat("Can not restore file {0}: {1}", file, error);
                    }

                    InvokeProgressChanged("Restoring file " + backupInfo.Path, current++ / files.Count() * 100);
                }
            }
        }