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