]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_purge.c
Retention period updates
[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
12 /*
13    Copyright (C) 2002 Kern Sibbald and John Walker
14
15    This program is free software; you can redistribute it and/or
16    modify it under the terms of the GNU General Public License as
17    published by the Free Software Foundation; either version 2 of
18    the License, or (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public
26    License along with this program; if not, write to the Free
27    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28    MA 02111-1307, USA.
29
30  */
31
32 #include "bacula.h"
33 #include "dird.h"
34 #include "ua.h"
35
36 /* Forward referenced functions */
37 int purge_files_from_client(UAContext *ua, CLIENT *client);
38 int purge_jobs_from_client(UAContext *ua, CLIENT *client);
39 void purge_files_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr );
40 void purge_jobs_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr);
41 void purge_files_from_job(UAContext *ua, JOB_DBR *jr);
42 static int mark_media_purged(UAContext *ua, MEDIA_DBR *mr);
43
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)strtod(row[0], NULL);
135    del->PurgedFiles[del->num_ids++] = (char)atoi(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)strtod(row[0], NULL);
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       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       "This command is DANGEROUS!!!\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. Normally you\n" 
191       "should use the PRUNE command instead.\n"));
192
193    if (!open_db(ua)) {
194       return 1;
195    }
196    switch (find_arg_keyword(ua, keywords)) {
197    /* Files */
198    case 0:
199       switch(find_arg_keyword(ua, files_keywords)) {
200       case 0:                         /* Job */
201       case 1:
202          if (get_job_dbr(ua, &jr)) {
203             purge_files_from_job(ua, &jr);
204          }
205          return 1;
206       case 2:                         /* client */
207          client = select_client_resource(ua);
208          purge_files_from_client(ua, client);
209          return 1;
210       case 3:
211          if (select_pool_and_media_dbr(ua, &pr, &mr)) {
212             purge_files_from_volume(ua, &pr, &mr);
213          }
214          return 1;
215       }
216    /* Jobs */
217    case 1:
218       switch(find_arg_keyword(ua, jobs_keywords)) {
219       case 0:                         /* client */
220          client = select_client_resource(ua);
221          purge_jobs_from_client(ua, client);
222          return 1;
223       case 1:
224          if (select_pool_and_media_dbr(ua, &pr, &mr)) {
225             purge_jobs_from_volume(ua, &pr, &mr);
226          }
227          return 1;
228       }
229    default:
230       break;
231    }
232    switch (do_keyword_prompt(ua, _("Choose item to purge"), keywords)) {
233    case 0:
234       client = select_client_resource(ua);
235       if (!client) {
236          return 1;
237       }
238       purge_files_from_client(ua, client);
239       break;
240    case 1:
241       client = select_client_resource(ua);
242       if (!client) {
243          return 1;
244       }
245       purge_jobs_from_client(ua, client);
246       break;
247    }
248    return 1;
249 }
250
251 /*
252  * Prune File records from the database. For any Job which
253  * is older than the retention period, we unconditionally delete
254  * all File records for that Job.  This is simple enough that no
255  * temporary tables are needed. We simply make an in memory list of
256  * the JobIds meeting the prune conditions, then delete all File records
257  * pointing to each of those JobIds.
258  */
259 int purge_files_from_client(UAContext *ua, CLIENT *client)
260 {
261    struct s_file_del_ctx del;
262    char *query = (char *)get_pool_memory(PM_MESSAGE);
263    int i;
264    CLIENT_DBR cr;
265
266    memset(&cr, 0, sizeof(cr));
267    memset(&del, 0, sizeof(del));
268
269    strcpy(cr.Name, client->hdr.name);
270    if (!db_create_client_record(ua->db, &cr)) {
271       return 0;
272    }
273
274    Mmsg(&query, select_jobsfiles_from_client, cr.ClientId);
275
276    Dmsg1(050, "select sql=%s\n", query);
277  
278    if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
279       bsendmsg(ua, "%s", db_strerror(ua->db));
280       Dmsg0(050, "Count failed\n");
281       goto bail_out;
282    }
283       
284    if (del.tot_ids == 0) {
285       bsendmsg(ua, _("No Files found for client %s to purge from %s catalog.\n"),
286          client->hdr.name, client->catalog->hdr.name);
287       goto bail_out;
288    }
289
290    if (del.tot_ids < MAX_DEL_LIST_LEN) {
291       del.max_ids = del.tot_ids + 1;
292    } else {
293       del.max_ids = MAX_DEL_LIST_LEN; 
294    }
295    del.tot_ids = 0;
296
297    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
298
299    db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
300
301    for (i=0; i < del.num_ids; i++) {
302       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
303       Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
304       db_sql_query(ua->db, query, NULL, (void *)NULL);
305       /* 
306        * Now mark Job as having files purged. This is necessary to
307        * avoid having too many Jobs to process in future prunings. If
308        * we don't do this, the number of JobId's in our in memory list
309        * will grow very large.
310        */
311       Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", del.JobId[i]);
312       db_sql_query(ua->db, query, NULL, (void *)NULL);
313       Dmsg1(050, "Del sql=%s\n", query);
314    }
315    bsendmsg(ua, _("%d Files for client %s purged from %s catalog.\n"), del.num_ids,
316       client->hdr.name, client->catalog->hdr.name);
317    
318 bail_out:
319    if (del.JobId) {
320       free(del.JobId);
321    }
322    free_pool_memory(query);
323    return 1;
324 }
325
326
327
328 /*
329  * Purging Jobs is a bit more complicated than purging Files
330  * because we delete Job records only if there is a more current
331  * backup of the FileSet. Otherwise, we keep the Job record.
332  * In other words, we never delete the only Job record that
333  * contains a current backup of a FileSet. This prevents the
334  * Volume from being recycled and destroying a current backup.
335  */
336 int purge_jobs_from_client(UAContext *ua, CLIENT *client)
337 {
338    struct s_job_del_ctx del;
339    char *query = (char *)get_pool_memory(PM_MESSAGE);
340    int i;
341    CLIENT_DBR cr;
342
343    memset(&cr, 0, sizeof(cr));
344    memset(&del, 0, sizeof(del));
345
346    strcpy(cr.Name, client->hdr.name);
347    if (!db_create_client_record(ua->db, &cr)) {
348       return 0;
349    }
350
351    Mmsg(&query, select_jobs_from_client, cr.ClientId);
352
353    Dmsg1(050, "select sql=%s\n", query);
354  
355    if (!db_sql_query(ua->db, query, job_count_handler, (void *)&del)) {
356       bsendmsg(ua, "%s", db_strerror(ua->db));
357       Dmsg0(050, "Count failed\n");
358       goto bail_out;
359    }
360    if (del.tot_ids == 0) {
361       bsendmsg(ua, _("No Jobs found for client %s to purge from %s catalog.\n"),
362          client->hdr.name, client->catalog->hdr.name);
363       goto bail_out;
364    }
365
366    if (del.tot_ids < MAX_DEL_LIST_LEN) {
367       del.max_ids = del.tot_ids + 1;
368    } else {
369       del.max_ids = MAX_DEL_LIST_LEN; 
370    }
371
372    del.tot_ids = 0;
373
374    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
375    del.PurgedFiles = (char *)malloc(del.max_ids);
376
377    db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
378
379    /* 
380     * OK, now we have the list of JobId's to be purged, first check
381     * if the Files have been purged, if not, purge (delete) them.
382     * Then delete the Job entry, and finally and JobMedia records.
383     */
384    for (i=0; i < del.num_ids; i++) {
385       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
386       if (!del.PurgedFiles[i]) {
387          Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
388          db_sql_query(ua->db, query, NULL, (void *)NULL);
389          Dmsg1(050, "Del sql=%s\n", query);
390       }
391
392       Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
393       db_sql_query(ua->db, query, NULL, (void *)NULL);
394       Dmsg1(050, "Del sql=%s\n", query);
395
396       Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
397       db_sql_query(ua->db, query, NULL, (void *)NULL);
398       Dmsg1(050, "Del sql=%s\n", query);
399    }
400    bsendmsg(ua, _("%d Jobs for client %s purged from %s catalog.\n"), del.num_ids,
401       client->hdr.name, client->catalog->hdr.name);
402    
403 bail_out:
404    if (del.JobId) {
405       free(del.JobId);
406    }
407    if (del.PurgedFiles) {
408       free(del.PurgedFiles);
409    }
410    free_pool_memory(query);
411    return 1;
412 }
413
414 void purge_files_from_job(UAContext *ua, JOB_DBR *jr)
415 {
416    char *query = (char *)get_pool_memory(PM_MESSAGE);
417    
418    Mmsg(&query, "DELETE FROM File WHERE JobId=%d", jr->JobId);
419    db_sql_query(ua->db, query, NULL, (void *)NULL);
420
421    Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", jr->JobId);
422    db_sql_query(ua->db, query, NULL, (void *)NULL);
423
424    free_pool_memory(query);
425 }
426
427 void purge_files_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr ) 
428 {} /* ***FIXME*** implement */
429
430 void purge_jobs_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr) 
431 {
432    char *query = (char *)get_pool_memory(PM_MESSAGE);
433    struct s_count_ctx cnt;
434    struct s_file_del_ctx del;
435    int i;
436    JOB_DBR jr;
437
438    memset(&jr, 0, sizeof(jr));
439    memset(&del, 0, sizeof(del));
440    cnt.count = 0;
441    Mmsg(&query, "SELECT count(*) FROM JobMedia WHERE MediaId=%d", mr->MediaId);
442    if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
443       bsendmsg(ua, "%s", db_strerror(ua->db));
444       Dmsg0(050, "Count failed\n");
445       goto bail_out;
446    }
447       
448    if (cnt.count == 0) {
449       bsendmsg(ua, "There are no Jobs associated with Volume %s. It is purged.\n",
450          mr->VolumeName);
451       if (!mark_media_purged(ua, mr)) {
452          goto bail_out;
453       }
454       goto bail_out;
455    }
456
457    if (cnt.count < MAX_DEL_LIST_LEN) {
458       del.max_ids = cnt.count + 1;
459    } else {
460       del.max_ids = MAX_DEL_LIST_LEN; 
461    }
462
463    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
464
465    Mmsg(&query, "SELECT JobId FROM JobMedia WHERE MediaId=%d", mr->MediaId);
466    if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
467       bsendmsg(ua, "%s", db_strerror(ua->db));
468       Dmsg0(050, "Count failed\n");
469       goto bail_out;
470    }
471
472    for (i=0; i < del.num_ids; i++) {
473       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
474       Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
475       db_sql_query(ua->db, query, NULL, (void *)NULL);
476       Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
477       db_sql_query(ua->db, query, NULL, (void *)NULL);
478       Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
479       db_sql_query(ua->db, query, NULL, (void *)NULL);
480       Dmsg1(050, "Del sql=%s\n", query);
481       del.num_del++;
482    }
483    if (del.JobId) {
484       free(del.JobId);
485    }
486    bsendmsg(ua, _("%d Files for Volume %s purged from catalog.\n"), del.num_del,
487       mr->VolumeName);
488
489    /* If purged, mark it so */
490    if (del.num_ids == del.num_del) {
491       mark_media_purged(ua, mr);
492    }
493
494 bail_out:   
495    free_pool_memory(query);
496 }
497
498 static int mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
499 {
500    if (strcmp(mr->VolStatus, "Append") == 0 || 
501        strcmp(mr->VolStatus, "Full")   == 0) {
502       strcpy(mr->VolStatus, "Purged");
503       if (!db_update_media_record(ua->db, mr)) {
504          bsendmsg(ua, "%s", db_strerror(ua->db));
505          return 0;
506       }
507    }
508    return 1;
509 }