/
sql_find.c
457 lines (414 loc) · 16 KB
/
sql_find.c
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2013 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* BAREOS Catalog Database Find record interface routines
*
* Note, generally, these routines are more complicated
* that a simple search by name or id. Such simple
* request are in get.c
*
* Kern Sibbald, December 2000
*/
#include "bareos.h"
#if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI
#include "cats.h"
#include "bdb_priv.h"
#include "sql_glue.h"
/* -----------------------------------------------------------------------
*
* Generic Routines (or almost generic)
*
* -----------------------------------------------------------------------
*/
/*
* Find job start time if JobId specified, otherwise
* find last Job start time Incremental and Differential saves.
*
* StartTime is returned in stime
* Job name is returned in job (MAX_NAME_LENGTH)
*
* Returns: 0 on failure
* 1 on success, jr is unchanged, but stime and job are set
*/
bool db_find_job_start_time(JCR *jcr, B_DB *mdb, JOB_DBR *jr, POOLMEM **stime, char *job)
{
bool retval = false;
SQL_ROW row;
char ed1[50], ed2[50];
char esc_name[MAX_ESCAPE_NAME_LENGTH];
db_lock(mdb);
mdb->db_escape_string(jcr, esc_name, jr->Name, strlen(jr->Name));
pm_strcpy(stime, "0000-00-00 00:00:00"); /* default */
job[0] = 0;
/* If no Id given, we must find corresponding job */
if (jr->JobId == 0) {
/* Differential is since last Full backup */
Mmsg(mdb->cmd,
"SELECT StartTime, Job FROM Job WHERE JobStatus IN ('T','W') AND Type='%c' AND "
"Level='%c' AND Name='%s' AND ClientId=%s AND FileSetId=%s "
"ORDER BY StartTime DESC LIMIT 1",
jr->JobType, L_FULL, esc_name,
edit_int64(jr->ClientId, ed1), edit_int64(jr->FileSetId, ed2));
if (jr->JobLevel == L_DIFFERENTIAL) {
/* SQL cmd for Differential backup already edited above */
/* Incremental is since last Full, Incremental, or Differential */
} else if (jr->JobLevel == L_INCREMENTAL) {
/*
* For an Incremental job, we must first ensure
* that a Full backup was done (cmd edited above)
* then we do a second look to find the most recent
* backup
*/
if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
Mmsg2(&mdb->errmsg, _("Query error for start time request: ERR=%s\nCMD=%s\n"),
sql_strerror(mdb), mdb->cmd);
goto bail_out;
}
if ((row = sql_fetch_row(mdb)) == NULL) {
sql_free_result(mdb);
Mmsg(mdb->errmsg, _("No prior Full backup Job record found.\n"));
goto bail_out;
}
sql_free_result(mdb);
/* Now edit SQL command for Incremental Job */
Mmsg(mdb->cmd,
"SELECT StartTime, Job FROM Job WHERE JobStatus IN ('T','W') AND Type='%c' AND "
"Level IN ('%c','%c','%c') AND Name='%s' AND ClientId=%s "
"AND FileSetId=%s ORDER BY StartTime DESC LIMIT 1",
jr->JobType, L_INCREMENTAL, L_DIFFERENTIAL, L_FULL, esc_name,
edit_int64(jr->ClientId, ed1), edit_int64(jr->FileSetId, ed2));
} else {
Mmsg1(mdb->errmsg, _("Unknown level=%d\n"), jr->JobLevel);
goto bail_out;
}
} else {
Dmsg1(100, "Submitting: %s\n", mdb->cmd);
Mmsg(mdb->cmd, "SELECT StartTime, Job FROM Job WHERE Job.JobId=%s",
edit_int64(jr->JobId, ed1));
}
if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
pm_strcpy(stime, ""); /* set EOS */
Mmsg2(&mdb->errmsg, _("Query error for start time request: ERR=%s\nCMD=%s\n"),
sql_strerror(mdb), mdb->cmd);
goto bail_out;
}
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg2(&mdb->errmsg, _("No Job record found: ERR=%s\nCMD=%s\n"),
sql_strerror(mdb), mdb->cmd);
sql_free_result(mdb);
goto bail_out;
}
Dmsg2(100, "Got start time: %s, job: %s\n", row[0], row[1]);
pm_strcpy(stime, row[0]);
bstrncpy(job, row[1], MAX_NAME_LENGTH);
sql_free_result(mdb);
retval = true;
bail_out:
db_unlock(mdb);
return retval;
}
/*
* Find the last job start time for the specified JobLevel
*
* StartTime is returned in stime
* Job name is returned in job (MAX_NAME_LENGTH)
*
* Returns: false on failure
* true on success, jr is unchanged, but stime and job are set
*/
bool db_find_last_job_start_time(JCR *jcr, B_DB *mdb, JOB_DBR *jr,
POOLMEM **stime, char *job, int JobLevel)
{
bool retval = false;
SQL_ROW row;
char ed1[50], ed2[50];
char esc_name[MAX_ESCAPE_NAME_LENGTH];
db_lock(mdb);
mdb->db_escape_string(jcr, esc_name, jr->Name, strlen(jr->Name));
pm_strcpy(stime, "0000-00-00 00:00:00"); /* default */
job[0] = 0;
Mmsg(mdb->cmd,
"SELECT StartTime, Job FROM Job WHERE JobStatus IN ('T','W') AND Type='%c' AND "
"Level='%c' AND Name='%s' AND ClientId=%s AND FileSetId=%s "
"ORDER BY StartTime DESC LIMIT 1",
jr->JobType, JobLevel, esc_name,
edit_int64(jr->ClientId, ed1), edit_int64(jr->FileSetId, ed2));
if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
Mmsg2(&mdb->errmsg, _("Query error for start time request: ERR=%s\nCMD=%s\n"),
sql_strerror(mdb), mdb->cmd);
goto bail_out;
}
if ((row = sql_fetch_row(mdb)) == NULL) {
sql_free_result(mdb);
Mmsg(mdb->errmsg, _("No prior Full backup Job record found.\n"));
goto bail_out;
}
Dmsg1(100, "Got start time: %s\n", row[0]);
pm_strcpy(stime, row[0]);
bstrncpy(job, row[1], MAX_NAME_LENGTH);
sql_free_result(mdb);
retval = true;
bail_out:
db_unlock(mdb);
return retval;
}
/*
* Find last failed job since given start-time
* it must be either Full or Diff.
*
* Returns: false on failure
* true on success, jr is unchanged and stime unchanged
* level returned in JobLevel
*/
bool db_find_failed_job_since(JCR *jcr, B_DB *mdb, JOB_DBR *jr, POOLMEM *stime, int &JobLevel)
{
bool retval = false;
SQL_ROW row;
char ed1[50], ed2[50];
char esc_name[MAX_ESCAPE_NAME_LENGTH];
db_lock(mdb);
mdb->db_escape_string(jcr, esc_name, jr->Name, strlen(jr->Name));
/* Differential is since last Full backup */
Mmsg(mdb->cmd,
"SELECT Level FROM Job WHERE JobStatus NOT IN ('T','W') AND "
"Type='%c' AND Level IN ('%c','%c') AND Name='%s' AND ClientId=%s "
"AND FileSetId=%s AND StartTime>'%s' "
"ORDER BY StartTime DESC LIMIT 1",
jr->JobType, L_FULL, L_DIFFERENTIAL, esc_name,
edit_int64(jr->ClientId, ed1), edit_int64(jr->FileSetId, ed2),
stime);
if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
goto bail_out;
}
if ((row = sql_fetch_row(mdb)) == NULL) {
sql_free_result(mdb);
goto bail_out;
}
JobLevel = (int)*row[0];
sql_free_result(mdb);
retval = true;
bail_out:
db_unlock(mdb);
return retval;
}
/*
* Find JobId of last job that ran. E.g. for
* VERIFY_CATALOG we want the JobId of the last INIT.
* For VERIFY_VOLUME_TO_CATALOG, we want the JobId of the last Job.
*
* Returns: true on success
* false on failure
*/
bool db_find_last_jobid(JCR *jcr, B_DB *mdb, const char *Name, JOB_DBR *jr)
{
bool retval = false;
SQL_ROW row;
char ed1[50];
char esc_name[MAX_ESCAPE_NAME_LENGTH];
db_lock(mdb);
/* Find last full */
Dmsg2(100, "JobLevel=%d JobType=%d\n", jr->JobLevel, jr->JobType);
if (jr->JobLevel == L_VERIFY_CATALOG) {
mdb->db_escape_string(jcr, esc_name, jr->Name, strlen(jr->Name));
Mmsg(mdb->cmd,
"SELECT JobId FROM Job WHERE Type='V' AND Level='%c' AND "
" JobStatus IN ('T','W') AND Name='%s' AND "
"ClientId=%s ORDER BY StartTime DESC LIMIT 1",
L_VERIFY_INIT, esc_name,
edit_int64(jr->ClientId, ed1));
} else if (jr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG ||
jr->JobLevel == L_VERIFY_DISK_TO_CATALOG ||
jr->JobType == JT_BACKUP) {
if (Name) {
mdb->db_escape_string(jcr, esc_name, (char*)Name,
MIN(strlen(Name), sizeof(esc_name)));
Mmsg(mdb->cmd,
"SELECT JobId FROM Job WHERE Type='B' AND JobStatus IN ('T','W') AND "
"Name='%s' ORDER BY StartTime DESC LIMIT 1", esc_name);
} else {
Mmsg(mdb->cmd,
"SELECT JobId FROM Job WHERE Type='B' AND JobStatus IN ('T','W') AND "
"ClientId=%s ORDER BY StartTime DESC LIMIT 1",
edit_int64(jr->ClientId, ed1));
}
} else {
Mmsg1(&mdb->errmsg, _("Unknown Job level=%d\n"), jr->JobLevel);
goto bail_out;
}
Dmsg1(100, "Query: %s\n", mdb->cmd);
if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
goto bail_out;
}
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(&mdb->errmsg, _("No Job found for: %s.\n"), mdb->cmd);
sql_free_result(mdb);
goto bail_out;
}
jr->JobId = str_to_int64(row[0]);
sql_free_result(mdb);
Dmsg1(100, "db_get_last_jobid: got JobId=%d\n", jr->JobId);
if (jr->JobId <= 0) {
Mmsg1(&mdb->errmsg, _("No Job found for: %s\n"), mdb->cmd);
goto bail_out;
}
retval = true;
bail_out:
db_unlock(mdb);
return retval;
}
/*
* Find Available Media (Volume) for Pool
*
* Find a Volume for a given PoolId, MediaType, and Status.
*
* Returns: 0 on failure
* numrows on success
*/
int db_find_next_volume(JCR *jcr, B_DB *mdb, int item, bool InChanger, MEDIA_DBR *mr)
{
SQL_ROW row = NULL;
int num_rows = 0;
const char *order;
char esc_type[MAX_ESCAPE_NAME_LENGTH];
char esc_status[MAX_ESCAPE_NAME_LENGTH];
char ed1[50];
db_lock(mdb);
mdb->db_escape_string(jcr, esc_type, mr->MediaType, strlen(mr->MediaType));
mdb->db_escape_string(jcr, esc_status, mr->VolStatus, strlen(mr->VolStatus));
if (item == -1) { /* find oldest volume */
/* Find oldest volume */
Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,"
"VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
"MediaType,VolStatus,PoolId,VolRetention,VolUseDuration,MaxVolJobs,"
"MaxVolFiles,Recycle,Slot,FirstWritten,LastWritten,InChanger,"
"EndFile,EndBlock,LabelType,LabelDate,StorageId,"
"Enabled,LocationId,RecycleCount,InitialWrite,"
"ScratchPoolId,RecyclePoolId,VolReadTime,VolWriteTime,"
"ActionOnPurge,EncryptionKey,MinBlocksize,MaxBlocksize "
"FROM Media WHERE PoolId=%s AND MediaType='%s' AND VolStatus IN ('Full',"
"'Recycle','Purged','Used','Append') AND Enabled=1 "
"ORDER BY LastWritten LIMIT 1",
edit_int64(mr->PoolId, ed1), esc_type);
item = 1;
} else {
POOL_MEM changer(PM_FNAME);
/* Find next available volume */
if (InChanger) {
Mmsg(changer, "AND InChanger=1 AND StorageId=%s",
edit_int64(mr->StorageId, ed1));
}
if (bstrcmp(mr->VolStatus, "Recycle") ||
bstrcmp(mr->VolStatus, "Purged")) {
order = "AND Recycle=1 ORDER BY LastWritten ASC,MediaId"; /* take oldest that can be recycled */
} else {
order = sql_media_order_most_recently_written[db_get_type_index(mdb)]; /* take most recently written */
}
Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,"
"VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
"MediaType,VolStatus,PoolId,VolRetention,VolUseDuration,MaxVolJobs,"
"MaxVolFiles,Recycle,Slot,FirstWritten,LastWritten,InChanger,"
"EndFile,EndBlock,LabelType,LabelDate,StorageId,"
"Enabled,LocationId,RecycleCount,InitialWrite,"
"ScratchPoolId,RecyclePoolId,VolReadTime,VolWriteTime,"
"ActionOnPurge,EncryptionKey,MinBlocksize,MaxBlocksize "
"FROM Media WHERE PoolId=%s AND MediaType='%s' AND Enabled=1 "
"AND VolStatus='%s' "
"%s "
"%s LIMIT %d",
edit_int64(mr->PoolId, ed1), esc_type,
esc_status, changer.c_str(), order, item);
}
Dmsg1(100, "fnextvol=%s\n", mdb->cmd);
if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
goto bail_out;
}
num_rows = sql_num_rows(mdb);
if (item > num_rows || item < 1) {
Dmsg2(050, "item=%d got=%d\n", item, num_rows);
Mmsg2(&mdb->errmsg, _("Request for Volume item %d greater than max %d or less than 1\n"),
item, num_rows);
num_rows = 0;
goto bail_out;
}
/* Note, we previously seeked to the row using: sql_data_seek(mdb, item-1);
* but this failed on PostgreSQL, so now we loop over all the records.
* This should not be too horrible since the maximum Volumes we look at
* in any case is 20.
*/
while (item-- > 0) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Dmsg1(050, "Fail fetch item=%d\n", item+1);
Mmsg1(&mdb->errmsg, _("No Volume record found for item %d.\n"), item);
sql_free_result(mdb);
num_rows = 0;
goto bail_out;
}
}
/* Return fields in Media Record */
mr->MediaId = str_to_int64(row[0]);
bstrncpy(mr->VolumeName, (row[1] != NULL) ? row[1] : "", sizeof(mr->VolumeName));
mr->VolJobs = str_to_int64(row[2]);
mr->VolFiles = str_to_int64(row[3]);
mr->VolBlocks = str_to_int64(row[4]);
mr->VolBytes = str_to_uint64(row[5]);
mr->VolMounts = str_to_int64(row[6]);
mr->VolErrors = str_to_int64(row[7]);
mr->VolWrites = str_to_int64(row[8]);
mr->MaxVolBytes = str_to_uint64(row[9]);
mr->VolCapacityBytes = str_to_uint64(row[10]);
bstrncpy(mr->MediaType, (row[11] != NULL) ? row[11] : "", sizeof(mr->MediaType));
bstrncpy(mr->VolStatus, (row[12] != NULL) ? row[12] : "", sizeof(mr->VolStatus));
mr->PoolId = str_to_int64(row[13]);
mr->VolRetention = str_to_uint64(row[14]);
mr->VolUseDuration = str_to_uint64(row[15]);
mr->MaxVolJobs = str_to_int64(row[16]);
mr->MaxVolFiles = str_to_int64(row[17]);
mr->Recycle = str_to_int64(row[18]);
mr->Slot = str_to_int64(row[19]);
bstrncpy(mr->cFirstWritten, (row[20] != NULL) ? row[20] : "", sizeof(mr->cFirstWritten));
mr->FirstWritten = (time_t)str_to_utime(mr->cFirstWritten);
bstrncpy(mr->cLastWritten, (row[21] != NULL) ? row[21] : "", sizeof(mr->cLastWritten));
mr->LastWritten = (time_t)str_to_utime(mr->cLastWritten);
mr->InChanger = str_to_uint64(row[22]);
mr->EndFile = str_to_uint64(row[23]);
mr->EndBlock = str_to_uint64(row[24]);
mr->LabelType = str_to_int64(row[25]);
bstrncpy(mr->cLabelDate, (row[26] != NULL) ? row[26] : "", sizeof(mr->cLabelDate));
mr->LabelDate = (time_t)str_to_utime(mr->cLabelDate);
mr->StorageId = str_to_int64(row[27]);
mr->Enabled = str_to_int64(row[28]);
mr->LocationId = str_to_int64(row[29]);
mr->RecycleCount = str_to_int64(row[30]);
bstrncpy(mr->cInitialWrite, (row[31] != NULL) ? row[31] : "", sizeof(mr->cInitialWrite));
mr->InitialWrite = (time_t)str_to_utime(mr->cInitialWrite);
mr->ScratchPoolId = str_to_int64(row[32]);
mr->RecyclePoolId = str_to_int64(row[33]);
mr->VolReadTime = str_to_int64(row[34]);
mr->VolWriteTime = str_to_int64(row[35]);
mr->ActionOnPurge = str_to_int64(row[36]);
bstrncpy(mr->EncrKey, (row[37] != NULL) ? row[37] : "", sizeof(mr->EncrKey));
mr->MinBlocksize = str_to_int32(row[38]);
mr->MaxBlocksize = str_to_int32(row[39]);
sql_free_result(mdb);
bail_out:
db_unlock(mdb);
Dmsg1(050, "Rtn numrows=%d\n", num_rows);
return num_rows;
}
#endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI */