]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_purge.c
New var.c file + implement multiple simultaneous jobs
[bacula/bacula] / bacula / src / dird / ua_purge.c
1 /*
2  *
3  *   Bacula Director -- User Agent Database Purge Command
4  *
5  *      Purges Files from specific JobIds
6  * or
7  *      Purges Jobs from Volumes
8  *
9  *     Kern Sibbald, February MMII
10  *
11  *   Version $Id$
12  */
13
14 /*
15    Copyright (C) 2002 Kern Sibbald and John Walker
16
17    This program is free software; you can redistribute it and/or
18    modify it under the terms of the GNU General Public License as
19    published by the Free Software Foundation; either version 2 of
20    the License, or (at your option) any later version.
21
22    This program is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public
28    License along with this program; if not, write to the Free
29    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30    MA 02111-1307, USA.
31
32  */
33
34 #include "bacula.h"
35 #include "dird.h"
36
37 /* Forward referenced functions */
38 int purge_files_from_client(UAContext *ua, CLIENT *client);
39 int purge_jobs_from_client(UAContext *ua, CLIENT *client);
40 void purge_files_from_volume(UAContext *ua, MEDIA_DBR *mr );
41 int purge_jobs_from_volume(UAContext *ua, MEDIA_DBR *mr);
42 void purge_files_from_job(UAContext *ua, JOB_DBR *jr);
43 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr);
44
45 #define MAX_DEL_LIST_LEN 1000000
46
47
48 static char *select_jobsfiles_from_client =
49    "SELECT JobId FROM Job "
50    "WHERE ClientId=%d "
51    "AND PurgedFiles=0";
52
53 static char *select_jobs_from_client =
54    "SELECT JobId, PurgedFiles FROM Job "
55    "WHERE ClientId=%d";
56
57
58 /* In memory list of JobIds */
59 struct s_file_del_ctx {
60    JobId_t *JobId;
61    int num_ids;                       /* ids stored */
62    int max_ids;                       /* size of array */
63    int num_del;                       /* number deleted */
64    int tot_ids;                       /* total to process */
65 };
66
67 struct s_job_del_ctx {
68    JobId_t *JobId;                    /* array of JobIds */
69    char *PurgedFiles;                 /* Array of PurgedFile flags */
70    int num_ids;                       /* ids stored */
71    int max_ids;                       /* size of array */
72    int num_del;                       /* number deleted */
73    int tot_ids;                       /* total to process */
74 };
75
76 struct s_count_ctx {
77    int count;
78 };
79
80 /*
81  * Called here to count entries to be deleted 
82  */
83 static int count_handler(void *ctx, int num_fields, char **row)
84 {
85    struct s_count_ctx *cnt = (struct s_count_ctx *)ctx;
86
87    if (row[0]) {
88       cnt->count = atoi(row[0]);
89    } else {
90       cnt->count = 0;
91    }
92    return 0;
93 }
94
95 /*
96  * Called here to count entries to be deleted 
97  */
98 static int file_count_handler(void *ctx, int num_fields, char **row)
99 {
100    struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
101    del->tot_ids++;
102    return 0;
103 }
104
105
106 static int job_count_handler(void *ctx, int num_fields, char **row)
107 {
108    struct s_job_del_ctx *del = (struct s_job_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  *   Purge records from database
157  *
158  *     Purge Files (from) [Job|JobId|Client|Volume]
159  *     Purge Jobs  (from) [Client|Volume]
160  *
161  *  N.B. Not all above is implemented yet.
162  */
163 int purgecmd(UAContext *ua, char *cmd)
164 {
165    CLIENT *client;
166    MEDIA_DBR mr;
167    JOB_DBR  jr;
168    static char *keywords[] = {
169       N_("files"),
170       N_("jobs"),
171       N_("volume"),
172       NULL};
173
174    static char *files_keywords[] = {
175       N_("Job"),
176       N_("JobId"),
177       N_("Client"),
178       N_("Volume"),
179       NULL};
180
181    static char *jobs_keywords[] = {
182       N_("Client"),
183       N_("Volume"),
184       NULL};
185       
186    bsendmsg(ua, _(
187       "\nThis command is can be DANGEROUS!!!\n\n"
188       "It purges (deletes) all Files from a Job,\n"
189       "JobId, Client or Volume; or it purges (deletes)\n"
190       "all Jobs from a Client or Volume without regard\n"
191       "for retention periods. Normally you should use the\n" 
192       "PRUNE command, which respects retention periods.\n"));
193
194    if (!open_db(ua)) {
195       return 1;
196    }
197    switch (find_arg_keyword(ua, keywords)) {
198    /* Files */
199    case 0:
200       switch(find_arg_keyword(ua, files_keywords)) {
201       case 0:                         /* Job */
202       case 1:                         /* JobId */
203          if (get_job_dbr(ua, &jr)) {
204             purge_files_from_job(ua, &jr);
205          }
206          return 1;
207       case 2:                         /* client */
208          client = get_client_resource(ua);
209          if (client) {
210             purge_files_from_client(ua, client);
211          }
212          return 1;
213       case 3:                         /* Volume */
214          if (select_media_dbr(ua, &mr)) {
215             purge_files_from_volume(ua, &mr);
216          }
217          return 1;
218       }
219    /* Jobs */
220    case 1:
221       switch(find_arg_keyword(ua, jobs_keywords)) {
222       case 0:                         /* client */
223          client = get_client_resource(ua);
224          if (client) {
225             purge_jobs_from_client(ua, client);
226          }
227          return 1;
228       case 1:                         /* Volume */
229          if (select_media_dbr(ua, &mr)) {
230             purge_jobs_from_volume(ua, &mr);
231          }
232          return 1;
233       }
234    /* Volume */
235    case 2:
236       if (select_media_dbr(ua, &mr)) {
237          purge_jobs_from_volume(ua, &mr);
238       }
239       return 1;
240    default:
241       break;
242    }
243    switch (do_keyword_prompt(ua, _("Choose item to purge"), keywords)) {
244    case 0:                            /* files */
245       client = get_client_resource(ua);
246       if (client) {
247          purge_files_from_client(ua, client);
248       }
249       break;
250    case 1:                            /* jobs */
251       client = get_client_resource(ua);
252       if (client) {
253          purge_jobs_from_client(ua, client);
254       }
255       break;
256    case 2:                            /* Volume */
257       if (select_media_dbr(ua, &mr)) {
258          purge_jobs_from_volume(ua, &mr);
259       }
260       break;
261    }
262    return 1;
263 }
264
265 /*
266  * Purge File records from the database. For any Job which
267  * is older than the retention period, we unconditionally delete
268  * all File records for that Job.  This is simple enough that no
269  * temporary tables are needed. We simply make an in memory list of
270  * the JobIds meeting the prune conditions, then delete all File records
271  * pointing to each of those JobIds.
272  */
273 int purge_files_from_client(UAContext *ua, CLIENT *client)
274 {
275    struct s_file_del_ctx del;
276    char *query = (char *)get_pool_memory(PM_MESSAGE);
277    int i;
278    CLIENT_DBR cr;
279
280    memset(&cr, 0, sizeof(cr));
281    memset(&del, 0, sizeof(del));
282
283    strcpy(cr.Name, client->hdr.name);
284    if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
285       return 0;
286    }
287
288    Mmsg(&query, select_jobsfiles_from_client, cr.ClientId);
289
290    Dmsg1(050, "select sql=%s\n", query);
291  
292    if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
293       bsendmsg(ua, "%s", db_strerror(ua->db));
294       Dmsg0(050, "Count failed\n");
295       goto bail_out;
296    }
297       
298    if (del.tot_ids == 0) {
299       bsendmsg(ua, _("No Files found for client %s to purge from %s catalog.\n"),
300          client->hdr.name, client->catalog->hdr.name);
301       goto bail_out;
302    }
303
304    if (del.tot_ids < MAX_DEL_LIST_LEN) {
305       del.max_ids = del.tot_ids + 1;
306    } else {
307       del.max_ids = MAX_DEL_LIST_LEN; 
308    }
309    del.tot_ids = 0;
310
311    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
312
313    db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
314
315    for (i=0; i < del.num_ids; i++) {
316       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
317       Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
318       db_sql_query(ua->db, query, NULL, (void *)NULL);
319       /* 
320        * Now mark Job as having files purged. This is necessary to
321        * avoid having too many Jobs to process in future prunings. If
322        * we don't do this, the number of JobId's in our in memory list
323        * will grow very large.
324        */
325       Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", del.JobId[i]);
326       db_sql_query(ua->db, query, NULL, (void *)NULL);
327       Dmsg1(050, "Del sql=%s\n", query);
328    }
329    bsendmsg(ua, _("%d Files for client %s purged from %s catalog.\n"), del.num_ids,
330       client->hdr.name, client->catalog->hdr.name);
331    
332 bail_out:
333    if (del.JobId) {
334       free(del.JobId);
335    }
336    free_pool_memory(query);
337    return 1;
338 }
339
340
341
342 /*
343  * Purge Job records from the database. For any Job which
344  * is older than the retention period, we unconditionally delete
345  * it and all File records for that Job.  This is simple enough that no
346  * temporary tables are needed. We simply make an in memory list of
347  * the JobIds meeting the prune conditions, then delete the Job,
348  * Files, and JobMedia records in that list.
349  */
350 int purge_jobs_from_client(UAContext *ua, CLIENT *client)
351 {
352    struct s_job_del_ctx del;
353    char *query = (char *)get_pool_memory(PM_MESSAGE);
354    int i;
355    CLIENT_DBR cr;
356
357    memset(&cr, 0, sizeof(cr));
358    memset(&del, 0, sizeof(del));
359
360    bstrncpy(cr.Name, client->hdr.name, sizeof(cr.Name));
361    if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
362       return 0;
363    }
364
365    Mmsg(&query, select_jobs_from_client, cr.ClientId);
366
367    Dmsg1(050, "select sql=%s\n", query);
368  
369    if (!db_sql_query(ua->db, query, job_count_handler, (void *)&del)) {
370       bsendmsg(ua, "%s", db_strerror(ua->db));
371       Dmsg0(050, "Count failed\n");
372       goto bail_out;
373    }
374    if (del.tot_ids == 0) {
375       bsendmsg(ua, _("No Jobs found for client %s to purge from %s catalog.\n"),
376          client->hdr.name, client->catalog->hdr.name);
377       goto bail_out;
378    }
379
380    if (del.tot_ids < MAX_DEL_LIST_LEN) {
381       del.max_ids = del.tot_ids + 1;
382    } else {
383       del.max_ids = MAX_DEL_LIST_LEN; 
384    }
385
386    del.tot_ids = 0;
387
388    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
389    del.PurgedFiles = (char *)malloc(del.max_ids);
390
391    db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
392
393    /* 
394     * OK, now we have the list of JobId's to be purged, first check
395     * if the Files have been purged, if not, purge (delete) them.
396     * Then delete the Job entry, and finally and JobMedia records.
397     */
398    for (i=0; i < del.num_ids; i++) {
399       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
400       if (!del.PurgedFiles[i]) {
401          Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
402          db_sql_query(ua->db, query, NULL, (void *)NULL);
403          Dmsg1(050, "Del sql=%s\n", query);
404       }
405
406       Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
407       db_sql_query(ua->db, query, NULL, (void *)NULL);
408       Dmsg1(050, "Del sql=%s\n", query);
409
410       Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
411       db_sql_query(ua->db, query, NULL, (void *)NULL);
412       Dmsg1(050, "Del sql=%s\n", query);
413    }
414    bsendmsg(ua, _("%d Jobs for client %s purged from %s catalog.\n"), del.num_ids,
415       client->hdr.name, client->catalog->hdr.name);
416    
417 bail_out:
418    if (del.JobId) {
419       free(del.JobId);
420    }
421    if (del.PurgedFiles) {
422       free(del.PurgedFiles);
423    }
424    free_pool_memory(query);
425    return 1;
426 }
427
428 void purge_files_from_job(UAContext *ua, JOB_DBR *jr)
429 {
430    char *query = (char *)get_pool_memory(PM_MESSAGE);
431    
432    Mmsg(&query, "DELETE FROM File WHERE JobId=%d", jr->JobId);
433    db_sql_query(ua->db, query, NULL, (void *)NULL);
434
435    Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", jr->JobId);
436    db_sql_query(ua->db, query, NULL, (void *)NULL);
437
438    free_pool_memory(query);
439 }
440
441 void purge_files_from_volume(UAContext *ua, MEDIA_DBR *mr ) 
442 {} /* ***FIXME*** implement */
443
444 /*
445  * Returns: 1 if Volume purged
446  *          0 if Volume not purged
447  */
448 int purge_jobs_from_volume(UAContext *ua, MEDIA_DBR *mr) 
449 {
450    char *query = (char *)get_pool_memory(PM_MESSAGE);
451    struct s_count_ctx cnt;
452    struct s_file_del_ctx del;
453    int i, stat = 0;
454    JOB_DBR jr;
455
456    stat = strcmp(mr->VolStatus, "Append") == 0 || 
457           strcmp(mr->VolStatus, "Full")   == 0 ||
458           strcmp(mr->VolStatus, "Used")   == 0 || 
459           strcmp(mr->VolStatus, "Error")  == 0; 
460    if (!stat) {
461       bsendmsg(ua, "\n");
462       bsendmsg(ua, _("Volume \"%s\" has VolStatus \"%s\" and cannot be purged.\n"
463                      "The VolStatus must be: Append, Full, Used, or Error to be purged.\n"),
464                      mr->VolumeName, mr->VolStatus);
465       goto bail_out;
466    }
467    
468    memset(&jr, 0, sizeof(jr));
469    memset(&del, 0, sizeof(del));
470    cnt.count = 0;
471    Mmsg(&query, "SELECT count(*) FROM JobMedia WHERE MediaId=%d", mr->MediaId);
472    if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
473       bsendmsg(ua, "%s", db_strerror(ua->db));
474       Dmsg0(050, "Count failed\n");
475       goto bail_out;
476    }
477       
478    if (cnt.count == 0) {
479       bsendmsg(ua, "There are no Jobs associated with Volume %s. Marking it purged.\n",
480          mr->VolumeName);
481       if (!mark_media_purged(ua, mr)) {
482          bsendmsg(ua, "%s", db_strerror(ua->db));
483          goto bail_out;
484       }
485       goto bail_out;
486    }
487
488    if (cnt.count < MAX_DEL_LIST_LEN) {
489       del.max_ids = cnt.count + 1;
490    } else {
491       del.max_ids = MAX_DEL_LIST_LEN; 
492    }
493
494    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
495
496    Mmsg(&query, "SELECT JobId FROM JobMedia WHERE MediaId=%d", mr->MediaId);
497    if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
498       bsendmsg(ua, "%s", db_strerror(ua->db));
499       Dmsg0(050, "Count failed\n");
500       goto bail_out;
501    }
502
503    for (i=0; i < del.num_ids; i++) {
504       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
505       Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
506       db_sql_query(ua->db, query, NULL, (void *)NULL);
507       Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
508       db_sql_query(ua->db, query, NULL, (void *)NULL);
509       Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
510       db_sql_query(ua->db, query, NULL, (void *)NULL);
511       Dmsg1(050, "Del sql=%s\n", query);
512       del.num_del++;
513    }
514    if (del.JobId) {
515       free(del.JobId);
516    }
517    bsendmsg(ua, _("%d File%s on Volume \"%s\" purged from catalog.\n"), del.num_del,
518       del.num_del==1?"":"s", mr->VolumeName);
519
520    /* If purged, mark it so */
521    if (del.num_ids == del.num_del) {
522       if (!(stat = mark_media_purged(ua, mr))) {
523          bsendmsg(ua, "%s", db_strerror(ua->db));
524       }
525    }
526
527 bail_out:   
528    free_pool_memory(query);
529    return stat;
530 }
531
532 /*
533  * IF volume status is Append, Full, Used, or Error, mark it Purged
534  *   Purged volumes can then be recycled (if enabled).
535  */
536 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
537 {
538    if (strcmp(mr->VolStatus, "Append") == 0 || 
539        strcmp(mr->VolStatus, "Full")   == 0 ||
540        strcmp(mr->VolStatus, "Used")   == 0 || 
541        strcmp(mr->VolStatus, "Error")  == 0) {
542       strcpy(mr->VolStatus, "Purged");
543       if (!db_update_media_record(ua->jcr, ua->db, mr)) {
544          return 0;
545       }
546       return 1;
547    } else {
548       bsendmsg(ua, _("Cannot purge Volume with VolStatus=%s\n"), mr->VolStatus);
549    }
550    return strcpy(mr->VolStatus, "Purged") == 0;
551 }