]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_cmds.c
kes Implement new prunning code that prunes up to 1000 jobs at
[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 /* Count Select JobIds for File deletion */
68 const char *count_select_job = 
69    "SELECT count(*) from Job "
70    "WHERE JobTDate<%s "
71    "AND ClientId=%s "
72    "AND PurgedFiles=0";
73
74
75 /* Select JobIds for File deletion. */
76 const char *select_job =
77    "SELECT JobId from Job "
78    "WHERE JobTDate<%s "
79    "AND ClientId=%s "
80    "AND PurgedFiles=0";
81
82 /* Delete temp tables and indexes  */
83 const char *drop_deltabs[] = {
84    "DROP TABLE DelCandidates",
85    "DROP INDEX DelInx1",
86    NULL};
87
88
89 /* List of SQL commands to create temp table and indicies  */
90 const char *create_deltabs[] = {
91    "CREATE TEMPORARY TABLE DelCandidates ("
92 #if defined(HAVE_MYSQL)
93       "JobId INTEGER UNSIGNED NOT NULL, "
94       "PurgedFiles TINYINT, "
95       "FileSetId INTEGER UNSIGNED, "
96       "JobFiles INTEGER UNSIGNED, "
97       "JobStatus BINARY(1))",
98 #elif defined(HAVE_POSTGRESQL)
99       "JobId INTEGER NOT NULL, "
100       "PurgedFiles SMALLINT, "
101       "FileSetId INTEGER, "
102       "JobFiles INTEGER, "
103       "JobStatus char(1))",
104 #else
105       "JobId INTEGER UNSIGNED NOT NULL, "
106       "PurgedFiles TINYINT, "
107       "FileSetId INTEGER UNSIGNED, "
108       "JobFiles INTEGER UNSIGNED, "
109       "JobStatus CHAR)",
110 #endif
111    "CREATE INDEX DelInx1 ON DelCandidates (JobId)",
112    NULL};
113
114 /* Fill candidates table with all Jobs subject to being deleted.
115  *  This is used for pruning Jobs (first the files, then the Jobs).
116  */
117 const char *insert_delcand =
118    "INSERT INTO DelCandidates "
119    "SELECT JobId,PurgedFiles,FileSetId,JobFiles,JobStatus FROM Job "
120    "WHERE Type='%c' "
121    "AND JobTDate<%s "
122    "AND ClientId=%s";
123
124 /*
125  * Select Jobs from the DelCandidates table that have a
126  * more recent backup -- i.e. are not the only backup.
127  * This is the list of Jobs to delete for a Backup Job.
128  * At the same time, we select "orphanned" jobs
129  * (i.e. no files, ...) for deletion.
130  */
131 #ifdef old_way
132 const char *select_backup_del =
133    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
134    "FROM Job,DelCandidates "
135    "WHERE (Job.JobTDate<%s AND ((DelCandidates.JobFiles=0) OR "
136    "(DelCandidates.JobStatus!='T'))) OR "
137    "(Job.JobTDate>%s "
138    "AND Job.ClientId=%s "
139    "AND Job.Level='F' AND Job.JobStatus='T' AND Job.Type IN ('B','M') "
140    "AND Job.FileSetId=DelCandidates.FileSetId)";
141
142 /* Select Jobs from the DelCandidates table that have a
143  * more recent InitCatalog -- i.e. are not the only InitCatalog
144  * This is the list of Jobs to delete for a Verify Job.
145  */
146 const char *select_verify_del =
147    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
148    "FROM Job,DelCandidates "
149    "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
150    "(Job.JobTDate>%s "
151    "AND Job.ClientId=%s "
152    "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
153    "AND Job.FileSetId=DelCandidates.FileSetId)";
154
155
156 /* Select Jobs from the DelCandidates table.
157  * This is the list of Jobs to delete for a Restore Job.
158  */
159 const char *select_restore_del =
160    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
161    "FROM Job,DelCandidates "
162    "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
163    "(Job.JobTDate>%s "
164    "AND Job.ClientId=%s "
165    "AND Job.Type='R')";
166
167 /* Select Jobs from the DelCandidates table.
168  * This is the list of Jobs to delete for an Admin Job.
169  */
170 const char *select_admin_del =
171    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
172    "FROM Job,DelCandidates "
173    "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
174    "(Job.JobTDate>%s "
175    "AND Job.ClientId=%s "
176    "AND Job.Type='D')";
177
178 /*
179  * Select Jobs from the DelCandidates table.
180  * This is the list of Jobs to delete for an Migrate Job.
181  */
182 const char *select_migrate_del =
183    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
184    "FROM Job,DelCandidates "
185    "WHERE (Job.JobTdate<%s AND DelCandidates.JobStatus!='T') OR "
186    "(Job.JobTDate>%s "
187    "AND Job.ClientId=%s "
188    "AND Job.Type='g')";
189
190 #else
191 /* Faster way */
192 const char *select_backup_del =
193    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
194    "FROM Job,DelCandidates "
195    "WHERE (Job.JobId=DelCandidates.JobId AND ((DelCandidates.JobFiles=0) OR "
196    "(DelCandidates.JobStatus!='T'))) OR "
197    "(Job.JobTDate>%s "
198    "AND Job.ClientId=%s "
199    "AND Job.Level='F' AND Job.JobStatus='T' AND Job.Type IN ('B','M') "
200    "AND Job.FileSetId=DelCandidates.FileSetId)";
201
202 /* Select Jobs from the DelCandidates table that have a
203  * more recent InitCatalog -- i.e. are not the only InitCatalog
204  * This is the list of Jobs to delete for a Verify Job.
205  */
206 const char *select_verify_del =
207    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
208    "FROM Job,DelCandidates "
209    "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
210    "(Job.JobTDate>%s "
211    "AND Job.ClientId=%s "
212    "AND Job.Type='V' AND Job.Level='V' AND Job.JobStatus='T' "
213    "AND Job.FileSetId=DelCandidates.FileSetId)";
214
215
216 /* Select Jobs from the DelCandidates table.
217  * This is the list of Jobs to delete for a Restore Job.
218  */
219 const char *select_restore_del =
220    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
221    "FROM Job,DelCandidates "
222    "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
223    "(Job.JobTDate>%s "
224    "AND Job.ClientId=%s "
225    "AND Job.Type='R')";
226
227 /* Select Jobs from the DelCandidates table.
228  * This is the list of Jobs to delete for an Admin Job.
229  */
230 const char *select_admin_del =
231    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
232    "FROM Job,DelCandidates "
233    "WHERE (Job.JobId=DelCandidates.JobId AND DelCandidates.JobStatus!='T') OR "
234    "(Job.JobTDate>%s "
235    "AND Job.ClientId=%s "
236    "AND Job.Type='D')";
237
238 /*
239  * Select Jobs from the DelCandidates table.
240  * This is the list of Jobs to delete for an Migrate Job.
241  */
242 const char *select_migrate_del =
243    "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
244    "FROM Job,DelCandidates "
245    "WHERE (Job.JobId=DelCanditates.JobId AND DelCandidates.JobStatus!='T') OR "
246    "(Job.JobTDate>%s "
247    "AND Job.ClientId=%s "
248    "AND Job.Type='g')";
249
250 #endif
251
252 /* ======= ua_restore.c */
253 const char *uar_count_files =
254    "SELECT JobFiles FROM Job WHERE JobId=%s";
255
256 /* List last 20 Jobs */
257 const char *uar_list_jobs =
258    "SELECT JobId,Client.Name as Client,StartTime,Level as "
259    "JobLevel,JobFiles,JobBytes "
260    "FROM Client,Job WHERE Client.ClientId=Job.ClientId AND JobStatus='T' "
261    "AND Type='B' ORDER BY StartTime DESC LIMIT 20";
262
263 #ifdef HAVE_MYSQL
264 /*  MYSQL IS NOT STANDARD SQL !!!!! */
265 /* List Jobs where a particular file is saved */
266 const char *uar_file =
267    "SELECT Job.JobId as JobId,"
268    "CONCAT(Path.Path,Filename.Name) as Name, "
269    "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
270    "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
271    "AND Client.ClientId=Job.ClientId "
272    "AND Job.JobId=File.JobId "
273    "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
274    "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20";
275 #else
276 /* List Jobs where a particular file is saved */
277 const char *uar_file =
278    "SELECT Job.JobId as JobId,"
279    "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";
286 #endif
287
288
289 /*
290  * Find all files for a particular JobId and insert them into
291  *  the tree during a restore.
292  */
293 const char *uar_sel_files =
294    "SELECT Path.Path,Filename.Name,FileIndex,JobId,LStat "
295    "FROM File,Filename,Path "
296    "WHERE File.JobId=%s AND Filename.FilenameId=File.FilenameId "
297    "AND Path.PathId=File.PathId";
298
299 const char *uar_del_temp  = "DROP TABLE temp";
300 const char *uar_del_temp1 = "DROP TABLE temp1";
301
302 const char *uar_create_temp =
303    "CREATE TEMPORARY TABLE temp ("
304 #ifdef HAVE_POSTGRESQL
305    "JobId INTEGER NOT NULL,"
306    "JobTDate BIGINT,"
307    "ClientId INTEGER,"
308    "Level CHAR,"
309    "JobFiles INTEGER,"
310    "JobBytes BIGINT,"
311    "StartTime TEXT,"
312    "VolumeName TEXT,"
313    "StartFile INTEGER,"
314    "VolSessionId INTEGER,"
315    "VolSessionTime INTEGER)";
316 #else
317    "JobId INTEGER UNSIGNED NOT NULL,"
318    "JobTDate BIGINT UNSIGNED,"
319    "ClientId INTEGER UNSIGNED,"
320    "Level CHAR,"
321    "JobFiles INTEGER UNSIGNED,"
322    "JobBytes BIGINT UNSIGNED,"
323    "StartTime TEXT,"
324    "VolumeName TEXT,"
325    "StartFile INTEGER UNSIGNED,"
326    "VolSessionId INTEGER UNSIGNED,"
327    "VolSessionTime INTEGER UNSIGNED)";
328 #endif
329
330 const char *uar_create_temp1 =
331    "CREATE TEMPORARY TABLE temp1 ("
332 #ifdef HAVE_POSTGRESQL
333    "JobId INTEGER NOT NULL,"
334    "JobTDate BIGINT)";
335 #else
336    "JobId INTEGER UNSIGNED NOT NULL,"
337    "JobTDate BIGINT UNSIGNED)";
338 #endif
339
340 const char *uar_last_full =
341    "INSERT INTO temp1 SELECT Job.JobId,JobTdate "
342    "FROM Client,Job,JobMedia,Media,FileSet WHERE Client.ClientId=%s "
343    "AND Job.ClientId=%s "
344    "AND Job.StartTime<'%s' "
345    "AND Level='F' AND JobStatus='T' AND Type='B' "
346    "AND JobMedia.JobId=Job.JobId "
347    "AND Media.Enabled=1 "
348    "AND JobMedia.MediaId=Media.MediaId "
349    "AND Job.FileSetId=FileSet.FileSetId "
350    "AND FileSet.FileSet='%s' "
351    "%s"
352    "ORDER BY Job.JobTDate DESC LIMIT 1";
353
354 const char *uar_full =
355    "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,"
356    "Job.ClientId,Job.Level,Job.JobFiles,Job.JobBytes,"
357    "StartTime,VolumeName,JobMedia.StartFile,VolSessionId,VolSessionTime "
358    "FROM temp1,Job,JobMedia,Media WHERE temp1.JobId=Job.JobId "
359    "AND Level='F' AND JobStatus='T' AND Type='B' "
360    "AND Media.Enabled=1 "
361    "AND JobMedia.JobId=Job.JobId "
362    "AND JobMedia.MediaId=Media.MediaId";
363
364 const char *uar_dif =
365    "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
366    "Job.Level,Job.JobFiles,Job.JobBytes,"
367    "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
368    "Job.VolSessionId,Job.VolSessionTime "
369    "FROM Job,JobMedia,Media,FileSet "
370    "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
371    "AND Job.ClientId=%s "
372    "AND JobMedia.JobId=Job.JobId "
373    "AND Media.Enabled=1 "
374    "AND JobMedia.MediaId=Media.MediaId "
375    "AND Job.Level='D' AND JobStatus='T' AND Type='B' "
376    "AND Job.FileSetId=FileSet.FileSetId "
377    "AND FileSet.FileSet='%s' "
378    "%s"
379    "ORDER BY Job.JobTDate DESC LIMIT 1";
380
381 const char *uar_inc =
382    "INSERT INTO temp SELECT Job.JobId,Job.JobTDate,Job.ClientId,"
383    "Job.Level,Job.JobFiles,Job.JobBytes,"
384    "Job.StartTime,Media.VolumeName,JobMedia.StartFile,"
385    "Job.VolSessionId,Job.VolSessionTime "
386    "FROM Job,JobMedia,Media,FileSet "
387    "WHERE Job.JobTDate>%s AND Job.StartTime<'%s' "
388    "AND Job.ClientId=%s "
389    "AND Media.Enabled=1 "
390    "AND JobMedia.JobId=Job.JobId "
391    "AND JobMedia.MediaId=Media.MediaId "
392    "AND Job.Level='I' AND JobStatus='T' AND Type='B' "
393    "AND Job.FileSetId=FileSet.FileSetId "
394    "AND FileSet.FileSet='%s' "
395    "%s";
396
397 #ifdef HAVE_POSTGRESQL
398 /* Note, the PostgreSQL will have a much uglier looking
399  * list since it cannot do GROUP BY of different values.
400  */
401 const char *uar_list_temp =
402    "SELECT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName,StartFile"
403    " FROM temp"
404    " ORDER BY StartTime,StartFile ASC";
405 #else
406 const char *uar_list_temp =
407    "SELECT JobId,Level,JobFiles,JobBytes,StartTime,VolumeName,StartFile"
408    " FROM temp"
409    " GROUP BY JobId ORDER BY StartTime,StartFile ASC";
410 #endif
411
412
413 const char *uar_sel_jobid_temp = "SELECT JobId FROM temp ORDER BY StartTime ASC";
414
415 const char *uar_sel_all_temp1 = "SELECT * FROM temp1";
416
417 const char *uar_sel_all_temp = "SELECT * FROM temp";
418
419
420
421 /* Select FileSet names for this Client */
422 const char *uar_sel_fileset =
423    "SELECT DISTINCT FileSet.FileSet FROM Job,"
424    "Client,FileSet WHERE Job.FileSetId=FileSet.FileSetId "
425    "AND Job.ClientId=%s AND Client.ClientId=%s "
426    "ORDER BY FileSet.FileSet";
427
428 /* Find MediaType used by this Job */
429 const char *uar_mediatype =
430    "SELECT MediaType FROM JobMedia,Media WHERE JobMedia.JobId=%s "
431    "AND JobMedia.MediaId=Media.MediaId";
432
433 /*
434  *  Find JobId, FileIndex for a given path/file and date
435  *  for use when inserting individual files into the tree.
436  */
437 const char *uar_jobid_fileindex =
438    "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
439    "WHERE Job.JobId=File.JobId "
440    "AND Job.StartTime<='%s' "
441    "AND Path.Path='%s' "
442    "AND Filename.Name='%s' "
443    "AND Client.Name='%s' "
444    "AND Job.ClientId=Client.ClientId "
445    "AND Path.PathId=File.PathId "
446    "AND Filename.FilenameId=File.FilenameId "
447    "ORDER BY Job.StartTime DESC LIMIT 1";
448
449 const char *uar_jobids_fileindex =
450    "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
451    "WHERE Job.JobId IN (%s) "
452    "AND Job.JobId=File.JobId "
453    "AND Job.StartTime<='%s' "
454    "AND Path.Path='%s' "
455    "AND Filename.Name='%s' "
456    "AND Client.Name='%s' "
457    "AND Job.ClientId=Client.ClientId "
458    "AND Path.PathId=File.PathId "
459    "AND Filename.FilenameId=File.FilenameId "
460    "ORDER BY Job.StartTime DESC LIMIT 1";
461
462 /* Query to get all files in a directory -- no recursing   
463  *  Note, for PostgreSQL since it respects the "Single Value
464  *  rule", the results of the SELECT will be unoptimized.
465  *  I.e. the same file will be restored multiple times, once
466  *  for each time it was backed up.
467  */
468
469 #ifdef HAVE_POSTGRESQL
470 const char *uar_jobid_fileindex_from_dir = 
471    "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
472    "WHERE Job.JobId IN (%s) "
473    "AND Job.JobId=File.JobId "
474    "AND Path.Path='%s' "
475    "AND Client.Name='%s' "
476    "AND Job.ClientId=Client.ClientId "
477    "AND Path.PathId=File.Pathid "
478    "AND Filename.FilenameId=File.FilenameId"; 
479 #else
480 const char *uar_jobid_fileindex_from_dir = 
481    "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
482    "WHERE Job.JobId IN (%s) "
483    "AND Job.JobId=File.JobId "
484    "AND Path.Path='%s' "
485    "AND Client.Name='%s' "
486    "AND Job.ClientId=Client.ClientId "
487    "AND Path.PathId=File.Pathid "
488    "AND Filename.FilenameId=File.FilenameId "
489    "GROUP BY File.FileIndex ";
490 #endif
491  
492 /* Query to get list of files from table -- presuably built by an external program */
493 const char *uar_jobid_fileindex_from_table = 
494    "SELECT JobId, FileIndex from %s";