]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_cmds.c
ebl fix escape bug
[bacula/bacula] / bacula / src / cats / sql_cmds.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2002-2007 Free Software Foundation Europe e.V.
5
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.
12
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.
17
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
21    02110-1301, USA.
22
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.
27 */
28 /*
29  *
30  *  This file contains all the SQL commands issued by the Director
31  *
32  *     Kern Sibbald, July MMII
33  *
34  *   Version $Id$
35  */
36
37 #include "bacula.h"
38 #include "cats.h"
39
40 /* For ua_update.c */
41 const char *list_pool = "SELECT * FROM Pool WHERE PoolId=%s";
42
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'"
49    " AND FileSet='%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";
55
56
57 /* ====== ua_prune.c */
58
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
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";
70
71 /* Count Select JobIds for File deletion */
72 const char *count_select_job = 
73    "SELECT count(*) from Job "
74    "WHERE JobTDate<%s "
75    "AND ClientId=%s "
76    "AND PurgedFiles=0";
77
78
79 /* Select JobIds for File deletion. */
80 const char *select_job =
81    "SELECT DISTINCT JobId from Job "
82    "WHERE JobTDate<%s "
83    "AND ClientId=%s "
84    "AND PurgedFiles=0";
85
86 /* Delete temp tables and indexes  */
87 const char *drop_deltabs[] = {
88    "DROP TABLE DelCandidates",
89    "DROP INDEX DelInx1",
90    NULL};
91
92
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, "
106       "JobFiles INTEGER, "
107       "JobStatus char(1))",
108 #else
109       "JobId INTEGER UNSIGNED NOT NULL, "
110       "PurgedFiles TINYINT, "
111       "FileSetId INTEGER UNSIGNED, "
112       "JobFiles INTEGER UNSIGNED, "
113       "JobStatus CHAR)",
114 #endif
115    "CREATE INDEX DelInx1 ON DelCandidates (JobId)",
116    NULL};
117
118 /* Fill candidates table with all Jobs subject to being deleted.
119  *  This is used for pruning Jobs (first the files, then the Jobs).
120  */
121 const char *insert_delcand =
122    "INSERT INTO DelCandidates "
123    "SELECT JobId,PurgedFiles,FileSetId,JobFiles,JobStatus FROM Job "
124    "WHERE Type='%c' "
125    "AND JobTDate<%s "
126    "AND ClientId=%s";
127
128 /*
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.
134  */
135 #ifdef old_way
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 "
141    "(Job.JobTDate>%s "
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)";
145
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.
149  */
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 "
154    "(Job.JobTDate>%s "
155    "AND Job.ClientId=%s "
156    "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
157    "AND Job.FileSetId=DelCandidates.FileSetId)";
158
159
160 /* Select Jobs from the DelCandidates table.
161  * This is the list of Jobs to delete for a Restore Job.
162  */
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 "
167    "(Job.JobTDate>%s "
168    "AND Job.ClientId=%s "
169    "AND Job.Type='R')";
170
171 /* Select Jobs from the DelCandidates table.
172  * This is the list of Jobs to delete for an Admin Job.
173  */
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 "
178    "(Job.JobTDate>%s "
179    "AND Job.ClientId=%s "
180    "AND Job.Type='D')";
181
182 /*
183  * Select Jobs from the DelCandidates table.
184  * This is the list of Jobs to delete for an Migrate Job.
185  */
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 "
190    "(Job.JobTDate>%s "
191    "AND Job.ClientId=%s "
192    "AND Job.Type='g')";
193
194 #else
195 /* Faster way */
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 "
201    "(Job.JobTDate>%s "
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)";
205
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.
209  */
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 "
214    "(Job.JobTDate>%s "
215    "AND Job.ClientId=%s "
216    "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
217    "AND Job.FileSetId=DelCandidates.FileSetId)";
218
219
220 /* Select Jobs from the DelCandidates table.
221  * This is the list of Jobs to delete for a Restore Job.
222  */
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 "
227    "(Job.JobTDate>%s "
228    "AND Job.ClientId=%s "
229    "AND Job.Type='R')";
230
231 /* Select Jobs from the DelCandidates table.
232  * This is the list of Jobs to delete for an Admin Job.
233  */
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 "
238    "(Job.JobTDate>%s "
239    "AND Job.ClientId=%s "
240    "AND Job.Type='D')";
241
242 /*
243  * Select Jobs from the DelCandidates table.
244  * This is the list of Jobs to delete for an Migrate Job.
245  */
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 "
250    "(Job.JobTDate>%s "
251    "AND Job.ClientId=%s "
252    "AND Job.Type='g')";
253
254 #endif
255
256 /* ======= ua_restore.c */
257 const char *uar_count_files =
258    "SELECT JobFiles FROM Job WHERE JobId=%s";
259
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";
266
267 #ifdef HAVE_MYSQL
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";
279 #else
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";
290 #endif
291
292
293 /*
294  * Find all files for a particular JobId and insert them into
295  *  the tree during a restore.
296  */
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";
302
303 const char *uar_del_temp  = "DROP TABLE temp";
304 const char *uar_del_temp1 = "DROP TABLE temp1";
305
306 const char *uar_create_temp =
307    "CREATE TEMPORARY TABLE temp ("
308 #ifdef HAVE_POSTGRESQL
309    "JobId INTEGER NOT NULL,"
310    "JobTDate BIGINT,"
311    "ClientId INTEGER,"
312    "Level CHAR,"
313    "JobFiles INTEGER,"
314    "JobBytes BIGINT,"
315    "StartTime TEXT,"
316    "VolumeName TEXT,"
317    "StartFile INTEGER,"
318    "VolSessionId INTEGER,"
319    "VolSessionTime INTEGER)";
320 #else
321    "JobId INTEGER UNSIGNED NOT NULL,"
322    "JobTDate BIGINT UNSIGNED,"
323    "ClientId INTEGER UNSIGNED,"
324    "Level CHAR,"
325    "JobFiles INTEGER UNSIGNED,"
326    "JobBytes BIGINT UNSIGNED,"
327    "StartTime TEXT,"
328    "VolumeName TEXT,"
329    "StartFile INTEGER UNSIGNED,"
330    "VolSessionId INTEGER UNSIGNED,"
331    "VolSessionTime INTEGER UNSIGNED)";
332 #endif
333
334 const char *uar_create_temp1 =
335    "CREATE TEMPORARY TABLE temp1 ("
336 #ifdef HAVE_POSTGRESQL
337    "JobId INTEGER NOT NULL,"
338    "JobTDate BIGINT)";
339 #else
340    "JobId INTEGER UNSIGNED NOT NULL,"
341    "JobTDate BIGINT UNSIGNED)";
342 #endif
343
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' "
355    "%s"
356    "ORDER BY Job.JobTDate DESC LIMIT 1";
357
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";
367
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' "
382    "%s"
383    "ORDER BY Job.JobTDate DESC LIMIT 1";
384
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' "
399    "%s";
400
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.
404  */
405 const char *uar_list_temp =
406    "SELECT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName,StartFile"
407    " FROM temp"
408    " ORDER BY StartTime,StartFile ASC";
409 #else
410 const char *uar_list_temp =
411    "SELECT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName,StartFile"
412    " FROM temp"
413    " GROUP BY JobId ORDER BY StartTime,StartFile ASC";
414 #endif
415
416
417 const char *uar_sel_jobid_temp = "SELECT JobId FROM temp ORDER BY StartTime ASC";
418
419 const char *uar_sel_all_temp1 = "SELECT * FROM temp1";
420
421 const char *uar_sel_all_temp = "SELECT * FROM temp";
422
423
424
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";
431
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";
436
437 /*
438  *  Find JobId, FileIndex for a given path/file and date
439  *  for use when inserting individual files into the tree.
440  */
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";
452
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";
465
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.
471  */
472
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"; 
483 #else
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 ";
494 #endif
495  
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";