summaryrefslogtreecommitdiff
path: root/tools/dump.cpp
blob: 51ca1951e5105314bf0b2bad41593193e41f684b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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"

#include <boost/program_options.hpp>

#include <fcntl.h>

namespace po = boost::program_options;

namespace dump {
    
    void doCollection( DBClientConnection & conn , const char * coll , path outputFile ){
        cout << "\t" << coll << " to " << outputFile.string() << endl;
        
        int out = open( outputFile.string().c_str() , O_WRONLY | O_CREAT | O_TRUNC , 0666 );
        assert( out );
        
        BSONObjBuilder query;
        auto_ptr<DBClientCursor> cursor = conn.query( coll , query.doneAndDecouple() );
        
        int num = 0;
        while ( cursor->more() ){    
            BSONObj obj = cursor->next();
            write( out , obj.objdata() , obj.objsize() );
            num++;
        }
        
        cout << "\t\t " << num << " objects" << endl;

        close( out );
    }
    
    void go( DBClientConnection & conn , const char * db , const path outdir ){
        cout << "DATABASE: " << db << endl;
        
        create_directories( outdir );

        string sns = db;
        sns += ".system.namespaces";
        
        BSONObjBuilder query;
        auto_ptr<DBClientCursor> cursor = conn.query( sns.c_str() , query.doneAndDecouple() );
        while ( cursor->more() ){
            BSONObj obj = cursor->next();
            if ( obj.toString().find( ".$" ) != string::npos )
                continue;
            
            const string name = obj.getField( "name" ).valuestr();
            const string filename = name.substr( strlen( db ) + 1 );
            
            doCollection( conn , name.c_str() , outdir / ( filename + ".bson" ) );

        }
        
    }

    void go( const char * host , const char * db , const char * outdir ){
        DBClientConnection conn;
        string errmsg;
        if ( ! conn.connect( host , errmsg ) ){
            cout << "couldn't connect : " << errmsg << endl;
            throw -11;
        }
        
        path root(outdir);
        
        if ( strlen( db ) == 1 && db[0] == '*' ){            
            cout << "all dbs" << endl;

            BSONObjBuilder query;
            query.appendBool( "listDatabases" , 1 );
    
            BSONObj res = conn.findOne( "admin.$cmd" , query.doneAndDecouple() );
            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;
                
                BSONObj db = dbs.getField( key ).embeddedObjectUserCheck();
                
                const char * dbName = db.getField( "name" ).valuestr();
                if ( (string)dbName == "local" )
                    continue;
                go ( conn , dbName , root / dbName );
            }
        }
        else {
            go( conn , db , root / db );
        }
    }

}


int main( int argc , char ** argv ){

    boost::filesystem::path::default_name_check( boost::filesystem::no_check );

    po::options_description options("dump parameters");
    options.add_options()
        ("help", "produce help message")
        ("host,h", po::value<string>() , "mongo host to connecto to")
        ("db,d" , po::value<string>() , "database to dump" )
        ("out" , po::value<string>() , "output directory" )
        ;
    
    po::variables_map vm;       
    po::store(po::parse_command_line(argc, argv, options), vm);
    po::notify(vm);   
    
    if ( vm.count("help") ){
        options.print( cerr );
        return 1;
    }

    const char * host = "127.0.0.1";
    const char * db = "*";
    const char * outdir = "dump";

    if ( vm.count( "host" ) )
        host = vm["host"].as<string>().c_str();
    
    if ( vm.count( "db" ) )
        db = vm["db"].as<string>().c_str();
    
    if ( vm.count( "out" ) )
        outdir = vm["db"].as<string>().c_str();
    
    cout << "mongo dump" << endl;
    cout << "\t host        \t" << host << endl;
    cout << "\t db          \t" << db << endl;
    cout << "\t output dir  \t" << outdir << endl;

    dump::go( host , db , outdir );
}