-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxbpreparer.cpp
308 lines (284 loc) · 11.1 KB
/
xbpreparer.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "xbpreparer.h"
XBPreparer::XBPreparer(QString target, int prepare_type, int incremental_idx_, QString restore_to, QString nxtrabackup_binary, QString xbstream_bin, QString qcompress_bin, bool compression, QString remote, QObject *parent) :
QObject(parent),
xtrabackup_binary(nxtrabackup_binary),
xbstream_binary(xbstream_bin),
qpress_path(qcompress_bin),
target_dir(target),
type(prepare_type),
incremental_idx(incremental_idx_),
restore_dir(restore_to),
use_compression(compression),
ssh_remote(remote)
{
}
bool removeDir(const QString & dirName)
{
bool result = false;
QDir dir(dirName);
if (dir.exists(dirName)) {
Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
if (info.isDir()) {
result = removeDir(info.absoluteFilePath());
}
else {
result = QFile::remove(info.absoluteFilePath());
}
if (!result) {
return result;
}
}
result = dir.rmdir(dirName);
}
return result;
}
void XBPreparer::prepare() {
qDebug() << "Prepare runs!" << type;
QProcess xtrabackup_prepare_process;
switch(type) {
case 0:
{
if(ssh_remote.length() != 0) {
qDebug() << "should unpack standalone full backup to" << target_dir
<< (target_dir+".xbstream");
QDir dir(target_dir);
dir.mkdir(target_dir);
QProcess xbunpack;
xbunpack.setStandardInputFile(target_dir+".xbstream");
xbunpack.setWorkingDirectory(target_dir);
xbunpack.start(
xbstream_binary,
QStringList()
<< "-x"
);
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
qDebug() << xbunpack.program() << xbunpack.arguments();
#endif
xbunpack.waitForFinished();
qDebug() << xbunpack.readAll() << xbunpack.readAllStandardError();
}
xtrabackup_prepare_process.start(
xtrabackup_binary,
QStringList()
<< "--prepare"
<< (QString("--target-dir=")+target_dir) );
break;
}
case 1:
{
QDir last_inc_dir(target_dir);
last_inc_dir.cdUp();
qDebug() << "Backup dir" << last_inc_dir.absolutePath();
QString inc_base = last_inc_dir.absolutePath() + "/full";
if(ssh_remote.length() != 0) {
qDebug() << "should unpack full backup to" << inc_base;
extract_xtrabackup_stream(inc_base+".xbstream", inc_base);
}
xtrabackup_prepare_process.start(
xtrabackup_binary,
QStringList()
<< "--prepare" << "--apply-log-only"
<< (QString("--target-dir=")+inc_base) );
break;
}
case 2:
{
if(ssh_remote.length() != 0) {
qDebug() << "should unpack incremental backup to" << target_dir
<< "from:"<< (target_dir+".xbstream");
extract_xtrabackup_stream(target_dir+".xbstream", target_dir);
}
QDir last_inc_dir(target_dir);
last_inc_dir.cdUp();
qDebug() << "Backup dir" << last_inc_dir.absolutePath();
QString inc_base = last_inc_dir.absolutePath() + "/full";
QString inc_path = last_inc_dir.absolutePath() + "/inc-1";
if(!last_inc_dir.exists(inc_base) && use_compression) {
QDir dir;
dir.mkdir(inc_base);
QProcess tar_process;
tar_process.start("tar",
QStringList()
<< "-C" << (inc_base + "/.")
<< "-xzf"
<< (last_inc_dir.absolutePath() + "/full.tar.gz")
);
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
qDebug() << tar_process.program() << tar_process.arguments();
#endif
tar_process.waitForFinished();
qDebug() << tar_process.readAll() << tar_process.readAllStandardError();
}
xtrabackup_prepare_process.start(
xtrabackup_binary,
QStringList()
<< "--prepare" << "--apply-log-only"
<< QString("--target-dir=").append(inc_base)
<< QString("--incremental-dir=").append(inc_path));
qDebug() << "running: " << xtrabackup_binary << "--prepare" << "--apply-log-only"
<< QString("--target-dir=").append(inc_base)
<< QString("--incremental-dir=").append(inc_path);
break;
}
default:
{
if(ssh_remote.length() != 0) {
qDebug() << "should unpack incremental backup to" << target_dir;
extract_xtrabackup_stream(target_dir+".xbstream", target_dir);
}
emit backup_ready();
return;
}
}
xtrabackup_prepare_process.waitForFinished();
qDebug() << "Backup prepared by" << xtrabackup_binary << type;
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
qDebug() << xtrabackup_prepare_process.program() << xtrabackup_prepare_process.arguments();
#endif
qDebug() << xtrabackup_prepare_process.readAll()
<< xtrabackup_prepare_process.readAllStandardError();
if(type == 1) {
if(use_compression) {
QDir last_inc_dir(target_dir);
last_inc_dir.cdUp();
QProcess tar_process;
tar_process.start("tar",
QStringList()
<< "-C" << (last_inc_dir.absolutePath() + "/full/.")
<< "-czf"
<< (last_inc_dir.absolutePath() + "/full.tar.gz")
<< "./"
);
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
qDebug() << tar_process.program() << tar_process.arguments();
#endif
tar_process.waitForFinished();
qDebug() << tar_process.readAll();
QDir dir1(last_inc_dir.absolutePath() + "/full");
//dir1.removeRecursively();
removeDir(dir1.absolutePath());
}
} else if(type == 2) {
emit preRotate();
QDir last_inc_dir(target_dir);
last_inc_dir.cdUp();
QDir dir1(last_inc_dir.absolutePath() + "/inc-1");
//dir1.removeRecursively();
removeDir(dir1.absolutePath());
for(int i=2; i<=incremental_idx; ++i) {
qDebug() << "Move" << last_inc_dir.absolutePath() + QString("/inc-%1").arg(i) <<
last_inc_dir.absolutePath() + QString("/inc-%1").arg(i-1);
last_inc_dir.rename(QString("inc-%1").arg(i),
QString("inc-%1").arg(i-1));
}
if(use_compression) { // ready to overwrite old full
QDir last_inc_dir(target_dir);
last_inc_dir.cdUp();
QProcess tar_process;
tar_process.start("tar",
QStringList()
<< "-C" << (last_inc_dir.absolutePath() + "/full/.")
<< "-czf"
<< (last_inc_dir.absolutePath() + "/full.tar.gz")
<< "./"
);
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
qDebug() << tar_process.program() << tar_process.arguments();
#endif
tar_process.waitForFinished();
qDebug() << tar_process.readAll() << tar_process.readAllStandardError();
QDir dir1(last_inc_dir.absolutePath() + "/full");
//dir1.removeRecursively();
removeDir(dir1.absolutePath());
}
}
emit backup_ready();
}
void XBPreparer::restoreBackup() {
QDir last_inc_dir(target_dir);
// last_inc_dir.cdUp();
QDir dir;
if(!dir.exists(restore_dir)) {
dir.mkdir(restore_dir);
}
qDebug() << "Backup dir" << last_inc_dir.absolutePath() << "Restore dir"<< restore_dir;
if(use_compression) {
QProcess tar_process;
tar_process.start("tar",
QStringList()
<< "-C" << (restore_dir + "/.")
<< "-xzf"
<< (last_inc_dir.absolutePath() + "/full.tar.gz")
);
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
qDebug() << tar_process.program() << tar_process.arguments();
#endif
tar_process.waitForFinished();
qDebug() << tar_process.readAll();
} else {
QString base_dir = last_inc_dir.absolutePath() + "/full";
QProcess rsync_process;
rsync_process.start("rsync",
QStringList() << "-av"
<< (base_dir + "/")
<< (restore_dir + "/.")
);
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
qDebug() << rsync_process.program() << rsync_process.arguments();
#endif
rsync_process.waitForFinished();
qDebug() << rsync_process.readAll();
}
QProcess xtrabackup_prepare_process;
for(int i=1; i<=incremental_idx; ++i) {
QString inc_path = last_inc_dir.absolutePath() + QString("/inc-%1").arg(i);
xtrabackup_prepare_process.start(
xtrabackup_binary,
QStringList()
<< "--prepare" << "--apply-log-only"
<< QString("--target-dir=").append(restore_dir)
<< QString("--incremental-dir=").append(inc_path));
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
qDebug() << xtrabackup_prepare_process.program() << xtrabackup_prepare_process.arguments();
#endif
xtrabackup_prepare_process.waitForFinished();
qDebug() << xtrabackup_prepare_process.readAll()
<< xtrabackup_prepare_process.readAllStandardError();
}
}
void XBPreparer::extract_xtrabackup_stream(QString src, QString dst) {
QDir dir;
dir.mkdir(dst);
QDir last_inc_dir(dst);
last_inc_dir.cdUp();
QProcess xbunpack;
xbunpack.setStandardInputFile(src);
xbunpack.setWorkingDirectory(dst);
xbunpack.start(
xbstream_binary,
QStringList()
<< "-x"
);
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
qDebug() << xbunpack.program() << xbunpack.arguments();
#endif
xbunpack.waitForFinished();
qDebug() << xbunpack.readAll() << xbunpack.readAllStandardError();
last_inc_dir.mkdir(last_inc_dir.absolutePath() + "/fake-full");
QFile cpinfo(dst+"/xtrabackup_checkpoints");
cpinfo.copy(last_inc_dir.absolutePath()+ "/fake-full/xtrabackup_checkpoints");
// if compression
QProcess decompressor;
decompressor.setWorkingDirectory(dst);
decompressor.start(
"bash",
QStringList()
<< "-c"
<< (QString("for bf in `find . -iname \"*\\.qp\"`; do %1 -d $bf $(dirname $bf) && rm $bf; done").arg(qpress_path))
);
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
qDebug() << decompressor.program() << decompressor.arguments();
#endif
decompressor.waitForFinished();
qDebug() << decompressor.readAll() << decompressor.readAllStandardError();
}