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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
// restore.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 "../util/mmap.h"
#include "tool.h"
#include <boost/program_options.hpp>
#include <fcntl.h>
using namespace mongo;
namespace po = boost::program_options;
class Restore : public Tool {
public:
bool _drop;
bool _objcheck;
Restore() : Tool( "restore" , true , "" , "" ) , _drop(false),_objcheck(false){
add_options()
("drop" , "drop each collection before import" )
("objcheck" , "validate object before inserting" )
;
add_hidden_options()
("dir", po::value<string>()->default_value("dump"), "directory to restore from")
;
addPositionArg("dir", 1);
}
virtual void printExtraHelp(ostream& out) {
out << "usage: " << _name << " [options] [directory or filename to restore from]" << endl;
}
int run(){
auth();
path root = getParam("dir");
_drop = hasParam( "drop" );
_objcheck = hasParam( "objcheck" );
/* If _db is not "" then the user specified a db name to restore as.
*
* In that case we better be given either a root directory that
* contains only .bson files or a single .bson file (a db).
*
* 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).
*/
drillDown(root, _db != "", _coll != "");
conn().getLastError();
return EXIT_CLEAN;
}
void drillDown( path root, bool use_db = false, bool use_coll = false ) {
log(2) << "drillDown: " << root.string() << endl;
if ( is_directory( root ) ) {
directory_iterator end;
directory_iterator i(root);
while ( i != end ) {
path p = *i;
i++;
if (use_db) {
if (is_directory(p)) {
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;
}
}
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;
}
}
drillDown(p, use_db, use_coll);
}
return;
}
if ( ! ( endsWith( root.string().c_str() , ".bson" ) ||
endsWith( root.string().c_str() , ".bin" ) ) ) {
cerr << "don't know what to do with [" << root.string() << "]" << endl;
return;
}
out() << root.string() << endl;
string ns;
if (use_db) {
ns += _db;
} else {
string dir = root.branch_path().string();
if ( dir.find( "/" ) == string::npos )
ns += dir;
else
ns += dir.substr( dir.find_last_of( "/" ) + 1 );
}
if (use_coll) {
ns += "." + _coll;
} else {
string l = root.leaf();
l = l.substr( 0 , l.find_last_of( "." ) );
ns += "." + l;
}
long long fileLength = file_size( root );
if ( fileLength == 0 ) {
out() << "file " + root.native_file_string() + " empty, skipping" << endl;
return;
}
out() << "\t going into namespace [" << ns << "]" << endl;
if ( _drop ){
out() << "\t dropping" << endl;
conn().dropCollection( ns );
}
string fileString = root.string();
ifstream file( fileString.c_str() , ios_base::in | ios_base::binary);
if ( ! file.is_open() ){
log() << "error opening file: " << fileString << endl;
return;
}
log(1) << "\t file size: " << fileLength << endl;
long long read = 0;
long long num = 0;
const int BUF_SIZE = 1024 * 1024 * 5;
boost::scoped_array<char> buf_holder(new char[BUF_SIZE]);
char * buf = buf_holder.get();
ProgressMeter m( fileLength );
while ( read < fileLength ) {
file.read( buf , 4 );
int size = ((int*)buf)[0];
if ( size >= BUF_SIZE ){
cerr << "got an object of size: " << size << " terminating..." << endl;
}
uassert( 10264 , "invalid object size" , size < BUF_SIZE );
file.read( buf + 4 , size - 4 );
BSONObj o( buf );
if ( _objcheck && ! o.valid() ){
cerr << "INVALID OBJECT - going try and pring out " << endl;
cerr << "size: " << size << endl;
BSONObjIterator i(o);
while ( i.more() ){
BSONElement e = i.next();
try {
e.validate();
}
catch ( ... ){
cerr << "\t\t NEXT ONE IS INVALID" << endl;
}
cerr << "\t name : " << e.fieldName() << " " << e.type() << endl;
cerr << "\t " << e << endl;
}
}
conn().insert( ns.c_str() , o );
read += o.objsize();
num++;
m.hit( o.objsize() );
}
uassert( 10265 , "counts don't match" , m.done() == fileLength );
out() << "\t " << m.hits() << " objects" << endl;
}
};
int main( int argc , char ** argv ) {
Restore restore;
return restore.main( argc , argv );
}
|