2011-06-16 22:38:05 -04:00
|
|
|
// @file restore.cpp
|
2009-01-11 23:02:19 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-04-27 15:27:52 -04:00
|
|
|
#include "../pch.h"
|
2009-01-11 23:02:19 -05:00
|
|
|
#include "../client/dbclient.h"
|
|
|
|
|
#include "../util/mmap.h"
|
2010-11-29 14:32:10 -05:00
|
|
|
#include "../util/version.h"
|
2009-09-24 11:02:00 -04:00
|
|
|
#include "tool.h"
|
2009-01-11 23:02:19 -05:00
|
|
|
|
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
2011-07-28 18:15:28 -04:00
|
|
|
#include <set>
|
2009-01-11 23:02:19 -05:00
|
|
|
|
2009-01-27 21:37:56 -05:00
|
|
|
using namespace mongo;
|
2009-01-14 17:17:24 -05:00
|
|
|
|
2009-01-27 21:37:56 -05:00
|
|
|
namespace po = boost::program_options;
|
2009-01-14 17:17:24 -05:00
|
|
|
|
2010-11-29 14:32:10 -05:00
|
|
|
namespace {
|
|
|
|
|
const char* OPLOG_SENTINEL = "$oplog"; // compare by ptr not strcmp
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-09 11:31:30 -04:00
|
|
|
class Restore : public BSONTool {
|
2009-01-27 21:37:56 -05:00
|
|
|
public:
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2010-03-05 13:07:29 -05:00
|
|
|
bool _drop;
|
2011-08-29 15:59:39 -04:00
|
|
|
bool _keepIndexVersion;
|
2010-12-09 22:59:28 -05:00
|
|
|
string _curns;
|
|
|
|
|
string _curdb;
|
2011-07-28 18:15:28 -04:00
|
|
|
set<string> _users; // For restoring users with --drop
|
2010-06-09 11:31:30 -04:00
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
Restore() : BSONTool( "restore" ) , _drop(false) {
|
2010-03-05 13:35:15 -05:00
|
|
|
add_options()
|
2011-01-04 00:40:41 -05:00
|
|
|
("drop" , "drop each collection before import" )
|
|
|
|
|
("oplogReplay" , "replay oplog for point-in-time restore")
|
2011-08-29 15:59:39 -04:00
|
|
|
("keepIndexVersion" , "don't upgrade indexes to newest version")
|
2011-01-04 00:40:41 -05:00
|
|
|
;
|
2010-03-05 13:35:15 -05:00
|
|
|
add_hidden_options()
|
2011-01-04 00:40:41 -05:00
|
|
|
("dir", po::value<string>()->default_value("dump"), "directory to restore from")
|
|
|
|
|
("indexesLast" , "wait to add indexes (now default)") // left in for backwards compatibility
|
|
|
|
|
;
|
2009-09-24 12:04:51 -04:00
|
|
|
addPositionArg("dir", 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void printExtraHelp(ostream& out) {
|
|
|
|
|
out << "usage: " << _name << " [options] [directory or filename to restore from]" << endl;
|
2009-01-27 21:37:56 -05:00
|
|
|
}
|
2009-09-11 14:15:28 -04:00
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
virtual int doRun() {
|
2009-08-12 16:31:22 -04:00
|
|
|
auth();
|
2009-09-15 13:26:08 -04:00
|
|
|
path root = getParam("dir");
|
2010-10-08 11:23:08 -04:00
|
|
|
|
|
|
|
|
// check if we're actually talking to a machine that can write
|
|
|
|
|
if (!isMaster()) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2010-03-05 13:07:29 -05:00
|
|
|
_drop = hasParam( "drop" );
|
2011-08-29 15:59:39 -04:00
|
|
|
_keepIndexVersion = hasParam("keepIndexVersion");
|
2009-09-15 17:02:12 -04:00
|
|
|
|
2010-11-29 14:32:10 -05:00
|
|
|
bool doOplog = hasParam( "oplogReplay" );
|
2011-01-04 00:40:41 -05:00
|
|
|
if (doOplog) {
|
2010-11-29 14:32:10 -05:00
|
|
|
// fail early if errors
|
|
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
if (_db != "") {
|
2010-11-29 14:32:10 -05:00
|
|
|
cout << "Can only replay oplog on full restore" << endl;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
if ( ! exists(root / "oplog.bson") ) {
|
2010-11-29 14:32:10 -05:00
|
|
|
cout << "No oplog file to replay. Make sure you run mongodump with --oplog." << endl;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BSONObj out;
|
2011-01-04 00:40:41 -05:00
|
|
|
if (! conn().simpleCommand("admin", &out, "buildinfo")) {
|
2010-11-29 14:32:10 -05:00
|
|
|
cout << "buildinfo command failed: " << out["errmsg"].String() << endl;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringData version = out["version"].valuestr();
|
2011-01-04 00:40:41 -05:00
|
|
|
if (versionCmp(version, "1.7.4-pre-") < 0) {
|
2010-11-29 14:32:10 -05:00
|
|
|
cout << "Can only replay oplog to server version >= 1.7.4" << endl;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-15 17:02:12 -04:00
|
|
|
/* If _db is not "" then the user specified a db name to restore as.
|
|
|
|
|
*
|
2009-09-24 11:54:42 -04:00
|
|
|
* In that case we better be given either a root directory that
|
|
|
|
|
* contains only .bson files or a single .bson file (a db).
|
2009-09-29 17:10:19 -04:00
|
|
|
*
|
|
|
|
|
* In the case where a collection name is specified we better be
|
|
|
|
|
* given either a root directory that contains only a single
|
|
|
|
|
* .bson file, or a single .bson file itself (a collection).
|
2009-09-15 17:02:12 -04:00
|
|
|
*/
|
2010-11-29 14:32:10 -05:00
|
|
|
drillDown(root, _db != "", _coll != "", true);
|
2010-02-09 17:50:38 -05:00
|
|
|
conn().getLastError();
|
2010-11-29 14:32:10 -05:00
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
if (doOplog) {
|
2010-11-29 14:32:10 -05:00
|
|
|
out() << "\t Replaying oplog" << endl;
|
|
|
|
|
_curns = OPLOG_SENTINEL;
|
|
|
|
|
processFile( root / "oplog.bson" );
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-15 17:02:12 -04:00
|
|
|
return EXIT_CLEAN;
|
2009-01-27 21:37:56 -05:00
|
|
|
}
|
2009-09-11 14:15:28 -04:00
|
|
|
|
2010-11-29 14:32:10 -05:00
|
|
|
void drillDown( path root, bool use_db, bool use_coll, bool top_level=false ) {
|
2009-09-15 13:48:26 -04:00
|
|
|
log(2) << "drillDown: " << root.string() << endl;
|
2009-01-27 21:37:56 -05:00
|
|
|
|
2010-11-05 16:05:47 -04:00
|
|
|
// skip hidden files and directories
|
2010-11-05 19:50:42 -04:00
|
|
|
if (root.leaf()[0] == '.' && root.leaf() != ".")
|
2010-11-05 16:05:47 -04:00
|
|
|
return;
|
|
|
|
|
|
2009-01-27 21:37:56 -05:00
|
|
|
if ( is_directory( root ) ) {
|
|
|
|
|
directory_iterator end;
|
|
|
|
|
directory_iterator i(root);
|
2010-07-07 20:17:48 -04:00
|
|
|
path indexes;
|
2009-01-27 21:37:56 -05:00
|
|
|
while ( i != end ) {
|
|
|
|
|
path p = *i;
|
2009-09-29 17:10:19 -04:00
|
|
|
i++;
|
2009-09-15 17:02:12 -04:00
|
|
|
|
|
|
|
|
if (use_db) {
|
2009-09-29 17:10:19 -04:00
|
|
|
if (is_directory(p)) {
|
2009-09-15 17:02:12 -04:00
|
|
|
cerr << "ERROR: root directory must be a dump of a single database" << endl;
|
|
|
|
|
cerr << " when specifying a db name with --db" << endl;
|
|
|
|
|
printHelp(cout);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-29 17:10:19 -04:00
|
|
|
if (use_coll) {
|
|
|
|
|
if (is_directory(p) || i != end) {
|
|
|
|
|
cerr << "ERROR: root directory must be a dump of a single collection" << endl;
|
|
|
|
|
cerr << " when specifying a collection name with --collection" << endl;
|
|
|
|
|
printHelp(cout);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-29 14:32:10 -05:00
|
|
|
// don't insert oplog
|
|
|
|
|
if (top_level && !use_db && p.leaf() == "oplog.bson")
|
|
|
|
|
continue;
|
|
|
|
|
|
2010-11-05 15:14:05 -04:00
|
|
|
if ( p.leaf() == "system.indexes.bson" )
|
2010-07-07 20:17:48 -04:00
|
|
|
indexes = p;
|
|
|
|
|
else
|
|
|
|
|
drillDown(p, use_db, use_coll);
|
2009-01-15 10:17:11 -05:00
|
|
|
}
|
2010-07-07 20:17:48 -04:00
|
|
|
|
|
|
|
|
if (!indexes.empty())
|
|
|
|
|
drillDown(indexes, use_db, use_coll);
|
|
|
|
|
|
2009-01-27 21:37:56 -05:00
|
|
|
return;
|
2009-01-15 10:17:11 -05:00
|
|
|
}
|
2009-09-11 14:15:28 -04:00
|
|
|
|
2009-01-27 21:37:56 -05:00
|
|
|
if ( ! ( endsWith( root.string().c_str() , ".bson" ) ||
|
|
|
|
|
endsWith( root.string().c_str() , ".bin" ) ) ) {
|
2011-06-16 22:38:05 -04:00
|
|
|
cerr << "don't know what to do with file [" << root.string() << "]" << endl;
|
2009-01-27 21:37:56 -05:00
|
|
|
return;
|
|
|
|
|
}
|
2009-09-11 14:15:28 -04:00
|
|
|
|
2010-06-05 22:37:59 -04:00
|
|
|
log() << root.string() << endl;
|
2009-09-11 14:15:28 -04:00
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
if ( root.leaf() == "system.profile.bson" ) {
|
2010-06-30 14:29:19 -04:00
|
|
|
log() << "\t skipping" << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-27 21:37:56 -05:00
|
|
|
string ns;
|
2009-09-15 17:02:12 -04:00
|
|
|
if (use_db) {
|
|
|
|
|
ns += _db;
|
2011-01-04 00:40:41 -05:00
|
|
|
}
|
2010-06-19 12:46:10 -04:00
|
|
|
else {
|
2009-01-27 21:37:56 -05:00
|
|
|
string dir = root.branch_path().string();
|
|
|
|
|
if ( dir.find( "/" ) == string::npos )
|
|
|
|
|
ns += dir;
|
|
|
|
|
else
|
|
|
|
|
ns += dir.substr( dir.find_last_of( "/" ) + 1 );
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2010-06-19 12:46:10 -04:00
|
|
|
if ( ns.size() == 0 )
|
|
|
|
|
ns = "test";
|
2009-01-27 21:37:56 -05:00
|
|
|
}
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2010-06-19 12:46:10 -04:00
|
|
|
assert( ns.size() );
|
2009-09-11 14:15:28 -04:00
|
|
|
|
2009-09-29 17:10:19 -04:00
|
|
|
if (use_coll) {
|
|
|
|
|
ns += "." + _coll;
|
2011-01-04 00:40:41 -05:00
|
|
|
}
|
|
|
|
|
else {
|
2009-01-27 21:37:56 -05:00
|
|
|
string l = root.leaf();
|
|
|
|
|
l = l.substr( 0 , l.find_last_of( "." ) );
|
|
|
|
|
ns += "." + l;
|
|
|
|
|
}
|
2009-09-11 14:15:28 -04:00
|
|
|
|
2009-01-27 21:37:56 -05:00
|
|
|
out() << "\t going into namespace [" << ns << "]" << endl;
|
2009-09-15 17:02:12 -04:00
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
if ( _drop ) {
|
2011-07-28 18:15:28 -04:00
|
|
|
if (root.leaf() != "system.users.bson" ) {
|
|
|
|
|
out() << "\t dropping" << endl;
|
|
|
|
|
conn().dropCollection( ns );
|
|
|
|
|
} else {
|
|
|
|
|
// Create map of the users currently in the DB
|
|
|
|
|
BSONObj fields = BSON("user" << 1);
|
|
|
|
|
scoped_ptr<DBClientCursor> cursor(conn().query(ns, Query(), 0, 0, &fields));
|
|
|
|
|
while (cursor->more()) {
|
|
|
|
|
BSONObj user = cursor->next();
|
|
|
|
|
_users.insert(user["user"].String());
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-03-05 13:07:29 -05:00
|
|
|
}
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2010-06-09 11:31:30 -04:00
|
|
|
_curns = ns.c_str();
|
2010-12-09 22:59:28 -05:00
|
|
|
_curdb = NamespaceString(_curns).db;
|
2010-06-09 11:31:30 -04:00
|
|
|
processFile( root );
|
2011-07-28 18:15:28 -04:00
|
|
|
if (_drop && root.leaf() == "system.users.bson") {
|
|
|
|
|
// Delete any users that used to exist but weren't in the dump file
|
|
|
|
|
for (set<string>::iterator it = _users.begin(); it != _users.end(); ++it) {
|
|
|
|
|
BSONObj userMatch = BSON("user" << *it);
|
|
|
|
|
conn().remove(ns, Query(userMatch));
|
|
|
|
|
}
|
|
|
|
|
_users.clear();
|
|
|
|
|
}
|
2010-06-09 11:31:30 -04:00
|
|
|
}
|
2010-03-05 13:07:29 -05:00
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
virtual void gotObject( const BSONObj& obj ) {
|
2010-11-29 14:32:10 -05:00
|
|
|
if (_curns == OPLOG_SENTINEL) { // intentional ptr compare
|
|
|
|
|
if (obj["op"].valuestr()[0] == 'n') // skip no-ops
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
string db = obj["ns"].valuestr();
|
|
|
|
|
db = db.substr(0, db.find('.'));
|
|
|
|
|
|
|
|
|
|
BSONObj cmd = BSON( "applyOps" << BSON_ARRAY( obj ) );
|
|
|
|
|
BSONObj out;
|
|
|
|
|
conn().runCommand(db, cmd, out);
|
2011-01-04 00:40:41 -05:00
|
|
|
}
|
2010-12-09 22:59:28 -05:00
|
|
|
else if ( endsWith( _curns.c_str() , ".system.indexes" )) {
|
2010-12-09 15:16:29 -05:00
|
|
|
/* Index construction is slightly special: when restoring
|
|
|
|
|
indexes, we must ensure that the ns attribute is
|
|
|
|
|
<dbname>.<indexname>, where <dbname> might be different
|
|
|
|
|
at restore time than what was dumped. Also, we're
|
|
|
|
|
stricter about errors for indexes than for regular
|
|
|
|
|
data. */
|
|
|
|
|
BSONObjBuilder bo;
|
|
|
|
|
BSONObjIterator i(obj);
|
2011-01-04 00:40:41 -05:00
|
|
|
while ( i.more() ) {
|
2010-12-09 15:16:29 -05:00
|
|
|
BSONElement e = i.next();
|
|
|
|
|
if (strcmp(e.fieldName(), "ns") == 0) {
|
|
|
|
|
NamespaceString n(e.String());
|
2010-12-09 22:59:28 -05:00
|
|
|
string s = _curdb + "." + n.coll;
|
2010-12-09 15:16:29 -05:00
|
|
|
bo.append("ns", s);
|
2011-01-04 00:40:41 -05:00
|
|
|
}
|
2011-08-29 15:59:39 -04:00
|
|
|
else if (strcmp(e.fieldName(), "v") != 0 || _keepIndexVersion) { // Remove index version number
|
2010-12-09 15:16:29 -05:00
|
|
|
bo.append(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
BSONObj o = bo.obj();
|
2010-12-09 22:59:28 -05:00
|
|
|
log(0) << o << endl;
|
2010-12-09 15:16:29 -05:00
|
|
|
conn().insert( _curns , o );
|
|
|
|
|
BSONObj err = conn().getLastErrorDetailed();
|
|
|
|
|
if ( ! ( err["err"].isNull() ) ) {
|
|
|
|
|
cerr << "Error creating index " << o["ns"].String();
|
|
|
|
|
cerr << ": " << err["code"].Int() << " " << err["err"].String() << endl;
|
|
|
|
|
cerr << "To resume index restoration, run " << _name << " on file" << _fileName << " manually." << endl;
|
2011-04-18 18:44:38 -04:00
|
|
|
::abort();
|
2010-12-09 15:16:29 -05:00
|
|
|
}
|
2011-01-04 00:40:41 -05:00
|
|
|
}
|
2011-07-28 18:15:28 -04:00
|
|
|
else if (_drop && endsWith(_curns.c_str(), ".system.users") && _users.count(obj["user"].String())) {
|
|
|
|
|
// Since system collections can't be dropped, we have to manually
|
|
|
|
|
// replace the contents of the system.users collection
|
|
|
|
|
BSONObj userMatch = BSON("user" << obj["user"].String());
|
|
|
|
|
conn().update(_curns, Query(userMatch), obj);
|
|
|
|
|
_users.erase(obj["user"].String());
|
|
|
|
|
} else {
|
2010-11-29 14:32:10 -05:00
|
|
|
conn().insert( _curns , obj );
|
|
|
|
|
}
|
2009-01-11 23:02:19 -05:00
|
|
|
}
|
2010-06-09 11:31:30 -04:00
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2009-01-27 21:37:56 -05:00
|
|
|
};
|
2009-01-11 23:02:19 -05:00
|
|
|
|
2009-01-27 21:37:56 -05:00
|
|
|
int main( int argc , char ** argv ) {
|
2009-02-09 11:24:21 -05:00
|
|
|
Restore restore;
|
|
|
|
|
return restore.main( argc , argv );
|
2009-01-11 23:02:19 -05:00
|
|
|
}
|