3 * This file contains all the SQL commands issued by the Director
5 * Kern Sibbald, July MMII
10 Bacula® - The Network Backup Solution
12 Copyright (C) 2002-2006 Free Software Foundation Europe e.V.
14 The main author of Bacula is Kern Sibbald, with contributions from
15 many others, a complete list can be found in the file AUTHORS.
16 This program is Free Software; you can redistribute it and/or
17 modify it under the terms of version two of the GNU General Public
18 License as published by the Free Software Foundation plus additions
19 that are listed in the file LICENSE.
21 This program is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 Bacula® is a registered trademark of John Walker.
32 The licensor of Bacula is the Free Software Foundation Europe
33 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
34 Switzerland, email:ftf@fsfeurope.org.
41 const char *list_pool = "SELECT * FROM Pool WHERE PoolId=%s";
43 /* For ua_dotcmds.c */
44 const char *client_backups =
45 "SELECT DISTINCT Job.JobId,Client.Name as Client,Level,StartTime,"
46 "JobFiles,JobBytes,VolumeName,MediaType,FileSet,Media.Enabled as Enabled"
47 " FROM Client,Job,JobMedia,Media,FileSet"
48 " WHERE Client.Name='%s'"
50 " AND Client.ClientId=Job.ClientId"
51 " AND JobStatus='T' AND Type='B'"
52 " AND JobMedia.JobId=Job.JobId AND JobMedia.MediaId=Media.MediaId"
53 " AND Job.FileSetId=FileSet.FileSetId"
54 " ORDER BY Job.StartTime";
57 /* ====== ua_prune.c */
59 const char *del_File = "DELETE FROM File WHERE JobId=%s";
60 const char *upd_Purged = "UPDATE Job Set PurgedFiles=1 WHERE JobId=%s";
61 const char *cnt_DelCand = "SELECT count(*) FROM DelCandidates";
62 const char *del_Job = "DELETE FROM Job WHERE JobId=%s";
63 const char *del_JobMedia = "DELETE FROM JobMedia WHERE JobId=%s";
64 const char *cnt_JobMedia = "SELECT count(*) FROM JobMedia WHERE MediaId=%s";
65 const char *sel_JobMedia = "SELECT JobId FROM JobMedia WHERE MediaId=%s";
67 /* Select JobIds for File deletion. */
68 const char *select_job =
69 "SELECT JobId from Job "
74 /* Delete temp tables and indexes */
75 const char *drop_deltabs[] = {
76 "DROP TABLE DelCandidates",
81 /* List of SQL commands to create temp table and indicies */
82 const char *create_deltabs[] = {
83 "CREATE TEMPORARY TABLE DelCandidates ("
84 #if defined(HAVE_MYSQL)
85 "JobId INTEGER UNSIGNED NOT NULL, "
86 "PurgedFiles TINYINT, "
87 "FileSetId INTEGER UNSIGNED, "
88 "JobFiles INTEGER UNSIGNED, "
89 "JobStatus BINARY(1))",
90 #elif defined(HAVE_POSTGRESQL)
91 "JobId INTEGER NOT NULL, "
92 "PurgedFiles SMALLINT, "
97 "JobId INTEGER UNSIGNED NOT NULL, "
98 "PurgedFiles TINYINT, "
99 "FileSetId INTEGER UNSIGNED, "
100 "JobFiles INTEGER UNSIGNED, "
103 "CREATE INDEX DelInx1 ON DelCandidates (JobId)",
106 /* Fill candidates table with all Jobs subject to being deleted.
107 * This is used for pruning Jobs (first the files, then the Jobs).
109 const char *insert_delcand =
110 "INSERT INTO DelCandidates "
111 "SELECT JobId,PurgedFiles,FileSetId,JobFiles,JobStatus FROM Job "
117 * Select Jobs from the DelCandidates table that have a
118 * more recent backup -- i.e. are not the only backup.
119 * This is the list of Jobs to delete for a Backup Job.
120 * At the same time, we select "orphanned" jobs
121 * (i.e. no files, ...) for deletion.
124 const char *select_backup_del =
125 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
126 "FROM Job,DelCandidates "
127 "WHERE (Job.JobTDate<%s AND ((DelCandidates.JobFiles=0) OR "
128 "(DelCandidates.JobStatus!='T'))) OR "
130 "AND Job.ClientId=%s "
131 "AND Job.Level='F' AND Job.JobStatus='T' AND Job.Type IN ('B','M') "
132 "AND Job.FileSetId=DelCandidates.FileSetId)";
134 /* Select Jobs from the DelCandidates table that have a
135 * more recent InitCatalog -- i.e. are not the only InitCatalog
136 * This is the list of Jobs to delete for a Verify Job.
138 const char *select_verify_del =
139 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
140 "FROM Job,DelCandidates "
141 "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
143 "AND Job.ClientId=%s "
144 "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
145 "AND Job.FileSetId=DelCandidates.FileSetId)";
148 /* Select Jobs from the DelCandidates table.
149 * This is the list of Jobs to delete for a Restore Job.
151 const char *select_restore_del =
152 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
153 "FROM Job,DelCandidates "
154 "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
156 "AND Job.ClientId=%s "
159 /* Select Jobs from the DelCandidates table.
160 * This is the list of Jobs to delete for an Admin Job.
162 const char *select_admin_del =
163 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
164 "FROM Job,DelCandidates "
165 "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
167 "AND Job.ClientId=%s "
171 * Select Jobs from the DelCandidates table.
172 * This is the list of Jobs to delete for an Migrate Job.
174 const char *select_migrate_del =
175 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
176 "FROM Job,DelCandidates "
177 "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
179 "AND Job.ClientId=%s "
184 const char *select_backup_del =
185 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
186 "FROM Job,DelCandidates "
187 "WHERE (Job.JobId=DelCandidates.JobId AND ((DelCandidates.JobFiles=0) OR "
188 "(DelCandidates.JobStatus!='T'))) OR "
190 "AND Job.ClientId=%s "
191 "AND Job.Level='F' AND Job.JobStatus='T' AND Job.Type IN ('B','M') "
192 "AND Job.FileSetId=DelCandidates.FileSetId)";
194 /* Select Jobs from the DelCandidates table that have a
195 * more recent InitCatalog -- i.e. are not the only InitCatalog
196 * This is the list of Jobs to delete for a Verify Job.
198 const char *select_verify_del =
199 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
200 "FROM Job,DelCandidates "
201 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
203 "AND Job.ClientId=%s "
204 "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
205 "AND Job.FileSetId=DelCandidates.FileSetId)";
208 /* Select Jobs from the DelCandidates table.
209 * This is the list of Jobs to delete for a Restore Job.
211 const char *select_restore_del =
212 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
213 "FROM Job,DelCandidates "
214 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
216 "AND Job.ClientId=%s "
219 /* Select Jobs from the DelCandidates table.
220 * This is the list of Jobs to delete for an Admin Job.
222 const char *select_admin_del =
223 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
224 "FROM Job,DelCandidates "
225 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
227 "AND Job.ClientId=%s "
231 * Select Jobs from the DelCandidates table.
232 * This is the list of Jobs to delete for an Migrate Job.
234 const char *select_migrate_del =
235 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
236 "FROM Job,DelCandidates "
237 "WHERE (Job.JobId=DelCanditates.JobId AND DelCandidates.JobStatus!='T') OR "
239 "AND Job.ClientId=%s "
244 /* ======= ua_restore.c */
245 const char *uar_count_files =
246 "SELECT JobFiles FROM Job WHERE JobId=%s";
248 /* List last 20 Jobs */
249 const char *uar_list_jobs =
250 "SELECT JobId,Client.Name as Client,StartTime,Level as "
251 "JobLevel,JobFiles,JobBytes "
252 "FROM Client,Job WHERE Client.ClientId=Job.ClientId AND JobStatus='T' "
253 "AND Type='B' ORDER BY StartTime DESC LIMIT 20";
256 /* MYSQL IS NOT STANDARD SQL !!!!! */
257 /* List Jobs where a particular file is saved */
258 const char *uar_file =
259 "SELECT Job.JobId as JobId,"
260 "CONCAT(Path.Path,Filename.Name) as Name, "
261 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
262 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
263 "AND Client.ClientId=Job.ClientId "
264 "AND Job.JobId=File.JobId "
265 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
266 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20";
268 /* List Jobs where a particular file is saved */
269 const char *uar_file =
270 "SELECT Job.JobId as JobId,"
271 "Path.Path||Filename.Name as Name, "
272 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
273 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
274 "AND Client.ClientId=Job.ClientId "
275 "AND Job.JobId=File.JobId "
276 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
277 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20";
282 * Find all files for a particular JobId and insert them into
283 * the tree during a restore.
285 const char *uar_sel_files =
286 "SELECT Path.Path,Filename.Name,FileIndex,JobId,LStat "
287 "FROM File,Filename,Path "
288 "WHERE File.JobId=%s AND Filename.FilenameId=File.FilenameId "
289 "AND Path.PathId=File.PathId";
291 const char *uar_del_temp = "DROP TABLE temp";
292 const char *uar_del_temp1 = "DROP TABLE temp1";
294 const char *uar_create_temp =
295 "CREATE TEMPORARY TABLE temp ("
296 #ifdef HAVE_POSTGRESQL
297 "JobId INTEGER NOT NULL,"
306 "VolSessionId INTEGER,"
307 "VolSessionTime INTEGER)";
309 "JobId INTEGER UNSIGNED NOT NULL,"
310 "JobTDate BIGINT UNSIGNED,"
311 "ClientId INTEGER UNSIGNED,"
313 "JobFiles INTEGER UNSIGNED,"
314 "JobBytes BIGINT UNSIGNED,"
317 "StartFile INTEGER UNSIGNED,"
318 "VolSessionId INTEGER UNSIGNED,"
319 "VolSessionTime INTEGER UNSIGNED)";
322 const char *uar_create_temp1 =
323 "CREATE TEMPORARY TABLE temp1 ("
324 #ifdef HAVE_POSTGRESQL
325 "JobId INTEGER NOT NULL,"
328 "JobId INTEGER UNSIGNED NOT NULL,"
329 "JobTDate BIGINT UNSIGNED)";
332 const char *uar_last_full =
333 "INSERT INTO temp1 SELECT Job.JobId,JobTdate "
334 "FROM Client,Job,JobMedia,Media,FileSet WHERE Client.ClientId=%s "
335 "AND Job.ClientId=%s "
336 "AND Job.StartTime<'%s' "
337 "AND Level='F' AND JobStatus='T' AND Type='B' "
338 "AND JobMedia.JobId=Job.JobId "
339 "AND Media.Enabled=1 "
340 "AND JobMedia.MediaId=Media.MediaId "
341 "AND Job.FileSetId=FileSet.FileSetId "
342 "AND FileSet.FileSet='%s' "
344 "ORDER BY Job.JobTDate DESC LIMIT 1";
346 const char *uar_full =
347 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,"
348 "Job.ClientId,Job.Level,Job.JobFiles,Job.JobBytes,"
349 "StartTime,VolumeName,JobMedia.StartFile,VolSessionId,VolSessionTime "
350 "FROM temp1,Job,JobMedia,Media WHERE temp1.JobId=Job.JobId "
351 "AND Level='F' AND JobStatus='T' AND Type='B' "
352 "AND Media.Enabled=1 "
353 "AND JobMedia.JobId=Job.JobId "
354 "AND JobMedia.MediaId=Media.MediaId";
356 const char *uar_dif =
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 JobMedia.JobId=Job.JobId "
365 "AND Media.Enabled=1 "
366 "AND JobMedia.MediaId=Media.MediaId "
367 "AND Job.Level='D' AND JobStatus='T' AND Type='B' "
368 "AND Job.FileSetId=FileSet.FileSetId "
369 "AND FileSet.FileSet='%s' "
371 "ORDER BY Job.JobTDate DESC LIMIT 1";
373 const char *uar_inc =
374 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
375 "Job.Level,Job.JobFiles,Job.JobBytes,"
376 "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
377 "Job.VolSessionId,Job.VolSessionTime "
378 "FROM Job,JobMedia,Media,FileSet "
379 "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
380 "AND Job.ClientId=%s "
381 "AND Media.Enabled=1 "
382 "AND JobMedia.JobId=Job.JobId "
383 "AND JobMedia.MediaId=Media.MediaId "
384 "AND Job.Level='I' AND JobStatus='T' AND Type='B' "
385 "AND Job.FileSetId=FileSet.FileSetId "
386 "AND FileSet.FileSet='%s' "
389 #ifdef HAVE_POSTGRESQL
390 /* Note, the PostgreSQL will have a much uglier looking
391 * list since it cannot do GROUP BY of different values.
393 const char *uar_list_temp =
394 "SELECT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName,StartFile"
396 " ORDER BY StartTime,StartFile ASC";
398 const char *uar_list_temp =
399 "SELECT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName,StartFile"
401 " GROUP BY JobId ORDER BY StartTime,StartFile ASC";
405 const char *uar_sel_jobid_temp = "SELECT JobId FROM temp ORDER BY StartTime ASC";
407 const char *uar_sel_all_temp1 = "SELECT * FROM temp1";
409 const char *uar_sel_all_temp = "SELECT * FROM temp";
413 /* Select FileSet names for this Client */
414 const char *uar_sel_fileset =
415 "SELECT DISTINCT FileSet.FileSet FROM Job,"
416 "Client,FileSet WHERE Job.FileSetId=FileSet.FileSetId "
417 "AND Job.ClientId=%s AND Client.ClientId=%s "
418 "ORDER BY FileSet.FileSet";
420 /* Find MediaType used by this Job */
421 const char *uar_mediatype =
422 "SELECT MediaType FROM JobMedia,Media WHERE JobMedia.JobId=%s "
423 "AND JobMedia.MediaId=Media.MediaId";
426 * Find JobId, FileIndex for a given path/file and date
427 * for use when inserting individual files into the tree.
429 const char *uar_jobid_fileindex =
430 "SELECT Job.JobId, File.FileIndex FROM Job,File,Path,Filename,Client "
431 "WHERE Job.JobId=File.JobId "
432 "AND Job.StartTime<'%s' "
433 "AND Path.Path='%s' "
434 "AND Filename.Name='%s' "
435 "AND Client.Name='%s' "
436 "AND Job.ClientId=Client.ClientId "
437 "AND Path.PathId=File.PathId "
438 "AND Filename.FilenameId=File.FilenameId "
439 "ORDER BY Job.StartTime DESC LIMIT 1";
441 const char *uar_jobids_fileindex =
442 "SELECT Job.JobId, File.FileIndex FROM Job,File,Path,Filename,Client "
443 "WHERE Job.JobId IN (%s) "
444 "AND Job.JobId=File.JobId "
445 "AND Job.StartTime<'%s' "
446 "AND Path.Path='%s' "
447 "AND Filename.Name='%s' "
448 "AND Client.Name='%s' "
449 "AND Job.ClientId=Client.ClientId "
450 "AND Path.PathId=File.PathId "
451 "AND Filename.FilenameId=File.FilenameId "
452 "ORDER BY Job.StartTime DESC LIMIT 1";
454 /* Query to get all files in a directory -- no recursing
455 * Note, for PostgreSQL since it respects the "Single Value
456 * rule", the results of the SELECT will be unoptimized.
457 * I.e. the same file will be restored multiple times, once
458 * for each time it was backed up.
461 #ifdef HAVE_POSTGRESQL
462 const char *uar_jobid_fileindex_from_dir =
463 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
464 "WHERE Job.JobId IN (%s) "
465 "AND Job.JobId=File.JobId "
466 "AND Path.Path='%s' "
467 "AND Client.Name='%s' "
468 "AND Job.ClientId=Client.ClientId "
469 "AND Path.PathId=File.Pathid "
470 "AND Filename.FilenameId=File.FilenameId";
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 "
481 "GROUP BY File.FileIndex ";
484 /* Query to get list of files from table -- presuably built by an external program */
485 const char *uar_jobid_fileindex_from_table =
486 "SELECT JobId, FileIndex from %s";