-
Notifications
You must be signed in to change notification settings - Fork 2
/
backup_plugin.cpp
180 lines (159 loc) · 7.38 KB
/
backup_plugin.cpp
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
// @file backup_plugin.cpp
/*======
This file is part of Percona Server for MongoDB.
Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
Percona Server for MongoDB 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.
Percona Server for MongoDB 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 Percona Server for MongoDB. If not, see
<http://www.gnu.org/licenses/>.
======= */
#include "mongo/pch.h"
#include <string>
#include <backup.h>
#include "manager.h"
#include "mongo/base/status.h"
#include "mongo/base/units.h"
#include "mongo/db/auth/action_set.h"
#include "mongo/db/auth/action_type.h"
#include "mongo/db/auth/authorization_manager.h"
#include "mongo/db/auth/privilege.h"
#include "mongo/db/client.h"
#include "mongo/db/commands.h"
#include "mongo/db/jsobj.h"
#include "mongo/plugins/command_loader.h"
namespace mongo {
namespace backup {
class BackupCommand : public Command {
public:
BackupCommand(const char *name) : Command(name) {}
virtual LockType locktype() const { return NONE; }
virtual bool requiresSync() const { return false; }
virtual bool lockGlobally() const { return false; }
virtual bool needsTxn() const { return false; }
virtual bool canRunInMultiStmtTxn() const { return true; }
virtual int txnFlags() const { return noTxnFlags(); }
virtual bool adminOnly() const { return true; }
virtual bool slaveOk() const { return true; }
};
class BackupStartCommand : public BackupCommand {
public:
BackupStartCommand() : BackupCommand("backupStart") {}
virtual void addRequiredPrivileges(const std::string& dbname,
const BSONObj& cmdObj,
std::vector<Privilege>* out) {
ActionSet actions;
actions.addAction(ActionType::backupStart);
out->push_back(Privilege(AuthorizationManager::SERVER_RESOURCE_NAME, actions));
}
virtual void help(stringstream &h) const {
h << "Starts a hot backup." << endl
<< "{ backupStart: <destination directory> }";
}
virtual bool run(const string &db, BSONObj &cmdObj, int options, string &errmsg, BSONObjBuilder &result, bool fromRepl) {
BSONElement e = cmdObj.firstElement();
string dest = e.str();
if (dest.empty()) {
errmsg = "invalid destination directory: '" + dest + "'";
return false;
}
Manager manager(cc());
return manager.start(dest, errmsg, result);
}
};
class BackupThrottleCommand : public BackupCommand {
public:
BackupThrottleCommand() : BackupCommand("backupThrottle") {}
virtual void addRequiredPrivileges(const std::string& dbname,
const BSONObj& cmdObj,
std::vector<Privilege>* out) {
ActionSet actions;
actions.addAction(ActionType::backupThrottle);
out->push_back(Privilege(AuthorizationManager::SERVER_RESOURCE_NAME, actions));
}
virtual void help(stringstream &h) const {
h << "Throttles hot backup to consume only N bytes/sec of I/O." << endl
<< "{ backupThrottle: <N> }" << endl
<< "N can be an integer or a string with a \"k/m/g\" suffix";
}
virtual bool run(const string &db, BSONObj &cmdObj, int options, string &errmsg, BSONObjBuilder &result, bool fromRepl) {
BSONElement e = cmdObj.firstElement();
long long bps;
if (e.type() == String) {
Status status = BytesQuantity<long long>::fromString(e.Stringdata(), bps);
if (!status.isOK()) {
stringstream ss;
ss << "error parsing number " << e.Stringdata() << ": " << status.codeString() << " " << status.reason();
errmsg = ss.str();
return false;
}
}
else {
if (!e.isNumber()) {
errmsg = "backupThrottle argument must be a number";
return false;
}
bps = e.safeNumberLong();
}
return Manager::throttle(bps, errmsg, result);
}
};
class BackupStatusCommand : public BackupCommand {
public:
BackupStatusCommand() : BackupCommand("backupStatus") {}
virtual void addRequiredPrivileges(const std::string& dbname,
const BSONObj& cmdObj,
std::vector<Privilege>* out) {
ActionSet actions;
actions.addAction(ActionType::backupStatus);
out->push_back(Privilege(AuthorizationManager::SERVER_RESOURCE_NAME, actions));
}
virtual void help(stringstream &h) const {
h << "Report the current status of hot backup." << endl
<< "{ backupStatus: <N> }";
}
virtual bool run(const string &db, BSONObj &cmdObj, int options, string &errmsg, BSONObjBuilder &result, bool fromRepl) {
return Manager::status(errmsg, result);
}
};
class BackupInterface : public plugins::CommandLoader {
protected:
bool preLoad(string &errmsg, BSONObjBuilder &result) {
StringData backupVersion(tokubackup_version_string);
if (backupVersion.find("disabled") != string::npos) {
errmsg = "cannot load backup_plugin: enterprise backup library support not found";
return false;
}
return true;
}
CommandVector commands() const {
CommandVector cmds;
cmds.push_back(boost::make_shared<BackupStartCommand>());
cmds.push_back(boost::make_shared<BackupThrottleCommand>());
cmds.push_back(boost::make_shared<BackupStatusCommand>());
return cmds;
}
public:
const string &name() const {
static const string n = "backup_plugin";
return n;
}
const string &version() const {
static const string v = string(tokubackup_version_string);
return v;
}
} backupInterface;
} // namespace backup
} // namespace mongo
extern "C" {
__attribute__((visibility("default")))
mongo::plugins::PluginInterface *TokuMX_Plugin__getInterface(void) {
return &mongo::backup::backupInterface;
}
}