summaryrefslogtreecommitdiffstats
path: root/keep/common/rdbmanager.cpp
blob: 4d86a89cebf083292d9ca282246775a6ac8b59d5 (plain)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* This file is part of the Keep project
   Copyright (C) 2006 Jean-Rémy Falleri <jr.falleri@laposte.net>

   Keep is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   Keep 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Keep; if not, write to the
   Free Software Foundation, Inc.,
   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.           */

#include "rdbmanager.h"

#include <kprocess.h>
#include <kdebug.h>
#include <qfile.h>
#include <keepsettings.h>

RDBManager::RDBManager()
{
}

RDBManager::~RDBManager()
{
}

void RDBManager::slotCheckBackup()
{
	QValueList<Backup> backups = outdatedBackupList();
	QValueList<Backup>::iterator it;
	for ( it = backups.begin(); it != backups.end(); ++it )
	{
		doBackup(*it);
	}
}

void RDBManager::slotForceBackup(QValueList<Backup> backupList)
{
	QValueList<Backup>::iterator it;
	for ( it = backupList.begin(); it != backupList.end(); ++it )
	{
		doBackup(*it);
	}
}

QString RDBManager::compareAtTime(Backup backup, QDateTime date)
{
	// Gets the rdiff-backup process
	KProcess *proc = RDBProcess();
	// Adds the options
	*proc << "--compare-at-time";
	*proc << QString::number(date.toTime_t()); // increment date
	// Adds source and dest
	*proc << QFile::encodeName(KProcess::quote(backup.source()));
	*proc << QFile::encodeName(KProcess::quote(backup.dest()));
	// Adds the listener
	RDBListener *listen = new RDBListener();
	connect(proc,SIGNAL(receivedStdout(KProcess *,char *,int)),listen,SLOT(receivedStdOut(KProcess *,char *,int)));
	// Starts the process
	if ( !proc->start(KProcess::Block, KProcess::AllOutput) )
        	kdDebug() << "Error starting rdiff-backup" << endl;

	QStringList output = listen->stdOut();
	QStringList::iterator it = output.begin();

	kdDebug() << "Executed process: " << proc->args() << endl;

	delete listen;
	delete proc;
	
	return (*it);
}

QString RDBManager::listChangedSince(Backup backup, QDateTime date)
{
	// Gets the rdiff-backup process
	KProcess *proc = RDBProcess();
	// Adds the options
	*proc << "--list-changed-since";
	*proc << QString::number(date.toTime_t()); // increment date
	// Adds the dest
	*proc << QFile::encodeName(KProcess::quote(backup.dest()));
	// Adds a listener
	RDBListener *listen = new RDBListener();
	connect(proc,SIGNAL(receivedStdout(KProcess *,char *,int)),listen,SLOT(receivedStdOut(KProcess *,char *,int)));
	// Starts the process
	if ( !proc->start(KProcess::Block, KProcess::AllOutput) )
        	kdDebug() << "Error starting rdiff-backup" << endl;

	QStringList output = listen->stdOut();
	QStringList::iterator it = output.begin();

	kdDebug() << "Executed process: " << proc->args() << endl;

	delete listen;
	delete proc;
	
	return (*it);
}

QString RDBManager::listAtTime(Backup backup, QDateTime date)
{
	// Gets the rdiff-backup process
	KProcess *proc = RDBProcess();
	// Adds the options
	*proc << "--list-at-time";
	*proc << QString::number(date.toTime_t()); // increment date
	// Adds the dest
	*proc << QFile::encodeName(KProcess::quote(backup.dest()));
	// Adds a listener
	RDBListener *listen = new RDBListener();
	connect(proc,SIGNAL(receivedStdout(KProcess *,char *,int)),listen,SLOT(receivedStdOut(KProcess *,char *,int)));
	// Starts the process
	if ( !proc->start(KProcess::Block, KProcess::AllOutput) )
        	kdDebug() << "Error starting rdiff-backup" << endl;

	QStringList output = listen->stdOut();
	QStringList::iterator it = output.begin();

	kdDebug() << "Executed process: " << proc->args() << endl;

	delete listen;
	delete proc;
	
	return (*it);
}

QValueList<QDateTime> RDBManager::incrementList(Backup backup)
{
	// Gets the rdiff-backup process
	KProcess *proc = RDBProcess();
	// Adds the options
	*proc << "--list-increments";
	*proc << "-v2";
	*proc << "--parsable-output";
	// Adds the dest
	*proc << QFile::encodeName(KProcess::quote(backup.dest()));
	// Adds a listener
	RDBListener *listen = new RDBListener();
	connect(proc,SIGNAL(receivedStdout(KProcess *,char *,int)),listen,SLOT(receivedStdOut(KProcess *,char *,int)));
	// Starts the process
	if ( !proc->start(KProcess::Block, KProcess::AllOutput) )
        	kdDebug() << "Error starting rdiff-backup" << endl;

	QStringList output = listen->stdOut();
	QStringList::iterator it = output.begin();
	
	QStringList lines = QStringList::split("\n",*it);
	QValueList<QDateTime> dateList;

	QStringList::iterator it2 = output.begin();
	for ( it2 = lines.begin(); it2 != lines.end(); ++it2 )
	{
		QStringList field = QStringList::split(" ",*it2);
		QStringList::iterator dateStr = field.begin();
		long timestamp = (*dateStr).toUInt();
		QDateTime datetime;
		datetime.setTime_t(timestamp);
		dateList.append(datetime);
	}
	
	kdDebug() << "Executed process: " << proc->args() << endl;

	delete listen;
	delete proc;
	
	return dateList;
}

QDateTime RDBManager::lastIncrement(Backup backup)
{
	QValueList<QDateTime> increments = incrementList(backup);
	QDateTime last = increments.last();
	return last;
}

QValueList<Backup> RDBManager::outdatedBackupList()
{
	BackupConfig config;
	QValueList<Backup> backups = config.backupList();
	QValueList<Backup> outdated;
	QValueList<Backup>::iterator it;
	kdDebug() << "Detecting outdated backup." << endl;
	for ( it = backups.begin(); it != backups.end(); ++it )
	{
		QDateTime last = lastIncrement(*it);
		QDate today = QDate::currentDate();
		if ( last.date().daysTo(today) >= (*it).interval() )
		{
			kdDebug() << "Detected outdated backup: " << (*it).source() << endl;
			outdated.append(*it);
		}
 	}
	return outdated;
}

void RDBManager::slotRestoreBackup(Backup backup,QDateTime time)
{
	// Gets the rdiff-backup process
	KProcess *proc = RDBProcess();
	// Adds the options
	*proc << "--force";
	*proc << "--restore-as-of";
	*proc << QString::number(time.toTime_t()); // Date of the increment to restore
	// Adds source and dest
	*proc << QFile::encodeName(KProcess::quote(backup.dest()));
	*proc << QFile::encodeName(KProcess::quote(backup.source()));
	// Adds a listener (for output recording)
	RDBListener *listen = new RDBListener();
	connect(proc,SIGNAL(receivedStdout(KProcess *,char *,int)),listen,SLOT(receivedStdOut(KProcess *,char *,int)));
	connect(proc,SIGNAL(receivedStderr(KProcess *,char *,int)),listen,SLOT(receivedStdErr(KProcess *,char *,int)));
	// Starts the process
	if ( !proc->start(KProcess::Block, KProcess::AllOutput) )
        	kdDebug() << "Error starting rdiff-backup" << endl;

	kdDebug() << "Executed process: " << proc->args() << endl;

	if ( !listen->isOk() )
	{
		kdDebug() << "Error message: " << listen->errorMessage() << endl;
		emit backupError(backup,listen->errorMessage());
	}
	
	delete listen;
	delete proc;
}

bool RDBManager::isRDB()
{
	// Gets the rdiff-backup process
	KProcess *proc = RDBProcess();
	*proc << "-V";
	// Adds a listener (for output recording)
	RDBListener *listen = new RDBListener();
	connect(proc,SIGNAL(receivedStdout(KProcess *,char *,int)),listen,SLOT(receivedStdOut(KProcess *,char *,int)));
	// Starts the process
	if ( !proc->start(KProcess::Block, KProcess::AllOutput) )
        	kdDebug() << "Error starting rdiff-backup" << endl;

	QStringList outList = listen->stdOut();
	QStringList::iterator out = outList.begin();

	kdDebug() << "Executed process: " << proc->args() << endl;
	
	delete listen;
	delete proc;

	if ( *out == "" )
		return false;
	return true;
}

QString RDBManager::RDBVersion()
{
	// Gets the rdiff-backup process
	KProcess *proc = RDBProcess();
	*proc << "-V";
	// Adds a listener (for output recording)
	RDBListener *listen = new RDBListener();
	connect(proc,SIGNAL(receivedStdout(KProcess *,char *,int)),listen,SLOT(receivedStdOut(KProcess *,char *,int)));
	// Starts the process
	if ( !proc->start(KProcess::Block, KProcess::AllOutput) )
        	kdDebug() << "Error starting rdiff-backup" << endl;

	QStringList outList = listen->stdOut();
	QStringList::iterator out = outList.begin();

	kdDebug() << "Executed process: " << proc->args() << endl;

	delete listen;
	delete proc;

	return (*out).mid(13,5);
}

void RDBManager::doBackup(Backup backup)
{
	// Gets the rdiff-backup process
	KProcess *proc = RDBProcess(KeepSettings::controlRDBPriority,KeepSettings::rDBPriority());
	// Adds include and exclude
	if ( backup.useIncludeExclude() )
	{
		QStringList includeExcludeList = backup.includeExcludeList();
		QStringList::iterator it;
		for ( it = includeExcludeList.begin(); it != includeExcludeList.end(); ++it )
		{
			QString type = (*it).left(1);
			QString file = (*it).right((*it).length() - 1);
			if ( type == "I" )
			{
				*proc << "--include";
				*proc << QFile::encodeName(KProcess::quote(file));
			}
			else if ( type == "E" )
			{
				*proc << "--exclude";
				*proc << QFile::encodeName(KProcess::quote(file));
			}
		}
	}	
	// Adds the option
	// For simple mode
	if ( !backup.useAdvancedConfig() )
	{
		if ( !backup.useCompression() )
			*proc << "--no-compression";
	
		if ( backup.excludeSpecialFiles() )
			*proc << "--exclude-special-files";
	}
	// For advanced mode
	else
	{
		QStringList optionList = backup.optionList();
		for ( QStringList::Iterator it = optionList.begin(); it != optionList.end(); ++it )
		{
			*proc << *it;
		}
	}
	// Adds source and dest
	*proc << QFile::encodeName(KProcess::quote(backup.source()));
	*proc << QFile::encodeName(KProcess::quote(backup.dest()));
	// Adds a listener (for output recording)
	RDBListener *listen = new RDBListener();
	connect(proc,SIGNAL(receivedStdout(KProcess *,char *,int)),listen,SLOT(receivedStdOut(KProcess *,char *,int)));
	connect(proc,SIGNAL(receivedStderr(KProcess *,char *,int)),listen,SLOT(receivedStdErr(KProcess *,char *,int)));
	// Starts the process
	if ( !proc->start(KProcess::Block, KProcess::AllOutput) )
        	kdDebug() << "Error starting rdiff-backup" << endl;
	
	kdDebug() << "Executed process: " << proc->args() << endl;
	kdDebug() << "Process status:  " << listen->isOk() << endl;

	if ( !listen->isOk() )
	{
		kdDebug() << "Error message: " << listen->errorMessage() << endl;
		emit backupError(backup,listen->errorMessage());
	}
	else
		emit backupSuccess(backup);

	delete listen;
	delete proc;

	if ( !backup.neverDelete() )
	{
		removeOldIncrements(backup);
	}
}

void RDBManager::removeOldIncrements(Backup backup)
{
	// Gets the rdiff-backup process
	KProcess *proc = RDBProcess();
	// Adds the options
	*proc << "--remove-older-than" << QString("%1").arg(backup.deleteAfter()) + "D";
	// Adds dest
	*proc << backup.dest();
	// Starts the process
	if ( !proc->start(KProcess::Block, KProcess::AllOutput) )
        	kdDebug() << "Error starting rdiff-backup" << endl;

	kdDebug() << "Executed process: " << proc->args() << endl;

	delete proc;
}

KProcess *RDBManager::RDBProcess(bool isNice,int niceLevel)
{
	KProcess *proc = new KProcess();
	proc->setUseShell(true);
	if ( isNice )
	{
		*proc << "nice" << "-n" << QString("%1").arg(niceLevel);
	}
	*proc << "rdiff-backup";

	return proc;
}

#include "rdbmanager.moc"