~evarlast/ubuntu/utopic/mongodb/upstart-workaround-debian-bug-718702

« back to all changes in this revision

Viewing changes to src/mongo/tools/restore.cpp

  • Committer: Package Import Robot
  • Author(s): James Page, James Page, Robie Basak
  • Date: 2013-05-29 17:44:42 UTC
  • mfrom: (44.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130529174442-z0a4qmoww4y0t458
Tags: 1:2.4.3-1ubuntu1
[ James Page ]
* Merge from Debian unstable, remaining changes:
  - Enable SSL support:
    + d/control: Add libssl-dev to BD's.
    + d/rules: Enabled --ssl option.
    + d/mongodb.conf: Add example SSL configuration options.
  - d/mongodb-server.mongodb.upstart: Add upstart configuration.
  - d/rules: Don't strip binaries during scons build for Ubuntu.
  - d/control: Add armhf to target archs.
  - d/p/SConscript.client.patch: fixup install of client libraries.
  - d/p/0010-install-libs-to-usr-lib-not-usr-lib64-Closes-588557.patch:
    Install libraries to lib not lib64.
* Dropped changes:
  - d/p/arm-support.patch: Included in Debian.
  - d/p/double-alignment.patch: Included in Debian.
  - d/rules,control: Debian also builds with avaliable system libraries
    now.
* Fix FTBFS due to gcc and boost upgrades in saucy:
  - d/p/0008-ignore-unused-local-typedefs.patch: Add -Wno-unused-typedefs
    to unbreak building with g++-4.8.
  - d/p/0009-boost-1.53.patch: Fixup signed/unsigned casting issue.

[ Robie Basak ]
* d/p/0011-Use-a-signed-char-to-store-BSONType-enumerations.patch: Fixup
  build failure on ARM due to missing signed'ness of char cast.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include <fstream>
28
28
#include <set>
29
29
 
 
30
#include "mongo/base/initializer.h"
30
31
#include "mongo/db/namespacestring.h"
31
32
#include "mongo/tools/tool.h"
32
33
#include "mongo/util/mmap.h"
33
 
#include "mongo/util/version.h"
 
34
#include "mongo/util/stringutils.h"
34
35
#include "mongo/db/json.h"
35
36
#include "mongo/client/dbclientcursor.h"
36
37
 
59
60
    int _oplogEntrySkips; // oplog entries skipped
60
61
    int _oplogEntryApplies; // oplog entries applied
61
62
    Restore() : BSONTool( "restore" ) , _drop(false) {
 
63
        // Default values set here will show up in help text, but will supercede any default value
 
64
        // used when calling getParam below.
62
65
        add_options()
63
66
        ("drop" , "drop each collection before import" )
64
67
        ("oplogReplay", "replay oplog for point-in-time restore")
67
70
        ("keepIndexVersion" , "don't upgrade indexes to newest version")
68
71
        ("noOptionsRestore" , "don't restore collection options")
69
72
        ("noIndexRestore" , "don't restore indexes")
70
 
        ("w" , po::value<int>()->default_value(1) , "minimum number of replicas per write" )
 
73
        ("w" , po::value<int>()->default_value(0) , "minimum number of replicas per write" )
71
74
        ;
72
75
        add_hidden_options()
73
76
        ("dir", po::value<string>()->default_value("dump"), "directory to restore from")
83
86
 
84
87
    virtual int doRun() {
85
88
 
86
 
        // authenticate
87
 
        enum Auth::Level authLevel = Auth::NONE;
88
 
        auth("", &authLevel);
89
 
        uassert(15935, "user does not have write access", authLevel == Auth::WRITE);
90
 
 
91
89
        boost::filesystem::path root = getParam("dir");
92
90
 
93
91
        // check if we're actually talking to a machine that can write
104
102
        _keepIndexVersion = hasParam("keepIndexVersion");
105
103
        _restoreOptions = !hasParam("noOptionsRestore");
106
104
        _restoreIndexes = !hasParam("noIndexRestore");
107
 
        _w = getParam( "w" , 1 );
 
105
        // Make sure default value set here stays in sync with the one set in the constructor above.
 
106
        _w = getParam( "w" , 0 );
108
107
 
109
108
        bool doOplog = hasParam( "oplogReplay" );
110
109
 
178
177
 
179
178
                    BSONObjBuilder tsRestrictBldr;
180
179
                    if (!tsOptime.isNull())
181
 
                        tsRestrictBldr.appendTimestamp("$gt", tsOptime.asDate());
182
 
                    tsRestrictBldr.appendTimestamp("$lt", _oplogLimitTS->asDate());
 
180
                        tsRestrictBldr << "$gt" << tsOptime;
 
181
                    tsRestrictBldr << "$lt" << *_oplogLimitTS.get();
183
182
 
184
183
                    BSONObj query = BSON("ts" << tsRestrictBldr.obj());
185
184
 
206
205
        drillDown(root, _db != "", _coll != "", !(_oplogLimitTS.get() == NULL), true);
207
206
 
208
207
        // should this happen for oplog replay as well?
209
 
        conn().getLastError(_db == "" ? "admin" : _db);
 
208
        string err = conn().getLastError(_db == "" ? "admin" : _db);
 
209
        if (!err.empty()) {
 
210
            error() << err;
 
211
        }
210
212
 
211
213
        if (doOplog) {
212
214
            log() << "\t Replaying oplog" << endl;
229
231
        LOG(2) << "drillDown: " << root.string() << endl;
230
232
 
231
233
        // skip hidden files and directories
232
 
        if (root.leaf()[0] == '.' && root.leaf() != ".")
 
234
        if (root.leaf().string()[0] == '.' && root.leaf().string() != ".")
233
235
            return;
234
236
 
235
237
        if ( is_directory( root ) ) {
258
260
                    }
259
261
                }
260
262
 
 
263
                // Ignore system.indexes.bson if we have *.metadata.json files
 
264
                if ( endsWith( p.string().c_str() , ".metadata.json" ) ) {
 
265
                    json_metadata = true;
 
266
                }
 
267
 
261
268
                // don't insert oplog
262
269
                if (top_level && !use_db && p.leaf() == "oplog.bson")
263
270
                    continue;
299
306
            ns += _db;
300
307
        }
301
308
        else {
302
 
            string dir = root.branch_path().string();
303
 
            if ( dir.find( "/" ) == string::npos )
304
 
                ns += dir;
305
 
            else
306
 
                ns += dir.substr( dir.find_last_of( "/" ) + 1 );
307
 
 
308
 
            if ( ns.size() == 0 )
 
309
            ns = root.parent_path().filename().string();
 
310
            if (ns.empty())
309
311
                ns = "test";
310
312
        }
311
313
 
312
314
        verify( ns.size() );
313
315
 
314
 
        string oldCollName = root.leaf(); // Name of the collection that was dumped from
 
316
        string oldCollName = root.leaf().string(); // Name of the collection that was dumped from
315
317
        oldCollName = oldCollName.substr( 0 , oldCollName.find_last_of( "." ) );
316
318
        if (use_coll) {
317
319
            ns += "." + _coll;
350
352
            if (!boost::filesystem::exists(metadataFile.string())) {
351
353
                // This is fine because dumps from before 2.1 won't have a metadata file, just print a warning.
352
354
                // System collections shouldn't have metadata so don't warn if that file is missing.
353
 
                if (!startsWith(metadataFile.leaf(), "system.")) {
 
355
                if (!startsWith(metadataFile.leaf().string(), "system.")) {
354
356
                    log() << metadataFile.string() << " not found. Skipping." << endl;
355
357
                }
356
358
            } else {
417
419
            _oplogEntryApplies++;
418
420
 
419
421
            // wait for ops to propagate to "w" nodes (doesn't warn if w used without replset)
420
 
            if ( _w > 1 ) {
421
 
                conn().getLastError(db, false, false, _w);
 
422
            if ( _w > 0 ) {
 
423
                string err = conn().getLastError(db, false, false, _w);
 
424
                if (!err.empty()) {
 
425
                    error() << "Error while replaying oplog: " << err;
 
426
                }
422
427
            }
423
428
        }
424
 
        else if ( endsWith( _curns.c_str() , ".system.indexes" )) {
 
429
        else if (NamespaceString(_curns).coll == "system.indexes") {
425
430
            createIndex(obj, true);
426
431
        }
427
432
        else if (_drop && endsWith(_curns.c_str(), ".system.users") && _users.count(obj["user"].String())) {
434
439
            conn().insert( _curns , obj );
435
440
 
436
441
            // wait for insert to propagate to "w" nodes (doesn't warn if w used without replset)
437
 
            if ( _w > 1 ) {
438
 
                conn().getLastErrorDetailed(_curdb, false, false, _w);
 
442
            if ( _w > 0 ) {
 
443
                string err = conn().getLastError(_curdb, false, false, _w);
 
444
                if (!err.empty()) {
 
445
                    error() << err;
 
446
                }
439
447
            }
440
448
        }
441
449
    }
451
459
        int objSize;
452
460
        BSONObj obj;
453
461
        obj = fromjson (buf.get(), &objSize);
454
 
        uassert(15934, "JSON object size didn't match file size", objSize == fileSize);
455
462
        return obj;
456
463
    }
457
464
 
568
575
    }
569
576
};
570
577
 
571
 
int main( int argc , char ** argv ) {
 
578
int main( int argc , char ** argv, char ** envp ) {
 
579
    mongo::runGlobalInitializersOrDie(argc, argv, envp);
572
580
    Restore restore;
573
581
    return restore.main( argc , argv );
574
582
}