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 three of the GNU Affero 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 Affero 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.
29 * This file contains all the SQL commands that are either issued by the
30 * Director or which are database backend specific.
32 * Kern Sibbald, July MMII
35 * Note, PostgreSQL imposes some constraints on using DISTINCT and GROUP BY
36 * for example, the following is illegal in PostgreSQL:
37 * SELECT DISTINCT JobId FROM temp ORDER BY StartTime ASC;
38 * because all the ORDER BY expressions must appear in the SELECT list!
44 const char *cleanup_created_job =
45 "UPDATE Job SET JobStatus='f', StartTime=SchedTime, EndTime=SchedTime "
46 "WHERE JobStatus = 'C'";
47 const char *cleanup_running_job =
48 "UPDATE Job SET JobStatus='f', EndTime=StartTime WHERE JobStatus = 'R'";
50 /* For sql_update.c db_update_stats */
51 const char *fill_jobhisto =
52 "INSERT INTO JobHisto ("
53 "JobId, Job, Name, Type, Level, ClientId, JobStatus, "
54 "SchedTime, StartTime, EndTime, RealEndTime, JobTDate, "
55 "VolSessionId, VolSessionTime, JobFiles, JobBytes, ReadBytes, "
56 "JobErrors, JobMissingFiles, PoolId, FileSetId, PriorJobId, "
57 "PurgedFiles, HasBase, Reviewed, Comment ) "
59 "JobId, Job, Name, Type, Level, ClientId, JobStatus, "
60 "SchedTime, StartTime, EndTime, RealEndTime, JobTDate, "
61 "VolSessionId, VolSessionTime, JobFiles, JobBytes, ReadBytes, "
62 "JobErrors, JobMissingFiles, PoolId, FileSetId, PriorJobId, "
63 "PurgedFiles, HasBase, Reviewed, Comment "
65 "WHERE JobStatus IN ('T','W','f','A','E') "
67 "(SELECT JobHisto.JobId "
68 "FROM JobHisto WHERE JobHisto.Jobid=Job.JobId) "
72 const char *list_pool = "SELECT * FROM Pool WHERE PoolId=%s";
74 /* For ua_dotcmds.c */
75 const char *client_backups =
76 "SELECT DISTINCT Job.JobId,Client.Name as Client,Level,StartTime,"
77 "JobFiles,JobBytes,VolumeName,MediaType,FileSet,Media.Enabled as Enabled"
78 " FROM Client,Job,JobMedia,Media,FileSet"
79 " WHERE Client.Name='%s'"
81 " AND Client.ClientId=Job.ClientId"
82 " AND JobStatus IN ('T','W') AND Type='B'"
83 " AND JobMedia.JobId=Job.JobId AND JobMedia.MediaId=Media.MediaId"
84 " AND Job.FileSetId=FileSet.FileSetId"
85 " ORDER BY Job.StartTime";
88 /* ====== ua_prune.c */
90 const char *del_File = "DELETE FROM File WHERE JobId=%s";
91 const char *upd_Purged = "UPDATE Job Set PurgedFiles=1 WHERE JobId=%s";
92 const char *cnt_DelCand = "SELECT count(*) FROM DelCandidates";
93 const char *del_Job = "DELETE FROM Job WHERE JobId=%s";
94 const char *del_JobMedia = "DELETE FROM JobMedia WHERE JobId=%s";
95 const char *cnt_JobMedia = "SELECT count(*) FROM JobMedia WHERE MediaId=%s";
97 const char *sel_JobMedia =
98 "SELECT DISTINCT JobMedia.JobId FROM JobMedia,Job "
99 "WHERE MediaId=%s AND Job.JobId=JobMedia.JobId "
100 "AND Job.JobTDate<%s";
102 /* Count Select JobIds for File deletion */
103 const char *count_select_job =
104 "SELECT count(*) from Job "
110 /* Select JobIds for File deletion. */
111 const char *select_job =
112 "SELECT DISTINCT JobId from Job "
117 /* Delete temp tables and indexes */
118 const char *drop_deltabs[] = {
119 "DROP TABLE DelCandidates",
122 const char *create_delindex = "CREATE INDEX DelInx1 ON DelCandidates (JobId)";
124 /* ======= ua_restore.c */
125 const char *uar_count_files =
126 "SELECT JobFiles FROM Job WHERE JobId=%s";
128 /* List last 20 Jobs */
129 const char *uar_list_jobs =
130 "SELECT JobId,Client.Name as Client,StartTime,Level as "
131 "JobLevel,JobFiles,JobBytes "
132 "FROM Client,Job WHERE Client.ClientId=Job.ClientId AND JobStatus IN ('T','W') "
133 "AND Type='B' ORDER BY StartTime DESC LIMIT 20";
135 const char *uar_print_jobs =
136 "SELECT DISTINCT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName"
137 " FROM Job JOIN JobMedia USING (JobId) JOIN Media USING (MediaId) "
138 " WHERE JobId IN (%s) "
139 " ORDER BY StartTime ASC";
142 * Find all files for a particular JobId and insert them into
143 * the tree during a restore.
145 const char *uar_sel_files =
146 "SELECT Path.Path,Filename.Name,FileIndex,JobId,LStat "
147 "FROM File,Filename,Path "
148 "WHERE File.JobId IN (%s) AND Filename.FilenameId=File.FilenameId "
149 "AND Path.PathId=File.PathId";
151 const char *uar_del_temp = "DROP TABLE temp";
152 const char *uar_del_temp1 = "DROP TABLE temp1";
154 const char *uar_last_full =
155 "INSERT INTO temp1 SELECT Job.JobId,JobTdate "
156 "FROM Client,Job,JobMedia,Media,FileSet WHERE Client.ClientId=%s "
157 "AND Job.ClientId=%s "
158 "AND Job.StartTime < '%s' "
159 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
160 "AND JobMedia.JobId=Job.JobId "
161 "AND Media.Enabled=1 "
162 "AND JobMedia.MediaId=Media.MediaId "
163 "AND Job.FileSetId=FileSet.FileSetId "
164 "AND FileSet.FileSet='%s' "
166 "ORDER BY Job.JobTDate DESC LIMIT 1";
168 const char *uar_full =
169 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,"
170 "Job.ClientId,Job.Level,Job.JobFiles,Job.JobBytes,"
171 "StartTime,VolumeName,JobMedia.StartFile,VolSessionId,VolSessionTime "
172 "FROM temp1,Job,JobMedia,Media WHERE temp1.JobId=Job.JobId "
173 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
174 "AND Media.Enabled=1 "
175 "AND JobMedia.JobId=Job.JobId "
176 "AND JobMedia.MediaId=Media.MediaId";
178 const char *uar_dif =
179 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
180 "Job.Level,Job.JobFiles,Job.JobBytes,"
181 "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
182 "Job.VolSessionId,Job.VolSessionTime "
183 "FROM Job,JobMedia,Media,FileSet "
184 "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
185 "AND Job.ClientId=%s "
186 "AND JobMedia.JobId=Job.JobId "
187 "AND Media.Enabled=1 "
188 "AND JobMedia.MediaId=Media.MediaId "
189 "AND Job.Level='D' AND JobStatus IN ('T','W') AND Type='B' "
190 "AND Job.FileSetId=FileSet.FileSetId "
191 "AND FileSet.FileSet='%s' "
193 "ORDER BY Job.JobTDate DESC LIMIT 1";
195 const char *uar_inc =
196 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
197 "Job.Level,Job.JobFiles,Job.JobBytes,"
198 "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
199 "Job.VolSessionId,Job.VolSessionTime "
200 "FROM Job,JobMedia,Media,FileSet "
201 "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
202 "AND Job.ClientId=%s "
203 "AND Media.Enabled=1 "
204 "AND JobMedia.JobId=Job.JobId "
205 "AND JobMedia.MediaId=Media.MediaId "
206 "AND Job.Level='I' AND JobStatus IN ('T','W') AND Type='B' "
207 "AND Job.FileSetId=FileSet.FileSetId "
208 "AND FileSet.FileSet='%s' "
211 const char *uar_list_temp =
212 "SELECT DISTINCT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName"
214 " ORDER BY StartTime ASC";
217 const char *uar_sel_jobid_temp =
218 "SELECT DISTINCT JobId,StartTime FROM temp ORDER BY StartTime ASC";
220 const char *uar_sel_all_temp1 = "SELECT * FROM temp1";
222 const char *uar_sel_all_temp = "SELECT * FROM temp";
226 /* Select FileSet names for this Client */
227 const char *uar_sel_fileset =
228 "SELECT DISTINCT FileSet.FileSet FROM Job,"
229 "Client,FileSet WHERE Job.FileSetId=FileSet.FileSetId "
230 "AND Job.ClientId=%s AND Client.ClientId=%s "
231 "ORDER BY FileSet.FileSet";
233 /* Select all different FileSet for this client
234 * This query doesn't guarantee that the Id is the latest
235 * version of the FileSet. Can be used with other queries that
236 * use Ids to select the FileSet name. (like in accurate)
238 const char *uar_sel_filesetid =
239 "SELECT MAX(FileSetId) "
240 "FROM FileSet JOIN Job USING (FileSetId) "
241 "WHERE Job.ClientId=%s "
244 /* Find MediaType used by this Job */
245 const char *uar_mediatype =
246 "SELECT MediaType FROM JobMedia,Media WHERE JobMedia.JobId=%s "
247 "AND JobMedia.MediaId=Media.MediaId";
250 * Find JobId, FileIndex for a given path/file and date
251 * for use when inserting individual files into the tree.
253 const char *uar_jobid_fileindex =
254 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
255 "WHERE Job.JobId=File.JobId "
256 "AND Job.StartTime<='%s' "
257 "AND Path.Path='%s' "
258 "AND Filename.Name='%s' "
259 "AND Client.Name='%s' "
260 "AND Job.ClientId=Client.ClientId "
261 "AND Path.PathId=File.PathId "
262 "AND Filename.FilenameId=File.FilenameId "
263 "AND JobStatus IN ('T','W') AND Type='B' "
264 "ORDER BY Job.StartTime DESC LIMIT 1";
266 const char *uar_jobids_fileindex =
267 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
268 "WHERE Job.JobId IN (%s) "
269 "AND Job.JobId=File.JobId "
270 "AND Job.StartTime<='%s' "
271 "AND Path.Path='%s' "
272 "AND Filename.Name='%s' "
273 "AND Client.Name='%s' "
274 "AND Job.ClientId=Client.ClientId "
275 "AND Path.PathId=File.PathId "
276 "AND Filename.FilenameId=File.FilenameId "
277 "ORDER BY Job.StartTime DESC LIMIT 1";
279 /* Query to get list of files from table -- presuably built by an external program */
280 const char *uar_jobid_fileindex_from_table =
281 "SELECT JobId,FileIndex from %s";
283 /* Get the list of the last recent version per Delta with a given jobid list
284 * This is a tricky part because with SQL the result of
286 * SELECT MAX(A), B, C, D FROM... GROUP BY (B,C)
288 * doesn't give the good result (for D).
290 * With PostgreSQL, we can use DISTINCT ON(), but with Mysql or Sqlite,
291 * we need an extra join using JobTDate.
293 const char *select_recent_version_with_basejob_default =
294 "SELECT FileId, Job.JobId AS JobId, FileIndex, File.PathId AS PathId, "
295 "File.FilenameId AS FilenameId, LStat, MD5, MarkId, "
296 "Job.JobTDate AS JobTDate "
298 "SELECT MAX(JobTDate) AS JobTDate, PathId, FilenameId "
300 "SELECT JobTDate, PathId, FilenameId " /* Get all normal files */
301 "FROM File JOIN Job USING (JobId) " /* from selected backup */
302 "WHERE File.JobId IN (%s) "
304 "SELECT JobTDate, PathId, FilenameId " /* Get all files from */
305 "FROM BaseFiles " /* BaseJob */
306 "JOIN File USING (FileId) "
307 "JOIN Job ON (BaseJobId = Job.JobId) "
308 "WHERE BaseFiles.JobId IN (%s) " /* Use Max(JobTDate) to find */
310 "GROUP BY PathId, FilenameId " /* the latest file version */
312 "WHERE (Job.JobId IN ( " /* Security, we force JobId to be valid */
313 "SELECT DISTINCT BaseJobId FROM BaseFiles WHERE JobId IN (%s)) "
314 "OR Job.JobId IN (%s)) "
315 "AND T1.JobTDate = Job.JobTDate " /* Join on JobTDate to get the orginal */
316 "AND Job.JobId = File.JobId " /* Job/File record */
317 "AND T1.PathId = File.PathId "
318 "AND T1.FilenameId = File.FilenameId";
320 const char *select_recent_version_with_basejob[5] = {
322 select_recent_version_with_basejob_default,
324 /* Postgresql */ /* The DISTINCT ON () permits to avoid extra join */
325 "SELECT DISTINCT ON (FilenameId, PathId) JobTDate, JobId, FileId, "
326 "FileIndex, PathId, FilenameId, LStat, MD5, MarkId "
328 "(SELECT FileId, JobId, PathId, FilenameId, FileIndex, LStat, MD5, MarkId "
329 "FROM File WHERE JobId IN (%s) "
331 "SELECT File.FileId, File.JobId, PathId, FilenameId, "
332 "File.FileIndex, LStat, MD5, MarkId "
333 "FROM BaseFiles JOIN File USING (FileId) "
334 "WHERE BaseFiles.JobId IN (%s) "
335 ") AS T JOIN Job USING (JobId) "
336 "ORDER BY FilenameId, PathId, JobTDate DESC ",
339 select_recent_version_with_basejob_default,
342 select_recent_version_with_basejob_default,
345 select_recent_version_with_basejob_default
348 /* We do the same thing than the previous query, but we include
349 * all delta parts. If the file has been deleted, we can have irrelevant
352 * The code that uses results should control the delta sequence with
353 * the following rules:
355 * Delta = Previous Delta + 1
357 * If we detect a gap, we can discard further pieces
358 * If a file starts at 1 instead of 0, the file has been deleted, and further
359 * pieces are useless.
361 * This control should be reset for each new file
363 const char *select_recent_version_with_basejob_and_delta_default =
364 "SELECT FileId, Job.JobId AS JobId, FileIndex, File.PathId AS PathId, "
365 "File.FilenameId AS FilenameId, LStat, MD5, MarkId, "
366 "Job.JobTDate AS JobTDate "
368 "SELECT MAX(JobTDate) AS JobTDate, PathId, FilenameId, MarkId "
370 "SELECT JobTDate, PathId, FilenameId, MarkId " /* Get all normal files */
371 "FROM File JOIN Job USING (JobId) " /* from selected backup */
372 "WHERE File.JobId IN (%s) "
374 "SELECT JobTDate, PathId, FilenameId, MarkId " /* Get all files from */
375 "FROM BaseFiles " /* BaseJob */
376 "JOIN File USING (FileId) "
377 "JOIN Job ON (BaseJobId = Job.JobId) "
378 "WHERE BaseFiles.JobId IN (%s) " /* Use Max(JobTDate) to find */
380 "GROUP BY PathId, FilenameId, MarkId " /* the latest file version */
382 "WHERE (Job.JobId IN ( " /* Security, we force JobId to be valid */
383 "SELECT DISTINCT BaseJobId FROM BaseFiles WHERE JobId IN (%s)) "
384 "OR Job.JobId IN (%s)) "
385 "AND T1.JobTDate = Job.JobTDate " /* Join on JobTDate to get the orginal */
386 "AND Job.JobId = File.JobId " /* Job/File record */
387 "AND T1.PathId = File.PathId "
388 "AND T1.FilenameId = File.FilenameId";
390 const char *select_recent_version_with_basejob_and_delta[5] = {
392 select_recent_version_with_basejob_and_delta_default,
394 /* Postgresql */ /* The DISTINCT ON () permits to avoid extra join */
395 "SELECT DISTINCT ON (FilenameId, PathId, MarkId) JobTDate, JobId, FileId, "
396 "FileIndex, PathId, FilenameId, LStat, MD5, MarkId "
398 "(SELECT FileId, JobId, PathId, FilenameId, FileIndex, LStat, MD5, MarkId "
399 "FROM File WHERE JobId IN (%s) "
401 "SELECT File.FileId, File.JobId, PathId, FilenameId, "
402 "File.FileIndex, LStat, MD5, MarkId "
403 "FROM BaseFiles JOIN File USING (FileId) "
404 "WHERE BaseFiles.JobId IN (%s) "
405 ") AS T JOIN Job USING (JobId) "
406 "ORDER BY FilenameId, PathId, MarkId, JobTDate DESC ",
409 select_recent_version_with_basejob_and_delta_default,
412 select_recent_version_with_basejob_and_delta_default,
415 select_recent_version_with_basejob_and_delta_default
418 /* Get the list of the last recent version with a given BaseJob jobid list
419 * We don't handle Delta with BaseJobs, they have only Full files
421 const char *select_recent_version_default =
422 "SELECT j1.JobId AS JobId, f1.FileId AS FileId, f1.FileIndex AS FileIndex, "
423 "f1.PathId AS PathId, f1.FilenameId AS FilenameId, "
424 "f1.LStat AS LStat, f1.MD5 AS MD5, j1.JobTDate "
425 "FROM ( " /* Choose the last version for each Path/Filename */
426 "SELECT max(JobTDate) AS JobTDate, PathId, FilenameId "
427 "FROM File JOIN Job USING (JobId) "
428 "WHERE File.JobId IN (%s) "
429 "GROUP BY PathId, FilenameId "
430 ") AS t1, Job AS j1, File AS f1 "
431 "WHERE t1.JobTDate = j1.JobTDate "
432 "AND j1.JobId IN (%s) "
433 "AND t1.FilenameId = f1.FilenameId "
434 "AND t1.PathId = f1.PathId "
435 "AND j1.JobId = f1.JobId";
437 const char *select_recent_version[5] = {
439 select_recent_version_default,
442 "SELECT DISTINCT ON (FilenameId, PathId) JobTDate, JobId, FileId, "
443 "FileIndex, PathId, FilenameId, LStat, MD5 "
444 "FROM File JOIN Job USING (JobId) "
445 "WHERE JobId IN (%s) "
446 "ORDER BY FilenameId, PathId, JobTDate DESC ",
449 select_recent_version_default,
452 select_recent_version_default,
455 select_recent_version_default
458 /* We don't create this table as TEMPORARY because MySQL MyISAM
459 * 5.0 and 5.1 are unable to run further queries in this mode
461 const char *create_temp_accurate_jobids_default =
462 "CREATE TABLE btemp3%s AS "
463 "SELECT JobId, StartTime, EndTime, JobTDate, PurgedFiles "
464 "FROM Job JOIN FileSet USING (FileSetId) "
465 "WHERE ClientId = %s "
466 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
467 "AND StartTime<'%s' "
468 "AND FileSet.FileSet=(SELECT FileSet FROM FileSet WHERE FileSetId = %s) "
469 "ORDER BY Job.JobTDate DESC LIMIT 1";
471 const char *create_temp_accurate_jobids[5] = {
473 create_temp_accurate_jobids_default,
475 create_temp_accurate_jobids_default,
477 create_temp_accurate_jobids_default,
479 create_temp_accurate_jobids_default,
481 "DECLARE GLOBAL TEMPORARY TABLE btemp3%s AS "
482 "SELECT JobId, StartTime, EndTime, JobTDate, PurgedFiles "
483 "FROM Job JOIN FileSet USING (FileSetId) "
484 "WHERE ClientId = %s "
485 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
486 "AND StartTime<'%s' "
487 "AND FileSet.FileSet=(SELECT FileSet FROM FileSet WHERE FileSetId = %s) "
488 "ORDER BY Job.JobTDate DESC FETCH FIRST 1 ROW ONLY "
489 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
492 const char *create_temp_basefile[5] = {
494 "CREATE TEMPORARY TABLE basefile%lld ("
495 "Path BLOB NOT NULL,"
496 "Name BLOB NOT NULL)",
498 "CREATE TEMPORARY TABLE basefile%lld ("
502 "CREATE TEMPORARY TABLE basefile%lld ("
506 "CREATE TEMPORARY TABLE basefile%lld ("
510 "DECLARE GLOBAL TEMPORARY TABLE basefile%lld ("
511 "Path VARBYTE(32000) NOT NULL,"
512 "Name VARBYTE(32000) NOT NULL) "
513 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
516 const char *create_temp_new_basefile[5] = {
518 "CREATE TEMPORARY TABLE new_basefile%lld AS "
519 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
520 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
522 "FROM ( %s ) AS Temp "
523 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
524 "JOIN Path ON (Path.PathId = Temp.PathId) "
525 "WHERE Temp.FileIndex > 0",
527 "CREATE TEMPORARY TABLE new_basefile%lld AS "
528 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
529 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
531 "FROM ( %s ) AS Temp "
532 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
533 "JOIN Path ON (Path.PathId = Temp.PathId) "
534 "WHERE Temp.FileIndex > 0",
536 "CREATE TEMPORARY TABLE new_basefile%lld AS "
537 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
538 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
540 "FROM ( %s ) AS Temp "
541 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
542 "JOIN Path ON (Path.PathId = Temp.PathId) "
543 "WHERE Temp.FileIndex > 0",
545 "CREATE TEMPORARY TABLE new_basefile%lld AS "
546 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
547 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
549 "FROM ( %s ) AS Temp "
550 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
551 "JOIN Path ON (Path.PathId = Temp.PathId) "
552 "WHERE Temp.FileIndex > 0",
554 "DECLARE GLOBAL TEMPORARY TABLE new_basefile%lld AS "
555 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
556 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
558 "FROM ( %s ) AS Temp "
559 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
560 "JOIN Path ON (Path.PathId = Temp.PathId) "
561 "WHERE Temp.FileIndex > 0 "
562 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
565 /* ====== ua_prune.c */
567 /* List of SQL commands to create temp table and indicies */
568 const char *create_deltabs[5] = {
570 "CREATE TEMPORARY TABLE DelCandidates ("
571 "JobId INTEGER UNSIGNED NOT NULL, "
572 "PurgedFiles TINYINT, "
573 "FileSetId INTEGER UNSIGNED, "
574 "JobFiles INTEGER UNSIGNED, "
575 "JobStatus BINARY(1))",
577 "CREATE TEMPORARY TABLE DelCandidates ("
578 "JobId INTEGER NOT NULL, "
579 "PurgedFiles SMALLINT, "
580 "FileSetId INTEGER, "
582 "JobStatus char(1))",
584 "CREATE TEMPORARY TABLE DelCandidates ("
585 "JobId INTEGER UNSIGNED NOT NULL, "
586 "PurgedFiles TINYINT, "
587 "FileSetId INTEGER UNSIGNED, "
588 "JobFiles INTEGER UNSIGNED, "
591 "CREATE TEMPORARY TABLE DelCandidates ("
592 "JobId INTEGER UNSIGNED NOT NULL, "
593 "PurgedFiles TINYINT, "
594 "FileSetId INTEGER UNSIGNED, "
595 "JobFiles INTEGER UNSIGNED, "
598 "DECLARE GLOBAL TEMPORARY TABLE DelCandidates ("
599 "JobId INTEGER NOT NULL, "
600 "PurgedFiles SMALLINT, "
601 "FileSetId INTEGER, "
603 "JobStatus CHAR(1)) "
604 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
607 /* ======= ua_purge.c */
609 /* Select the first available Copy Job that must be upgraded to a Backup job when the original backup job is expired. */
611 const char *uap_upgrade_copies_oldest_job_default =
612 "CREATE TEMPORARY TABLE cpy_tmp AS "
613 "SELECT MIN(JobId) AS JobId FROM Job " /* Choose the oldest job */
614 "WHERE Type='%c' " /* JT_JOB_COPY */
615 "AND ( PriorJobId IN (%s) " /* JobId selection */
620 "WHERE JobId IN (%s) " /* JobId selection */
624 "GROUP BY PriorJobId "; /* one result per copy */
626 const char *uap_upgrade_copies_oldest_job[5] = {
628 uap_upgrade_copies_oldest_job_default,
630 uap_upgrade_copies_oldest_job_default,
632 uap_upgrade_copies_oldest_job_default,
634 uap_upgrade_copies_oldest_job_default,
636 "DECLARE GLOBAL TEMPORARY TABLE cpy_tmp AS "
637 "SELECT MIN(JobId) AS JobId FROM Job " /* Choose the oldest job */
638 "WHERE Type='%c' " /* JT_JOB_COPY */
639 "AND ( PriorJobId IN (%s) " /* JobId selection */
644 "WHERE JobId IN (%s) " /* JobId selection */
648 "GROUP BY PriorJobId " /* one result per copy */
649 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
652 /* ======= ua_restore.c */
654 /* List Jobs where a particular file is saved */
655 const char *uar_file[5] = {
657 "SELECT Job.JobId as JobId,"
658 "CONCAT(Path.Path,Filename.Name) as Name, "
659 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
660 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
661 "AND Client.ClientId=Job.ClientId "
662 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
663 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
664 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
666 "SELECT Job.JobId as JobId,"
667 "Path.Path||Filename.Name as Name, "
668 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
669 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
670 "AND Client.ClientId=Job.ClientId "
671 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
672 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
673 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
675 "SELECT Job.JobId as JobId,"
676 "Path.Path||Filename.Name as Name, "
677 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
678 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
679 "AND Client.ClientId=Job.ClientId "
680 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
681 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
682 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
684 "SELECT Job.JobId as JobId,"
685 "Path.Path||Filename.Name as Name, "
686 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
687 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
688 "AND Client.ClientId=Job.ClientId "
689 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
690 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
691 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
693 "SELECT Job.JobId as JobId,"
694 "Path.Path||Filename.Name as Name, "
695 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
696 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
697 "AND Client.ClientId=Job.ClientId "
698 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
699 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
700 "AND Filename.Name='%s' ORDER BY StartTime DESC FETCH FIRST 20 ROWS ONLY"
703 const char *uar_create_temp[5] = {
705 "CREATE TEMPORARY TABLE temp ("
706 "JobId INTEGER UNSIGNED NOT NULL,"
707 "JobTDate BIGINT UNSIGNED,"
708 "ClientId INTEGER UNSIGNED,"
710 "JobFiles INTEGER UNSIGNED,"
711 "JobBytes BIGINT UNSIGNED,"
714 "StartFile INTEGER UNSIGNED,"
715 "VolSessionId INTEGER UNSIGNED,"
716 "VolSessionTime INTEGER UNSIGNED)",
718 "CREATE TEMPORARY TABLE temp ("
719 "JobId INTEGER NOT NULL,"
728 "VolSessionId INTEGER,"
729 "VolSessionTime INTEGER)",
731 "CREATE TEMPORARY TABLE temp ("
732 "JobId INTEGER UNSIGNED NOT NULL,"
733 "JobTDate BIGINT UNSIGNED,"
734 "ClientId INTEGER UNSIGNED,"
736 "JobFiles INTEGER UNSIGNED,"
737 "JobBytes BIGINT UNSIGNED,"
740 "StartFile INTEGER UNSIGNED,"
741 "VolSessionId INTEGER UNSIGNED,"
742 "VolSessionTime INTEGER UNSIGNED)",
744 "CREATE TEMPORARY TABLE temp ("
745 "JobId INTEGER UNSIGNED NOT NULL,"
746 "JobTDate BIGINT UNSIGNED,"
747 "ClientId INTEGER UNSIGNED,"
749 "JobFiles INTEGER UNSIGNED,"
750 "JobBytes BIGINT UNSIGNED,"
753 "StartFile INTEGER UNSIGNED,"
754 "VolSessionId INTEGER UNSIGNED,"
755 "VolSessionTime INTEGER UNSIGNED)",
757 "DECLARE GLOBAL TEMPORARY TABLE temp ("
758 "JobId INTEGER NOT NULL,"
764 "StartTime TIMESTAMP WITHOUT TIME ZONE,"
765 "VolumeName VARBYTE(128),"
767 "VolSessionId INTEGER,"
768 "VolSessionTime INTEGER) "
769 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
772 const char *uar_create_temp1[5] = {
774 "CREATE TEMPORARY TABLE temp1 ("
775 "JobId INTEGER UNSIGNED NOT NULL,"
776 "JobTDate BIGINT UNSIGNED)",
778 "CREATE TEMPORARY TABLE temp1 ("
779 "JobId INTEGER NOT NULL,"
782 "CREATE TEMPORARY TABLE temp1 ("
783 "JobId INTEGER UNSIGNED NOT NULL,"
784 "JobTDate BIGINT UNSIGNED)",
786 "CREATE TEMPORARY TABLE temp1 ("
787 "JobId INTEGER UNSIGNED NOT NULL,"
788 "JobTDate BIGINT UNSIGNED)",
790 "DECLARE GLOBAL TEMPORARY TABLE temp1 ("
791 "JobId INTEGER NOT NULL,"
793 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
796 /* Query to get all files in a directory -- no recursing
797 * Note, for PostgreSQL since it respects the "Single Value
798 * rule", the results of the SELECT will be unoptimized.
799 * I.e. the same file will be restored multiple times, once
800 * for each time it was backed up.
803 const char *uar_jobid_fileindex_from_dir[5] = {
805 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
806 "WHERE Job.JobId IN (%s) "
807 "AND Job.JobId=File.JobId "
808 "AND Path.Path='%s' "
809 "AND Client.Name='%s' "
810 "AND Job.ClientId=Client.ClientId "
811 "AND Path.PathId=File.Pathid "
812 "AND Filename.FilenameId=File.FilenameId "
813 "GROUP BY File.FileIndex ",
815 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
816 "WHERE Job.JobId IN (%s) "
817 "AND Job.JobId=File.JobId "
818 "AND Path.Path='%s' "
819 "AND Client.Name='%s' "
820 "AND Job.ClientId=Client.ClientId "
821 "AND Path.PathId=File.Pathid "
822 "AND Filename.FilenameId=File.FilenameId",
824 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
825 "WHERE Job.JobId IN (%s) "
826 "AND Job.JobId=File.JobId "
827 "AND Path.Path='%s' "
828 "AND Client.Name='%s' "
829 "AND Job.ClientId=Client.ClientId "
830 "AND Path.PathId=File.Pathid "
831 "AND Filename.FilenameId=File.FilenameId "
832 "GROUP BY File.FileIndex ",
834 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
835 "WHERE Job.JobId IN (%s) "
836 "AND Job.JobId=File.JobId "
837 "AND Path.Path='%s' "
838 "AND Client.Name='%s' "
839 "AND Job.ClientId=Client.ClientId "
840 "AND Path.PathId=File.Pathid "
841 "AND Filename.FilenameId=File.FilenameId "
842 "GROUP BY File.FileIndex ",
844 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
845 "WHERE Job.JobId IN (%s) "
846 "AND Job.JobId=File.JobId "
847 "AND Path.Path='%s' "
848 "AND Client.Name='%s' "
849 "AND Job.ClientId=Client.ClientId "
850 "AND Path.PathId=File.Pathid "
851 "AND Filename.FilenameId=File.FilenameId"
854 const char *sql_media_order_most_recently_written[5] = {
856 "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId",
858 "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId",
860 "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId",
862 "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId",
864 "ORDER BY IFNULL(LastWritten, '1970-01-01 00:00:00') DESC,MediaId"
867 const char *sql_get_max_connections[5] = {
869 "SHOW VARIABLES LIKE 'max_connections'",
871 "SHOW max_connections",
880 /* Row number of the max_connections setting */
881 const uint32_t sql_get_max_connections_index[5] = {
894 const char *sql_bvfs_select[5] = {
896 "CREATE TABLE %s AS ( "
897 "SELECT JobId, FileIndex, FileId, max(JobTDate) as JobTDate "
899 "GROUP BY PathId, FilenameId "
900 "HAVING FileIndex > 0)",
902 "CREATE TABLE %s AS ( "
903 "SELECT JobId, FileIndex, FileId "
905 "SELECT DISTINCT ON (PathId, FilenameId) "
906 "JobId, FileIndex, FileId "
908 "ORDER BY PathId, FilenameId, JobTDate DESC "
910 "WHERE FileIndex > 0)",
919 const char *sql_bvfs_list_files_default =
920 "SELECT 'F', PathId, T1.FilenameId, Filename.Name, "
921 "File.JobId, LStat, FileId "
923 "SELECT MAX(JobTDate) AS JobTDate, PathId, FilenameId "
925 "SELECT JobTDate, PathId, FilenameId "
926 "FROM File JOIN Job USING (JobId) "
927 "WHERE File.JobId IN (%s) AND PathId = %s "
929 "SELECT JobTDate, PathId, FilenameId "
931 "JOIN File USING (FileId) "
932 "JOIN Job ON (BaseJobId = Job.JobId) "
933 "WHERE BaseFiles.JobId IN (%s) AND PathId = %s "
934 ") AS tmp GROUP BY PathId, FilenameId LIMIT %lld OFFSET %lld"
935 ") AS T1 JOIN Filename USING (FilenameId) "
936 "WHERE T1.JobTDate = Job.JobTDate "
937 "AND Job.JobId = File.JobId "
938 "AND T1.PathId = File.PathId "
939 "AND T1.FilenameId = File.FilenameId "
940 "AND Filename.Name != '' "
941 " %s " /* AND Name LIKE '' */
942 "AND (Job.JobId IN ( "
943 "SELECT DISTINCT BaseJobId FROM BaseFiles WHERE JobId IN (%s)) "
944 "OR Job.JobId IN (%s)) ";
946 const char *sql_bvfs_list_files[] = {
948 /* JobId PathId JobId PathId Limit Offset AND? Filename? JobId JobId*/
949 sql_bvfs_list_files_default,
951 /* JobId PathId JobId PathId WHERE? Filename? Limit Offset*/
953 "SELECT DISTINCT ON (FilenameId) 'F', PathId, T.FilenameId, "
954 "Filename.Name, JobId, LStat, FileId "
956 "(SELECT FileId, JobId, PathId, FilenameId, FileIndex, LStat, MD5 "
957 "FROM File WHERE JobId IN (%s) AND PathId = %s "
959 "SELECT File.FileId, File.JobId, PathId, FilenameId, "
960 "File.FileIndex, LStat, MD5 "
961 "FROM BaseFiles JOIN File USING (FileId) "
962 "WHERE BaseFiles.JobId IN (%s) AND File.PathId = %s "
963 ") AS T JOIN Job USING (JobId) JOIN Filename USING (FilenameId) "
964 " WHERE Filename.Name != '' "
965 " %s " /* AND Name LIKE '' */
966 "ORDER BY FilenameId, StartTime DESC LIMIT %lld OFFSET %lld",
969 sql_bvfs_list_files_default,
972 sql_bvfs_list_files_default,
975 sql_bvfs_list_files_default