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