4
* Copyright (C) 2008 10gen Inc.
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Affero General Public License, version 3,
8
* as published by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Affero General Public License for more details.
15
* You should have received a copy of the GNU Affero General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#include "../stdafx.h"
20
#include "../client/dbclient.h"
21
#include "../util/mmap.h"
24
#include <boost/program_options.hpp>
28
using namespace mongo;
30
namespace po = boost::program_options;
32
class Restore : public Tool {
34
Restore() : Tool( "restore" , "" , "" ){
36
("dir", po::value<string>()->default_value("dump"), "directory to restore from")
38
addPositionArg("dir", 1);
41
virtual void printExtraHelp(ostream& out) {
42
out << "usage: " << _name << " [options] [directory or filename to restore from]" << endl;
47
path root = getParam("dir");
49
/* If _db is not "" then the user specified a db name to restore as.
51
* In that case we better be given either a root directory that
52
* contains only .bson files or a single .bson file (a db).
54
* In the case where a collection name is specified we better be
55
* given either a root directory that contains only a single
56
* .bson file, or a single .bson file itself (a collection).
58
drillDown(root, _db != "", _coll != "");
62
void drillDown( path root, bool use_db = false, bool use_coll = false ) {
63
log(2) << "drillDown: " << root.string() << endl;
65
if ( is_directory( root ) ) {
66
directory_iterator end;
67
directory_iterator i(root);
73
if (is_directory(p)) {
74
cerr << "ERROR: root directory must be a dump of a single database" << endl;
75
cerr << " when specifying a db name with --db" << endl;
82
if (is_directory(p) || i != end) {
83
cerr << "ERROR: root directory must be a dump of a single collection" << endl;
84
cerr << " when specifying a collection name with --collection" << endl;
90
drillDown(p, use_db, use_coll);
95
if ( ! ( endsWith( root.string().c_str() , ".bson" ) ||
96
endsWith( root.string().c_str() , ".bin" ) ) ) {
97
cerr << "don't know what to do with [" << root.string() << "]" << endl;
101
out() << root.string() << endl;
107
string dir = root.branch_path().string();
108
if ( dir.find( "/" ) == string::npos )
111
ns += dir.substr( dir.find_last_of( "/" ) + 1 );
117
string l = root.leaf();
118
l = l.substr( 0 , l.find_last_of( "." ) );
122
long long fileLength = file_size( root );
124
if ( fileLength == 0 ) {
125
out() << "file " + root.native_file_string() + " empty, skipping" << endl;
129
out() << "\t going into namespace [" << ns << "]" << endl;
131
string fileString = root.string();
132
ifstream file( fileString.c_str() , ios_base::in | ios_base::binary);
133
if ( ! file.is_open() ){
134
log() << "error opening file: " << fileString << endl;
138
log(1) << "\t file size: " << fileLength << endl;
143
const int BUF_SIZE = 1024 * 1024 * 5;
144
char * buf = (char*)malloc( BUF_SIZE );
146
ProgressMeter m( fileLength );
148
while ( read < fileLength ) {
149
file.read( buf , 4 );
150
int size = ((int*)buf)[0];
151
if ( size >= BUF_SIZE ){
152
cerr << "got an object of size: " << size << " terminating..." << endl;
154
uassert( 10264 , "invalid object size" , size < BUF_SIZE );
156
file.read( buf + 4 , size - 4 );
159
conn().insert( ns.c_str() , o );
164
m.hit( o.objsize() );
169
uassert( 10265 , "counts don't match" , m.done() == fileLength );
170
out() << "\t " << m.hits() << " objects" << endl;
174
int main( int argc , char ** argv ) {
176
return restore.main( argc , argv );