Skip to content

Instantly share code, notes, and snippets.

@linjer
Last active August 29, 2015 14:23
Show Gist options
  • Save linjer/cfedb5b24b9dd6c6925a to your computer and use it in GitHub Desktop.
Save linjer/cfedb5b24b9dd6c6925a to your computer and use it in GitHub Desktop.
Script for seeding local development mongodb instance from data on a production server
// Copies data from every mongo collection in production to your local instance
// Usage:
// 1. Configure the desired parameters below
// 2. $ mongo local-mongo-seed-all-collections.js
/*=================================
* Configuration parameters
*===============================*/
var config = {
SOURCE_SERVER: '127.0.0.1',
USERNAME: 'user',
PASSWORD: 'password',
DESTINATION_DB: 'test',
COLLECTION_EXCLUDES: [
'system.indexes',
'system.profile',
'system.users'
// e.g., 'users', accounts
],
DEFAULT_LIMIT: 10000, // Unless specified in LIMITS, max number of objects to copy from a collection
LIMITS: { // Maps collectionName -> max number of entries to copy
'users': 15000,
'accounts': 350
}
};
/*=================================
* Utility methods
*===============================*/
var validateExcludes = function(allCollections) {
var filtered = config.COLLECTION_EXCLUDES.filter(function(n) {
return allCollections.indexOf(n) != -1;
});
if((config.COLLECTION_EXCLUDES - filtered.length) !== 0) {
print('Invalid collection exclusions found. Did you have a typo?');
print('You provided: ' + config.COLLECTION_EXCLUDES);
print('Validated: ' + filtered);
config.COLLECTION_EXCLUDES = filtered;
}
};
var copyWithOrphans = function() {
var collections = prod.getCollectionNames();
var collectionCount = 1;
validateExcludes(collections);
collections.forEach(function(collection) {
if(config.COLLECTION_EXCLUDES.indexOf(collection) == -1) {
var limit = collection in config.LIMITS ? config.LIMITS[collection] : config.DEFAULT_LIMIT;
var copyCount = 0;
print('Collection [' + collectionCount + '/' + (collections.length - config.COLLECTION_EXCLUDES.length) +
']: ' + collection);
var result = prod.getCollection(collection).find().sort({_id:-1}).limit(limit);
while (result.hasNext()) {
var obj = result.next();
// print('\t' + tojson(obj)); // Too verbose
local.getCollection(collection).insert(obj);
copyCount++;
}
print('\tSuccessfully copied newest ' + copyCount + '/' + result.count() + ' objects.');
print('\tLocal: ' + collection + ' now has ' + local.getCollection(collection).count() + ' objects.');
collectionCount++;
}
});
};
/*=================================
* Script runner body
*===============================*/
var local = db.getSiblingDB(config.DESTINATION_DB);
print(db.serverStatus().host + ' destination database: ' + local);
var prodConnection = new Mongo(config.SOURCE_SERVER);
var prod = prodConnection.getDB('connectifier');
prod.auth(config.USERNAME, config.PASSWORD);
prodConnection.setReadPref('secondary');
var startTime = new Date().getTime();
copyWithOrphans();
var endTime = new Date().getTime();
var totalMS = endTime - startTime;
var min = Math.floor(totalMS/60000);
var sec = Math.floor((totalMS%60000)/1000);
var milli = Math.floor((totalMS%60000%1000));
print('Finished! Total execution time: ' + min + 'min, ' + sec + 'sec, ' + milli + 'ms.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment