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