static void Main(string[] args) { // Parameters Check if (args == null || args.Length == 0) { // Error Console.WriteLine("No arguments received. Type pass '-h' to receive a list of parameters"); System.Environment.Exit(-101); } // Should print help ? if (args.Where(t => t == "-h").FirstOrDefault() != null) { // Printing Help - Parameters PrintHelp(); return; } // Parsing Arguments - Sanity Check ParseArguments(args); // Reading App Config LoadConfiguration(); Console.WriteLine("Reaching Databases"); // Building Connection Strings String sourceConnString = MongoDbContext.BuildConnectionString(_sourceUsername, _sourcePassword, _sourceServer, _authDatabaseNameSource); String targetConnString = MongoDbContext.BuildConnectionString(_targetUsername, _targetPassword, _targetServer, _authDatabaseNameTarget); // Reaching Databases MongoDatabase sourceDatabase = MongoDbContext.GetServer(sourceConnString).GetDatabase(_sourceDatabaseName); MongoDatabase targetDatabase = MongoDbContext.GetServer(targetConnString).GetDatabase(_targetDatabaseName); Console.WriteLine("Merging Data"); // Picking which method to use switch (_mergeMode) { case MergeMode.FullDatabaseMerge: Console.WriteLine("Merging Full Database into Collection : " + _targetCollectionName); Merger.DatabaseMerge(sourceDatabase, targetDatabase, _targetCollectionName, _insertBatchSize); break; case MergeMode.CollectionsMerge: Console.WriteLine("Merging Collections from List, into Collection : " + _targetCollectionName); Merger.CollectionsMerge(sourceDatabase, targetDatabase, _targetCollectionName, _collections, _insertBatchSize); break; case MergeMode.CollectionsMaskMerge: Console.WriteLine("Merging Collections that matches : " + _collections.Value.First() + " into Collection" + _targetCollectionName); Merger.CollectionsMerge(sourceDatabase, targetDatabase, _targetCollectionName, _collections.Value.First(), _insertBatchSize); break; } Console.WriteLine("Merge Finished"); Console.ReadLine(); }
static void Main(string[] args) { // Parameters Check if (args == null || args.Length == 0) { // Error Console.WriteLine("No arguments received. Type pass '-h' to receive a list of parameters"); System.Environment.Exit(-101); } // Should print help ? if (args.Where(t => t == "-h").FirstOrDefault() != null) { // Printing Help - Parameters PrintHelp(); return; } // Reading App Config LoadConfiguration(); // Parsing Arguments ParseArguments(args); Console.WriteLine("Reaching Database"); // Building Connection Strings String sourceConnString = MongoDbContext.BuildConnectionString(_sourceUsername, _sourcePassword, _sourceServer, _authDatabaseName); // Reaching Database MongoDatabase sourceDatabase = MongoDbContext.GetServer(sourceConnString).GetDatabase(_sourceDatabaseName); // Picking which method to use switch (_duplicateMode) { case DuplicateMode.DuplicateCollections: Console.WriteLine("Duplicating collection from List"); Duplicator.CollectionsDuplicate(sourceDatabase, _collections, _insertBatchSize, _copyIndexes, _duplicationSuffix); break; case DuplicateMode.DuplicateCollectionsWithMask: Console.WriteLine("Duplicating Collections that matches : " + _collections.Value.First()); Duplicator.CollectionsDuplicate(sourceDatabase, _collections.Value.First(), _insertBatchSize, _copyIndexes, _duplicationSuffix); break; } Console.WriteLine("Duplication Finished"); Console.ReadLine(); }
private static void CheckConnections(FlexibleOptions options) { // source server if (!String.IsNullOrWhiteSpace(options.Get("source"))) { if (options.Get("source").IndexOf("://") < 1) { options.Set("source", "mongodb://" + options.Get("source")); } var mongoUri = new MongoUrlBuilder(options.Get("source")); if (mongoUri.ConnectTimeout.TotalSeconds < 30) { mongoUri.ConnectTimeout = TimeSpan.FromSeconds(30); } if (mongoUri.SocketTimeout.TotalMinutes < 4) { mongoUri.SocketTimeout = TimeSpan.FromMinutes(4); } if (mongoUri.MaxConnectionIdleTime.TotalSeconds < 30) { mongoUri.MaxConnectionIdleTime = TimeSpan.FromSeconds(30); } // check for missing uri parameters if (!String.IsNullOrWhiteSpace(_sourceUsername) && String.IsNullOrWhiteSpace(mongoUri.Username)) { mongoUri.Username = _sourceUsername; } if (!String.IsNullOrWhiteSpace(_sourcePassword) && String.IsNullOrWhiteSpace(mongoUri.Password)) { mongoUri.Password = _sourcePassword; } if (!String.IsNullOrWhiteSpace(_sourceAuthDatabase) && String.IsNullOrWhiteSpace(mongoUri.AuthenticationSource)) { mongoUri.AuthenticationSource = _sourceAuthDatabase; } options.Set("source", mongoUri.ToString()); } else { options.Set("source", MongoDbContext.BuildConnectionString(_sourceUsername, _sourcePassword, true, true, _sourceServer, 30000, 4 * 60000, _sourceAuthDatabase)); } // check connection try { MongoDbContext.GetServer(options.Get("source")).Ping(); } catch (Exception ex) { logger.Error("Failed to connect to source mongodb server. Uri: {0}. Details: {1}", options.Get("source"), ex.Message); ConsoleUtils.CloseApplication(-111, true); } // target server if (!String.IsNullOrWhiteSpace(options.Get("target"))) { if (options.Get("target").IndexOf("://") < 1) { options.Set("target", "mongodb://" + options.Get("target")); } var mongoUri = new MongoUrlBuilder(options.Get("target")); if (mongoUri.ConnectTimeout.TotalSeconds < 30) { mongoUri.ConnectTimeout = TimeSpan.FromSeconds(30); } if (mongoUri.SocketTimeout.TotalMinutes < 4) { mongoUri.SocketTimeout = TimeSpan.FromMinutes(4); } if (mongoUri.MaxConnectionIdleTime.TotalSeconds < 30) { mongoUri.MaxConnectionIdleTime = TimeSpan.FromSeconds(30); } // check for missing uri parameters if (!String.IsNullOrWhiteSpace(_sourceUsername) && String.IsNullOrWhiteSpace(mongoUri.Username)) { mongoUri.Username = _sourceUsername; } if (!String.IsNullOrWhiteSpace(_sourcePassword) && String.IsNullOrWhiteSpace(mongoUri.Password)) { mongoUri.Password = _sourcePassword; } if (!String.IsNullOrWhiteSpace(_sourceAuthDatabase) && String.IsNullOrWhiteSpace(mongoUri.AuthenticationSource)) { mongoUri.AuthenticationSource = _sourceAuthDatabase; } options.Set("target", mongoUri.ToString()); } else { options.Set("target", MongoDbContext.BuildConnectionString(_targetUsername, _targetPassword, true, true, _targetServer, 30000, 4 * 60000, _targetAuthDatabase)); } // check connection try { MongoDbContext.GetServer(options.Get("target")).Ping(); } catch (Exception ex) { logger.Error("Failed to connect to target mongodb server. Uri: {0}. Details: {1}", options.Get("target"), ex.Message); ConsoleUtils.CloseApplication(-112, true); } }
private static void Execute(FlexibleOptions options) { Logger.Info("Start"); // Args Sanity Check if (options.Options == null || options.Options.Count == 0) { Console.WriteLine("No arguments received."); System.Environment.Exit(-101); } // Prompts for user Input Console.WriteLine("Is the configuration correct ? Y/N"); var key = Console.ReadKey().Key; // Checking Key if (key == ConsoleKey.N) // N = "NO" { Console.WriteLine(" => 'NO' : Aborting"); System.Environment.Exit(-102); } else if (key != ConsoleKey.Y) // Anything other than "N" and "Y" is an error. { Console.WriteLine(" => 'Wrong Key Pressed' : Expected either 'Y' or 'N'"); System.Environment.Exit(-102); } Console.WriteLine(" => Proceeding with Export."); // Sanity Check of Config and Arguments if (!ValidateConfig(options)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Missing MongoDB Configuration Parameter. (Server, Database, Collection and Credentials are Mandatory)"); Console.ForegroundColor = ConsoleColor.White; System.Environment.Exit(-103); } // Creating instance of MongoDB String sourceConnString = MongoDbContext.BuildConnectionString(options["sourceUsername"], options["sourcePassword"], options["sourceServer"], options["authDatabaseName"]); // Reaching Databases MongoDatabase sourceDatabase = MongoDbContext.GetServer(sourceConnString).GetDatabase(options["sourceDatabaseName"]); // Assembling "Query" to MongoDB, if any query text was provided QueryDocument query = String.IsNullOrWhiteSpace(options["mongoQuery"]) ? null : new QueryDocument(QueryDocument.Parse(options["mongoQuery"])); // Checking if the provided Collection Exists if (!sourceDatabase.CollectionExists(options["collection"])) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Collection [ " + options["collection"] + " ] does not exists on the specified database"); Console.ForegroundColor = ConsoleColor.White; System.Environment.Exit(-104); } if (options["format"].ToUpper() == "CSV") { // Loading Export Configuration from XML File if (!JsonToCSV.LoadExportLayout(options["layoutFile"])) { // Error Checking Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error Loading Export Layout"); Console.WriteLine("Message : " + JsonToCSV.errorMessage); Console.ForegroundColor = ConsoleColor.White; System.Environment.Exit(-105); } } // Setting up MongoDB Cursor MongoCursor cursor = sourceDatabase.GetCollection <BsonDocument> (options["collection"]).Find(query); cursor.SetFlags(QueryFlags.NoCursorTimeout); // Checking for the need to apply limit int _limit = options.Get <int>("limit", -1); if (_limit != -1) { cursor.SetLimit(_limit); } // Counters int recordsProcessed = 0; // JSON Settings to keep the "JSON" output as "Strict" var jsonSettings = new JsonWriterSettings() { OutputMode = JsonOutputMode.Strict }; // File Writer using (StreamWriter fWriter = new StreamWriter(options["outputFile"], false, Encoding.UTF8)) { // Auto Flush fWriter.AutoFlush = true; // Output File Line string fileLine = String.Empty; // Should we add headers to the output CSV file? if (options["format"].ToUpper() == "CSV" && options.Get <bool>("addHeader", false)) { // Writing Headers fWriter.WriteLine(JsonToCSV.Fields); } // Iterating over documents found using the query foreach (BsonDocument document in cursor) { // Picking which export method will be used if (options["format"].ToUpper() == "CSV") { // Extracting data from it fileLine = JsonToCSV.BsonToCSV(document); } else { fileLine = document.ToJson(jsonSettings); } // Checking for errors if (String.IsNullOrWhiteSpace(fileLine)) { continue; } // Writing to output csv fWriter.WriteLine(fileLine.Replace(System.Environment.NewLine, "<br>")); // Counting if (recordsProcessed++ % 100 == 0) { Console.WriteLine("Processed : " + recordsProcessed); } } } Logger.Info("End"); }