2 Bacula® - The Network Backup Solution
4 Copyright (C) 2002-2010 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!
43 const char *cleanup_created_job =
44 "UPDATE Job SET JobStatus='f', StartTime=SchedTime, EndTime=SchedTime "
45 "WHERE JobStatus = 'C'";
46 const char *cleanup_running_job =
47 "UPDATE Job SET JobStatus='f', EndTime=StartTime WHERE JobStatus = 'R'";
49 /* For sql_update.c db_update_stats */
50 const char *fill_jobhisto =
51 "INSERT INTO JobHisto ("
52 "JobId, Job, Name, Type, Level, ClientId, JobStatus, "
53 "SchedTime, StartTime, EndTime, RealEndTime, JobTDate, "
54 "VolSessionId, VolSessionTime, JobFiles, JobBytes, ReadBytes, "
55 "JobErrors, JobMissingFiles, PoolId, FileSetId, PriorJobId, "
56 "PurgedFiles, HasBase, Reviewed, Comment ) "
58 "JobId, Job, Name, Type, Level, ClientId, JobStatus, "
59 "SchedTime, StartTime, EndTime, RealEndTime, JobTDate, "
60 "VolSessionId, VolSessionTime, JobFiles, JobBytes, ReadBytes, "
61 "JobErrors, JobMissingFiles, PoolId, FileSetId, PriorJobId, "
62 "PurgedFiles, HasBase, Reviewed, Comment "
64 "WHERE JobStatus IN ('T','W','f','A','E') "
66 "(SELECT JobHisto.JobId "
67 "FROM JobHisto WHERE JobHisto.Jobid=Job.JobId) "
71 const char *list_pool = "SELECT * FROM Pool WHERE PoolId=%s";
73 /* For ua_dotcmds.c */
74 const char *client_backups =
75 "SELECT DISTINCT Job.JobId,Client.Name as Client,Level,StartTime,"
76 "JobFiles,JobBytes,VolumeName,MediaType,FileSet,Media.Enabled as Enabled"
77 " FROM Client,Job,JobMedia,Media,FileSet"
78 " WHERE Client.Name='%s'"
80 " AND Client.ClientId=Job.ClientId"
81 " AND JobStatus IN ('T','W') AND Type='B'"
82 " AND JobMedia.JobId=Job.JobId AND JobMedia.MediaId=Media.MediaId"
83 " AND Job.FileSetId=FileSet.FileSetId"
84 " ORDER BY Job.StartTime";
86 /* ====== ua_prune.c */
88 const char *sel_JobMedia =
89 "SELECT DISTINCT JobMedia.JobId FROM JobMedia,Job "
90 "WHERE MediaId=%s AND Job.JobId=JobMedia.JobId "
91 "AND Job.JobTDate<%s";
93 /* Delete temp tables and indexes */
94 const char *drop_deltabs[] = {
95 "DROP TABLE DelCandidates",
98 const char *create_delindex = "CREATE INDEX DelInx1 ON DelCandidates (JobId)";
100 /* ======= ua_restore.c */
101 const char *uar_count_files =
102 "SELECT JobFiles FROM Job WHERE JobId=%s";
104 /* List last 20 Jobs */
105 const char *uar_list_jobs =
106 "SELECT JobId,Client.Name as Client,StartTime,Level as "
107 "JobLevel,JobFiles,JobBytes "
108 "FROM Client,Job WHERE Client.ClientId=Job.ClientId AND JobStatus IN ('T','W') "
109 "AND Type='B' ORDER BY StartTime DESC LIMIT 20";
111 const char *uar_print_jobs =
112 "SELECT DISTINCT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName"
113 " FROM Job JOIN JobMedia USING (JobId) JOIN Media USING (MediaId) "
114 " WHERE JobId IN (%s) "
115 " ORDER BY StartTime ASC";
118 * Find all files for a particular JobId and insert them into
119 * the tree during a restore.
121 const char *uar_sel_files =
122 "SELECT Path.Path,Filename.Name,FileIndex,JobId,LStat "
123 "FROM File,Filename,Path "
124 "WHERE File.JobId IN (%s) AND Filename.FilenameId=File.FilenameId "
125 "AND Path.PathId=File.PathId";
127 const char *uar_del_temp = "DROP TABLE temp";
128 const char *uar_del_temp1 = "DROP TABLE temp1";
130 const char *uar_last_full =
131 "INSERT INTO temp1 SELECT Job.JobId,JobTdate "
132 "FROM Client,Job,JobMedia,Media,FileSet WHERE Client.ClientId=%s "
133 "AND Job.ClientId=%s "
134 "AND Job.StartTime < '%s' "
135 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
136 "AND JobMedia.JobId=Job.JobId "
137 "AND Media.Enabled=1 "
138 "AND JobMedia.MediaId=Media.MediaId "
139 "AND Job.FileSetId=FileSet.FileSetId "
140 "AND FileSet.FileSet='%s' "
142 "ORDER BY Job.JobTDate DESC LIMIT 1";
144 const char *uar_full =
145 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,"
146 "Job.ClientId,Job.Level,Job.JobFiles,Job.JobBytes,"
147 "StartTime,VolumeName,JobMedia.StartFile,VolSessionId,VolSessionTime "
148 "FROM temp1,Job,JobMedia,Media WHERE temp1.JobId=Job.JobId "
149 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
150 "AND Media.Enabled=1 "
151 "AND JobMedia.JobId=Job.JobId "
152 "AND JobMedia.MediaId=Media.MediaId";
154 const char *uar_dif =
155 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
156 "Job.Level,Job.JobFiles,Job.JobBytes,"
157 "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
158 "Job.VolSessionId,Job.VolSessionTime "
159 "FROM Job,JobMedia,Media,FileSet "
160 "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
161 "AND Job.ClientId=%s "
162 "AND JobMedia.JobId=Job.JobId "
163 "AND Media.Enabled=1 "
164 "AND JobMedia.MediaId=Media.MediaId "
165 "AND Job.Level='D' AND JobStatus IN ('T','W') AND Type='B' "
166 "AND Job.FileSetId=FileSet.FileSetId "
167 "AND FileSet.FileSet='%s' "
169 "ORDER BY Job.JobTDate DESC LIMIT 1";
171 const char *uar_inc =
172 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
173 "Job.Level,Job.JobFiles,Job.JobBytes,"
174 "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
175 "Job.VolSessionId,Job.VolSessionTime "
176 "FROM Job,JobMedia,Media,FileSet "
177 "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
178 "AND Job.ClientId=%s "
179 "AND Media.Enabled=1 "
180 "AND JobMedia.JobId=Job.JobId "
181 "AND JobMedia.MediaId=Media.MediaId "
182 "AND Job.Level='I' AND JobStatus IN ('T','W') AND Type='B' "
183 "AND Job.FileSetId=FileSet.FileSetId "
184 "AND FileSet.FileSet='%s' "
187 const char *uar_list_temp =
188 "SELECT DISTINCT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName"
190 " ORDER BY StartTime ASC";
193 const char *uar_sel_jobid_temp =
194 "SELECT DISTINCT JobId,StartTime FROM temp ORDER BY StartTime ASC";
196 const char *uar_sel_all_temp1 = "SELECT * FROM temp1";
198 const char *uar_sel_all_temp = "SELECT * FROM temp";
202 /* Select FileSet names for this Client */
203 const char *uar_sel_fileset =
204 "SELECT DISTINCT FileSet.FileSet FROM Job,"
205 "Client,FileSet WHERE Job.FileSetId=FileSet.FileSetId "
206 "AND Job.ClientId=%s AND Client.ClientId=%s "
207 "ORDER BY FileSet.FileSet";
209 /* Select all different FileSet for this client
210 * This query doesn't guarantee that the Id is the latest
211 * version of the FileSet. Can be used with other queries that
212 * use Ids to select the FileSet name. (like in accurate)
214 const char *uar_sel_filesetid =
215 "SELECT MAX(FileSetId) "
216 "FROM FileSet JOIN Job USING (FileSetId) "
217 "WHERE Job.ClientId=%s "
221 * Find JobId, FileIndex for a given path/file and date
222 * for use when inserting individual files into the tree.
224 const char *uar_jobid_fileindex =
225 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
226 "WHERE Job.JobId=File.JobId "
227 "AND Job.StartTime<='%s' "
228 "AND Path.Path='%s' "
229 "AND Filename.Name='%s' "
230 "AND Client.Name='%s' "
231 "AND Job.ClientId=Client.ClientId "
232 "AND Path.PathId=File.PathId "
233 "AND Filename.FilenameId=File.FilenameId "
234 "AND JobStatus IN ('T','W') AND Type='B' "
235 "ORDER BY Job.StartTime DESC LIMIT 1";
237 const char *uar_jobids_fileindex =
238 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
239 "WHERE Job.JobId IN (%s) "
240 "AND Job.JobId=File.JobId "
241 "AND Job.StartTime<='%s' "
242 "AND Path.Path='%s' "
243 "AND Filename.Name='%s' "
244 "AND Client.Name='%s' "
245 "AND Job.ClientId=Client.ClientId "
246 "AND Path.PathId=File.PathId "
247 "AND Filename.FilenameId=File.FilenameId "
248 "ORDER BY Job.StartTime DESC LIMIT 1";
250 /* Query to get list of files from table -- presuably built by an external program */
251 const char *uar_jobid_fileindex_from_table =
252 "SELECT JobId,FileIndex FROM %s ORDER BY JobId, FileIndex ASC";
254 /* Get the list of the last recent version per Delta with a given jobid list
255 * This is a tricky part because with SQL the result of
257 * SELECT MAX(A), B, C, D FROM... GROUP BY (B,C)
259 * doesn't give the good result (for D).
261 * With PostgreSQL, we can use DISTINCT ON(), but with Mysql or Sqlite,
262 * we need an extra join using JobTDate.
264 const char *select_recent_version_with_basejob_default =
265 "SELECT FileId, Job.JobId AS JobId, FileIndex, File.PathId AS PathId, "
266 "File.FilenameId AS FilenameId, LStat, MD5, DeltaSeq, "
267 "Job.JobTDate AS JobTDate "
269 "SELECT MAX(JobTDate) AS JobTDate, PathId, FilenameId "
271 "SELECT JobTDate, PathId, FilenameId " /* Get all normal files */
272 "FROM File JOIN Job USING (JobId) " /* from selected backup */
273 "WHERE File.JobId IN (%s) "
275 "SELECT JobTDate, PathId, FilenameId " /* Get all files from */
276 "FROM BaseFiles " /* BaseJob */
277 "JOIN File USING (FileId) "
278 "JOIN Job ON (BaseJobId = Job.JobId) "
279 "WHERE BaseFiles.JobId IN (%s) " /* Use Max(JobTDate) to find */
281 "GROUP BY PathId, FilenameId " /* the latest file version */
283 "WHERE (Job.JobId IN ( " /* Security, we force JobId to be valid */
284 "SELECT DISTINCT BaseJobId FROM BaseFiles WHERE JobId IN (%s)) "
285 "OR Job.JobId IN (%s)) "
286 "AND T1.JobTDate = Job.JobTDate " /* Join on JobTDate to get the orginal */
287 "AND Job.JobId = File.JobId " /* Job/File record */
288 "AND T1.PathId = File.PathId "
289 "AND T1.FilenameId = File.FilenameId";
291 const char *select_recent_version_with_basejob[] = {
293 select_recent_version_with_basejob_default,
295 /* Postgresql */ /* The DISTINCT ON () permits to avoid extra join */
296 "SELECT DISTINCT ON (FilenameId, PathId) JobTDate, JobId, FileId, "
297 "FileIndex, PathId, FilenameId, LStat, MD5, DeltaSeq "
299 "(SELECT FileId, JobId, PathId, FilenameId, FileIndex, LStat, MD5, DeltaSeq "
300 "FROM File WHERE JobId IN (%s) "
302 "SELECT File.FileId, File.JobId, PathId, FilenameId, "
303 "File.FileIndex, LStat, MD5, DeltaSeq "
304 "FROM BaseFiles JOIN File USING (FileId) "
305 "WHERE BaseFiles.JobId IN (%s) "
306 ") AS T JOIN Job USING (JobId) "
307 "ORDER BY FilenameId, PathId, JobTDate DESC ",
310 select_recent_version_with_basejob_default,
313 select_recent_version_with_basejob_default
316 /* We do the same thing than the previous query, but we include
317 * all delta parts. If the file has been deleted, we can have irrelevant
320 * The code that uses results should control the delta sequence with
321 * the following rules:
323 * Delta = Previous Delta + 1
325 * If we detect a gap, we can discard further pieces
326 * If a file starts at 1 instead of 0, the file has been deleted, and further
327 * pieces are useless.
329 * This control should be reset for each new file
331 const char *select_recent_version_with_basejob_and_delta_default =
332 "SELECT FileId, Job.JobId AS JobId, FileIndex, File.PathId AS PathId, "
333 "File.FilenameId AS FilenameId, LStat, MD5, File.DeltaSeq AS DeltaSeq, "
334 "Job.JobTDate AS JobTDate "
336 "SELECT MAX(JobTDate) AS JobTDate, PathId, FilenameId, DeltaSeq "
338 "SELECT JobTDate, PathId, FilenameId, DeltaSeq " /*Get all normal files*/
339 "FROM File JOIN Job USING (JobId) " /* from selected backup */
340 "WHERE File.JobId IN (%s) "
342 "SELECT JobTDate, PathId, FilenameId, DeltaSeq " /*Get all files from */
343 "FROM BaseFiles " /* BaseJob */
344 "JOIN File USING (FileId) "
345 "JOIN Job ON (BaseJobId = Job.JobId) "
346 "WHERE BaseFiles.JobId IN (%s) " /* Use Max(JobTDate) to find */
348 "GROUP BY PathId, FilenameId, DeltaSeq " /* the latest file version */
350 "WHERE (Job.JobId IN ( " /* Security, we force JobId to be valid */
351 "SELECT DISTINCT BaseJobId FROM BaseFiles WHERE JobId IN (%s)) "
352 "OR Job.JobId IN (%s)) "
353 "AND T1.JobTDate = Job.JobTDate " /* Join on JobTDate to get the orginal */
354 "AND Job.JobId = File.JobId " /* Job/File record */
355 "AND T1.PathId = File.PathId "
356 "AND T1.FilenameId = File.FilenameId";
358 const char *select_recent_version_with_basejob_and_delta[] = {
360 select_recent_version_with_basejob_and_delta_default,
362 /* Postgresql */ /* The DISTINCT ON () permits to avoid extra join */
363 "SELECT DISTINCT ON (FilenameId, PathId, DeltaSeq) JobTDate, JobId, FileId, "
364 "FileIndex, PathId, FilenameId, LStat, MD5, DeltaSeq "
366 "(SELECT FileId, JobId, PathId, FilenameId, FileIndex, LStat, MD5,DeltaSeq "
367 "FROM File WHERE JobId IN (%s) "
369 "SELECT File.FileId, File.JobId, PathId, FilenameId, "
370 "File.FileIndex, LStat, MD5, DeltaSeq "
371 "FROM BaseFiles JOIN File USING (FileId) "
372 "WHERE BaseFiles.JobId IN (%s) "
373 ") AS T JOIN Job USING (JobId) "
374 "ORDER BY FilenameId, PathId, DeltaSeq, JobTDate DESC ",
377 select_recent_version_with_basejob_and_delta_default,
380 select_recent_version_with_basejob_and_delta_default
383 /* Get the list of the last recent version with a given BaseJob jobid list
384 * We don't handle Delta with BaseJobs, they have only Full files
386 const char *select_recent_version_default =
387 "SELECT j1.JobId AS JobId, f1.FileId AS FileId, f1.FileIndex AS FileIndex, "
388 "f1.PathId AS PathId, f1.FilenameId AS FilenameId, "
389 "f1.LStat AS LStat, f1.MD5 AS MD5, j1.JobTDate "
390 "FROM ( " /* Choose the last version for each Path/Filename */
391 "SELECT max(JobTDate) AS JobTDate, PathId, FilenameId "
392 "FROM File JOIN Job USING (JobId) "
393 "WHERE File.JobId IN (%s) "
394 "GROUP BY PathId, FilenameId "
395 ") AS t1, Job AS j1, File AS f1 "
396 "WHERE t1.JobTDate = j1.JobTDate "
397 "AND j1.JobId IN (%s) "
398 "AND t1.FilenameId = f1.FilenameId "
399 "AND t1.PathId = f1.PathId "
400 "AND j1.JobId = f1.JobId";
402 const char *select_recent_version[] = {
404 select_recent_version_default,
407 "SELECT DISTINCT ON (FilenameId, PathId) JobTDate, JobId, FileId, "
408 "FileIndex, PathId, FilenameId, LStat, MD5 "
409 "FROM File JOIN Job USING (JobId) "
410 "WHERE JobId IN (%s) "
411 "ORDER BY FilenameId, PathId, JobTDate DESC ",
414 select_recent_version_default,
417 select_recent_version_default
420 /* We don't create this table as TEMPORARY because MySQL MyISAM
421 * 5.0 and 5.1 are unable to run further queries in this mode
423 const char *create_temp_accurate_jobids_default =
424 "CREATE TABLE btemp3%s AS "
425 "SELECT JobId, StartTime, EndTime, JobTDate, PurgedFiles "
426 "FROM Job JOIN FileSet USING (FileSetId) "
427 "WHERE ClientId = %s "
428 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
429 "AND StartTime<'%s' "
430 "AND FileSet.FileSet=(SELECT FileSet FROM FileSet WHERE FileSetId = %s) "
431 "ORDER BY Job.JobTDate DESC LIMIT 1";
433 const char *create_temp_accurate_jobids[] = {
435 create_temp_accurate_jobids_default,
438 create_temp_accurate_jobids_default,
441 create_temp_accurate_jobids_default,
444 "DECLARE GLOBAL TEMPORARY TABLE btemp3%s AS "
445 "SELECT JobId, StartTime, EndTime, JobTDate, PurgedFiles "
446 "FROM Job JOIN FileSet USING (FileSetId) "
447 "WHERE ClientId = %s "
448 "AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
449 "AND StartTime<'%s' "
450 "AND FileSet.FileSet=(SELECT FileSet FROM FileSet WHERE FileSetId = %s) "
451 "ORDER BY Job.JobTDate DESC FETCH FIRST 1 ROW ONLY "
452 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
455 const char *create_temp_basefile[] = {
457 "CREATE TEMPORARY TABLE basefile%lld ("
458 "Path BLOB NOT NULL,"
459 "Name BLOB NOT NULL)",
462 "CREATE TEMPORARY TABLE basefile%lld ("
467 "CREATE TEMPORARY TABLE basefile%lld ("
472 "DECLARE GLOBAL TEMPORARY TABLE basefile%lld ("
473 "Path VARBYTE(32000) NOT NULL,"
474 "Name VARBYTE(32000) NOT NULL) "
475 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
478 const char *create_temp_new_basefile[] = {
480 "CREATE TEMPORARY TABLE new_basefile%lld AS "
481 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
482 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
484 "FROM ( %s ) AS Temp "
485 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
486 "JOIN Path ON (Path.PathId = Temp.PathId) "
487 "WHERE Temp.FileIndex > 0",
490 "CREATE TEMPORARY TABLE new_basefile%lld AS "
491 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
492 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
494 "FROM ( %s ) AS Temp "
495 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
496 "JOIN Path ON (Path.PathId = Temp.PathId) "
497 "WHERE Temp.FileIndex > 0",
500 "CREATE TEMPORARY TABLE new_basefile%lld AS "
501 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
502 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
504 "FROM ( %s ) AS Temp "
505 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
506 "JOIN Path ON (Path.PathId = Temp.PathId) "
507 "WHERE Temp.FileIndex > 0",
510 "DECLARE GLOBAL TEMPORARY TABLE new_basefile%lld AS "
511 "SELECT Path.Path AS Path, Filename.Name AS Name, Temp.FileIndex AS FileIndex,"
512 "Temp.JobId AS JobId, Temp.LStat AS LStat, Temp.FileId AS FileId, "
514 "FROM ( %s ) AS Temp "
515 "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
516 "JOIN Path ON (Path.PathId = Temp.PathId) "
517 "WHERE Temp.FileIndex > 0 "
518 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
521 /* ====== ua_prune.c */
523 /* List of SQL commands to create temp table and indicies */
524 const char *create_deltabs[] = {
526 "CREATE TEMPORARY TABLE DelCandidates ("
527 "JobId INTEGER UNSIGNED NOT NULL, "
528 "PurgedFiles TINYINT, "
529 "FileSetId INTEGER UNSIGNED, "
530 "JobFiles INTEGER UNSIGNED, "
531 "JobStatus BINARY(1))",
534 "CREATE TEMPORARY TABLE DelCandidates ("
535 "JobId INTEGER NOT NULL, "
536 "PurgedFiles SMALLINT, "
537 "FileSetId INTEGER, "
539 "JobStatus char(1))",
542 "CREATE TEMPORARY TABLE DelCandidates ("
543 "JobId INTEGER UNSIGNED NOT NULL, "
544 "PurgedFiles TINYINT, "
545 "FileSetId INTEGER UNSIGNED, "
546 "JobFiles INTEGER UNSIGNED, "
550 "DECLARE GLOBAL TEMPORARY TABLE DelCandidates ("
551 "JobId INTEGER NOT NULL, "
552 "PurgedFiles SMALLINT, "
553 "FileSetId INTEGER, "
555 "JobStatus CHAR(1)) "
556 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
559 /* ======= ua_purge.c */
561 /* Select the first available Copy Job that must be upgraded to a Backup job when the original backup job is expired. */
563 const char *uap_upgrade_copies_oldest_job_default =
564 "CREATE TEMPORARY TABLE cpy_tmp AS "
565 "SELECT MIN(JobId) AS JobId FROM Job " /* Choose the oldest job */
566 "WHERE Type='%c' " /* JT_JOB_COPY */
567 "AND ( PriorJobId IN (%s) " /* JobId selection */
572 "WHERE JobId IN (%s) " /* JobId selection */
576 "GROUP BY PriorJobId "; /* one result per copy */
578 const char *uap_upgrade_copies_oldest_job[] = {
580 uap_upgrade_copies_oldest_job_default,
583 uap_upgrade_copies_oldest_job_default,
586 uap_upgrade_copies_oldest_job_default,
589 "DECLARE GLOBAL TEMPORARY TABLE cpy_tmp AS "
590 "SELECT MIN(JobId) AS JobId FROM Job " /* Choose the oldest job */
591 "WHERE Type='%c' " /* JT_JOB_COPY */
592 "AND ( PriorJobId IN (%s) " /* JobId selection */
597 "WHERE JobId IN (%s) " /* JobId selection */
601 "GROUP BY PriorJobId " /* one result per copy */
602 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
605 /* ======= ua_restore.c */
607 /* List Jobs where a particular file is saved */
608 const char *uar_file[] = {
610 "SELECT Job.JobId as JobId,"
611 "CONCAT(Path.Path,Filename.Name) as Name, "
612 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
613 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
614 "AND Client.ClientId=Job.ClientId "
615 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
616 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
617 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
620 "SELECT Job.JobId as JobId,"
621 "Path.Path||Filename.Name as Name, "
622 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
623 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
624 "AND Client.ClientId=Job.ClientId "
625 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
626 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
627 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
630 "SELECT Job.JobId as JobId,"
631 "Path.Path||Filename.Name as Name, "
632 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
633 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
634 "AND Client.ClientId=Job.ClientId "
635 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
636 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
637 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
640 "SELECT Job.JobId as JobId,"
641 "Path.Path||Filename.Name as Name, "
642 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
643 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
644 "AND Client.ClientId=Job.ClientId "
645 "AND Job.JobId=File.JobId AND File.FileIndex > 0 "
646 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
647 "AND Filename.Name='%s' ORDER BY StartTime DESC FETCH FIRST 20 ROWS ONLY"
650 const char *uar_create_temp[] = {
652 "CREATE TEMPORARY TABLE temp ("
653 "JobId INTEGER UNSIGNED NOT NULL,"
654 "JobTDate BIGINT UNSIGNED,"
655 "ClientId INTEGER UNSIGNED,"
657 "JobFiles INTEGER UNSIGNED,"
658 "JobBytes BIGINT UNSIGNED,"
661 "StartFile INTEGER UNSIGNED,"
662 "VolSessionId INTEGER UNSIGNED,"
663 "VolSessionTime INTEGER UNSIGNED)",
666 "CREATE TEMPORARY TABLE temp ("
667 "JobId INTEGER NOT NULL,"
676 "VolSessionId INTEGER,"
677 "VolSessionTime INTEGER)",
680 "CREATE TEMPORARY TABLE temp ("
681 "JobId INTEGER UNSIGNED NOT NULL,"
682 "JobTDate BIGINT UNSIGNED,"
683 "ClientId INTEGER UNSIGNED,"
685 "JobFiles INTEGER UNSIGNED,"
686 "JobBytes BIGINT UNSIGNED,"
689 "StartFile INTEGER UNSIGNED,"
690 "VolSessionId INTEGER UNSIGNED,"
691 "VolSessionTime INTEGER UNSIGNED)",
694 "DECLARE GLOBAL TEMPORARY TABLE temp ("
695 "JobId INTEGER NOT NULL,"
701 "StartTime TIMESTAMP WITHOUT TIME ZONE,"
702 "VolumeName VARBYTE(128),"
704 "VolSessionId INTEGER,"
705 "VolSessionTime INTEGER) "
706 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
709 const char *uar_create_temp1[] = {
711 "CREATE TEMPORARY TABLE temp1 ("
712 "JobId INTEGER UNSIGNED NOT NULL,"
713 "JobTDate BIGINT UNSIGNED)",
716 "CREATE TEMPORARY TABLE temp1 ("
717 "JobId INTEGER NOT NULL,"
721 "CREATE TEMPORARY TABLE temp1 ("
722 "JobId INTEGER UNSIGNED NOT NULL,"
723 "JobTDate BIGINT UNSIGNED)",
726 "DECLARE GLOBAL TEMPORARY TABLE temp1 ("
727 "JobId INTEGER NOT NULL,"
729 "ON COMMIT PRESERVE ROWS WITH NORECOVERY"
732 /* Query to get all files in a directory -- no recursing
733 * Note, for PostgreSQL since it respects the "Single Value
734 * rule", the results of the SELECT will be unoptimized.
735 * I.e. the same file will be restored multiple times, once
736 * for each time it was backed up.
739 const char *uar_jobid_fileindex_from_dir[] = {
741 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
742 "WHERE Job.JobId IN (%s) "
743 "AND Job.JobId=File.JobId "
744 "AND Path.Path='%s' "
745 "AND Client.Name='%s' "
746 "AND Job.ClientId=Client.ClientId "
747 "AND Path.PathId=File.Pathid "
748 "AND Filename.FilenameId=File.FilenameId "
749 "GROUP BY File.FileIndex ",
752 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
753 "WHERE Job.JobId IN (%s) "
754 "AND Job.JobId=File.JobId "
755 "AND Path.Path='%s' "
756 "AND Client.Name='%s' "
757 "AND Job.ClientId=Client.ClientId "
758 "AND Path.PathId=File.Pathid "
759 "AND Filename.FilenameId=File.FilenameId",
762 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
763 "WHERE Job.JobId IN (%s) "
764 "AND Job.JobId=File.JobId "
765 "AND Path.Path='%s' "
766 "AND Client.Name='%s' "
767 "AND Job.ClientId=Client.ClientId "
768 "AND Path.PathId=File.Pathid "
769 "AND Filename.FilenameId=File.FilenameId "
770 "GROUP BY File.FileIndex ",
773 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
774 "WHERE Job.JobId IN (%s) "
775 "AND Job.JobId=File.JobId "
776 "AND Path.Path='%s' "
777 "AND Client.Name='%s' "
778 "AND Job.ClientId=Client.ClientId "
779 "AND Path.PathId=File.Pathid "
780 "AND Filename.FilenameId=File.FilenameId"
783 const char *sql_media_order_most_recently_written[] = {
785 "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId",
788 "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId",
791 "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId",
794 "ORDER BY IFNULL(LastWritten, '1970-01-01 00:00:00') DESC,MediaId"
797 const char *sql_get_max_connections[] = {
799 "SHOW VARIABLES LIKE 'max_connections'",
802 "SHOW max_connections",
811 /* TODO: Check for corner cases with MySQL and SQLite3
812 * The Group By can return strange numbers when having multiple
813 * version of a file in the same dataset.
815 const char *sql_bvfs_select[] = {
817 "CREATE TABLE %s AS ( "
818 "SELECT JobId, FileIndex, FileId, max(JobTDate) as JobTDate "
820 "GROUP BY PathId, FilenameId "
821 "HAVING FileIndex > 0)",
824 "CREATE TABLE %s AS ( "
825 "SELECT JobId, FileIndex, FileId "
827 "SELECT DISTINCT ON (PathId, FilenameId) "
828 "JobId, FileIndex, FileId "
830 "ORDER BY PathId, FilenameId, JobTDate DESC "
832 "WHERE FileIndex > 0)",
835 "CREATE TABLE %s AS "
836 "SELECT JobId, FileIndex, FileId, max(JobTDate) as JobTDate "
838 "GROUP BY PathId, FilenameId "
839 "HAVING FileIndex > 0",
846 const char *sql_bvfs_list_files_default =
847 "SELECT 'F', T1.PathId, T1.FilenameId, Filename.Name, "
848 "File.JobId, File.LStat, File.FileId "
850 "SELECT MAX(JobTDate) AS JobTDate, PathId, FilenameId "
852 "SELECT JobTDate, PathId, FilenameId "
853 "FROM File JOIN Job USING (JobId) "
854 "WHERE File.JobId IN (%s) AND PathId = %s "
856 "SELECT JobTDate, PathId, FilenameId "
858 "JOIN File USING (FileId) "
859 "JOIN Job ON (BaseJobId = Job.JobId) "
860 "WHERE BaseFiles.JobId IN (%s) AND PathId = %s "
861 ") AS tmp GROUP BY PathId, FilenameId LIMIT %lld OFFSET %lld"
862 ") AS T1 JOIN Filename USING (FilenameId) "
863 "WHERE T1.JobTDate = Job.JobTDate "
864 "AND Job.JobId = File.JobId "
865 "AND T1.PathId = File.PathId "
866 "AND T1.FilenameId = File.FilenameId "
867 "AND Filename.Name != '' "
868 " %s " /* AND Name LIKE '' */
869 "AND (Job.JobId IN ( "
870 "SELECT DISTINCT BaseJobId FROM BaseFiles WHERE JobId IN (%s)) "
871 "OR Job.JobId IN (%s)) ";
873 const char *sql_bvfs_list_files[] = {
875 /* JobId PathId JobId PathId Limit Offset AND? Filename? JobId JobId*/
876 sql_bvfs_list_files_default,
878 /* JobId PathId JobId PathId WHERE? Filename? Limit Offset*/
880 "SELECT DISTINCT ON (FilenameId) 'F', PathId, T.FilenameId, "
881 "Filename.Name, JobId, LStat, FileId "
883 "(SELECT FileId, JobId, PathId, FilenameId, FileIndex, LStat, MD5 "
884 "FROM File WHERE JobId IN (%s) AND PathId = %s "
886 "SELECT File.FileId, File.JobId, PathId, FilenameId, "
887 "File.FileIndex, LStat, MD5 "
888 "FROM BaseFiles JOIN File USING (FileId) "
889 "WHERE BaseFiles.JobId IN (%s) AND File.PathId = %s "
890 ") AS T JOIN Job USING (JobId) JOIN Filename USING (FilenameId) "
891 " WHERE Filename.Name != '' "
892 " %s " /* AND Name LIKE '' */
893 "ORDER BY FilenameId, StartTime DESC LIMIT %lld OFFSET %lld",
896 sql_bvfs_list_files_default,
899 sql_bvfs_list_files_default,
902 sql_bvfs_list_files_default
905 const char *batch_lock_path_query[] = {
907 "LOCK TABLES Path write, batch write, Path as p write",
910 "BEGIN; LOCK TABLE Path IN SHARE ROW EXCLUSIVE MODE",
919 const char *batch_lock_filename_query[] = {
921 "LOCK TABLES Filename write, batch write, Filename as f write",
924 "BEGIN; LOCK TABLE Filename IN SHARE ROW EXCLUSIVE MODE",
933 const char *batch_unlock_tables_query[] = {
947 const char *batch_fill_path_query[] = {
949 "INSERT INTO Path (Path) "
950 "SELECT a.Path FROM "
951 "(SELECT DISTINCT Path FROM batch) AS a WHERE NOT EXISTS "
952 "(SELECT Path FROM Path AS p WHERE p.Path = a.Path)",
955 "INSERT INTO Path (Path) "
956 "SELECT a.Path FROM "
957 "(SELECT DISTINCT Path FROM batch) AS a "
958 "WHERE NOT EXISTS (SELECT Path FROM Path WHERE Path = a.Path) ",
961 "INSERT INTO Path (Path) "
962 "SELECT DISTINCT Path FROM batch "
963 "EXCEPT SELECT Path FROM Path",
966 "INSERT INTO Path (Path) "
967 "SELECT DISTINCT b.Path FROM batch b "
968 "WHERE NOT EXISTS (SELECT Path FROM Path p WHERE p.Path = b.Path)"
971 const char *batch_fill_filename_query[] = {
973 "INSERT INTO Filename (Name) "
974 "SELECT a.Name FROM "
975 "(SELECT DISTINCT Name FROM batch) AS a WHERE NOT EXISTS "
976 "(SELECT Name FROM Filename AS f WHERE f.Name = a.Name)",
979 "INSERT INTO Filename (Name) "
980 "SELECT a.Name FROM "
981 "(SELECT DISTINCT Name FROM batch) as a "
983 "(SELECT Name FROM Filename WHERE Name = a.Name)",
986 "INSERT INTO Filename (Name) "
987 "SELECT DISTINCT Name FROM batch "
988 "EXCEPT SELECT Name FROM Filename",
991 "INSERT INTO Filename (Name) "
992 "SELECT DISTINCT b.Name FROM batch b "
993 "WHERE NOT EXISTS (SELECT Name FROM Filename f WHERE f.Name = b.Name)"
996 const char *match_query[] = {
1004 "LIKE", /* MATCH doesn't seems to work anymore... */