0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 01:21:03 +01:00
mongodb/tools/dump.cpp

122 lines
3.6 KiB
C++
Raw Normal View History

2008-12-29 23:52:45 +01:00
// dump.cpp
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../stdafx.h"
#include "../client/dbclient.h"
2009-09-24 17:02:00 +02:00
#include "tool.h"
2008-12-29 23:52:45 +01:00
#include <fcntl.h>
2009-01-27 21:16:09 +01:00
using namespace mongo;
2008-12-29 23:52:45 +01:00
2009-01-27 21:16:09 +01:00
namespace po = boost::program_options;
2009-01-14 23:17:24 +01:00
2009-01-27 21:16:09 +01:00
class Dump : public Tool {
public:
Dump() : Tool( "dump" , "*" ){
add_options()
("out,o", po::value<string>()->default_value("dump"), "output directory")
2009-01-27 21:16:09 +01:00
;
}
2009-09-11 20:15:28 +02:00
2009-01-27 21:16:09 +01:00
void doCollection( const string coll , path outputFile ) {
cout << "\t" << coll << " to " << outputFile.string() << endl;
ofstream out;
2009-10-10 04:11:15 +02:00
out.open( outputFile.string().c_str() , ios_base::out | ios_base::binary );
uassert( "couldn't open file" , out.good() );
2009-09-11 20:15:28 +02:00
ProgressMeter m( conn( true ).count( coll.c_str() ) );
auto_ptr<DBClientCursor> cursor = conn( true ).query( coll.c_str() , Query().snapshot() , 0 , 0 , 0 , Option_SlaveOk | Option_NoCursorTimeout );
2009-01-27 21:16:09 +01:00
while ( cursor->more() ) {
BSONObj obj = cursor->next();
out.write( obj.objdata() , obj.objsize() );
m.hit();
}
2009-09-11 20:15:28 +02:00
cout << "\t\t " << m.done() << " objects" << endl;
2009-09-11 20:15:28 +02:00
out.close();
2009-09-11 20:15:28 +02:00
}
2009-01-27 21:16:09 +01:00
void go( const string db , const path outdir ) {
cout << "DATABASE: " << db << "\t to \t" << outdir.string() << endl;
2009-08-12 22:31:22 +02:00
2009-01-27 21:16:09 +01:00
create_directories( outdir );
2009-09-11 20:15:28 +02:00
2009-01-27 21:16:09 +01:00
string sns = db + ".system.namespaces";
auto_ptr<DBClientCursor> cursor = conn( true ).query( sns.c_str() , Query() , 0 , 0 , 0 , Option_SlaveOk | Option_NoCursorTimeout );
2009-01-27 21:16:09 +01:00
while ( cursor->more() ) {
BSONObj obj = cursor->next();
if ( obj.toString().find( ".$" ) != string::npos )
continue;
2009-09-11 20:15:28 +02:00
2009-01-27 21:16:09 +01:00
const string name = obj.getField( "name" ).valuestr();
const string filename = name.substr( db.size() + 1 );
2009-09-11 20:15:28 +02:00
2009-06-29 05:26:34 +02:00
if ( _coll.length() > 0 && db + "." + _coll != name && _coll != name )
continue;
2009-09-11 20:15:28 +02:00
2009-01-27 21:16:09 +01:00
doCollection( name.c_str() , outdir / ( filename + ".bson" ) );
2009-09-11 20:15:28 +02:00
2009-01-27 21:16:09 +01:00
}
2009-09-11 20:15:28 +02:00
2009-01-27 21:16:09 +01:00
}
2009-01-14 23:17:24 +01:00
int run(){
2009-01-27 21:16:09 +01:00
path root( getParam("out") );
2009-01-27 21:16:09 +01:00
string db = _db;
2009-09-11 20:15:28 +02:00
2009-01-27 21:16:09 +01:00
if ( db == "*" ){
cout << "all dbs" << endl;
2009-08-12 22:31:22 +02:00
auth( "admin" );
2009-09-11 20:15:28 +02:00
BSONObj res = conn( true ).findOne( "admin.$cmd" , BSON( "listDatabases" << 1 ) );
2009-01-27 21:16:09 +01:00
BSONObj dbs = res.getField( "databases" ).embeddedObjectUserCheck();
set<string> keys;
dbs.getFieldNames( keys );
for ( set<string>::iterator i = keys.begin() ; i != keys.end() ; i++ ) {
string key = *i;
2009-09-11 20:15:28 +02:00
2009-01-27 21:16:09 +01:00
BSONObj dbobj = dbs.getField( key ).embeddedObjectUserCheck();
2009-09-11 20:15:28 +02:00
2009-01-27 21:16:09 +01:00
const char * dbName = dbobj.getField( "name" ).valuestr();
if ( (string)dbName == "local" )
continue;
2009-01-14 23:17:24 +01:00
2009-01-27 21:16:09 +01:00
go ( dbName , root / dbName );
}
}
2009-01-27 21:16:09 +01:00
else {
2009-08-12 22:31:22 +02:00
auth( db );
2009-01-27 21:16:09 +01:00
go( db , root / db );
2008-12-29 23:52:45 +01:00
}
return 0;
2008-12-29 23:52:45 +01:00
}
2009-01-27 21:16:09 +01:00
};
2008-12-29 23:52:45 +01:00
2009-01-27 21:16:09 +01:00
int main( int argc , char ** argv ) {
Dump d;
return d.main( argc , argv );
2008-12-29 23:52:45 +01:00
}