]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_prune.c
Prune SQL fix; finish Linux rescue; doc
[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, (char)JobType, 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       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->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 }