3 * Bacula Director -- User Agent Database Purge Command
5 * Purges Files from specific JobIds
7 * Purges Jobs from Volumes
9 * Kern Sibbald, February MMII
15 Copyright (C) 2002 Kern Sibbald and John Walker
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.
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.
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,
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 static int mark_media_purged(UAContext *ua, MEDIA_DBR *mr);
47 #define MAX_DEL_LIST_LEN 1000000
50 static char *select_jobsfiles_from_client =
51 "SELECT JobId FROM Job "
55 static char *select_jobs_from_client =
56 "SELECT JobId, PurgedFiles FROM Job "
60 /* In memory list of JobIds */
61 struct s_file_del_ctx {
63 int num_ids; /* ids stored */
64 int max_ids; /* size of array */
65 int num_del; /* number deleted */
66 int tot_ids; /* total to process */
69 struct s_job_del_ctx {
70 JobId_t *JobId; /* array of JobIds */
71 char *PurgedFiles; /* Array of PurgedFile flags */
72 int num_ids; /* ids stored */
73 int max_ids; /* size of array */
74 int num_del; /* number deleted */
75 int tot_ids; /* total to process */
83 * Called here to count entries to be deleted
85 static int count_handler(void *ctx, int num_fields, char **row)
87 struct s_count_ctx *cnt = (struct s_count_ctx *)ctx;
90 cnt->count = atoi(row[0]);
98 * Called here to count entries to be deleted
100 static int file_count_handler(void *ctx, int num_fields, char **row)
102 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
108 static int job_count_handler(void *ctx, int num_fields, char **row)
110 struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
117 * Called here to make in memory list of JobIds to be
118 * deleted and the associated PurgedFiles flag.
119 * The in memory list will then be transversed
120 * to issue the SQL DELETE commands. Note, the list
121 * is allowed to get to MAX_DEL_LIST_LEN to limit the
122 * maximum malloc'ed memory.
124 static int job_delete_handler(void *ctx, int num_fields, char **row)
126 struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
128 if (del->num_ids == MAX_DEL_LIST_LEN) {
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) * del->max_ids);
134 del->PurgedFiles = (char *)brealloc(del->PurgedFiles, del->max_ids);
136 del->JobId[del->num_ids] = (JobId_t)strtod(row[0], NULL);
137 del->PurgedFiles[del->num_ids++] = (char)atoi(row[0]);
141 static int file_delete_handler(void *ctx, int num_fields, char **row)
143 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
145 if (del->num_ids == MAX_DEL_LIST_LEN) {
148 if (del->num_ids == del->max_ids) {
149 del->max_ids = (del->max_ids * 3) / 2;
150 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) *
153 del->JobId[del->num_ids++] = (JobId_t)strtod(row[0], NULL);
158 * Purge records from database
160 * Purge Files (from) [Job|JobId|Client|Volume]
161 * Purge Jobs (from) [Client|Volume]
163 * N.B. Not all above is implemented yet.
165 int purgecmd(UAContext *ua, char *cmd)
171 static char *keywords[] = {
176 static char *files_keywords[] = {
183 static char *jobs_keywords[] = {
189 "This command is DANGEROUS!!!\n"
190 "It purges (deletes) all Files from a Job,\n"
191 "JobId, Client or Volume; or it purges (deletes)\n"
192 "all Jobs from a Client or Volume. Normally you\n"
193 "should use the PRUNE command instead.\n"));
198 switch (find_arg_keyword(ua, keywords)) {
201 switch(find_arg_keyword(ua, files_keywords)) {
204 if (get_job_dbr(ua, &jr)) {
205 purge_files_from_job(ua, &jr);
209 client = select_client_resource(ua);
210 purge_files_from_client(ua, client);
213 if (select_pool_and_media_dbr(ua, &pr, &mr)) {
214 purge_files_from_volume(ua, &pr, &mr);
220 switch(find_arg_keyword(ua, jobs_keywords)) {
222 client = select_client_resource(ua);
223 purge_jobs_from_client(ua, client);
226 if (select_pool_and_media_dbr(ua, &pr, &mr)) {
227 purge_jobs_from_volume(ua, &pr, &mr);
234 switch (do_keyword_prompt(ua, _("Choose item to purge"), keywords)) {
236 client = select_client_resource(ua);
240 purge_files_from_client(ua, client);
243 client = select_client_resource(ua);
247 purge_jobs_from_client(ua, client);
254 * Prune File records from the database. For any Job which
255 * is older than the retention period, we unconditionally delete
256 * all File records for that Job. This is simple enough that no
257 * temporary tables are needed. We simply make an in memory list of
258 * the JobIds meeting the prune conditions, then delete all File records
259 * pointing to each of those JobIds.
261 int purge_files_from_client(UAContext *ua, CLIENT *client)
263 struct s_file_del_ctx del;
264 char *query = (char *)get_pool_memory(PM_MESSAGE);
268 memset(&cr, 0, sizeof(cr));
269 memset(&del, 0, sizeof(del));
271 strcpy(cr.Name, client->hdr.name);
272 if (!db_create_client_record(ua->db, &cr)) {
276 Mmsg(&query, select_jobsfiles_from_client, cr.ClientId);
278 Dmsg1(050, "select sql=%s\n", query);
280 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
281 bsendmsg(ua, "%s", db_strerror(ua->db));
282 Dmsg0(050, "Count failed\n");
286 if (del.tot_ids == 0) {
287 bsendmsg(ua, _("No Files found for client %s to purge from %s catalog.\n"),
288 client->hdr.name, client->catalog->hdr.name);
292 if (del.tot_ids < MAX_DEL_LIST_LEN) {
293 del.max_ids = del.tot_ids + 1;
295 del.max_ids = MAX_DEL_LIST_LEN;
299 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
301 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
303 for (i=0; i < del.num_ids; i++) {
304 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
305 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
306 db_sql_query(ua->db, query, NULL, (void *)NULL);
308 * Now mark Job as having files purged. This is necessary to
309 * avoid having too many Jobs to process in future prunings. If
310 * we don't do this, the number of JobId's in our in memory list
311 * will grow very large.
313 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", del.JobId[i]);
314 db_sql_query(ua->db, query, NULL, (void *)NULL);
315 Dmsg1(050, "Del sql=%s\n", query);
317 bsendmsg(ua, _("%d Files for client %s purged from %s catalog.\n"), del.num_ids,
318 client->hdr.name, client->catalog->hdr.name);
324 free_pool_memory(query);
331 * Purging Jobs is a bit more complicated than purging Files
332 * because we delete Job records only if there is a more current
333 * backup of the FileSet. Otherwise, we keep the Job record.
334 * In other words, we never delete the only Job record that
335 * contains a current backup of a FileSet. This prevents the
336 * Volume from being recycled and destroying a current backup.
338 int purge_jobs_from_client(UAContext *ua, CLIENT *client)
340 struct s_job_del_ctx del;
341 char *query = (char *)get_pool_memory(PM_MESSAGE);
345 memset(&cr, 0, sizeof(cr));
346 memset(&del, 0, sizeof(del));
348 strcpy(cr.Name, client->hdr.name);
349 if (!db_create_client_record(ua->db, &cr)) {
353 Mmsg(&query, select_jobs_from_client, cr.ClientId);
355 Dmsg1(050, "select sql=%s\n", query);
357 if (!db_sql_query(ua->db, query, job_count_handler, (void *)&del)) {
358 bsendmsg(ua, "%s", db_strerror(ua->db));
359 Dmsg0(050, "Count failed\n");
362 if (del.tot_ids == 0) {
363 bsendmsg(ua, _("No Jobs found for client %s to purge from %s catalog.\n"),
364 client->hdr.name, client->catalog->hdr.name);
368 if (del.tot_ids < MAX_DEL_LIST_LEN) {
369 del.max_ids = del.tot_ids + 1;
371 del.max_ids = MAX_DEL_LIST_LEN;
376 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
377 del.PurgedFiles = (char *)malloc(del.max_ids);
379 db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
382 * OK, now we have the list of JobId's to be purged, first check
383 * if the Files have been purged, if not, purge (delete) them.
384 * Then delete the Job entry, and finally and JobMedia records.
386 for (i=0; i < del.num_ids; i++) {
387 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
388 if (!del.PurgedFiles[i]) {
389 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
390 db_sql_query(ua->db, query, NULL, (void *)NULL);
391 Dmsg1(050, "Del sql=%s\n", query);
394 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
395 db_sql_query(ua->db, query, NULL, (void *)NULL);
396 Dmsg1(050, "Del sql=%s\n", query);
398 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
399 db_sql_query(ua->db, query, NULL, (void *)NULL);
400 Dmsg1(050, "Del sql=%s\n", query);
402 bsendmsg(ua, _("%d Jobs for client %s purged from %s catalog.\n"), del.num_ids,
403 client->hdr.name, client->catalog->hdr.name);
409 if (del.PurgedFiles) {
410 free(del.PurgedFiles);
412 free_pool_memory(query);
416 void purge_files_from_job(UAContext *ua, JOB_DBR *jr)
418 char *query = (char *)get_pool_memory(PM_MESSAGE);
420 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", jr->JobId);
421 db_sql_query(ua->db, query, NULL, (void *)NULL);
423 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", jr->JobId);
424 db_sql_query(ua->db, query, NULL, (void *)NULL);
426 free_pool_memory(query);
429 void purge_files_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr )
430 {} /* ***FIXME*** implement */
432 void purge_jobs_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
434 char *query = (char *)get_pool_memory(PM_MESSAGE);
435 struct s_count_ctx cnt;
436 struct s_file_del_ctx del;
440 memset(&jr, 0, sizeof(jr));
441 memset(&del, 0, sizeof(del));
443 Mmsg(&query, "SELECT count(*) FROM JobMedia WHERE MediaId=%d", mr->MediaId);
444 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
445 bsendmsg(ua, "%s", db_strerror(ua->db));
446 Dmsg0(050, "Count failed\n");
450 if (cnt.count == 0) {
451 bsendmsg(ua, "There are no Jobs associated with Volume %s. It is purged.\n",
453 if (!mark_media_purged(ua, mr)) {
459 if (cnt.count < MAX_DEL_LIST_LEN) {
460 del.max_ids = cnt.count + 1;
462 del.max_ids = MAX_DEL_LIST_LEN;
465 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
467 Mmsg(&query, "SELECT JobId FROM JobMedia WHERE MediaId=%d", mr->MediaId);
468 if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
469 bsendmsg(ua, "%s", db_strerror(ua->db));
470 Dmsg0(050, "Count failed\n");
474 for (i=0; i < del.num_ids; i++) {
475 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
476 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
477 db_sql_query(ua->db, query, NULL, (void *)NULL);
478 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
479 db_sql_query(ua->db, query, NULL, (void *)NULL);
480 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
481 db_sql_query(ua->db, query, NULL, (void *)NULL);
482 Dmsg1(050, "Del sql=%s\n", query);
488 bsendmsg(ua, _("%d Files for Volume %s purged from catalog.\n"), del.num_del,
491 /* If purged, mark it so */
492 if (del.num_ids == del.num_del) {
493 mark_media_purged(ua, mr);
497 free_pool_memory(query);
500 static int mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
502 if (strcmp(mr->VolStatus, "Append") == 0 ||
503 strcmp(mr->VolStatus, "Full") == 0) {
504 strcpy(mr->VolStatus, "Purged");
505 if (!db_update_media_record(ua->db, mr)) {
506 bsendmsg(ua, "%s", db_strerror(ua->db));