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 plus additions
11 that are listed in the file LICENSE.
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
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";
66 const char *sel_JobMedia =
67 "SELECT DISTINCT JobMedia.JobId FROM JobMedia,Job "
68 "WHERE MediaId=%s AND Job.JobId=JobMedia.JobId "
69 "AND Job.JobTDate<%s";
71 /* Count Select JobIds for File deletion */
72 const char *count_select_job =
73 "SELECT count(*) from Job "
79 /* Select JobIds for File deletion. */
80 const char *select_job =
81 "SELECT DISTINCT JobId from Job "
86 /* Delete temp tables and indexes */
87 const char *drop_deltabs[] = {
88 "DROP TABLE DelCandidates",
93 /* List of SQL commands to create temp table and indicies */
94 const char *create_deltabs[] = {
95 "CREATE TEMPORARY TABLE DelCandidates ("
96 #if defined(HAVE_MYSQL)
97 "JobId INTEGER UNSIGNED NOT NULL, "
98 "PurgedFiles TINYINT, "
99 "FileSetId INTEGER UNSIGNED, "
100 "JobFiles INTEGER UNSIGNED, "
101 "JobStatus BINARY(1))",
102 #elif defined(HAVE_POSTGRESQL)
103 "JobId INTEGER NOT NULL, "
104 "PurgedFiles SMALLINT, "
105 "FileSetId INTEGER, "
107 "JobStatus char(1))",
109 "JobId INTEGER UNSIGNED NOT NULL, "
110 "PurgedFiles TINYINT, "
111 "FileSetId INTEGER UNSIGNED, "
112 "JobFiles INTEGER UNSIGNED, "
115 "CREATE INDEX DelInx1 ON DelCandidates (JobId)",
118 /* Fill candidates table with all Jobs subject to being deleted.
119 * This is used for pruning Jobs (first the files, then the Jobs).
121 const char *insert_delcand =
122 "INSERT INTO DelCandidates "
123 "SELECT JobId,PurgedFiles,FileSetId,JobFiles,JobStatus FROM Job "
129 * Select Jobs from the DelCandidates table that have a
130 * more recent backup -- i.e. are not the only backup.
131 * This is the list of Jobs to delete for a Backup Job.
132 * At the same time, we select "orphanned" jobs
133 * (i.e. no files, ...) for deletion.
136 const char *select_backup_del =
137 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
138 "FROM Job,DelCandidates "
139 "WHERE (Job.JobTDate<%s AND ((DelCandidates.JobFiles=0) OR "
140 "(DelCandidates.JobStatus!='T'))) OR "
142 "AND Job.ClientId=%s "
143 "AND Job.Level='F' AND Job.JobStatus='T' AND Job.Type IN ('B','M') "
144 "AND Job.FileSetId=DelCandidates.FileSetId)";
146 /* Select Jobs from the DelCandidates table that have a
147 * more recent InitCatalog -- i.e. are not the only InitCatalog
148 * This is the list of Jobs to delete for a Verify Job.
150 const char *select_verify_del =
151 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
152 "FROM Job,DelCandidates "
153 "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
155 "AND Job.ClientId=%s "
156 "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
157 "AND Job.FileSetId=DelCandidates.FileSetId)";
160 /* Select Jobs from the DelCandidates table.
161 * This is the list of Jobs to delete for a Restore Job.
163 const char *select_restore_del =
164 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
165 "FROM Job,DelCandidates "
166 "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
168 "AND Job.ClientId=%s "
171 /* Select Jobs from the DelCandidates table.
172 * This is the list of Jobs to delete for an Admin Job.
174 const char *select_admin_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 "
183 * Select Jobs from the DelCandidates table.
184 * This is the list of Jobs to delete for an Migrate Job.
186 const char *select_migrate_del =
187 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
188 "FROM Job,DelCandidates "
189 "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
191 "AND Job.ClientId=%s "
196 const char *select_backup_del =
197 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
198 "FROM Job,DelCandidates "
199 "WHERE (Job.JobId=DelCandidates.JobId AND ((DelCandidates.JobFiles=0) OR "
200 "(DelCandidates.JobStatus!='T'))) OR "
202 "AND Job.ClientId=%s "
203 "AND Job.Level='F' AND Job.JobStatus='T' AND Job.Type IN ('B','M') "
204 "AND Job.FileSetId=DelCandidates.FileSetId)";
206 /* Select Jobs from the DelCandidates table that have a
207 * more recent InitCatalog -- i.e. are not the only InitCatalog
208 * This is the list of Jobs to delete for a Verify Job.
210 const char *select_verify_del =
211 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
212 "FROM Job,DelCandidates "
213 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
215 "AND Job.ClientId=%s "
216 "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
217 "AND Job.FileSetId=DelCandidates.FileSetId)";
220 /* Select Jobs from the DelCandidates table.
221 * This is the list of Jobs to delete for a Restore Job.
223 const char *select_restore_del =
224 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
225 "FROM Job,DelCandidates "
226 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
228 "AND Job.ClientId=%s "
231 /* Select Jobs from the DelCandidates table.
232 * This is the list of Jobs to delete for an Admin Job.
234 const char *select_admin_del =
235 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
236 "FROM Job,DelCandidates "
237 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
239 "AND Job.ClientId=%s "
243 * Select Jobs from the DelCandidates table.
244 * This is the list of Jobs to delete for an Migrate Job.
246 const char *select_migrate_del =
247 "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
248 "FROM Job,DelCandidates "
249 "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
251 "AND Job.ClientId=%s "
256 /* ======= ua_restore.c */
257 const char *uar_count_files =
258 "SELECT JobFiles FROM Job WHERE JobId=%s";
260 /* List last 20 Jobs */
261 const char *uar_list_jobs =
262 "SELECT JobId,Client.Name as Client,StartTime,Level as "
263 "JobLevel,JobFiles,JobBytes "
264 "FROM Client,Job WHERE Client.ClientId=Job.ClientId AND JobStatus='T' "
265 "AND Type='B' ORDER BY StartTime DESC LIMIT 20";
268 /* MYSQL IS NOT STANDARD SQL !!!!! */
269 /* List Jobs where a particular file is saved */
270 const char *uar_file =
271 "SELECT Job.JobId as JobId,"
272 "CONCAT(Path.Path,Filename.Name) as Name, "
273 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
274 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
275 "AND Client.ClientId=Job.ClientId "
276 "AND Job.JobId=File.JobId "
277 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
278 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20";
280 /* List Jobs where a particular file is saved */
281 const char *uar_file =
282 "SELECT Job.JobId as JobId,"
283 "Path.Path||Filename.Name as Name, "
284 "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
285 "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
286 "AND Client.ClientId=Job.ClientId "
287 "AND Job.JobId=File.JobId "
288 "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
289 "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20";
294 * Find all files for a particular JobId and insert them into
295 * the tree during a restore.
297 const char *uar_sel_files =
298 "SELECT Path.Path,Filename.Name,FileIndex,JobId,LStat "
299 "FROM File,Filename,Path "
300 "WHERE File.JobId=%s AND Filename.FilenameId=File.FilenameId "
301 "AND Path.PathId=File.PathId";
303 const char *uar_del_temp = "DROP TABLE temp";
304 const char *uar_del_temp1 = "DROP TABLE temp1";
306 const char *uar_create_temp =
307 "CREATE TEMPORARY TABLE temp ("
308 #ifdef HAVE_POSTGRESQL
309 "JobId INTEGER NOT NULL,"
318 "VolSessionId INTEGER,"
319 "VolSessionTime INTEGER)";
321 "JobId INTEGER UNSIGNED NOT NULL,"
322 "JobTDate BIGINT UNSIGNED,"
323 "ClientId INTEGER UNSIGNED,"
325 "JobFiles INTEGER UNSIGNED,"
326 "JobBytes BIGINT UNSIGNED,"
329 "StartFile INTEGER UNSIGNED,"
330 "VolSessionId INTEGER UNSIGNED,"
331 "VolSessionTime INTEGER UNSIGNED)";
334 const char *uar_create_temp1 =
335 "CREATE TEMPORARY TABLE temp1 ("
336 #ifdef HAVE_POSTGRESQL
337 "JobId INTEGER NOT NULL,"
340 "JobId INTEGER UNSIGNED NOT NULL,"
341 "JobTDate BIGINT UNSIGNED)";
344 const char *uar_last_full =
345 "INSERT INTO temp1 SELECT Job.JobId,JobTdate "
346 "FROM Client,Job,JobMedia,Media,FileSet WHERE Client.ClientId=%s "
347 "AND Job.ClientId=%s "
348 "AND Job.StartTime<'%s' "
349 "AND Level='F' AND JobStatus='T' AND Type='B' "
350 "AND JobMedia.JobId=Job.JobId "
351 "AND Media.Enabled=1 "
352 "AND JobMedia.MediaId=Media.MediaId "
353 "AND Job.FileSetId=FileSet.FileSetId "
354 "AND FileSet.FileSet='%s' "
356 "ORDER BY Job.JobTDate DESC LIMIT 1";
358 const char *uar_full =
359 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,"
360 "Job.ClientId,Job.Level,Job.JobFiles,Job.JobBytes,"
361 "StartTime,VolumeName,JobMedia.StartFile,VolSessionId,VolSessionTime "
362 "FROM temp1,Job,JobMedia,Media WHERE temp1.JobId=Job.JobId "
363 "AND Level='F' AND JobStatus='T' AND Type='B' "
364 "AND Media.Enabled=1 "
365 "AND JobMedia.JobId=Job.JobId "
366 "AND JobMedia.MediaId=Media.MediaId";
368 const char *uar_dif =
369 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
370 "Job.Level,Job.JobFiles,Job.JobBytes,"
371 "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
372 "Job.VolSessionId,Job.VolSessionTime "
373 "FROM Job,JobMedia,Media,FileSet "
374 "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
375 "AND Job.ClientId=%s "
376 "AND JobMedia.JobId=Job.JobId "
377 "AND Media.Enabled=1 "
378 "AND JobMedia.MediaId=Media.MediaId "
379 "AND Job.Level='D' AND JobStatus='T' AND Type='B' "
380 "AND Job.FileSetId=FileSet.FileSetId "
381 "AND FileSet.FileSet='%s' "
383 "ORDER BY Job.JobTDate DESC LIMIT 1";
385 const char *uar_inc =
386 "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
387 "Job.Level,Job.JobFiles,Job.JobBytes,"
388 "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
389 "Job.VolSessionId,Job.VolSessionTime "
390 "FROM Job,JobMedia,Media,FileSet "
391 "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
392 "AND Job.ClientId=%s "
393 "AND Media.Enabled=1 "
394 "AND JobMedia.JobId=Job.JobId "
395 "AND JobMedia.MediaId=Media.MediaId "
396 "AND Job.Level='I' AND JobStatus='T' AND Type='B' "
397 "AND Job.FileSetId=FileSet.FileSetId "
398 "AND FileSet.FileSet='%s' "
401 #ifdef HAVE_POSTGRESQL
402 /* Note, the PostgreSQL will have a much uglier looking
403 * list since it cannot do GROUP BY of different values.
405 const char *uar_list_temp =
406 "SELECT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName,StartFile"
408 " ORDER BY StartTime,StartFile ASC";
410 const char *uar_list_temp =
411 "SELECT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName,StartFile"
413 " GROUP BY JobId ORDER BY StartTime,StartFile ASC";
417 const char *uar_sel_jobid_temp = "SELECT JobId FROM temp ORDER BY StartTime ASC";
419 const char *uar_sel_all_temp1 = "SELECT * FROM temp1";
421 const char *uar_sel_all_temp = "SELECT * FROM temp";
425 /* Select FileSet names for this Client */
426 const char *uar_sel_fileset =
427 "SELECT DISTINCT FileSet.FileSet FROM Job,"
428 "Client,FileSet WHERE Job.FileSetId=FileSet.FileSetId "
429 "AND Job.ClientId=%s AND Client.ClientId=%s "
430 "ORDER BY FileSet.FileSet";
432 /* Find MediaType used by this Job */
433 const char *uar_mediatype =
434 "SELECT MediaType FROM JobMedia,Media WHERE JobMedia.JobId=%s "
435 "AND JobMedia.MediaId=Media.MediaId";
438 * Find JobId, FileIndex for a given path/file and date
439 * for use when inserting individual files into the tree.
441 const char *uar_jobid_fileindex =
442 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
443 "WHERE Job.JobId=File.JobId "
444 "AND Job.StartTime<='%s' "
445 "AND Path.Path='%s' "
446 "AND Filename.Name='%s' "
447 "AND Client.Name='%s' "
448 "AND Job.ClientId=Client.ClientId "
449 "AND Path.PathId=File.PathId "
450 "AND Filename.FilenameId=File.FilenameId "
451 "ORDER BY Job.StartTime DESC LIMIT 1";
453 const char *uar_jobids_fileindex =
454 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
455 "WHERE Job.JobId IN (%s) "
456 "AND Job.JobId=File.JobId "
457 "AND Job.StartTime<='%s' "
458 "AND Path.Path='%s' "
459 "AND Filename.Name='%s' "
460 "AND Client.Name='%s' "
461 "AND Job.ClientId=Client.ClientId "
462 "AND Path.PathId=File.PathId "
463 "AND Filename.FilenameId=File.FilenameId "
464 "ORDER BY Job.StartTime DESC LIMIT 1";
466 /* Query to get all files in a directory -- no recursing
467 * Note, for PostgreSQL since it respects the "Single Value
468 * rule", the results of the SELECT will be unoptimized.
469 * I.e. the same file will be restored multiple times, once
470 * for each time it was backed up.
473 #ifdef HAVE_POSTGRESQL
474 const char *uar_jobid_fileindex_from_dir =
475 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
476 "WHERE Job.JobId IN (%s) "
477 "AND Job.JobId=File.JobId "
478 "AND Path.Path='%s' "
479 "AND Client.Name='%s' "
480 "AND Job.ClientId=Client.ClientId "
481 "AND Path.PathId=File.Pathid "
482 "AND Filename.FilenameId=File.FilenameId";
484 const char *uar_jobid_fileindex_from_dir =
485 "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
486 "WHERE Job.JobId IN (%s) "
487 "AND Job.JobId=File.JobId "
488 "AND Path.Path='%s' "
489 "AND Client.Name='%s' "
490 "AND Job.ClientId=Client.ClientId "
491 "AND Path.PathId=File.Pathid "
492 "AND Filename.FilenameId=File.FilenameId "
493 "GROUP BY File.FileIndex ";
496 /* Query to get list of files from table -- presuably built by an external program */
497 const char *uar_jobid_fileindex_from_table =
498 "SELECT JobId, FileIndex from %s";