]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_prune.c
Add jcr to DB arguments
[bacula/bacula] / bacula / src / dird / ua_prune.c
1 /*
2  *
3  *   Bacula Director -- User Agent Database prune Command
4  *      Applies retention periods
5  *
6  *     Kern Sibbald, February MMII
7  *
8  *   Version $Id$
9  */
10
11 /*
12    Copyright (C) 2002-2003 Kern Sibbald and John Walker
13
14    This program is free software; you can redistribute it and/or
15    modify it under the terms of the GNU General Public License as
16    published by the Free Software Foundation; either version 2 of
17    the License, or (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public
25    License along with this program; if not, write to the Free
26    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27    MA 02111-1307, USA.
28
29  */
30
31 #include "bacula.h"
32 #include "dird.h"
33 #include "ua.h"
34
35 /* Imported functions */
36 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr);
37
38 /* Forward referenced functions */
39 int prune_files(UAContext *ua, CLIENT *client);
40 int prune_jobs(UAContext *ua, CLIENT *client, int JobType);
41 int prune_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr);
42
43
44 #define MAX_DEL_LIST_LEN 1000000
45
46 /* Imported variables */
47 extern char *select_job;
48 extern char *drop_deltabs[];
49 extern char *create_deltabs[];
50 extern char *insert_delcand;
51 extern char *select_backup_del;
52 extern char *select_verify_del;
53 extern char *select_restore_del;
54 extern char *cnt_File;
55 extern char *del_File;
56 extern char *upd_Purged;
57 extern char *cnt_DelCand;
58 extern char *del_Job;
59 extern char *del_JobMedia;
60 extern char *cnt_JobMedia;
61 extern char *sel_JobMedia;
62
63
64 /* In memory list of JobIds */
65 struct s_file_del_ctx {
66    JobId_t *JobId;
67    int num_ids;                       /* ids stored */
68    int max_ids;                       /* size of array */
69    int num_del;                       /* number deleted */
70    int tot_ids;                       /* total to process */
71 };
72
73 struct s_job_del_ctx {
74    JobId_t *JobId;                    /* array of JobIds */
75    char *PurgedFiles;                 /* Array of PurgedFile flags */
76    int num_ids;                       /* ids stored */
77    int max_ids;                       /* size of array */
78    int num_del;                       /* number deleted */
79    int tot_ids;                       /* total to process */
80 };
81
82 struct s_count_ctx {
83    int count;
84 };
85
86
87 /*
88  * Called here to count entries to be deleted 
89  */
90 static int count_handler(void *ctx, int num_fields, char **row)
91 {
92    struct s_count_ctx *cnt = (struct s_count_ctx *)ctx;
93
94    if (row[0]) {
95       cnt->count = atoi(row[0]);
96    } else {
97       cnt->count = 0;
98    }
99    return 0;
100 }
101
102
103 /*
104  * Called here to count the number of Jobs to be pruned
105  */
106 static int file_count_handler(void *ctx, int num_fields, char **row)
107 {
108    struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
109    del->tot_ids++;
110    return 0;
111 }
112
113
114 /*
115  * Called here to make in memory list of JobIds to be
116  *  deleted and the associated PurgedFiles flag.
117  *  The in memory list will then be transversed
118  *  to issue the SQL DELETE commands.  Note, the list
119  *  is allowed to get to MAX_DEL_LIST_LEN to limit the
120  *  maximum malloc'ed memory.
121  */
122 static int job_delete_handler(void *ctx, int num_fields, char **row)
123 {
124    struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
125
126    if (del->num_ids == MAX_DEL_LIST_LEN) {  
127       return 1;
128    }
129    if (del->num_ids == del->max_ids) {
130       del->max_ids = (del->max_ids * 3) / 2;
131       del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) * del->max_ids);
132       del->PurgedFiles = (char *)brealloc(del->PurgedFiles, del->max_ids);
133    }
134    del->JobId[del->num_ids] = (JobId_t)str_to_int64(row[0]);
135    del->PurgedFiles[del->num_ids++] = (char)str_to_int64(row[0]);
136    return 0;
137 }
138
139 static int file_delete_handler(void *ctx, int num_fields, char **row)
140 {
141    struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
142
143    if (del->num_ids == MAX_DEL_LIST_LEN) {  
144       return 1;
145    }
146    if (del->num_ids == del->max_ids) {
147       del->max_ids = (del->max_ids * 3) / 2;
148       del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) *
149          del->max_ids);
150    }
151    del->JobId[del->num_ids++] = (JobId_t)str_to_int64(row[0]);
152    return 0;
153 }
154
155 /*
156  *   Prune records from database
157  *
158  *    prune files (from) client=xxx
159  *    prune jobs (from) client=xxx
160  *    prune volume=xxx  
161  */
162 int prunecmd(UAContext *ua, char *cmd)
163 {
164    CLIENT *client;
165    POOL_DBR pr;
166    MEDIA_DBR mr;
167
168    static char *keywords[] = {
169       N_("Files"),
170       N_("Jobs"),
171       N_("Volume"),
172       NULL};
173    if (!open_db(ua)) {
174       return 01;
175    }
176    switch (find_arg_keyword(ua, keywords)) {
177    case 0:
178       client = select_client_resource(ua);
179       if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
180          return 0;
181       }
182       prune_files(ua, client);
183       return 1;
184    case 1:
185       client = select_client_resource(ua);
186       if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
187          return 0;
188       }
189       /* ****FIXME**** allow user to select JobType */
190       prune_jobs(ua, client, JT_BACKUP);
191       return 1;
192    case 2:
193       if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
194          return 0;
195       }
196       if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
197          return 0;
198       }
199       prune_volume(ua, &pr, &mr);
200       return 1;
201    default:
202       break;
203    }
204    switch (do_keyword_prompt(ua, _("Choose item to prune"), keywords)) {
205    case 0:
206       client = select_client_resource(ua);
207       if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
208          return 0;
209       }
210       prune_files(ua, client);
211       break;
212    case 1:
213       client = select_client_resource(ua);
214       if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
215          return 0;
216       }
217       /* ****FIXME**** allow user to select JobType */
218       prune_jobs(ua, client, JT_BACKUP);
219       break;
220    case 2:
221       if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
222          return 0;
223       }
224       if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
225          return 0;
226       }
227       prune_volume(ua, &pr, &mr);
228       return 1;
229    }
230    return 1;
231 }
232
233 /*
234  * Prune File records from the database. For any Job which
235  * is older than the retention period, we unconditionally delete
236  * all File records for that Job.  This is simple enough that no
237  * temporary tables are needed. We simply make an in memory list of
238  * the JobIds meeting the prune conditions, then delete all File records
239  * pointing to each of those JobIds.
240  *
241  * This routine assumes you want the pruning to be done. All checking
242  *  must be done before calling this routine.
243  */
244 int prune_files(UAContext *ua, CLIENT *client)
245 {
246    struct s_file_del_ctx del;
247    POOLMEM *query = get_pool_memory(PM_MESSAGE);
248    int i;
249    utime_t now, period;
250    CLIENT_DBR cr;
251    char ed1[50], ed2[50];
252
253    db_lock(ua->db);
254    memset(&cr, 0, sizeof(cr));
255    memset(&del, 0, sizeof(del));
256    strcpy(cr.Name, client->hdr.name);
257    if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
258       db_unlock(ua->db);
259       return 0;
260    }
261
262    period = client->FileRetention;
263    now = (utime_t)time(NULL);
264        
265    /* Select Jobs -- for counting */
266    Mmsg(&query, select_job, edit_uint64(now - period, ed1), cr.ClientId);
267    Dmsg1(050, "select sql=%s\n", query);
268    if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
269       if (ua->verbose) {
270          bsendmsg(ua, "%s", db_strerror(ua->db));
271       }
272       Dmsg0(050, "Count failed\n");
273       goto bail_out;
274    }
275       
276    if (del.tot_ids == 0) {
277       if (ua->verbose) {
278          bsendmsg(ua, _("No Files found to prune.\n"));
279       }
280       goto bail_out;
281    }
282
283    if (del.tot_ids < MAX_DEL_LIST_LEN) {
284       del.max_ids = del.tot_ids + 1;
285    } else {
286       del.max_ids = MAX_DEL_LIST_LEN; 
287    }
288    del.tot_ids = 0;
289
290    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
291
292    /* Now process same set but making delete list */
293    db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
294
295    for (i=0; i < del.num_ids; i++) {
296       struct s_count_ctx cnt;
297       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
298       Mmsg(&query, cnt_File, del.JobId[i]);
299       cnt.count = 0;
300       db_sql_query(ua->db, query, count_handler, (void *)&cnt);
301       del.tot_ids += cnt.count;
302       Mmsg(&query, del_File, del.JobId[i]);
303       db_sql_query(ua->db, query, NULL, (void *)NULL);
304       /* 
305        * Now mark Job as having files purged. This is necessary to
306        * avoid having too many Jobs to process in future prunings. If
307        * we don't do this, the number of JobId's in our in memory list
308        * will grow very large.
309        */
310       Mmsg(&query, upd_Purged, del.JobId[i]);
311       db_sql_query(ua->db, query, NULL, (void *)NULL);
312       Dmsg1(050, "Del sql=%s\n", query);
313    }
314    edit_uint64_with_commas(del.tot_ids, ed1);
315    edit_uint64_with_commas(del.num_ids, ed2);
316    bsendmsg(ua, _("Pruned %s Files from %s Jobs for client %s from catalog.\n"), 
317       ed1, ed2, client->hdr.name);
318    
319 bail_out:
320    db_unlock(ua->db);
321    if (del.JobId) {
322       free(del.JobId);
323    }
324    free_pool_memory(query);
325    return 1;
326 }
327
328
329 static void drop_temp_tables(UAContext *ua) 
330 {
331    int i;
332    for (i=0; drop_deltabs[i]; i++) {
333       db_sql_query(ua->db, drop_deltabs[i], NULL, (void *)NULL);
334    }
335 }
336
337 static int create_temp_tables(UAContext *ua) 
338 {
339    int i;
340    /* Create temp tables and indicies */
341    for (i=0; create_deltabs[i]; i++) {
342       if (!db_sql_query(ua->db, create_deltabs[i], NULL, (void *)NULL)) {
343          bsendmsg(ua, "%s", db_strerror(ua->db));
344          Dmsg0(050, "create DelTables table failed\n");
345          return 0;
346       }
347    }
348    return 1;
349 }
350
351
352
353 /*
354  * Purging Jobs is a bit more complicated than purging Files
355  * because we delete Job records only if there is a more current
356  * backup of the FileSet. Otherwise, we keep the Job record.
357  * In other words, we never delete the only Job record that
358  * contains a current backup of a FileSet. This prevents the
359  * Volume from being recycled and destroying a current backup.
360  *
361  * For Verify Jobs, we do not delete the last InitCatalog.
362  *
363  * For Restore Jobs there are no restrictions.
364  */
365 int prune_jobs(UAContext *ua, CLIENT *client, int JobType)
366 {
367    struct s_job_del_ctx del;
368    struct s_count_ctx cnt;
369    char *query = (char *)get_pool_memory(PM_MESSAGE);
370    int i;
371    utime_t now, period;
372    CLIENT_DBR cr;
373    char ed1[50];
374
375    db_lock(ua->db);
376    memset(&cr, 0, sizeof(cr));
377    memset(&del, 0, sizeof(del));
378    strcpy(cr.Name, client->hdr.name);
379    if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
380       db_unlock(ua->db);
381       return 0;
382    }
383
384    period = client->JobRetention;
385    now = (utime_t)time(NULL);
386
387    /* Drop any previous temporary tables still there */
388    drop_temp_tables(ua);
389
390    /* Create temp tables and indicies */
391    if (!create_temp_tables(ua)) {
392       goto bail_out;
393    }
394
395    /* 
396     * Select all files that are older than the JobRetention period
397     *  and stuff them into the "DeletionCandidates" table.
398     */
399    edit_uint64(now - period, ed1);
400    Mmsg(&query, insert_delcand, (char)JobType, ed1, cr.ClientId);
401    if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
402       if (ua->verbose) {
403          bsendmsg(ua, "%s", db_strerror(ua->db));
404       }
405       Dmsg0(050, "insert delcand failed\n");
406       goto bail_out;
407    }
408
409    /* Count Files to be deleted */
410    strcpy(query, cnt_DelCand);
411    Dmsg1(100, "select sql=%s\n", query);
412    cnt.count = 0;
413    if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
414       bsendmsg(ua, "%s", db_strerror(ua->db));
415       Dmsg0(050, "Count failed\n");
416       goto bail_out;
417    }
418       
419    if (cnt.count == 0) {
420       if (ua->verbose) {
421          bsendmsg(ua, _("No Jobs found to prune.\n"));
422       }
423       goto bail_out;
424    }
425
426    if (cnt.count < MAX_DEL_LIST_LEN) {
427       del.max_ids = cnt.count + 1;
428    } else {
429       del.max_ids = MAX_DEL_LIST_LEN; 
430    }
431    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
432    del.PurgedFiles = (char *)malloc(del.max_ids);
433
434    switch (JobType) {
435    case JT_ADMIN:
436    case JT_BACKUP:
437       Mmsg(&query, select_backup_del, ed1, cr.ClientId);
438       break;
439    case JT_RESTORE:
440       Mmsg(&query, select_restore_del, ed1, cr.ClientId);
441       break;
442    case JT_VERIFY:
443       Mmsg(&query, select_verify_del, ed1, cr.ClientId);
444       break;
445    }
446    if (!db_sql_query(ua->db, query, job_delete_handler, (void *)&del)) {
447       bsendmsg(ua, "%s", db_strerror(ua->db));
448    }
449
450    /* 
451     * OK, now we have the list of JobId's to be pruned, first check
452     * if the Files have been purged, if not, purge (delete) them.
453     * Then delete the Job entry, and finally and JobMedia records.
454     */
455    for (i=0; i < del.num_ids; i++) {
456       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
457       if (!del.PurgedFiles[i]) {
458          Mmsg(&query, del_File, del.JobId[i]);
459          if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
460             bsendmsg(ua, "%s", db_strerror(ua->db));
461          }
462          Dmsg1(050, "Del sql=%s\n", query);
463       }
464
465       Mmsg(&query, del_Job, del.JobId[i]);
466       if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
467          bsendmsg(ua, "%s", db_strerror(ua->db));
468       }
469       Dmsg1(050, "Del sql=%s\n", query);
470
471       Mmsg(&query, del_JobMedia, del.JobId[i]);
472       if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
473          bsendmsg(ua, "%s", db_strerror(ua->db));
474       }
475       Dmsg1(050, "Del sql=%s\n", query);
476    }
477    bsendmsg(ua, _("Pruned %d %s for client %s from catalog.\n"), del.num_ids,
478       del.num_ids==1?_("Job"):_("Jobs"), client->hdr.name);
479    
480 bail_out:
481    drop_temp_tables(ua);
482    db_unlock(ua->db);
483    if (del.JobId) {
484       free(del.JobId);
485    }
486    if (del.PurgedFiles) {
487       free(del.PurgedFiles);
488    }
489    free_pool_memory(query);
490    return 1;
491 }
492
493 /*
494  * Prune a given Volume
495  */
496 int prune_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
497 {
498    char *query = (char *)get_pool_memory(PM_MESSAGE);
499    struct s_count_ctx cnt;
500    struct s_file_del_ctx del;
501    int i, stat = 0;
502    JOB_DBR jr;
503    utime_t now, period;
504
505    db_lock(ua->db);
506    memset(&jr, 0, sizeof(jr));
507    memset(&del, 0, sizeof(del));
508    cnt.count = 0;
509    Mmsg(&query, cnt_JobMedia, mr->MediaId);
510    if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
511       bsendmsg(ua, "%s", db_strerror(ua->db));
512       Dmsg0(050, "Count failed\n");
513       goto bail_out;
514    }
515       
516    if (cnt.count == 0) {
517       if (ua->verbose) {
518          bsendmsg(ua, "There are no Jobs associated with Volume %s. Marking it purged.\n",
519             mr->VolumeName);
520       }
521       stat = mark_media_purged(ua, mr);
522       goto bail_out;
523    }
524
525    if (cnt.count < MAX_DEL_LIST_LEN) {
526       del.max_ids = cnt.count + 1;
527    } else {
528       del.max_ids = MAX_DEL_LIST_LEN; 
529    }
530
531    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
532
533    /* ***FIXME*** could make this do JobTDate check too */
534    Mmsg(&query, sel_JobMedia, mr->MediaId);
535    if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
536       if (ua->verbose) {
537          bsendmsg(ua, "%s", db_strerror(ua->db));
538       }
539       Dmsg0(050, "Count failed\n");
540       goto bail_out;
541    }
542
543    /* Use Volume Retention to prune Jobs and Files */
544    period = mr->VolRetention;
545    now = (utime_t)time(NULL);
546
547    Dmsg3(200, "Now=%d period=%d now-period=%d\n", (int)now, (int)period,
548       (int)(now-period));
549    for (i=0; i < del.num_ids; i++) {
550       jr.JobId = del.JobId[i];
551       if (!db_get_job_record(ua->jcr, ua->db, &jr)) {
552          continue;
553       }
554       Dmsg2(200, "Looking at %s JobTdate=%d\n", jr.Job, (int)jr.JobTDate);
555       if (jr.JobTDate >= (now - period)) {
556          continue;
557       }
558       Dmsg2(200, "Delete JobId=%d Job=%s\n", del.JobId[i], jr.Job);
559       Mmsg(&query, del_File, del.JobId[i]);
560       db_sql_query(ua->db, query, NULL, (void *)NULL);
561       Mmsg(&query, del_Job, del.JobId[i]);
562       db_sql_query(ua->db, query, NULL, (void *)NULL);
563       Mmsg(&query, del_JobMedia, del.JobId[i]);
564       db_sql_query(ua->db, query, NULL, (void *)NULL);
565       Dmsg1(050, "Del sql=%s\n", query);
566       del.num_del++;
567    }
568    if (del.JobId) {
569       free(del.JobId);
570    }
571    if (ua->verbose && del.num_del != 0) {
572       bsendmsg(ua, _("Pruned %d %s on Volume %s from catalog.\n"), del.num_del,
573          del.num_del == 1 ? "Job" : "Jobs", mr->VolumeName);
574    }
575
576    /* If purged, mark it so */
577    if (del.num_ids == del.num_del) {
578       Dmsg0(200, "Volume is purged.\n");
579       stat = mark_media_purged(ua, mr);
580    }
581
582 bail_out:
583    db_unlock(ua->db);
584    free_pool_memory(query);
585    return stat;
586 }