2 Bacula® - The Network Backup Solution
4 Copyright (C) 2002-2007 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 John Walker.
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!
48 const char *list_pool = "SELECT * FROM Pool WHERE PoolId=%s";
50 /* For ua_dotcmds.c */
51 const char *client_backups =
52 "SELECT DISTINCT Job.JobId,Client.Name as Client,Level,StartTime,"
53 "JobFiles,JobBytes,VolumeName,MediaType,FileSet,Media.Enabled as Enabled"
54 " FROM Client,Job,JobMedia,Media,FileSet"
55 " WHERE Client.Name='%s'"
57 " AND Client.ClientId=Job.ClientId"
58 " AND JobStatus='T' AND Type='B'"
59 " AND JobMedia.JobId=Job.JobId AND JobMedia.MediaId=Media.MediaId"
60 " AND Job.FileSetId=FileSet.FileSetId"
61 " ORDER BY Job.StartTime";
64 /* ====== ua_prune.c */
66 const char *del_File = "DELETE FROM File WHERE JobId=%s";
67 const char *upd_Purged = "UPDATE Job Set PurgedFiles=1 WHERE JobId=%s";
68 const char *cnt_DelCand = "SELECT count(*) FROM DelCandidates";
69 const char *del_Job = "DELETE FROM Job WHERE JobId=%s";
70 const char *del_JobMedia = "DELETE FROM JobMedia WHERE JobId=%s";
71 const char *cnt_JobMedia = "SELECT count(*) FROM JobMedia WHERE MediaId=%s";
73 const char *sel_JobMedia =
74 "SELECT DISTINCT JobMedia.JobId FROM JobMedia,Job "
75 "WHERE MediaId=%s AND Job.JobId=JobMedia.JobId "
76 "AND Job.JobTDate<%s";
78 /* Count Select JobIds for File deletion */
79 const char *count_select_job =
80 "SELECT count(*) from Job "
86 /* Select JobIds for File deletion. */
87 const char *select_job =
88 "SELECT DISTINCT JobId from Job "
93 /* Delete temp tables and indexes */
94 const char *drop_deltabs[] = {
95 "DROP TABLE DelCandidates",
100 /* List of SQL commands to create temp table and indicies */
101 const char *create_deltabs[] = {
102 "CREATE TEMPORARY TABLE DelCandidates ("
103 #if defined(HAVE_MYSQL)
104 "JobId INTEGER UNSIGNED NOT NULL, "
105 "PurgedFiles TINYINT, "
106 "FileSetId INTEGER UNSIGNED, "
107 "JobFiles INTEGER UNSIGNED, "
108 "JobStatus BINARY(1))",
109 #elif defined(HAVE_POSTGRESQL)
110 "JobId INTEGER NOT NULL, "
111 "PurgedFiles SMALLINT, "
112 "FileSetId INTEGER, "
114 "JobStatus char(1))",
116 "JobId INTEGER UNSIGNED NOT NULL, "
117 "PurgedFiles TINYINT, "
118 "FileSetId INTEGER UNSIGNED, "
119 "JobFiles INTEGER UNSIGNED, "
122 "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!='T'))) OR "
149 "AND Job.ClientId=%s "
150 "AND Job.Level='F' AND Job.JobStatus='T' 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!='T') OR "
162 "AND Job.ClientId=%s "
163 "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
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!='T') 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!='T') 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!='T') OR "
198 "AND Job.ClientId=%s "
203 const char *select_backup_del =
204 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
205 "FROM Job,DelCandidates "
206 "WHERE (Job.JobId=DelCandidates.JobId AND ((DelCandidates.JobFiles=0) OR "
207 "(DelCandidates.JobStatus!='T'))) OR "
209 "AND Job.ClientId=%s "
210 "AND Job.Level='F' AND Job.JobStatus='T' AND Job.Type IN ('B','M') "
211 "AND Job.FileSetId=DelCandidates.FileSetId)";
213 /* Select Jobs from the DelCandidates table that have a
214 * more recent InitCatalog -- i.e. are not the only InitCatalog
215 * This is the list of Jobs to delete for a Verify Job.
217 const char *select_verify_del =
218 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
219 "FROM Job,DelCandidates "
220 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
222 "AND Job.ClientId=%s "
223 "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
224 "AND Job.FileSetId=DelCandidates.FileSetId)";
227 /* Select Jobs from the DelCandidates table.
228 * This is the list of Jobs to delete for a Restore Job.
230 const char *select_restore_del =
231 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
232 "FROM Job,DelCandidates "
233 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
235 "AND Job.ClientId=%s "
238 /* Select Jobs from the DelCandidates table.
239 * This is the list of Jobs to delete for an Admin Job.
241 const char *select_admin_del =
242 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
243 "FROM Job,DelCandidates "
244 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
246 "AND Job.ClientId=%s "
250 * Select Jobs from the DelCandidates table.
251 * This is the list of Jobs to delete for an Migrate Job.
253 const char *select_migrate_del =
254 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
255 "FROM Job,DelCandidates "
256 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
258 "AND Job.ClientId=%s "
263 /* ======= ua_restore.c */
264 const char *uar_count_files =
265 "SELECT JobFiles FROM Job WHERE JobId=%s";
267 /* List last 20 Jobs */
268 const char *uar_list_jobs =
269 "SELECT JobId,Client.Name as Client,StartTime,Level as "
270 "JobLevel,JobFiles,JobBytes "
271 "FROM Client,Job WHERE Client.ClientId=Job.ClientId AND JobStatus='T' "
272 "AND Type='B' ORDER BY StartTime DESC LIMIT 20";
275 /* MYSQL IS NOT STANDARD SQL !!!!! */
276 /* List Jobs where a particular file is saved */
277 const char *uar_file =
278 "SELECT Job.JobId as JobId,"
279 "CONCAT(Path.Path,Filename.Name) as Name, "
280 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
281 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
282 "AND Client.ClientId=Job.ClientId "
283 "AND Job.JobId=File.JobId "
284 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
285 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20";
287 /* List Jobs where a particular file is saved */
288 const char *uar_file =
289 "SELECT Job.JobId as JobId,"
290 "Path.Path||Filename.Name as Name, "
291 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
292 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
293 "AND Client.ClientId=Job.ClientId "
294 "AND Job.JobId=File.JobId "
295 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
296 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20";
301 * Find all files for a particular JobId and insert them into
302 * the tree during a restore.
304 const char *uar_sel_files =
305 "SELECT Path.Path,Filename.Name,FileIndex,JobId,LStat "
306 "FROM File,Filename,Path "
307 "WHERE File.JobId=%s AND Filename.FilenameId=File.FilenameId "
308 "AND Path.PathId=File.PathId";
310 const char *uar_del_temp = "DROP TABLE temp";
311 const char *uar_del_temp1 = "DROP TABLE temp1";
313 const char *uar_create_temp =
314 "CREATE TEMPORARY TABLE temp ("
315 #ifdef HAVE_POSTGRESQL
316 "JobId INTEGER NOT NULL,"
325 "VolSessionId INTEGER,"
326 "VolSessionTime INTEGER)";
328 "JobId INTEGER UNSIGNED NOT NULL,"
329 "JobTDate BIGINT UNSIGNED,"
330 "ClientId INTEGER UNSIGNED,"
332 "JobFiles INTEGER UNSIGNED,"
333 "JobBytes BIGINT UNSIGNED,"
336 "StartFile INTEGER UNSIGNED,"
337 "VolSessionId INTEGER UNSIGNED,"
338 "VolSessionTime INTEGER UNSIGNED)";
341 const char *uar_create_temp1 =
342 "CREATE TEMPORARY TABLE temp1 ("
343 #ifdef HAVE_POSTGRESQL
344 "JobId INTEGER NOT NULL,"
347 "JobId INTEGER UNSIGNED NOT NULL,"
348 "JobTDate BIGINT UNSIGNED)";
351 const char *uar_last_full =
352 "INSERT INTO temp1 SELECT Job.JobId,JobTdate "
353 "FROM Client,Job,JobMedia,Media,FileSet WHERE Client.ClientId=%s "
354 "AND Job.ClientId=%s "
355 "AND Job.StartTime<'%s' "
356 "AND Level='F' AND JobStatus='T' AND Type='B' "
357 "AND JobMedia.JobId=Job.JobId "
358 "AND Media.Enabled=1 "
359 "AND JobMedia.MediaId=Media.MediaId "
360 "AND Job.FileSetId=FileSet.FileSetId "
361 "AND FileSet.FileSet='%s' "
363 "ORDER BY Job.JobTDate DESC LIMIT 1";
365 const char *uar_full =
366 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,"
367 "Job.ClientId,Job.Level,Job.JobFiles,Job.JobBytes,"
368 "StartTime,VolumeName,JobMedia.StartFile,VolSessionId,VolSessionTime "
369 "FROM temp1,Job,JobMedia,Media WHERE temp1.JobId=Job.JobId "
370 "AND Level='F' AND JobStatus='T' AND Type='B' "
371 "AND Media.Enabled=1 "
372 "AND JobMedia.JobId=Job.JobId "
373 "AND JobMedia.MediaId=Media.MediaId";
375 const char *uar_dif =
376 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
377 "Job.Level,Job.JobFiles,Job.JobBytes,"
378 "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
379 "Job.VolSessionId,Job.VolSessionTime "
380 "FROM Job,JobMedia,Media,FileSet "
381 "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
382 "AND Job.ClientId=%s "
383 "AND JobMedia.JobId=Job.JobId "
384 "AND Media.Enabled=1 "
385 "AND JobMedia.MediaId=Media.MediaId "
386 "AND Job.Level='D' AND JobStatus='T' AND Type='B' "
387 "AND Job.FileSetId=FileSet.FileSetId "
388 "AND FileSet.FileSet='%s' "
390 "ORDER BY Job.JobTDate DESC LIMIT 1";
392 const char *uar_inc =
393 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
394 "Job.Level,Job.JobFiles,Job.JobBytes,"
395 "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
396 "Job.VolSessionId,Job.VolSessionTime "
397 "FROM Job,JobMedia,Media,FileSet "
398 "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
399 "AND Job.ClientId=%s "
400 "AND Media.Enabled=1 "
401 "AND JobMedia.JobId=Job.JobId "
402 "AND JobMedia.MediaId=Media.MediaId "
403 "AND Job.Level='I' AND JobStatus='T' AND Type='B' "
404 "AND Job.FileSetId=FileSet.FileSetId "
405 "AND FileSet.FileSet='%s' "
408 const char *uar_list_temp =
409 "SELECT DISTINCT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName"
411 " ORDER BY StartTime ASC";
414 const char *uar_sel_jobid_temp =
415 "SELECT DISTINCT JobId,StartTime FROM temp ORDER BY StartTime ASC";
417 const char *uar_sel_all_temp1 = "SELECT * FROM temp1";
419 const char *uar_sel_all_temp = "SELECT * FROM temp";
423 /* Select FileSet names for this Client */
424 const char *uar_sel_fileset =
425 "SELECT DISTINCT FileSet.FileSet FROM Job,"
426 "Client,FileSet WHERE Job.FileSetId=FileSet.FileSetId "
427 "AND Job.ClientId=%s AND Client.ClientId=%s "
428 "ORDER BY FileSet.FileSet";
430 /* Find MediaType used by this Job */
431 const char *uar_mediatype =
432 "SELECT MediaType FROM JobMedia,Media WHERE JobMedia.JobId=%s "
433 "AND JobMedia.MediaId=Media.MediaId";
436 * Find JobId, FileIndex for a given path/file and date
437 * for use when inserting individual files into the tree.
439 const char *uar_jobid_fileindex =
440 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
441 "WHERE Job.JobId=File.JobId "
442 "AND Job.StartTime<='%s' "
443 "AND Path.Path='%s' "
444 "AND Filename.Name='%s' "
445 "AND Client.Name='%s' "
446 "AND Job.ClientId=Client.ClientId "
447 "AND Path.PathId=File.PathId "
448 "AND Filename.FilenameId=File.FilenameId "
449 "ORDER BY Job.StartTime DESC LIMIT 1";
451 const char *uar_jobids_fileindex =
452 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
453 "WHERE Job.JobId IN (%s) "
454 "AND Job.JobId=File.JobId "
455 "AND Job.StartTime<='%s' "
456 "AND Path.Path='%s' "
457 "AND Filename.Name='%s' "
458 "AND Client.Name='%s' "
459 "AND Job.ClientId=Client.ClientId "
460 "AND Path.PathId=File.PathId "
461 "AND Filename.FilenameId=File.FilenameId "
462 "ORDER BY Job.StartTime DESC LIMIT 1";
464 /* Query to get all files in a directory -- no recursing
465 * Note, for PostgreSQL since it respects the "Single Value
466 * rule", the results of the SELECT will be unoptimized.
467 * I.e. the same file will be restored multiple times, once
468 * for each time it was backed up.
471 #ifdef HAVE_POSTGRESQL
472 const char *uar_jobid_fileindex_from_dir =
473 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
474 "WHERE Job.JobId IN (%s) "
475 "AND Job.JobId=File.JobId "
476 "AND Path.Path='%s' "
477 "AND Client.Name='%s' "
478 "AND Job.ClientId=Client.ClientId "
479 "AND Path.PathId=File.Pathid "
480 "AND Filename.FilenameId=File.FilenameId";
482 const char *uar_jobid_fileindex_from_dir =
483 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
484 "WHERE Job.JobId IN (%s) "
485 "AND Job.JobId=File.JobId "
486 "AND Path.Path='%s' "
487 "AND Client.Name='%s' "
488 "AND Job.ClientId=Client.ClientId "
489 "AND Path.PathId=File.Pathid "
490 "AND Filename.FilenameId=File.FilenameId "
491 "GROUP BY File.FileIndex ";
494 /* Query to get list of files from table -- presuably built by an external program */
495 const char *uar_jobid_fileindex_from_table =
496 "SELECT JobId,FileIndex from %s";