2 Bacula® - The Network Backup Solution
4 Copyright (C) 2002-2008 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * This file contains all the SQL commands issued by the Director
32 * Kern Sibbald, July MMII
37 * Note, PostgreSQL imposes some constraints on using DISTINCT and GROUP BY
38 * for example, the following is illegal in PostgreSQL:
39 * SELECT DISTINCT JobId FROM temp ORDER BY StartTime ASC;
40 * because all the ORDER BY expressions must appear in the SELECT list!
47 const char *cleanup_created_job =
48 "UPDATE Job SET JobStatus='f', StartTime=SchedTime, EndTime=SchedTime "
49 "WHERE JobStatus = 'C'";
50 const char *cleanup_running_job =
51 "UPDATE Job SET JobStatus='f', EndTime=StartTime WHERE JobStatus = 'R'";
53 /* For sql_update.c db_update_stats */
54 const char *fill_jobhisto =
55 "INSERT INTO JobHisto ("
56 "JobId, Job, Name, Type, Level, ClientId, JobStatus, "
57 "SchedTime, StartTime, EndTime, RealEndTime, JobTDate, "
58 "VolSessionId, VolSessionTime, JobFiles, JobBytes, ReadBytes, "
59 "JobErrors, JobMissingFiles, PoolId, FileSetId, PriorJobId, "
60 "PurgedFiles, HasBase ) "
62 "JobId, Job, Name, Type, Level, ClientId, JobStatus, "
63 "SchedTime, StartTime, EndTime, RealEndTime, JobTDate, "
64 "VolSessionId, VolSessionTime, JobFiles, JobBytes, ReadBytes, "
65 "JobErrors, JobMissingFiles, PoolId, FileSetId, PriorJobId, "
66 "PurgedFiles, HasBase "
68 "WHERE JobStatus IN ('T','W','f','A','E') "
69 "AND JobId NOT IN (SELECT JobId FROM JobHisto) "
73 const char *list_pool = "SELECT * FROM Pool WHERE PoolId=%s";
75 /* For ua_dotcmds.c */
76 const char *client_backups =
77 "SELECT DISTINCT Job.JobId,Client.Name as Client,Level,StartTime,"
78 "JobFiles,JobBytes,VolumeName,MediaType,FileSet,Media.Enabled as Enabled"
79 " FROM Client,Job,JobMedia,Media,FileSet"
80 " WHERE Client.Name='%s'"
82 " AND Client.ClientId=Job.ClientId"
83 " AND JobStatus IN ('T','W') AND Type='B'"
84 " AND JobMedia.JobId=Job.JobId AND JobMedia.MediaId=Media.MediaId"
85 " AND Job.FileSetId=FileSet.FileSetId"
86 " ORDER BY Job.StartTime";
89 /* ====== ua_prune.c */
91 const char *del_File = "DELETE FROM File WHERE JobId=%s";
92 const char *upd_Purged = "UPDATE Job Set PurgedFiles=1 WHERE JobId=%s";
93 const char *cnt_DelCand = "SELECT count(*) FROM DelCandidates";
94 const char *del_Job = "DELETE FROM Job WHERE JobId=%s";
95 const char *del_JobMedia = "DELETE FROM JobMedia WHERE JobId=%s";
96 const char *cnt_JobMedia = "SELECT count(*) FROM JobMedia WHERE MediaId=%s";
98 const char *sel_JobMedia =
99 "SELECT DISTINCT JobMedia.JobId FROM JobMedia,Job "
100 "WHERE MediaId=%s AND Job.JobId=JobMedia.JobId "
101 "AND Job.JobTDate<%s";
103 /* Count Select JobIds for File deletion */
104 const char *count_select_job =
105 "SELECT count(*) from Job "
111 /* Select JobIds for File deletion. */
112 const char *select_job =
113 "SELECT DISTINCT JobId from Job "
118 /* Delete temp tables and indexes */
119 const char *drop_deltabs[] = {
120 "DROP TABLE DelCandidates",
123 const char *create_delindex = "CREATE INDEX DelInx1 ON DelCandidates (JobId)";
125 /* Fill candidates table with all Jobs subject to being deleted.
126 * This is used for pruning Jobs (first the files, then the Jobs).
128 const char *insert_delcand =
129 "INSERT INTO DelCandidates "
130 "SELECT JobId,PurgedFiles,FileSetId,JobFiles,JobStatus FROM Job "
136 * Select Jobs from the DelCandidates table that have a
137 * more recent backup -- i.e. are not the only backup.
138 * This is the list of Jobs to delete for a Backup Job.
139 * At the same time, we select "orphanned" jobs
140 * (i.e. no files, ...) for deletion.
143 const char *select_backup_del =
144 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
145 "FROM Job,DelCandidates "
146 "WHERE (Job.JobTDate<%s AND ((DelCandidates.JobFiles=0) OR "
147 "(DelCandidates.JobStatus NOT IN ('T','W')))) OR "
149 "AND Job.ClientId=%s "
150 "AND Job.Level='F' AND Job.JobStatus IN ('T','W') AND Job.Type IN ('B','M') "
151 "AND Job.FileSetId=DelCandidates.FileSetId)";
153 /* Select Jobs from the DelCandidates table that have a
154 * more recent InitCatalog -- i.e. are not the only InitCatalog
155 * This is the list of Jobs to delete for a Verify Job.
157 const char *select_verify_del =
158 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
159 "FROM Job,DelCandidates "
160 "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus NOT IN ('T','W')) OR "
162 "AND Job.ClientId=%s "
163 "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus IN ('T','W') "
164 "AND Job.FileSetId=DelCandidates.FileSetId)";
167 /* Select Jobs from the DelCandidates table.
168 * This is the list of Jobs to delete for a Restore Job.
170 const char *select_restore_del =
171 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
172 "FROM Job,DelCandidates "
173 "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus NOT IN ('T','W')) OR "
175 "AND Job.ClientId=%s "
178 /* Select Jobs from the DelCandidates table.
179 * This is the list of Jobs to delete for an Admin Job.
181 const char *select_admin_del =
182 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
183 "FROM Job,DelCandidates "
184 "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus NOT IN ('T','W')) OR "
186 "AND Job.ClientId=%s "
190 * Select Jobs from the DelCandidates table.
191 * This is the list of Jobs to delete for an Migrate Job.
193 const char *select_migrate_del =
194 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
195 "FROM Job,DelCandidates "
196 "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus NOT IN ('T','W')) OR "
198 "AND Job.ClientId=%s "
202 * Select Jobs from the DelCandidates table.
203 * This is the list of Jobs to delete for an Copy Job.
205 const char *select_copy_del =
206 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
207 "FROM Job,DelCandidates "
208 "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus NOT IN ('T','W')) OR "
210 "AND Job.ClientId=%s "
216 const char *select_backup_del =
217 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
218 "FROM Job,DelCandidates "
219 "WHERE (Job.JobId=DelCandidates.JobId AND ((DelCandidates.JobFiles=0) OR "
220 "(DelCandidates.JobStatus NOT IN ('T','W')))) OR "
222 "AND Job.ClientId=%s "
223 "AND Job.Level='F' AND Job.JobStatus IN ('T','W') AND Job.Type IN ('B','M') "
224 "AND Job.FileSetId=DelCandidates.FileSetId)";
226 /* Select Jobs from the DelCandidates table that have a
227 * more recent InitCatalog -- i.e. are not the only InitCatalog
228 * This is the list of Jobs to delete for a Verify Job.
230 const char *select_verify_del =
231 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
232 "FROM Job,DelCandidates "
233 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus NOT IN ('T','W')) OR "
235 "AND Job.ClientId=%s "
236 "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus IN ('T','W') "
237 "AND Job.FileSetId=DelCandidates.FileSetId)";
240 /* Select Jobs from the DelCandidates table.
241 * This is the list of Jobs to delete for a Restore Job.
243 const char *select_restore_del =
244 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
245 "FROM Job,DelCandidates "
246 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus NOT IN ('T','W')) OR "
248 "AND Job.ClientId=%s "
251 /* Select Jobs from the DelCandidates table.
252 * This is the list of Jobs to delete for an Admin Job.
254 const char *select_admin_del =
255 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
256 "FROM Job,DelCandidates "
257 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus NOT IN ('T','W')) OR "
259 "AND Job.ClientId=%s "
263 * Select Jobs from the DelCandidates table.
264 * This is the list of Jobs to delete for an Migrate Job.
266 const char *select_migrate_del =
267 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
268 "FROM Job,DelCandidates "
269 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus NOT IN ('T','W')) OR "
271 "AND Job.ClientId=%s "
274 const char *select_copy_del =
275 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
276 "FROM Job,DelCandidates "
277 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus NOT IN ('T','W')) OR "
279 "AND Job.ClientId=%s "
285 /* ======= ua_restore.c */
286 const char *uar_count_files =
287 "SELECT JobFiles FROM Job WHERE JobId=%s";
289 /* List last 20 Jobs */
290 const char *uar_list_jobs =
291 "SELECT JobId,Client.Name as Client,StartTime,Level as "
292 "JobLevel,JobFiles,JobBytes "
293 "FROM Client,Job WHERE Client.ClientId=Job.ClientId AND JobStatus IN ('T','W') "
294 "AND Type='B' ORDER BY StartTime DESC LIMIT 20";
296 const char *uar_print_jobs =
297 "SELECT DISTINCT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName"
298 " FROM Job JOIN JobMedia USING (JobId) JOIN Media USING (MediaId) "
299 " WHERE JobId IN (%s) "
300 " ORDER BY StartTime ASC";
303 * Find all files for a particular JobId and insert them into
304 * the tree during a restore.
306 const char *uar_sel_files =
307 "SELECT Path.Path,Filename.Name,FileIndex,JobId,LStat "
308 "FROM File,Filename,Path "
309 "WHERE File.JobId IN (%s) AND Filename.FilenameId=File.FilenameId "
310 "AND Path.PathId=File.PathId";
312 const char *uar_del_temp = "DROP TABLE temp";
313 const char *uar_del_temp1 = "DROP TABLE temp1";
315 const char *uar_last_full =
316 "INSERT INTO temp1 SELECT Job.JobId,JobTdate "
317 "FROM Client,Job,JobMedia,Media,FileSet WHERE Client.ClientId=%s "
318 "AND Job.ClientId=%s "
319 "AND Job.StartTime < '%s' "
320 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
321 "AND JobMedia.JobId=Job.JobId "
322 "AND Media.Enabled=1 "
323 "AND JobMedia.MediaId=Media.MediaId "
324 "AND Job.FileSetId=FileSet.FileSetId "
325 "AND FileSet.FileSet='%s' "
327 "ORDER BY Job.JobTDate DESC LIMIT 1";
329 const char *uar_full =
330 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,"
331 "Job.ClientId,Job.Level,Job.JobFiles,Job.JobBytes,"
332 "StartTime,VolumeName,JobMedia.StartFile,VolSessionId,VolSessionTime "
333 "FROM temp1,Job,JobMedia,Media WHERE temp1.JobId=Job.JobId "
334 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
335 "AND Media.Enabled=1 "
336 "AND JobMedia.JobId=Job.JobId "
337 "AND JobMedia.MediaId=Media.MediaId";
339 const char *uar_dif =
340 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
341 "Job.Level,Job.JobFiles,Job.JobBytes,"
342 "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
343 "Job.VolSessionId,Job.VolSessionTime "
344 "FROM Job,JobMedia,Media,FileSet "
345 "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
346 "AND Job.ClientId=%s "
347 "AND JobMedia.JobId=Job.JobId "
348 "AND Media.Enabled=1 "
349 "AND JobMedia.MediaId=Media.MediaId "
350 "AND Job.Level='D' AND JobStatus IN ('T','W') AND Type='B' "
351 "AND Job.FileSetId=FileSet.FileSetId "
352 "AND FileSet.FileSet='%s' "
354 "ORDER BY Job.JobTDate DESC LIMIT 1";
356 const char *uar_inc =
357 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
358 "Job.Level,Job.JobFiles,Job.JobBytes,"
359 "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
360 "Job.VolSessionId,Job.VolSessionTime "
361 "FROM Job,JobMedia,Media,FileSet "
362 "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
363 "AND Job.ClientId=%s "
364 "AND Media.Enabled=1 "
365 "AND JobMedia.JobId=Job.JobId "
366 "AND JobMedia.MediaId=Media.MediaId "
367 "AND Job.Level='I' AND JobStatus IN ('T','W') AND Type='B' "
368 "AND Job.FileSetId=FileSet.FileSetId "
369 "AND FileSet.FileSet='%s' "
372 const char *uar_list_temp =
373 "SELECT DISTINCT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName"
375 " ORDER BY StartTime ASC";
378 const char *uar_sel_jobid_temp =
379 "SELECT DISTINCT JobId,StartTime FROM temp ORDER BY StartTime ASC";
381 const char *uar_sel_all_temp1 = "SELECT * FROM temp1";
383 const char *uar_sel_all_temp = "SELECT * FROM temp";
387 /* Select FileSet names for this Client */
388 const char *uar_sel_fileset =
389 "SELECT DISTINCT FileSet.FileSet FROM Job,"
390 "Client,FileSet WHERE Job.FileSetId=FileSet.FileSetId "
391 "AND Job.ClientId=%s AND Client.ClientId=%s "
392 "ORDER BY FileSet.FileSet";
394 /* Find MediaType used by this Job */
395 const char *uar_mediatype =
396 "SELECT MediaType FROM JobMedia,Media WHERE JobMedia.JobId=%s "
397 "AND JobMedia.MediaId=Media.MediaId";
400 * Find JobId, FileIndex for a given path/file and date
401 * for use when inserting individual files into the tree.
403 const char *uar_jobid_fileindex =
404 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
405 "WHERE Job.JobId=File.JobId "
406 "AND Job.StartTime<='%s' "
407 "AND Path.Path='%s' "
408 "AND Filename.Name='%s' "
409 "AND Client.Name='%s' "
410 "AND Job.ClientId=Client.ClientId "
411 "AND Path.PathId=File.PathId "
412 "AND Filename.FilenameId=File.FilenameId "
413 "AND JobStatus IN ('T','W') AND Type='B' "
414 "ORDER BY Job.StartTime DESC LIMIT 1";
416 const char *uar_jobids_fileindex =
417 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
418 "WHERE Job.JobId IN (%s) "
419 "AND Job.JobId=File.JobId "
420 "AND Job.StartTime<='%s' "
421 "AND Path.Path='%s' "
422 "AND Filename.Name='%s' "
423 "AND Client.Name='%s' "
424 "AND Job.ClientId=Client.ClientId "
425 "AND Path.PathId=File.PathId "
426 "AND Filename.FilenameId=File.FilenameId "
427 "ORDER BY Job.StartTime DESC LIMIT 1";
429 /* Query to get list of files from table -- presuably built by an external program */
430 const char *uar_jobid_fileindex_from_table =
431 "SELECT JobId,FileIndex from %s";
436 * This file contains all the SQL commands issued by the Director
438 * Kern Sibbald, July MMII
443 * Note, PostgreSQL imposes some constraints on using DISTINCT and GROUP BY
444 * for example, the following is illegal in PostgreSQL:
445 * SELECT DISTINCT JobId FROM temp ORDER BY StartTime ASC;
446 * because all the ORDER BY expressions must appear in the SELECT list!
453 /* Get the list of the last recent version with a given jobid list
454 * This is a tricky part because with SQL the result of
456 * SELECT MAX(A), B, C, D FROM... GROUP BY (B,C)
458 * doesn't give the good result (for D).
460 * With PostgreSQL, we can use DISTINCT ON(), but with Mysql or Sqlite,
461 * we need an extra join using JobTDate.
463 const char *select_recent_version_with_basejob[4] = {
465 "SELECT FileId, Job.JobId AS JobId, FileIndex, File.PathId AS PathId, "
466 "File.FilenameId AS FilenameId, LStat, MD5 "
468 "SELECT MAX(JobTDate) AS JobTDate, PathId, FilenameId "
470 "SELECT JobTDate, PathId, FilenameId " /* Get all normal files */
471 "FROM File JOIN Job USING (JobId) " /* from selected backup */
472 "WHERE JobId IN (%s) "
474 "SELECT JobTDate, PathId, FilenameId " /* Get all files from */
475 "FROM BaseFiles " /* BaseJob */
476 "JOIN File USING (FileId) "
477 "JOIN Job ON (BaseJobId = Job.JobId) "
478 "WHERE BaseFiles.JobId IN (%s) " /* Use Max(JobTDate) to find */
479 ") AS tmp GROUP BY PathId, FilenameId " /* the latest file version */
481 "WHERE (Job.JobId IN ( " /* Security, we force JobId to be valid */
482 "SELECT DISTINCT BaseJobId FROM BaseFiles WHERE JobId IN (%s)) "
483 "OR Job.JobId IN (%s)) "
484 "AND T1.JobTDate = Job.JobTDate " /* Join on JobTDate to get the orginal */
485 "AND Job.JobId = File.JobId " /* Job/File record */
486 "AND T1.PathId = File.PathId "
487 "AND T1.FilenameId = File.FilenameId",
489 /* Postgresql */ /* The DISTINCT ON () permits to avoid extra join */
490 "SELECT DISTINCT ON (FilenameId, PathId) StartTime, JobId, FileId, "
491 "FileIndex, PathId, FilenameId, LStat, MD5 "
493 "(SELECT FileId, JobId, PathId, FilenameId, FileIndex, LStat, MD5 "
494 "FROM File WHERE JobId IN (%s) "
496 "SELECT File.FileId, File.JobId, PathId, FilenameId, "
497 "File.FileIndex, LStat, MD5 "
498 "FROM BaseFiles JOIN File USING (FileId) "
499 "WHERE BaseFiles.JobId IN (%s) "
500 ") AS T JOIN Job USING (JobId) "
501 "ORDER BY FilenameId, PathId, StartTime DESC ",
503 /* SQLite */ /* See Mysql section for doc */
504 "SELECT FileId, Job.JobId AS JobId, FileIndex, File.PathId AS PathId, "
505 "File.FilenameId AS FilenameId, LStat, MD5 "
507 "SELECT MAX(JobTDate) AS JobTDate, PathId, FilenameId "
509 "SELECT JobTDate, PathId, FilenameId "
510 "FROM File JOIN Job USING (JobId) "
511 "WHERE JobId IN (%s) "
513 "SELECT JobTDate, PathId, FilenameId "
515 "JOIN File USING (FileId) "
516 "JOIN Job ON (BaseJobId = Job.JobId) "
517 "WHERE BaseFiles.JobId IN (%s) "
518 ") AS tmp GROUP BY PathId, FilenameId "
520 "WHERE (Job.JobId IN ( "
521 "SELECT DISTINCT BaseJobId FROM BaseFiles WHERE JobId IN (%s)) "
522 "OR Job.JobId IN (%s)) "
523 "AND T1.JobTDate = Job.JobTDate "
524 "AND Job.JobId = File.JobId "
525 "AND T1.PathId = File.PathId "
526 "AND T1.FilenameId = File.FilenameId",
528 /* SQLite3 */ /* See Mysql section for doc */
529 "SELECT FileId, Job.JobId AS JobId, FileIndex, File.PathId AS PathId, "
530 "File.FilenameId AS FilenameId, LStat, MD5 "
532 "SELECT MAX(JobTDate) AS JobTDate, PathId, FilenameId "
534 "SELECT JobTDate, PathId, FilenameId "
535 "FROM File JOIN Job USING (JobId) "
536 "WHERE JobId IN (%s) "
538 "SELECT JobTDate, PathId, FilenameId "
540 "JOIN File USING (FileId) "
541 "JOIN Job ON (BaseJobId = Job.JobId) "
542 "WHERE BaseFiles.JobId IN (%s) "
543 ") AS tmp GROUP BY PathId, FilenameId "
545 "WHERE (Job.JobId IN ( "
546 "SELECT DISTINCT BaseJobId FROM BaseFiles WHERE JobId IN (%s)) "
547 "OR Job.JobId IN (%s)) "
548 "AND T1.JobTDate = Job.JobTDate "
549 "AND Job.JobId = File.JobId "
550 "AND T1.PathId = File.PathId "
551 "AND T1.FilenameId = File.FilenameId"
554 /* Get the list of the last recent version with a given BaseJob jobid list */
555 const char *select_recent_version[4] = {
557 "SELECT j1.JobId AS JobId, f1.FileId AS FileId, f1.FileIndex AS FileIndex, "
558 "f1.PathId AS PathId, f1.FilenameId AS FilenameId, "
559 "f1.LStat AS LStat, f1.MD5 AS MD5 "
560 "FROM ( " /* Choose the last version for each Path/Filename */
561 "SELECT max(JobTDate) AS JobTDate, PathId, FilenameId "
562 "FROM File JOIN Job USING (JobId) "
563 "WHERE JobId IN (%s) "
564 "GROUP BY PathId, FilenameId "
565 ") AS t1, Job AS j1, File AS f1 "
566 "WHERE t1.JobTDate = j1.JobTDate "
567 "AND j1.JobId IN (%s) "
568 "AND t1.FilenameId = f1.FilenameId "
569 "AND t1.PathId = f1.PathId "
570 "AND j1.JobId = f1.JobId",
573 "SELECT DISTINCT ON (FilenameId, PathId) StartTime, JobId, FileId, "
574 "FileIndex, PathId, FilenameId, LStat, MD5 "
575 "FROM File JOIN Job USING (JobId) "
576 "WHERE JobId IN (%s) "
577 "ORDER BY FilenameId, PathId, StartTime DESC ",
580 "SELECT j1.JobId AS JobId, f1.FileId AS FileId, f1.FileIndex AS FileIndex, "
581 "f1.PathId AS PathId, f1.FilenameId AS FilenameId, "
582 "f1.LStat AS LStat, f1.MD5 AS MD5 "
584 "SELECT max(JobTDate) AS JobTDate, PathId, FilenameId "
585 "FROM File JOIN Job USING (JobId) "
586 "WHERE JobId IN (%s) "
587 "GROUP BY PathId, FilenameId "
588 ") AS t1, Job AS j1, File AS f1 "
589 "WHERE t1.JobTDate = j1.JobTDate "
590 "AND j1.JobId IN (%s) "
591 "AND t1.FilenameId = f1.FilenameId "
592 "AND t1.PathId = f1.PathId "
593 "AND j1.JobId = f1.JobId",
596 "SELECT j1.JobId AS JobId, f1.FileId AS FileId, f1.FileIndex AS FileIndex, "
597 "f1.PathId AS PathId, f1.FilenameId AS FilenameId, "
598 "f1.LStat AS LStat, f1.MD5 AS MD5 "
600 "SELECT max(JobTDate) AS JobTDate, PathId, FilenameId "
601 "FROM File JOIN Job USING (JobId) "
602 "WHERE JobId IN (%s) "
603 "GROUP BY PathId, FilenameId "
604 ") AS t1, Job AS j1, File AS f1 "
605 "WHERE t1.JobTDate = j1.JobTDate "
606 "AND j1.JobId IN (%s) "
607 "AND t1.FilenameId = f1.FilenameId "
608 "AND t1.PathId = f1.PathId "
609 "AND j1.JobId = f1.JobId"
612 /* ====== ua_prune.c */
614 /* List of SQL commands to create temp table and indicies */
615 const char *create_deltabs[4] = {
617 "CREATE TEMPORARY TABLE DelCandidates ("
618 "JobId INTEGER UNSIGNED NOT NULL, "
619 "PurgedFiles TINYINT, "
620 "FileSetId INTEGER UNSIGNED, "
621 "JobFiles INTEGER UNSIGNED, "
622 "JobStatus BINARY(1))",
624 "CREATE TEMPORARY TABLE DelCandidates ("
625 "JobId INTEGER NOT NULL, "
626 "PurgedFiles SMALLINT, "
627 "FileSetId INTEGER, "
629 "JobStatus char(1))",
631 "CREATE TEMPORARY TABLE DelCandidates ("
632 "JobId INTEGER UNSIGNED NOT NULL, "
633 "PurgedFiles TINYINT, "
634 "FileSetId INTEGER UNSIGNED, "
635 "JobFiles INTEGER UNSIGNED, "
638 "CREATE TEMPORARY TABLE DelCandidates ("
639 "JobId INTEGER UNSIGNED NOT NULL, "
640 "PurgedFiles TINYINT, "
641 "FileSetId INTEGER UNSIGNED, "
642 "JobFiles INTEGER UNSIGNED, "
645 /* ======= ua_restore.c */
647 /* List Jobs where a particular file is saved */
648 const char *uar_file[4] = {
650 "SELECT Job.JobId as JobId,"
651 "CONCAT(Path.Path,Filename.Name) as Name, "
652 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
653 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
654 "AND Client.ClientId=Job.ClientId "
655 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
656 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
657 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
659 "SELECT Job.JobId as JobId,"
660 "Path.Path||Filename.Name as Name, "
661 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
662 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
663 "AND Client.ClientId=Job.ClientId "
664 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
665 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
666 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
668 "SELECT Job.JobId as JobId,"
669 "Path.Path||Filename.Name as Name, "
670 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
671 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
672 "AND Client.ClientId=Job.ClientId "
673 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
674 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
675 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
677 "SELECT Job.JobId as JobId,"
678 "Path.Path||Filename.Name as Name, "
679 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
680 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
681 "AND Client.ClientId=Job.ClientId "
682 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
683 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
684 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20"};
686 const char *uar_create_temp[4] = {
688 "CREATE TEMPORARY TABLE temp ("
689 "JobId INTEGER UNSIGNED NOT NULL,"
690 "JobTDate BIGINT UNSIGNED,"
691 "ClientId INTEGER UNSIGNED,"
693 "JobFiles INTEGER UNSIGNED,"
694 "JobBytes BIGINT UNSIGNED,"
697 "StartFile INTEGER UNSIGNED,"
698 "VolSessionId INTEGER UNSIGNED,"
699 "VolSessionTime INTEGER UNSIGNED)",
701 "CREATE TEMPORARY TABLE temp ("
702 "JobId INTEGER NOT NULL,"
711 "VolSessionId INTEGER,"
712 "VolSessionTime INTEGER)",
714 "CREATE TEMPORARY TABLE temp ("
715 "JobId INTEGER UNSIGNED NOT NULL,"
716 "JobTDate BIGINT UNSIGNED,"
717 "ClientId INTEGER UNSIGNED,"
719 "JobFiles INTEGER UNSIGNED,"
720 "JobBytes BIGINT UNSIGNED,"
723 "StartFile INTEGER UNSIGNED,"
724 "VolSessionId INTEGER UNSIGNED,"
725 "VolSessionTime INTEGER UNSIGNED)",
727 "CREATE TEMPORARY TABLE temp ("
728 "JobId INTEGER UNSIGNED NOT NULL,"
729 "JobTDate BIGINT UNSIGNED,"
730 "ClientId INTEGER UNSIGNED,"
732 "JobFiles INTEGER UNSIGNED,"
733 "JobBytes BIGINT UNSIGNED,"
736 "StartFile INTEGER UNSIGNED,"
737 "VolSessionId INTEGER UNSIGNED,"
738 "VolSessionTime INTEGER UNSIGNED)"};
740 const char *uar_create_temp1[4] = {
742 "CREATE TEMPORARY TABLE temp1 ("
743 "JobId INTEGER UNSIGNED NOT NULL,"
744 "JobTDate BIGINT UNSIGNED)",
746 "CREATE TEMPORARY TABLE temp1 ("
747 "JobId INTEGER NOT NULL,"
750 "CREATE TEMPORARY TABLE temp1 ("
751 "JobId INTEGER UNSIGNED NOT NULL,"
752 "JobTDate BIGINT UNSIGNED)",
754 "CREATE TEMPORARY TABLE temp1 ("
755 "JobId INTEGER UNSIGNED NOT NULL,"
756 "JobTDate BIGINT UNSIGNED)"};
758 /* Query to get all files in a directory -- no recursing
759 * Note, for PostgreSQL since it respects the "Single Value
760 * rule", the results of the SELECT will be unoptimized.
761 * I.e. the same file will be restored multiple times, once
762 * for each time it was backed up.
765 const char *uar_jobid_fileindex_from_dir[4] = {
767 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
768 "WHERE Job.JobId IN (%s) "
769 "AND Job.JobId=File.JobId "
770 "AND Path.Path='%s' "
771 "AND Client.Name='%s' "
772 "AND Job.ClientId=Client.ClientId "
773 "AND Path.PathId=File.Pathid "
774 "AND Filename.FilenameId=File.FilenameId "
775 "GROUP BY File.FileIndex ",
777 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
778 "WHERE Job.JobId IN (%s) "
779 "AND Job.JobId=File.JobId "
780 "AND Path.Path='%s' "
781 "AND Client.Name='%s' "
782 "AND Job.ClientId=Client.ClientId "
783 "AND Path.PathId=File.Pathid "
784 "AND Filename.FilenameId=File.FilenameId",
786 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
787 "WHERE Job.JobId IN (%s) "
788 "AND Job.JobId=File.JobId "
789 "AND Path.Path='%s' "
790 "AND Client.Name='%s' "
791 "AND Job.ClientId=Client.ClientId "
792 "AND Path.PathId=File.Pathid "
793 "AND Filename.FilenameId=File.FilenameId "
794 "GROUP BY File.FileIndex ",
796 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
797 "WHERE Job.JobId IN (%s) "
798 "AND Job.JobId=File.JobId "
799 "AND Path.Path='%s' "
800 "AND Client.Name='%s' "
801 "AND Job.ClientId=Client.ClientId "
802 "AND Path.PathId=File.Pathid "
803 "AND Filename.FilenameId=File.FilenameId "
804 "GROUP BY File.FileIndex "};