]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_cmds.c
ebl add RecyclePool to Pool. Media will take Pool.RecyclePool
[bacula/bacula] / bacula / src / cats / sql_cmds.c
1 /*
2  *
3  *  This file contains all the SQL commands issued by the Director
4  *
5  *     Kern Sibbald, July MMII
6  *
7  *   Version $Id$
8  */
9 /*
10    Bacula® - The Network Backup Solution
11
12    Copyright (C) 2002-2006 Free Software Foundation Europe e.V.
13
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.
20
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.
25
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
29    02110-1301, USA.
30
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.
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 const char *sel_JobMedia = "SELECT JobId FROM JobMedia WHERE MediaId=%s";
66
67 /* Select JobIds for File deletion. */
68 const char *select_job =
69    "SELECT JobId from Job "
70    "WHERE JobTDate<%s "
71    "AND ClientId=%s "
72    "AND PurgedFiles=0";
73
74 /* Delete temp tables and indexes  */
75 const char *drop_deltabs[] = {
76    "DROP TABLE DelCandidates",
77    "DROP INDEX DelInx1",
78    NULL};
79
80
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, "
93       "FileSetId INTEGER, "
94       "JobFiles INTEGER, "
95       "JobStatus char(1))",
96 #else
97       "JobId INTEGER UNSIGNED NOT NULL, "
98       "PurgedFiles TINYINT, "
99       "FileSetId INTEGER UNSIGNED, "
100       "JobFiles INTEGER UNSIGNED, "
101       "JobStatus CHAR)",
102 #endif
103    "CREATE INDEX DelInx1 ON DelCandidates (JobId)",
104    NULL};
105
106 /* Fill candidates table with all Jobs subject to being deleted.
107  *  This is used for pruning Jobs (first the files, then the Jobs).
108  */
109 const char *insert_delcand =
110    "INSERT INTO DelCandidates "
111    "SELECT JobId,PurgedFiles,FileSetId,JobFiles,JobStatus FROM Job "
112    "WHERE Type='%c' "
113    "AND JobTDate<%s "
114    "AND ClientId=%s";
115
116 /*
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.
122  */
123 #ifdef old_way
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 "
129    "(Job.JobTDate>%s "
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)";
133
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.
137  */
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 "
142    "(Job.JobTDate>%s "
143    "AND Job.ClientId=%s "
144    "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
145    "AND Job.FileSetId=DelCandidates.FileSetId)";
146
147
148 /* Select Jobs from the DelCandidates table.
149  * This is the list of Jobs to delete for a Restore Job.
150  */
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 "
155    "(Job.JobTDate>%s "
156    "AND Job.ClientId=%s "
157    "AND Job.Type='R')";
158
159 /* Select Jobs from the DelCandidates table.
160  * This is the list of Jobs to delete for an Admin Job.
161  */
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 "
166    "(Job.JobTDate>%s "
167    "AND Job.ClientId=%s "
168    "AND Job.Type='D')";
169
170 /*
171  * Select Jobs from the DelCandidates table.
172  * This is the list of Jobs to delete for an Migrate Job.
173  */
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 "
178    "(Job.JobTDate>%s "
179    "AND Job.ClientId=%s "
180    "AND Job.Type='g')";
181
182 #else
183 /* Faster way */
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 "
189    "(Job.JobTDate>%s "
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)";
193
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.
197  */
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 "
202    "(Job.JobTDate>%s "
203    "AND Job.ClientId=%s "
204    "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
205    "AND Job.FileSetId=DelCandidates.FileSetId)";
206
207
208 /* Select Jobs from the DelCandidates table.
209  * This is the list of Jobs to delete for a Restore Job.
210  */
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 "
215    "(Job.JobTDate>%s "
216    "AND Job.ClientId=%s "
217    "AND Job.Type='R')";
218
219 /* Select Jobs from the DelCandidates table.
220  * This is the list of Jobs to delete for an Admin Job.
221  */
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 "
226    "(Job.JobTDate>%s "
227    "AND Job.ClientId=%s "
228    "AND Job.Type='D')";
229
230 /*
231  * Select Jobs from the DelCandidates table.
232  * This is the list of Jobs to delete for an Migrate Job.
233  */
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 "
238    "(Job.JobTDate>%s "
239    "AND Job.ClientId=%s "
240    "AND Job.Type='g')";
241
242 #endif
243
244 /* ======= ua_restore.c */
245 const char *uar_count_files =
246    "SELECT JobFiles FROM Job WHERE JobId=%s";
247
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";
254
255 #ifdef HAVE_MYSQL
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";
267 #else
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";
278 #endif
279
280
281 /*
282  * Find all files for a particular JobId and insert them into
283  *  the tree during a restore.
284  */
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";
290
291 const char *uar_del_temp  = "DROP TABLE temp";
292 const char *uar_del_temp1 = "DROP TABLE temp1";
293
294 const char *uar_create_temp =
295    "CREATE TEMPORARY TABLE temp ("
296 #ifdef HAVE_POSTGRESQL
297    "JobId INTEGER NOT NULL,"
298    "JobTDate BIGINT,"
299    "ClientId INTEGER,"
300    "Level CHAR,"
301    "JobFiles INTEGER,"
302    "JobBytes BIGINT,"
303    "StartTime TEXT,"
304    "VolumeName TEXT,"
305    "StartFile INTEGER,"
306    "VolSessionId INTEGER,"
307    "VolSessionTime INTEGER)";
308 #else
309    "JobId INTEGER UNSIGNED NOT NULL,"
310    "JobTDate BIGINT UNSIGNED,"
311    "ClientId INTEGER UNSIGNED,"
312    "Level CHAR,"
313    "JobFiles INTEGER UNSIGNED,"
314    "JobBytes BIGINT UNSIGNED,"
315    "StartTime TEXT,"
316    "VolumeName TEXT,"
317    "StartFile INTEGER UNSIGNED,"
318    "VolSessionId INTEGER UNSIGNED,"
319    "VolSessionTime INTEGER UNSIGNED)";
320 #endif
321
322 const char *uar_create_temp1 =
323    "CREATE TEMPORARY TABLE temp1 ("
324 #ifdef HAVE_POSTGRESQL
325    "JobId INTEGER NOT NULL,"
326    "JobTDate BIGINT)";
327 #else
328    "JobId INTEGER UNSIGNED NOT NULL,"
329    "JobTDate BIGINT UNSIGNED)";
330 #endif
331
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' "
343    "%s"
344    "ORDER BY Job.JobTDate DESC LIMIT 1";
345
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";
355
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' "
370    "%s"
371    "ORDER BY Job.JobTDate DESC LIMIT 1";
372
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' "
387    "%s";
388
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.
392  */
393 const char *uar_list_temp =
394    "SELECT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName,StartFile"
395    " FROM temp"
396    " ORDER BY StartTime,StartFile ASC";
397 #else
398 const char *uar_list_temp =
399    "SELECT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName,StartFile"
400    " FROM temp"
401    " GROUP BY JobId ORDER BY StartTime,StartFile ASC";
402 #endif
403
404
405 const char *uar_sel_jobid_temp = "SELECT JobId FROM temp ORDER BY StartTime ASC";
406
407 const char *uar_sel_all_temp1 = "SELECT * FROM temp1";
408
409 const char *uar_sel_all_temp = "SELECT * FROM temp";
410
411
412
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";
419
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";
424
425 /*
426  *  Find JobId, FileIndex for a given path/file and date
427  *  for use when inserting individual files into the tree.
428  */
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";
440
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";
453
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.
459  */
460
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"; 
471 #else
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 ";
482 #endif
483  
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";