-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyqbackupmain.cpp
More file actions
222 lines (178 loc) · 9.54 KB
/
myqbackupmain.cpp
File metadata and controls
222 lines (178 loc) · 9.54 KB
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
#include "myqbackupmain.h"
MyQBackupMain::MyQBackupMain(QObject *parent) :
QObject(parent)
{
conf = new MyQBackupConfiguration(this);
incremental_idx=-1;
}
void MyQBackupMain::start() {
qDebug() << "Backup destination: " << conf->backup_dest;
if(conf->max_incrementals) {
if(conf->is_restore_mode) {
qDebug() << "restore incremental backup";
} else {
qDebug() << "Making incremental backup";
}
}
QDir backup_dest_dir(conf->backup_dest);
QString backup_dest = conf->backup_dest;
if(!backup_dest_dir.exists()) {
qDebug() << "Backup destination directory doesn't exists at" << conf->backup_dest;
exit(1);
}
if(! conf->is_restore_mode) {
if(conf->max_incrementals) {
ni_inc_base = backup_dest_dir.absolutePath() + "/mysql-base-full";
if(backup_dest_dir.exists("full") || backup_dest_dir.exists("fake-full")
|| QFile::exists(backup_dest_dir.absolutePath() + "/full.tar.gz")
|| QFile::exists(backup_dest_dir.absolutePath() + "/full.xbstream")
) {
qDebug() << "Full backup already exists";
incremental_idx=1;
for(incremental_idx=1;incremental_idx <= conf->max_incrementals; ++ incremental_idx) {
if(backup_dest_dir.exists(QString("inc-%1").arg(incremental_idx))) {
qDebug() << "Found existing incremental backup";
} else {
break;
}
}
ni_inc_last = backup_dest_dir.absolutePath() + "/mysql-last-full";
qDebug() << "Next backup number:"<<incremental_idx;
if(incremental_idx == 1) {
backup_inc_base = backup_dest_dir.absolutePath() + "/fake-full";
} else {
backup_inc_base = backup_dest_dir.absolutePath() + QString("/inc-%1").arg(incremental_idx-1);
}
backup_inc_path = backup_dest_dir.absolutePath() + QString("/inc-%1").arg(incremental_idx);
backup_dest = backup_inc_path;
} else {
incremental_idx=0;
backup_inc_base = backup_dest_dir.absolutePath() + "/full";
backup_inc_path.clear();
backup_dest = backup_inc_base;
}
} else {
// full non-incremental backup
}
} else {
incremental_idx = conf->max_incrementals;
ni_inc_base = backup_dest_dir.absolutePath() + "/mysql-base-full";
ni_inc_last = backup_dest_dir.absolutePath() + "/mysql-last-full";
backup_inc_path = backup_dest_dir.absolutePath() + QString("/inc-%1").arg(conf->max_incrementals);
}
MySQLConnection *myconnection = new MySQLConnection(conf->server,
conf->port,
conf->user,
conf->password, conf->database, this);
version = myconnection->version;
if(version.startsWith("5.6.")) {
xtrabackup_binary = "xtrabackup_56";
} else if(version.startsWith("5.5.")) {
xtrabackup_binary = "xtrabackup_55";
} else if(version.startsWith("5.1.")) {
xtrabackup_binary = "xtrabackup";
} else if(version.startsWith("5.0.")) {
xtrabackup_binary = "xtrabackup";
}
QString qpress = conf->xtrabackup_path + "qpress";
QString xbstream = conf->xtrabackup_path + "xbstream";
QString xbbinary = conf->xtrabackup_path.append(xtrabackup_binary);
if(!QFile::exists(xbbinary)) {
qDebug() << "Can't find xtrabackup binary at" << xbbinary;
exit(1);
}
if(!QFile::exists(xbstream)) {
qDebug() << "Can't find xbstream binary at" << xbstream;
exit(1);
}
if(!QFile::exists(qpress)) {
qDebug() << "Can't find qpress binary at" << qpress;
exit(1);
}
qDebug() << "binary:"<< xbbinary;
qDebug() << "Dir for files:"<< backup_dest;
qDebug() << "Base path for incremental:"<< backup_inc_base;
qDebug() << "Incremental backup path:"<< backup_inc_path;
// need remote backup controller for:
// time xtrabackup_55 --defaults-group="mysqld" --backup --target-dir=$PWD --tmpdir=$PWD --compress --compress-threads=8 --parallel=4 --stream=xbstream > db.xbstream
XBBackupController *backupctl = new XBBackupController(backup_dest, backup_inc_base, backup_inc_path, this);
XBStreamer *backup_streamer = new XBStreamer(backup_dest, backup_inc_base, backup_inc_path, conf->ssh_host, xbbinary, this);
SshFileCreationWatcher *ssh_file_watcher = new SshFileCreationWatcher(conf->ssh_host, this);
NonInnoDBSyncer *rsync_syncer = new NonInnoDBSyncer(backup_dest,
ni_inc_base, ni_inc_last,
conf->restore_dir, conf->max_incrementals, myconnection,
(conf->ssh_host+":"+myconnection->datadir), this);
XBPreparer *directory_preparer = 0;
if(conf->max_incrementals == 0) { // prepare standalone full backup
directory_preparer = new XBPreparer(backup_dest, 0, incremental_idx,
conf->restore_dir, xbbinary, xbstream, qpress, conf->compression, conf->ssh_host, this);
} else if(incremental_idx == 0) {
directory_preparer = new XBPreparer(backup_dest, 1, incremental_idx,
conf->restore_dir, xbbinary, xbstream, qpress, conf->compression, conf->ssh_host, this);
} else if(incremental_idx > conf->max_incrementals) {
directory_preparer = new XBPreparer(backup_dest, 2, incremental_idx,
conf->restore_dir, xbbinary, xbstream, qpress, conf->compression, conf->ssh_host, this);
} else {
directory_preparer = new XBPreparer(backup_dest, 3, incremental_idx,
conf->restore_dir, xbbinary, xbstream, qpress, conf->compression, conf->ssh_host, this);
}
// backup
if(! conf->is_restore_mode) {
if(conf->ssh_host.length() == 0) {
connect(backupctl, SIGNAL(terminate()), this, SLOT(quit()));
FileCreationWatcherThread* xtrabackup_start_watcher = new FileCreationWatcherThread(this);
connect(xtrabackup_start_watcher, SIGNAL(file_created()),
backupctl, SLOT(xtrabackup_process_started()));
xtrabackup_start_watcher->watch_for_file(backup_dest + "/xtrabackup_suspended_1");
FileCreationWatcherThread* xtrabackup_finish_watcher = new FileCreationWatcherThread(this);
connect(xtrabackup_finish_watcher, SIGNAL(file_created()),
backupctl, SLOT(xtrabackup_copied_all_innodb_data()));
xtrabackup_finish_watcher->watch_for_file(backup_dest + "/xtrabackup_suspended_2");
connect(backupctl, SIGNAL(dataCopied()),
rsync_syncer, SLOT(startBackup()));
connect(rsync_syncer, SIGNAL(readyToUnlock()),
backupctl, SLOT(xtrabackup_process_finished()));
connect(backupctl, SIGNAL(finished_ok()),
rsync_syncer, SLOT(unlockTables()));
connect(rsync_syncer, SIGNAL(backupComplete()),
directory_preparer, SLOT(prepare()));
connect(directory_preparer, SIGNAL(preRotate()),
rsync_syncer, SLOT(rotateBackup()));
connect(directory_preparer, SIGNAL(backup_ready()),
this, SLOT(quit()));
backupctl->start(xbbinary);
} else {
// in case of errors exit
connect(backup_streamer, SIGNAL(terminate()),
this, SLOT(quit()));
// at the end of xtrabackup backup, lock tables
connect(ssh_file_watcher, SIGNAL(file_created()),
myconnection, SLOT(lock_all_tables()));
// when tables became locked, start rsync backup
connect(ssh_file_watcher, SIGNAL(file_created()),
rsync_syncer, SLOT(startBackup()));
// at the end of rsync backup unlock database
connect(rsync_syncer, SIGNAL(readyToUnlock()),
myconnection, SLOT(unlock_all_tables()));
// when tables became unlocked we are ready to stop innodb log shipping, by removing lock file
connect(myconnection, SIGNAL(all_tables_unlocked()),
ssh_file_watcher, SLOT(remove_file()));
// when lock file removed, stopping log shipping
connect(ssh_file_watcher, SIGNAL(file_removed()),
backup_streamer, SLOT(xbbackup_finished()));
// when xtrabackup process finished everything is done and we can exit
connect(backup_streamer, SIGNAL(finished_ok()),
directory_preparer, SLOT(prepare()));
// after preparing backup we can exit
connect(directory_preparer, SIGNAL(backup_ready()),
this, SLOT(quit()));
ssh_file_watcher->watch_for_file(conf->remotetmp+"/xtrabackup_suspended_2");
backup_streamer->start();
}
} else {
// restore
directory_preparer->restoreBackup();
rsync_syncer->restoreBackup();
exit(0);
}
}