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