summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--jstests/tool/dumprestore1.js20
-rw-r--r--jstests/tool/dumprestore2.js24
-rw-r--r--jstests/tool/exportimport1.js20
-rw-r--r--jstests/tool/exportimport2.js24
-rw-r--r--shell/servers.js49
-rw-r--r--tools/dump.cpp2
-rw-r--r--tools/export.cpp23
7 files changed, 158 insertions, 4 deletions
diff --git a/jstests/tool/dumprestore1.js b/jstests/tool/dumprestore1.js
new file mode 100644
index 00000000000..73f8feae558
--- /dev/null
+++ b/jstests/tool/dumprestore1.js
@@ -0,0 +1,20 @@
+// dumprestore1.js
+
+t = new ToolTest( "dumprestore1" );
+
+c = t.startDB( "foo" );
+assert.eq( 0 , c.count() , "setup1" );
+c.save( { a : 22 } );
+assert.eq( 1 , c.count() , "setup2" );
+
+t.runTool( "dump" , "--out" , t.ext );
+
+c.drop();
+assert.eq( 0 , c.count() , "after drop" );
+
+t.runTool( "restore" , "--dir" , t.ext );
+assert.soon( "c.findOne()" , "no data after sleep" );
+assert.eq( 1 , c.count() , "after restore 2" );
+assert.eq( 22 , c.findOne().a , "after restore 2" );
+
+t.stop();
diff --git a/jstests/tool/dumprestore2.js b/jstests/tool/dumprestore2.js
new file mode 100644
index 00000000000..8b9291ed916
--- /dev/null
+++ b/jstests/tool/dumprestore2.js
@@ -0,0 +1,24 @@
+// dumprestore2.js
+
+t = new ToolTest( "dumprestore2" );
+
+c = t.startDB( "foo" );
+assert.eq( 0 , c.count() , "setup1" );
+c.save( { a : 22 } );
+assert.eq( 1 , c.count() , "setup2" );
+t.stop();
+
+t.runTool( "dump" , "--dbpath" , t.dbpath , "--out" , t.ext );
+
+resetDbpath( t.dbpath );
+assert.eq( 0 , listFiles( t.dbpath ).length , "clear" );
+
+t.runTool( "restore" , "--dbpath" , t.dbpath , "--dir" , t.ext );
+
+c = t.startDB( "foo" );
+assert.soon( "c.findOne()" , "no data after startup" );
+assert.eq( 1 , c.count() , "after restore 2" );
+assert.eq( 22 , c.findOne().a , "after restore 2" );
+
+t.stop();
+
diff --git a/jstests/tool/exportimport1.js b/jstests/tool/exportimport1.js
new file mode 100644
index 00000000000..22934febb2e
--- /dev/null
+++ b/jstests/tool/exportimport1.js
@@ -0,0 +1,20 @@
+// exportimport1.js
+
+t = new ToolTest( "exportimport1" );
+
+c = t.startDB( "foo" );
+assert.eq( 0 , c.count() , "setup1" );
+c.save( { a : 22 } );
+assert.eq( 1 , c.count() , "setup2" );
+
+t.runTool( "export" , "--out" , t.extFile , "-d" , t.baseName , "-c" , "foo" );
+
+c.drop();
+assert.eq( 0 , c.count() , "after drop" , "-d" , t.baseName , "-c" , "foo" );;
+
+t.runTool( "import" , "--file" , t.extFile , "-d" , t.baseName , "-c" , "foo" );
+assert.soon( "c.findOne()" , "no data after sleep" );
+assert.eq( 1 , c.count() , "after restore 2" );
+assert.eq( 22 , c.findOne().a , "after restore 2" );
+
+t.stop();
diff --git a/jstests/tool/exportimport2.js b/jstests/tool/exportimport2.js
new file mode 100644
index 00000000000..fbcf2399d8c
--- /dev/null
+++ b/jstests/tool/exportimport2.js
@@ -0,0 +1,24 @@
+// exportimport2.js
+
+t = new ToolTest( "exportimport2" );
+
+c = t.startDB( "foo" );
+assert.eq( 0 , c.count() , "setup1" );
+c.save( { a : 22 } );
+assert.eq( 1 , c.count() , "setup2" );
+t.stop();
+
+t.runTool( "export" , "--dbpath" , t.dbpath , "--out" , t.extFile , "-d" , t.baseName , "-c" , "foo" );
+
+resetDbpath( t.dbpath );
+assert.eq( 0 , listFiles( t.dbpath ).length , "clear" );
+
+t.runTool( "import" , "--dbpath" , t.dbpath , "--file" , t.extFile , "-d" , t.baseName , "-c" , "foo" );
+
+c = t.startDB( "foo" );
+assert.soon( "c.findOne()" , "no data after startup" );
+assert.eq( 1 , c.count() , "after restore 2" );
+assert.eq( 22 , c.findOne().a , "after restore 2" );
+
+t.stop();
+
diff --git a/shell/servers.js b/shell/servers.js
index 4be377c42e5..884b114ae60 100644
--- a/shell/servers.js
+++ b/shell/servers.js
@@ -388,3 +388,52 @@ ReplPair.prototype.toString = function() {
ret += " " + this._annotatedNode( this.rightC_ );
return ret;
}
+
+
+ToolTest = function( name ){
+ this.name = name;
+ this.port = allocatePorts(1)[0];
+ this.baseName = "jstests_tool_" + name;
+ this.root = "/data/db/" + this.baseName;
+ this.dbpath = this.root + "/";
+ this.ext = this.root + "_external/";
+ this.extFile = this.root + "_external/a";
+ resetDbpath( this.dbpath );
+}
+
+ToolTest.prototype.startDB = function( coll ){
+ assert( ! this.m , "db already running" );
+
+ this.m = startMongoProgram( "mongod" , "--port", this.port , "--dbpath" , this.dbpath , "--nohttpinterface", "--noprealloc" , "--smallfiles" , "--bind_ip", "127.0.0.1" );
+ this.db = this.m.getDB( this.baseName );
+ if ( coll )
+ return this.db.getCollection( coll );
+ return this.db;
+}
+
+ToolTest.prototype.stop = function(){
+ if ( ! this.m )
+ return;
+ stopMongod( this.port );
+ this.m = null;
+ this.db = null;
+}
+
+ToolTest.prototype.runTool = function(){
+ var a = [ "mongo" + arguments[0] ];
+
+ var hasdbpath = false;
+
+ for ( var i=1; i<arguments.length; i++ ){
+ a.push( arguments[i] );
+ if ( arguments[i] == "--dbpath" )
+ hasdbpath = true;
+ }
+
+ if ( ! hasdbpath ){
+ a.push( "--host" );
+ a.push( "127.0.0.1:" + this.port );
+ }
+
+ runMongoProgram.apply( null , a );
+}
diff --git a/tools/dump.cpp b/tools/dump.cpp
index ff354f725e4..c67b6b1fd33 100644
--- a/tools/dump.cpp
+++ b/tools/dump.cpp
@@ -38,7 +38,7 @@ public:
cout << "\t" << coll << " to " << outputFile.string() << endl;
ofstream out;
- out.open( outputFile.string().c_str() );
+ out.open( outputFile.string().c_str() , ios_base::out | ios_base::binary );
uassert( "couldn't open file" , out.good() );
ProgressMeter m( conn( true ).count( coll.c_str() ) );
diff --git a/tools/export.cpp b/tools/export.cpp
index f142cefecb4..16b1cd8baa3 100644
--- a/tools/export.cpp
+++ b/tools/export.cpp
@@ -47,8 +47,21 @@ public:
const bool csv = hasParam( "csv" );
ostream *outPtr = &cout;
string outfile = getParam( "out" );
- if ( hasParam( "out" ) )
- outPtr = new ofstream( outfile.c_str() );
+ auto_ptr<ofstream> fileStream;
+ if ( hasParam( "out" ) ){
+ size_t idx = outfile.rfind( "/" );
+ if ( idx != string::npos ){
+ string dir = outfile.substr( 0 , idx + 1 );
+ create_directories( dir );
+ }
+ ofstream * s = new ofstream( outfile.c_str() , ios_base::out | ios_base::binary );
+ fileStream.reset( s );
+ outPtr = s;
+ if ( ! s->good() ){
+ cerr << "couldn't open [" << outfile << "]" << endl;
+ return -1;
+ }
+ }
ostream &out = *outPtr;
BSONObj * fieldsToReturn = 0;
@@ -85,8 +98,10 @@ public:
}
out << endl;
}
-
+
+ long long num = 0;
while ( cursor->more() ) {
+ num++;
BSONObj obj = cursor->next();
if ( csv ){
for ( vector<string>::iterator i=_fields.begin(); i != _fields.end(); i++ ){
@@ -104,6 +119,8 @@ public:
}
}
+
+ cout << "exported " << num << " records" << endl;
return 0;
}