]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_purge.c
restore + SQL editing cleanups
[bacula/bacula] / bacula / src / dird / ua_purge.c
1 /*
2  *
3  *   Bacula Director -- User Agent Database Purge Command
4  *
5  *      Purges Files from specific JobIds
6  * or
7  *      Purges Jobs from Volumes
8  *
9  *     Kern Sibbald, February MMII
10  *
11  *   Version $Id$
12  */
13
14 /*
15    Copyright (C) 2002 Kern Sibbald and John Walker
16
17    This program is free software; you can redistribute it and/or
18    modify it under the terms of the GNU General Public License as
19    published by the Free Software Foundation; either version 2 of
20    the License, or (at your option) any later version.
21
22    This program is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public
28    License along with this program; if not, write to the Free
29    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30    MA 02111-1307, USA.
31
32  */
33
34 #include "bacula.h"
35 #include "dird.h"
36 #include "ua.h"
37
38 /* Forward referenced functions */
39 int purge_files_from_client(UAContext *ua, CLIENT *client);
40 int purge_jobs_from_client(UAContext *ua, CLIENT *client);
41 void purge_files_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr );
42 void purge_jobs_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr);
43 void purge_files_from_job(UAContext *ua, JOB_DBR *jr);
44 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr);
45
46 #define MAX_DEL_LIST_LEN 1000000
47
48
49 static char *select_jobsfiles_from_client =
50    "SELECT JobId FROM Job "
51    "WHERE ClientId=%d "
52    "AND PurgedFiles=0";
53
54 static char *select_jobs_from_client =
55    "SELECT JobId, PurgedFiles FROM Job "
56    "WHERE ClientId=%d";
57
58
59 /* In memory list of JobIds */
60 struct s_file_del_ctx {
61    JobId_t *JobId;
62    int num_ids;                       /* ids stored */
63    int max_ids;                       /* size of array */
64    int num_del;                       /* number deleted */
65    int tot_ids;                       /* total to process */
66 };
67
68 struct s_job_del_ctx {
69    JobId_t *JobId;                    /* array of JobIds */
70    char *PurgedFiles;                 /* Array of PurgedFile flags */
71    int num_ids;                       /* ids stored */
72    int max_ids;                       /* size of array */
73    int num_del;                       /* number deleted */
74    int tot_ids;                       /* total to process */
75 };
76
77 struct s_count_ctx {
78    int count;
79 };
80
81 /*
82  * Called here to count entries to be deleted 
83  */
84 static int count_handler(void *ctx, int num_fields, char **row)
85 {
86    struct s_count_ctx *cnt = (struct s_count_ctx *)ctx;
87
88    if (row[0]) {
89       cnt->count = atoi(row[0]);
90    } else {
91       cnt->count = 0;
92    }
93    return 0;
94 }
95
96 /*
97  * Called here to count entries to be deleted 
98  */
99 static int file_count_handler(void *ctx, int num_fields, char **row)
100 {
101    struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
102    del->tot_ids++;
103    return 0;
104 }
105
106
107 static int job_count_handler(void *ctx, int num_fields, char **row)
108 {
109    struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
110    del->tot_ids++;
111    return 0;
112 }
113
114
115 /*
116  * Called here to make in memory list of JobIds to be
117  *  deleted and the associated PurgedFiles flag.
118  *  The in memory list will then be transversed
119  *  to issue the SQL DELETE commands.  Note, the list
120  *  is allowed to get to MAX_DEL_LIST_LEN to limit the
121  *  maximum malloc'ed memory.
122  */
123 static int job_delete_handler(void *ctx, int num_fields, char **row)
124 {
125    struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
126
127    if (del->num_ids == MAX_DEL_LIST_LEN) {  
128       return 1;
129    }
130    if (del->num_ids == del->max_ids) {
131       del->max_ids = (del->max_ids * 3) / 2;
132       del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) * del->max_ids);
133       del->PurgedFiles = (char *)brealloc(del->PurgedFiles, del->max_ids);
134    }
135    del->JobId[del->num_ids] = (JobId_t)str_to_int64(row[0]);
136    del->PurgedFiles[del->num_ids++] = (char)str_to_int64(row[0]);
137    return 0;
138 }
139
140 static int file_delete_handler(void *ctx, int num_fields, char **row)
141 {
142    struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
143
144    if (del->num_ids == MAX_DEL_LIST_LEN) {  
145       return 1;
146    }
147    if (del->num_ids == del->max_ids) {
148       del->max_ids = (del->max_ids * 3) / 2;
149       del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) *
150          del->max_ids);
151    }
152    del->JobId[del->num_ids++] = (JobId_t)str_to_int64(row[0]);
153    return 0;
154 }
155
156 /*
157  *   Purge records from database
158  *
159  *     Purge Files (from) [Job|JobId|Client|Volume]
160  *     Purge Jobs  (from) [Client|Volume]
161  *
162  *  N.B. Not all above is implemented yet.
163  */
164 int purgecmd(UAContext *ua, char *cmd)
165 {
166    CLIENT *client;
167    MEDIA_DBR mr;
168    POOL_DBR pr;
169    JOB_DBR  jr;
170    static char *keywords[] = {
171       N_("files"),
172       N_("jobs"),
173       NULL};
174
175    static char *files_keywords[] = {
176       N_("Job"),
177       N_("JobId"),
178       N_("Client"),
179       N_("Volume"),
180       NULL};
181
182    static char *jobs_keywords[] = {
183       N_("Client"),
184       N_("Volume"),
185       NULL};
186       
187    bsendmsg(ua, _(
188       "This command is DANGEROUS!!!\n"
189       "It purges (deletes) all Files from a Job,\n"
190       "JobId, Client or Volume; or it purges (deletes)\n"
191       "all Jobs from a Client or Volume. Normally you\n" 
192       "should use the PRUNE command instead.\n"));
193
194    if (!open_db(ua)) {
195       return 1;
196    }
197    switch (find_arg_keyword(ua, keywords)) {
198    /* Files */
199    case 0:
200       switch(find_arg_keyword(ua, files_keywords)) {
201       case 0:                         /* Job */
202       case 1:
203          if (get_job_dbr(ua, &jr)) {
204             purge_files_from_job(ua, &jr);
205          }
206          return 1;
207       case 2:                         /* client */
208          client = select_client_resource(ua);
209          purge_files_from_client(ua, client);
210          return 1;
211       case 3:
212          if (select_pool_and_media_dbr(ua, &pr, &mr)) {
213             purge_files_from_volume(ua, &pr, &mr);
214          }
215          return 1;
216       }
217    /* Jobs */
218    case 1:
219       switch(find_arg_keyword(ua, jobs_keywords)) {
220       case 0:                         /* client */
221          client = select_client_resource(ua);
222          purge_jobs_from_client(ua, client);
223          return 1;
224       case 1:
225          if (select_pool_and_media_dbr(ua, &pr, &mr)) {
226             purge_jobs_from_volume(ua, &pr, &mr);
227          }
228          return 1;
229       }
230    default:
231       break;
232    }
233    switch (do_keyword_prompt(ua, _("Choose item to purge"), keywords)) {
234    case 0:
235       client = select_client_resource(ua);
236       if (!client) {
237          return 1;
238       }
239       purge_files_from_client(ua, client);
240       break;
241    case 1:
242       client = select_client_resource(ua);
243       if (!client) {
244          return 1;
245       }
246       purge_jobs_from_client(ua, client);
247       break;
248    }
249    return 1;
250 }
251
252 /*
253  * Prune File records from the database. For any Job which
254  * is older than the retention period, we unconditionally delete
255  * all File records for that Job.  This is simple enough that no
256  * temporary tables are needed. We simply make an in memory list of
257  * the JobIds meeting the prune conditions, then delete all File records
258  * pointing to each of those JobIds.
259  */
260 int purge_files_from_client(UAContext *ua, CLIENT *client)
261 {
262    struct s_file_del_ctx del;
263    char *query = (char *)get_pool_memory(PM_MESSAGE);
264    int i;
265    CLIENT_DBR cr;
266
267    memset(&cr, 0, sizeof(cr));
268    memset(&del, 0, sizeof(del));
269
270    strcpy(cr.Name, client->hdr.name);
271    if (!db_create_client_record(ua->db, &cr)) {
272       return 0;
273    }
274
275    Mmsg(&query, select_jobsfiles_from_client, cr.ClientId);
276
277    Dmsg1(050, "select sql=%s\n", query);
278  
279    if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
280       bsendmsg(ua, "%s", db_strerror(ua->db));
281       Dmsg0(050, "Count failed\n");
282       goto bail_out;
283    }
284       
285    if (del.tot_ids == 0) {
286       bsendmsg(ua, _("No Files found for client %s to purge from %s catalog.\n"),
287          client->hdr.name, client->catalog->hdr.name);
288       goto bail_out;
289    }
290
291    if (del.tot_ids < MAX_DEL_LIST_LEN) {
292       del.max_ids = del.tot_ids + 1;
293    } else {
294       del.max_ids = MAX_DEL_LIST_LEN; 
295    }
296    del.tot_ids = 0;
297
298    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
299
300    db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
301
302    for (i=0; i < del.num_ids; i++) {
303       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
304       Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
305       db_sql_query(ua->db, query, NULL, (void *)NULL);
306       /* 
307        * Now mark Job as having files purged. This is necessary to
308        * avoid having too many Jobs to process in future prunings. If
309        * we don't do this, the number of JobId's in our in memory list
310        * will grow very large.
311        */
312       Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", del.JobId[i]);
313       db_sql_query(ua->db, query, NULL, (void *)NULL);
314       Dmsg1(050, "Del sql=%s\n", query);
315    }
316    bsendmsg(ua, _("%d Files for client %s purged from %s catalog.\n"), del.num_ids,
317       client->hdr.name, client->catalog->hdr.name);
318    
319 bail_out:
320    if (del.JobId) {
321       free(del.JobId);
322    }
323    free_pool_memory(query);
324    return 1;
325 }
326
327
328
329 /*
330  * Purging Jobs is a bit more complicated than purging Files
331  * because we delete Job records only if there is a more current
332  * backup of the FileSet. Otherwise, we keep the Job record.
333  * In other words, we never delete the only Job record that
334  * contains a current backup of a FileSet. This prevents the
335  * Volume from being recycled and destroying a current backup.
336  */
337 int purge_jobs_from_client(UAContext *ua, CLIENT *client)
338 {
339    struct s_job_del_ctx del;
340    char *query = (char *)get_pool_memory(PM_MESSAGE);
341    int i;
342    CLIENT_DBR cr;
343
344    memset(&cr, 0, sizeof(cr));
345    memset(&del, 0, sizeof(del));
346
347    strcpy(cr.Name, client->hdr.name);
348    if (!db_create_client_record(ua->db, &cr)) {
349       return 0;
350    }
351
352    Mmsg(&query, select_jobs_from_client, cr.ClientId);
353
354    Dmsg1(050, "select sql=%s\n", query);
355  
356    if (!db_sql_query(ua->db, query, job_count_handler, (void *)&del)) {
357       bsendmsg(ua, "%s", db_strerror(ua->db));
358       Dmsg0(050, "Count failed\n");
359       goto bail_out;
360    }
361    if (del.tot_ids == 0) {
362       bsendmsg(ua, _("No Jobs found for client %s to purge from %s catalog.\n"),
363          client->hdr.name, client->catalog->hdr.name);
364       goto bail_out;
365    }
366
367    if (del.tot_ids < MAX_DEL_LIST_LEN) {
368       del.max_ids = del.tot_ids + 1;
369    } else {
370       del.max_ids = MAX_DEL_LIST_LEN; 
371    }
372
373    del.tot_ids = 0;
374
375    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
376    del.PurgedFiles = (char *)malloc(del.max_ids);
377
378    db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
379
380    /* 
381     * OK, now we have the list of JobId's to be purged, first check
382     * if the Files have been purged, if not, purge (delete) them.
383     * Then delete the Job entry, and finally and JobMedia records.
384     */
385    for (i=0; i < del.num_ids; i++) {
386       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
387       if (!del.PurgedFiles[i]) {
388          Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
389          db_sql_query(ua->db, query, NULL, (void *)NULL);
390          Dmsg1(050, "Del sql=%s\n", query);
391       }
392
393       Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
394       db_sql_query(ua->db, query, NULL, (void *)NULL);
395       Dmsg1(050, "Del sql=%s\n", query);
396
397       Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
398       db_sql_query(ua->db, query, NULL, (void *)NULL);
399       Dmsg1(050, "Del sql=%s\n", query);
400    }
401    bsendmsg(ua, _("%d Jobs for client %s purged from %s catalog.\n"), del.num_ids,
402       client->hdr.name, client->catalog->hdr.name);
403    
404 bail_out:
405    if (del.JobId) {
406       free(del.JobId);
407    }
408    if (del.PurgedFiles) {
409       free(del.PurgedFiles);
410    }
411    free_pool_memory(query);
412    return 1;
413 }
414
415 void purge_files_from_job(UAContext *ua, JOB_DBR *jr)
416 {
417    char *query = (char *)get_pool_memory(PM_MESSAGE);
418    
419    Mmsg(&query, "DELETE FROM File WHERE JobId=%d", jr->JobId);
420    db_sql_query(ua->db, query, NULL, (void *)NULL);
421
422    Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", jr->JobId);
423    db_sql_query(ua->db, query, NULL, (void *)NULL);
424
425    free_pool_memory(query);
426 }
427
428 void purge_files_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr ) 
429 {} /* ***FIXME*** implement */
430
431 void purge_jobs_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr) 
432 {
433    char *query = (char *)get_pool_memory(PM_MESSAGE);
434    struct s_count_ctx cnt;
435    struct s_file_del_ctx del;
436    int i;
437    JOB_DBR jr;
438
439    memset(&jr, 0, sizeof(jr));
440    memset(&del, 0, sizeof(del));
441    cnt.count = 0;
442    Mmsg(&query, "SELECT count(*) FROM JobMedia WHERE MediaId=%d", mr->MediaId);
443    if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
444       bsendmsg(ua, "%s", db_strerror(ua->db));
445       Dmsg0(050, "Count failed\n");
446       goto bail_out;
447    }
448       
449    if (cnt.count == 0) {
450       bsendmsg(ua, "There are no Jobs associated with Volume %s. Marking it purged.\n",
451          mr->VolumeName);
452       if (!mark_media_purged(ua, mr)) {
453          goto bail_out;
454       }
455       goto bail_out;
456    }
457
458    if (cnt.count < MAX_DEL_LIST_LEN) {
459       del.max_ids = cnt.count + 1;
460    } else {
461       del.max_ids = MAX_DEL_LIST_LEN; 
462    }
463
464    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
465
466    Mmsg(&query, "SELECT JobId FROM JobMedia WHERE MediaId=%d", mr->MediaId);
467    if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
468       bsendmsg(ua, "%s", db_strerror(ua->db));
469       Dmsg0(050, "Count failed\n");
470       goto bail_out;
471    }
472
473    for (i=0; i < del.num_ids; i++) {
474       Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
475       Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
476       db_sql_query(ua->db, query, NULL, (void *)NULL);
477       Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
478       db_sql_query(ua->db, query, NULL, (void *)NULL);
479       Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
480       db_sql_query(ua->db, query, NULL, (void *)NULL);
481       Dmsg1(050, "Del sql=%s\n", query);
482       del.num_del++;
483    }
484    if (del.JobId) {
485       free(del.JobId);
486    }
487    bsendmsg(ua, _("%d Files for Volume %s purged from catalog.\n"), del.num_del,
488       mr->VolumeName);
489
490    /* If purged, mark it so */
491    if (del.num_ids == del.num_del) {
492       mark_media_purged(ua, mr);
493    }
494
495 bail_out:   
496    free_pool_memory(query);
497 }
498
499 /*
500  * IF volume status is Append, Full, or Used, mark it Purged
501  *   Purged volumes can then be recycled (if enabled).
502  */
503 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
504 {
505    if (strcmp(mr->VolStatus, "Append") == 0 || 
506        strcmp(mr->VolStatus, "Full")   == 0 ||
507        strcmp(mr->VolStatus, "Used") == 0) {
508       strcpy(mr->VolStatus, "Purged");
509       if (!db_update_media_record(ua->db, mr)) {
510          if (ua->verbose) {
511             bsendmsg(ua, "%s", db_strerror(ua->db));
512          }
513          return 0;
514       }
515       return 1;
516    }
517    return strcpy(mr->VolStatus, "Purged") == 0;
518 }