Beispiel #1
0
        /// <summary>
        /// Migrates data and indexes of all collections of a certain database, to another
        /// </summary>
        /// <param name="sourceServer">Source mongodb server  - Where the data will come from.</param>
        /// <param name="targetServer">Target mongodb server - Where the data will go to.</param>
        /// <param name="sourceDatabases">The source databases.</param>
        /// <param name="targetDatabases">The target databases.</param>
        /// <param name="collections">The collections.</param>
        /// <param name="insertBatchSize">Size (in records) of the chunk of data that will be inserted per batch.</param>
        /// <param name="copyIndexes">True if the indexes should be copied aswell, false otherwise.</param>
        /// <param name="dropCollections">The drop collections.</param>
        /// <param name="threads">The threads.</param>
        public static void DatabaseCopy(MongoServer sourceServer, MongoServer targetServer, List <string> sourceDatabases, List <string> targetDatabases, List <string> collections, string targetCollection, int insertBatchSize = -1, bool copyIndexes = true, bool dropCollections = false, bool skipCount = false, bool eraseObjectId = false, int threads = 1, FlexibleOptions options = null)
        {
            if (threads <= 1)
            {
                threads = 1;
            }

            // check if we are on the same server!
            bool sameServer = ServersAreEqual(sourceServer, targetServer);

            // Validating whether we received multiple matches (or collection names) when the "target collection" has value
            var databases           = ListDatabases(sourceServer, targetServer, sourceDatabases, targetDatabases);
            var matchingCollections = ListCollections(databases.First().Item1, collections, targetCollection).ToList();

            if (matchingCollections != null && matchingCollections.Count > 1 && !String.IsNullOrWhiteSpace(targetCollection))
            {
                // Error. In order to specify a "TargetCollection" there should be only one collection matching the mask or received as argument (as it's source)
                NLog.LogManager.GetLogger("DatabaseCopy").Error("In order to specify a 'TargetCollection' there should be only one collection matching the mask or received as argument (as it's source)");
                return;
            }

            // create our thread manager and start producing tasks...
            using (var mgr = new MongoToolsLib.SimpleHelpers.ParallelTasks <CopyInfo> (0, threads, 1000, CollectionCopy))
            {
                // list databases
                foreach (var db in ListDatabases(sourceServer, targetServer, sourceDatabases, targetDatabases))
                {
                    foreach (var col in ListCollections(db.Item1, collections, targetCollection))
                    {
                        // sanity checks
                        if (sameServer && db.Item1.ToString() == db.Item2.ToString() && col.Item1.ToString() == col.Item2.ToString())
                        {
                            NLog.LogManager.GetLogger("DatabaseCopy").Warn("Skiping collection, since it would be copied to itself! Database: {0}, Collection: {1}", db.Item1, col.Item1);
                            continue;
                        }

                        // process task
                        mgr.AddTask(new CopyInfo
                        {
                            SourceDatabase   = db.Item1,
                            TargetDatabase   = db.Item2,
                            SourceCollection = col.Item1,
                            TargetCollection = col.Item2,
                            BatchSize        = insertBatchSize,
                            CopyIndexes      = copyIndexes,
                            DropCollections  = dropCollections,
                            EraseObjectId    = eraseObjectId,
                            Options          = options,
                            SkipCount        = skipCount
                        });
                    }
                }
                mgr.CloseAndWait();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Migrates data and indexes of all collections of a certain database, to another
        /// </summary>
        /// <param name="sourceServer">Source mongodb server  - Where the data will come from.</param>
        /// <param name="targetServer">Target mongodb server - Where the data will go to.</param>
        /// <param name="sourceDatabases">The source databases.</param>
        /// <param name="targetDatabases">The target databases.</param>
        /// <param name="collections">The collections.</param>
        /// <param name="insertBatchSize">Size (in records) of the chunk of data that will be inserted per batch.</param>
        /// <param name="copyIndexes">True if the indexes should be copied aswell, false otherwise.</param>
        /// <param name="dropCollections">The drop collections.</param>
        /// <param name="threads">The threads.</param>
        public static void DatabaseCopy(MongoServer sourceServer, MongoServer targetServer, List <string> sourceDatabases, List <string> targetDatabases, List <string> collections, int insertBatchSize = -1, bool copyIndexes = true, bool dropCollections = false, int threads = 1, FlexibleOptions options = null)
        {
            if (threads <= 1)
            {
                threads = 1;
            }

            // check if we are on the same server!
            bool sameServer = ServersAreEqual(sourceServer, targetServer);

            // create our thread manager and start producing tasks...
            using (var mgr = new MongoToolsLib.SimpleHelpers.ParallelTasks <CopyInfo> (0, threads, 1000, CollectionCopy))
            {
                // list databases
                foreach (var db in ListDatabases(sourceServer, targetServer, sourceDatabases, targetDatabases))
                {
                    foreach (var col in ListCollections(db.Item1, collections))
                    {
                        // sanity checks
                        if (sameServer && db.Item1 == db.Item2 && col.Item1 == col.Item2)
                        {
                            NLog.LogManager.GetLogger("DatabaseCopy").Warn("Skiping collection, since it would be copied to itself! Database: {0}, Collection: {1}", db.Item1, col.Item1);
                            continue;
                            //throw new Exception ("Source and target servers and databases are the same!");
                        }

                        // process task
                        mgr.AddTask(new CopyInfo
                        {
                            SourceDatabase   = db.Item1,
                            TargetDatabase   = db.Item2,
                            SourceCollection = col.Item1,
                            TargetCollection = col.Item2,
                            BatchSize        = insertBatchSize,
                            CopyIndexes      = copyIndexes,
                            DropCollections  = dropCollections,
                            Options          = options
                        });
                    }
                }
                mgr.CloseAndWait();
            }
        }