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