]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_prune.c
Doc, Vol name scan, misc
[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    int kw;
167
168    static char *keywords[] = {
169       N_("Files"),
170       N_("Jobs"),
171       N_("Volume"),
172       NULL};
173
174    if (!open_db(ua)) {
175       return 01;
176    }
177
178    /* First search args */
179    kw = find_arg_keyword(ua, keywords);  
180    if (kw < 0 || kw > 2) {
181       /* no args, so ask user */
182       kw = do_keyword_prompt(ua, _("Choose item to prune"), keywords);  
183    }       
184     
185    switch (kw) {
186    case 0:  /* prune files */
187       client = select_client_resource(ua);
188       if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
189          return 0;
190       }
191       prune_files(ua, client);
192       return 1;
193    case 1:  /* prune jobs */
194       client = select_client_resource(ua);
195       if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
196          return 0;
197       }
198       /* ****FIXME**** allow user to select JobType */
199       prune_jobs(ua, client, JT_BACKUP);
200       return 1;
201    case 2:  /* prune volume */
202       if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
203          return 0;
204       }
205       if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
206          return 0;
207       }
208       prune_volume(ua, &pr, &mr);
209       return 1;
210    default:
211       break;
212    }
213
214    return 1;
215 }
216
217 /*
218  * Prune File records from the database. For any Job which
219  * is older than the retention period, we unconditionally delete
220  * all File records for that Job.  This is simple enough that no
221  * temporary tables are needed. We simply make an in memory list of
222  * the JobIds meeting the prune conditions, then delete all File records
223  * pointing to each of those JobIds.
224  *
225  * This routine assumes you want the pruning to be done. All checking
226  *  must be done before calling this routine.
227  */
228 int prune_files(UAContext *ua, CLIENT *client)
229 {
230    struct s_file_del_ctx del;
231    POOLMEM *query = get_pool_memory(PM_MESSAGE);
232    int i;
233    utime_t now, period;
234    CLIENT_DBR cr;
235    char ed1[50], ed2[50];
236
237    db_lock(ua->db);
238    memset(&cr, 0, sizeof(cr));
239    memset(&del, 0, sizeof(del));
240    strcpy(cr.Name, client->hdr.name);
241    if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
242       db_unlock(ua->db);
243       return 0;
244    }
245
246    period = client->FileRetention;
247    now = (utime_t)time(NULL);
248        
249    /* Select Jobs -- for counting */
250    Mmsg(&query, select_job, edit_uint64(now - period, ed1), cr.ClientId);
251    Dmsg1(050, "select sql=%s\n", query);
252    if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
253       if (ua->verbose) {
254          bsendmsg(ua, "%s", db_strerror(ua->db));
255       }
256       Dmsg0(050, "Count failed\n");
257       goto bail_out;
258    }
259       
260    if (del.tot_ids == 0) {
261       if (ua->verbose) {
262          bsendmsg(ua, _("No Files found to prune.\n"));
263       }
264       goto bail_out;
265    }
266
267    if (del.tot_ids < MAX_DEL_LIST_LEN) {
268       del.max_ids = del.tot_ids + 1;
269    } else {
270       del.max_ids = MAX_DEL_LIST_LEN; 
271    }
272    del.tot_ids = 0;
273
274    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
275
276    /* Now process same set but making delete list */
277    db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
278
279    for (i=0; i < del.num_ids; i++) {
280       struct s_count_ctx cnt;
281       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
282       Mmsg(&query, cnt_File, del.JobId[i]);
283       cnt.count = 0;
284       db_sql_query(ua->db, query, count_handler, (void *)&cnt);
285       del.tot_ids += cnt.count;
286       Mmsg(&query, del_File, del.JobId[i]);
287       db_sql_query(ua->db, query, NULL, (void *)NULL);
288       /* 
289        * Now mark Job as having files purged. This is necessary to
290        * avoid having too many Jobs to process in future prunings. If
291        * we don't do this, the number of JobId's in our in memory list
292        * will grow very large.
293        */
294       Mmsg(&query, upd_Purged, del.JobId[i]);
295       db_sql_query(ua->db, query, NULL, (void *)NULL);
296       Dmsg1(050, "Del sql=%s\n", query);
297    }
298    edit_uint64_with_commas(del.tot_ids, ed1);
299    edit_uint64_with_commas(del.num_ids, ed2);
300    bsendmsg(ua, _("Pruned %s Files from %s Jobs for client %s from catalog.\n"), 
301       ed1, ed2, client->hdr.name);
302    
303 bail_out:
304    db_unlock(ua->db);
305    if (del.JobId) {
306       free(del.JobId);
307    }
308    free_pool_memory(query);
309    return 1;
310 }
311
312
313 static void drop_temp_tables(UAContext *ua) 
314 {
315    int i;
316    for (i=0; drop_deltabs[i]; i++) {
317       db_sql_query(ua->db, drop_deltabs[i], NULL, (void *)NULL);
318    }
319 }
320
321 static int create_temp_tables(UAContext *ua) 
322 {
323    int i;
324    /* Create temp tables and indicies */
325    for (i=0; create_deltabs[i]; i++) {
326       if (!db_sql_query(ua->db, create_deltabs[i], NULL, (void *)NULL)) {
327          bsendmsg(ua, "%s", db_strerror(ua->db));
328          Dmsg0(050, "create DelTables table failed\n");
329          return 0;
330       }
331    }
332    return 1;
333 }
334
335
336
337 /*
338  * Purging Jobs is a bit more complicated than purging Files
339  * because we delete Job records only if there is a more current
340  * backup of the FileSet. Otherwise, we keep the Job record.
341  * In other words, we never delete the only Job record that
342  * contains a current backup of a FileSet. This prevents the
343  * Volume from being recycled and destroying a current backup.
344  *
345  * For Verify Jobs, we do not delete the last InitCatalog.
346  *
347  * For Restore Jobs there are no restrictions.
348  */
349 int prune_jobs(UAContext *ua, CLIENT *client, int JobType)
350 {
351    struct s_job_del_ctx del;
352    struct s_count_ctx cnt;
353    char *query = (char *)get_pool_memory(PM_MESSAGE);
354    int i;
355    utime_t now, period;
356    CLIENT_DBR cr;
357    char ed1[50];
358
359    db_lock(ua->db);
360    memset(&cr, 0, sizeof(cr));
361    memset(&del, 0, sizeof(del));
362    strcpy(cr.Name, client->hdr.name);
363    if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
364       db_unlock(ua->db);
365       return 0;
366    }
367
368    period = client->JobRetention;
369    now = (utime_t)time(NULL);
370
371    /* Drop any previous temporary tables still there */
372    drop_temp_tables(ua);
373
374    /* Create temp tables and indicies */
375    if (!create_temp_tables(ua)) {
376       goto bail_out;
377    }
378
379    /* 
380     * Select all files that are older than the JobRetention period
381     *  and stuff them into the "DeletionCandidates" table.
382     */
383    edit_uint64(now - period, ed1);
384    Mmsg(&query, insert_delcand, (char)JobType, ed1, cr.ClientId);
385    if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
386       if (ua->verbose) {
387          bsendmsg(ua, "%s", db_strerror(ua->db));
388       }
389       Dmsg0(050, "insert delcand failed\n");
390       goto bail_out;
391    }
392
393    /* Count Files to be deleted */
394    strcpy(query, cnt_DelCand);
395    Dmsg1(100, "select sql=%s\n", query);
396    cnt.count = 0;
397    if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
398       bsendmsg(ua, "%s", db_strerror(ua->db));
399       Dmsg0(050, "Count failed\n");
400       goto bail_out;
401    }
402       
403    if (cnt.count == 0) {
404       if (ua->verbose) {
405          bsendmsg(ua, _("No Jobs found to prune.\n"));
406       }
407       goto bail_out;
408    }
409
410    if (cnt.count < MAX_DEL_LIST_LEN) {
411       del.max_ids = cnt.count + 1;
412    } else {
413       del.max_ids = MAX_DEL_LIST_LEN; 
414    }
415    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
416    del.PurgedFiles = (char *)malloc(del.max_ids);
417
418    switch (JobType) {
419    case JT_ADMIN:
420    case JT_BACKUP:
421       Mmsg(&query, select_backup_del, ed1, cr.ClientId);
422       break;
423    case JT_RESTORE:
424       Mmsg(&query, select_restore_del, ed1, cr.ClientId);
425       break;
426    case JT_VERIFY:
427       Mmsg(&query, select_verify_del, ed1, cr.ClientId);
428       break;
429    }
430    if (!db_sql_query(ua->db, query, job_delete_handler, (void *)&del)) {
431       bsendmsg(ua, "%s", db_strerror(ua->db));
432    }
433
434    /* 
435     * OK, now we have the list of JobId's to be pruned, first check
436     * if the Files have been purged, if not, purge (delete) them.
437     * Then delete the Job entry, and finally and JobMedia records.
438     */
439    for (i=0; i < del.num_ids; i++) {
440       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
441       if (!del.PurgedFiles[i]) {
442          Mmsg(&query, del_File, del.JobId[i]);
443          if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
444             bsendmsg(ua, "%s", db_strerror(ua->db));
445          }
446          Dmsg1(050, "Del sql=%s\n", query);
447       }
448
449       Mmsg(&query, del_Job, del.JobId[i]);
450       if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
451          bsendmsg(ua, "%s", db_strerror(ua->db));
452       }
453       Dmsg1(050, "Del sql=%s\n", query);
454
455       Mmsg(&query, del_JobMedia, del.JobId[i]);
456       if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
457          bsendmsg(ua, "%s", db_strerror(ua->db));
458       }
459       Dmsg1(050, "Del sql=%s\n", query);
460    }
461    bsendmsg(ua, _("Pruned %d %s for client %s from catalog.\n"), del.num_ids,
462       del.num_ids==1?_("Job"):_("Jobs"), client->hdr.name);
463    
464 bail_out:
465    drop_temp_tables(ua);
466    db_unlock(ua->db);
467    if (del.JobId) {
468       free(del.JobId);
469    }
470    if (del.PurgedFiles) {
471       free(del.PurgedFiles);
472    }
473    free_pool_memory(query);
474    return 1;
475 }
476
477 /*
478  * Prune a given Volume
479  */
480 int prune_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
481 {
482    char *query = (char *)get_pool_memory(PM_MESSAGE);
483    struct s_count_ctx cnt;
484    struct s_file_del_ctx del;
485    int i, stat = 0;
486    JOB_DBR jr;
487    utime_t now, period;
488
489    db_lock(ua->db);
490    memset(&jr, 0, sizeof(jr));
491    memset(&del, 0, sizeof(del));
492    cnt.count = 0;
493    Mmsg(&query, cnt_JobMedia, mr->MediaId);
494    if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
495       bsendmsg(ua, "%s", db_strerror(ua->db));
496       Dmsg0(050, "Count failed\n");
497       goto bail_out;
498    }
499       
500    if (cnt.count == 0) {
501       if (ua->verbose) {
502          bsendmsg(ua, "There are no Jobs associated with Volume %s. Marking it purged.\n",
503             mr->VolumeName);
504       }
505       stat = mark_media_purged(ua, mr);
506       goto bail_out;
507    }
508
509    if (cnt.count < MAX_DEL_LIST_LEN) {
510       del.max_ids = cnt.count + 1;
511    } else {
512       del.max_ids = MAX_DEL_LIST_LEN; 
513    }
514
515    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
516
517    /* ***FIXME*** could make this do JobTDate check too */
518    Mmsg(&query, sel_JobMedia, mr->MediaId);
519    if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
520       if (ua->verbose) {
521          bsendmsg(ua, "%s", db_strerror(ua->db));
522       }
523       Dmsg0(050, "Count failed\n");
524       goto bail_out;
525    }
526
527    /* Use Volume Retention to prune Jobs and Files */
528    period = mr->VolRetention;
529    now = (utime_t)time(NULL);
530
531    Dmsg3(200, "Now=%d period=%d now-period=%d\n", (int)now, (int)period,
532       (int)(now-period));
533    for (i=0; i < del.num_ids; i++) {
534       jr.JobId = del.JobId[i];
535       if (!db_get_job_record(ua->jcr, ua->db, &jr)) {
536          continue;
537       }
538       Dmsg2(200, "Looking at %s JobTdate=%d\n", jr.Job, (int)jr.JobTDate);
539       if (jr.JobTDate >= (now - period)) {
540          continue;
541       }
542       Dmsg2(200, "Delete JobId=%d Job=%s\n", del.JobId[i], jr.Job);
543       Mmsg(&query, del_File, del.JobId[i]);
544       db_sql_query(ua->db, query, NULL, (void *)NULL);
545       Mmsg(&query, del_Job, del.JobId[i]);
546       db_sql_query(ua->db, query, NULL, (void *)NULL);
547       Mmsg(&query, del_JobMedia, del.JobId[i]);
548       db_sql_query(ua->db, query, NULL, (void *)NULL);
549       Dmsg1(050, "Del sql=%s\n", query);
550       del.num_del++;
551    }
552    if (del.JobId) {
553       free(del.JobId);
554    }
555    if (ua->verbose && del.num_del != 0) {
556       bsendmsg(ua, _("Pruned %d %s on Volume %s from catalog.\n"), del.num_del,
557          del.num_del == 1 ? "Job" : "Jobs", mr->VolumeName);
558    }
559
560    /* If purged, mark it so */
561    if (del.num_ids == del.num_del) {
562       Dmsg0(200, "Volume is purged.\n");
563       stat = mark_media_purged(ua, mr);
564    }
565
566 bail_out:
567    db_unlock(ua->db);
568    free_pool_memory(query);
569    return stat;
570 }