]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_purge.c
Corrections to Recycle Oldest code
[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    POOL_DBR pr;
168    JOB_DBR  jr;
169    static char *keywords[] = {
170       N_("files"),
171       N_("jobs"),
172       N_("volume"),
173       NULL};
174
175    static char *files_keywords[] = {
176       N_("Job"),
177       N_("JobId"),
178       N_("Client"),
179       N_("Volume"),
180       NULL};
181
182    static char *jobs_keywords[] = {
183       N_("Client"),
184       N_("Volume"),
185       NULL};
186       
187    bsendmsg(ua, _(
188       "This command is DANGEROUS!!!\n"
189       "It purges (deletes) all Files from a Job,\n"
190       "JobId, Client or Volume; or it purges (deletes)\n"
191       "all Jobs from a Client or Volume. Normally you\n" 
192       "should use the PRUNE command instead.\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 = select_client_resource(ua);
209          purge_files_from_client(ua, client);
210          return 1;
211       case 3:                         /* Volume */
212          if (select_pool_and_media_dbr(ua, &pr, &mr)) {
213             purge_files_from_volume(ua, &mr);
214          }
215          return 1;
216       }
217    /* Jobs */
218    case 1:
219       switch(find_arg_keyword(ua, jobs_keywords)) {
220       case 0:                         /* client */
221          client = select_client_resource(ua);
222          purge_jobs_from_client(ua, client);
223          return 1;
224       case 1:                         /* Volume */
225          if (select_pool_and_media_dbr(ua, &pr, &mr)) {
226             purge_jobs_from_volume(ua, &mr);
227          }
228          return 1;
229       }
230    /* Volume */
231    case 2:
232       if (select_pool_and_media_dbr(ua, &pr, &mr)) {
233          purge_jobs_from_volume(ua, &mr);
234       }
235       return 1;
236    default:
237       break;
238    }
239    switch (do_keyword_prompt(ua, _("Choose item to purge"), keywords)) {
240    case 0:                            /* files */
241       client = select_client_resource(ua);
242       if (!client) {
243          return 1;
244       }
245       purge_files_from_client(ua, client);
246       break;
247    case 1:                            /* jobs */
248       client = select_client_resource(ua);
249       if (!client) {
250          return 1;
251       }
252       purge_jobs_from_client(ua, client);
253       break;
254    case 2:                            /* Volume */
255       if (select_pool_and_media_dbr(ua, &pr, &mr)) {
256          purge_jobs_from_volume(ua, &mr);
257       }
258       break;
259    }
260    return 1;
261 }
262
263 /*
264  * Prune File records from the database. For any Job which
265  * is older than the retention period, we unconditionally delete
266  * all File records for that Job.  This is simple enough that no
267  * temporary tables are needed. We simply make an in memory list of
268  * the JobIds meeting the prune conditions, then delete all File records
269  * pointing to each of those JobIds.
270  */
271 int purge_files_from_client(UAContext *ua, CLIENT *client)
272 {
273    struct s_file_del_ctx del;
274    char *query = (char *)get_pool_memory(PM_MESSAGE);
275    int i;
276    CLIENT_DBR cr;
277
278    memset(&cr, 0, sizeof(cr));
279    memset(&del, 0, sizeof(del));
280
281    strcpy(cr.Name, client->hdr.name);
282    if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
283       return 0;
284    }
285
286    Mmsg(&query, select_jobsfiles_from_client, cr.ClientId);
287
288    Dmsg1(050, "select sql=%s\n", query);
289  
290    if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
291       bsendmsg(ua, "%s", db_strerror(ua->db));
292       Dmsg0(050, "Count failed\n");
293       goto bail_out;
294    }
295       
296    if (del.tot_ids == 0) {
297       bsendmsg(ua, _("No Files found for client %s to purge from %s catalog.\n"),
298          client->hdr.name, client->catalog->hdr.name);
299       goto bail_out;
300    }
301
302    if (del.tot_ids < MAX_DEL_LIST_LEN) {
303       del.max_ids = del.tot_ids + 1;
304    } else {
305       del.max_ids = MAX_DEL_LIST_LEN; 
306    }
307    del.tot_ids = 0;
308
309    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
310
311    db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
312
313    for (i=0; i < del.num_ids; i++) {
314       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
315       Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
316       db_sql_query(ua->db, query, NULL, (void *)NULL);
317       /* 
318        * Now mark Job as having files purged. This is necessary to
319        * avoid having too many Jobs to process in future prunings. If
320        * we don't do this, the number of JobId's in our in memory list
321        * will grow very large.
322        */
323       Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", del.JobId[i]);
324       db_sql_query(ua->db, query, NULL, (void *)NULL);
325       Dmsg1(050, "Del sql=%s\n", query);
326    }
327    bsendmsg(ua, _("%d Files for client %s purged from %s catalog.\n"), del.num_ids,
328       client->hdr.name, client->catalog->hdr.name);
329    
330 bail_out:
331    if (del.JobId) {
332       free(del.JobId);
333    }
334    free_pool_memory(query);
335    return 1;
336 }
337
338
339
340 /*
341  * Purging Jobs is a bit more complicated than purging Files
342  * because we delete Job records only if there is a more current
343  * backup of the FileSet. Otherwise, we keep the Job record.
344  * In other words, we never delete the only Job record that
345  * contains a current backup of a FileSet. This prevents the
346  * Volume from being recycled and destroying a current backup.
347  */
348 int purge_jobs_from_client(UAContext *ua, CLIENT *client)
349 {
350    struct s_job_del_ctx del;
351    char *query = (char *)get_pool_memory(PM_MESSAGE);
352    int i;
353    CLIENT_DBR cr;
354
355    memset(&cr, 0, sizeof(cr));
356    memset(&del, 0, sizeof(del));
357
358    strcpy(cr.Name, client->hdr.name);
359    if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
360       return 0;
361    }
362
363    Mmsg(&query, select_jobs_from_client, cr.ClientId);
364
365    Dmsg1(050, "select sql=%s\n", query);
366  
367    if (!db_sql_query(ua->db, query, job_count_handler, (void *)&del)) {
368       bsendmsg(ua, "%s", db_strerror(ua->db));
369       Dmsg0(050, "Count failed\n");
370       goto bail_out;
371    }
372    if (del.tot_ids == 0) {
373       bsendmsg(ua, _("No Jobs found for client %s to purge from %s catalog.\n"),
374          client->hdr.name, client->catalog->hdr.name);
375       goto bail_out;
376    }
377
378    if (del.tot_ids < MAX_DEL_LIST_LEN) {
379       del.max_ids = del.tot_ids + 1;
380    } else {
381       del.max_ids = MAX_DEL_LIST_LEN; 
382    }
383
384    del.tot_ids = 0;
385
386    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
387    del.PurgedFiles = (char *)malloc(del.max_ids);
388
389    db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
390
391    /* 
392     * OK, now we have the list of JobId's to be purged, first check
393     * if the Files have been purged, if not, purge (delete) them.
394     * Then delete the Job entry, and finally and JobMedia records.
395     */
396    for (i=0; i < del.num_ids; i++) {
397       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
398       if (!del.PurgedFiles[i]) {
399          Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
400          db_sql_query(ua->db, query, NULL, (void *)NULL);
401          Dmsg1(050, "Del sql=%s\n", query);
402       }
403
404       Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
405       db_sql_query(ua->db, query, NULL, (void *)NULL);
406       Dmsg1(050, "Del sql=%s\n", query);
407
408       Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
409       db_sql_query(ua->db, query, NULL, (void *)NULL);
410       Dmsg1(050, "Del sql=%s\n", query);
411    }
412    bsendmsg(ua, _("%d Jobs for client %s purged from %s catalog.\n"), del.num_ids,
413       client->hdr.name, client->catalog->hdr.name);
414    
415 bail_out:
416    if (del.JobId) {
417       free(del.JobId);
418    }
419    if (del.PurgedFiles) {
420       free(del.PurgedFiles);
421    }
422    free_pool_memory(query);
423    return 1;
424 }
425
426 void purge_files_from_job(UAContext *ua, JOB_DBR *jr)
427 {
428    char *query = (char *)get_pool_memory(PM_MESSAGE);
429    
430    Mmsg(&query, "DELETE FROM File WHERE JobId=%d", jr->JobId);
431    db_sql_query(ua->db, query, NULL, (void *)NULL);
432
433    Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", jr->JobId);
434    db_sql_query(ua->db, query, NULL, (void *)NULL);
435
436    free_pool_memory(query);
437 }
438
439 void purge_files_from_volume(UAContext *ua, MEDIA_DBR *mr ) 
440 {} /* ***FIXME*** implement */
441
442 /*
443  * Returns: 1 if Volume purged
444  *          0 if Volume not purged
445  */
446 int purge_jobs_from_volume(UAContext *ua, MEDIA_DBR *mr) 
447 {
448    char *query = (char *)get_pool_memory(PM_MESSAGE);
449    struct s_count_ctx cnt;
450    struct s_file_del_ctx del;
451    int i, stat = 0;
452    JOB_DBR jr;
453
454    memset(&jr, 0, sizeof(jr));
455    memset(&del, 0, sizeof(del));
456    cnt.count = 0;
457    Mmsg(&query, "SELECT count(*) FROM JobMedia WHERE MediaId=%d", mr->MediaId);
458    if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
459       bsendmsg(ua, "%s", db_strerror(ua->db));
460       Dmsg0(050, "Count failed\n");
461       goto bail_out;
462    }
463       
464    if (cnt.count == 0) {
465       bsendmsg(ua, "There are no Jobs associated with Volume %s. Marking it purged.\n",
466          mr->VolumeName);
467       if (!mark_media_purged(ua, mr)) {
468          bsendmsg(ua, "%s", db_strerror(ua->db));
469          goto bail_out;
470       }
471       goto bail_out;
472    }
473
474    if (cnt.count < MAX_DEL_LIST_LEN) {
475       del.max_ids = cnt.count + 1;
476    } else {
477       del.max_ids = MAX_DEL_LIST_LEN; 
478    }
479
480    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
481
482    Mmsg(&query, "SELECT JobId FROM JobMedia WHERE MediaId=%d", mr->MediaId);
483    if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
484       bsendmsg(ua, "%s", db_strerror(ua->db));
485       Dmsg0(050, "Count failed\n");
486       goto bail_out;
487    }
488
489    for (i=0; i < del.num_ids; i++) {
490       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
491       Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
492       db_sql_query(ua->db, query, NULL, (void *)NULL);
493       Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
494       db_sql_query(ua->db, query, NULL, (void *)NULL);
495       Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
496       db_sql_query(ua->db, query, NULL, (void *)NULL);
497       Dmsg1(050, "Del sql=%s\n", query);
498       del.num_del++;
499    }
500    if (del.JobId) {
501       free(del.JobId);
502    }
503    bsendmsg(ua, _("%d File%s on Volume %s purged from catalog.\n"), del.num_del,
504       del.num_del==1?"":"s", mr->VolumeName);
505
506    /* If purged, mark it so */
507    if (del.num_ids == del.num_del) {
508       if (!(stat = mark_media_purged(ua, mr))) {
509          bsendmsg(ua, "%s", db_strerror(ua->db));
510       }
511    }
512
513 bail_out:   
514    free_pool_memory(query);
515    return stat;
516 }
517
518 /*
519  * IF volume status is Append, Full, Used, or Error, mark it Purged
520  *   Purged volumes can then be recycled (if enabled).
521  */
522 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
523 {
524    if (strcmp(mr->VolStatus, "Append") == 0 || 
525        strcmp(mr->VolStatus, "Full")   == 0 ||
526        strcmp(mr->VolStatus, "Used")   == 0 || 
527        strcmp(mr->VolStatus, "Error")  == 0) {
528       strcpy(mr->VolStatus, "Purged");
529       if (!db_update_media_record(ua->jcr, ua->db, mr)) {
530          return 0;
531       }
532       return 1;
533    } else {
534       bsendmsg(ua, _("Cannot purge Volume with VolStatus=%s\n"), mr->VolStatus);
535    }
536    return strcpy(mr->VolStatus, "Purged") == 0;
537 }