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,
37 /* Forward referenced functions */
38 int purge_files_from_client(UAContext *ua, CLIENT *client);
39 int purge_jobs_from_client(UAContext *ua, CLIENT *client);
40 void purge_files_from_volume(UAContext *ua, MEDIA_DBR *mr );
41 int purge_jobs_from_volume(UAContext *ua, MEDIA_DBR *mr);
42 void purge_files_from_job(UAContext *ua, JOB_DBR *jr);
43 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr);
45 #define MAX_DEL_LIST_LEN 1000000
48 static char *select_jobsfiles_from_client =
49 "SELECT JobId FROM Job "
53 static char *select_jobs_from_client =
54 "SELECT JobId, PurgedFiles FROM Job "
58 /* In memory list of JobIds */
59 struct s_file_del_ctx {
61 int num_ids; /* ids stored */
62 int max_ids; /* size of array */
63 int num_del; /* number deleted */
64 int tot_ids; /* total to process */
67 struct s_job_del_ctx {
68 JobId_t *JobId; /* array of JobIds */
69 char *PurgedFiles; /* Array of PurgedFile flags */
70 int num_ids; /* ids stored */
71 int max_ids; /* size of array */
72 int num_del; /* number deleted */
73 int tot_ids; /* total to process */
81 * Called here to count entries to be deleted
83 static int count_handler(void *ctx, int num_fields, char **row)
85 struct s_count_ctx *cnt = (struct s_count_ctx *)ctx;
88 cnt->count = atoi(row[0]);
96 * Called here to count entries to be deleted
98 static int file_count_handler(void *ctx, int num_fields, char **row)
100 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
106 static int job_count_handler(void *ctx, int num_fields, char **row)
108 struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
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.
122 static int job_delete_handler(void *ctx, int num_fields, char **row)
124 struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
126 if (del->num_ids == MAX_DEL_LIST_LEN) {
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);
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]);
139 static int file_delete_handler(void *ctx, int num_fields, char **row)
141 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
143 if (del->num_ids == MAX_DEL_LIST_LEN) {
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) *
151 del->JobId[del->num_ids++] = (JobId_t)str_to_int64(row[0]);
156 * Purge records from database
158 * Purge Files (from) [Job|JobId|Client|Volume]
159 * Purge Jobs (from) [Client|Volume]
161 * N.B. Not all above is implemented yet.
163 int purgecmd(UAContext *ua, char *cmd)
168 static char *keywords[] = {
174 static char *files_keywords[] = {
181 static char *jobs_keywords[] = {
187 "\nThis command is can be DANGEROUS!!!\n\n"
188 "It purges (deletes) all Files from a Job,\n"
189 "JobId, Client or Volume; or it purges (deletes)\n"
190 "all Jobs from a Client or Volume without regard\n"
191 "for retention periods. Normally you should use the\n"
192 "PRUNE command, which respects retention periods.\n"));
197 switch (find_arg_keyword(ua, keywords)) {
200 switch(find_arg_keyword(ua, files_keywords)) {
203 if (get_job_dbr(ua, &jr)) {
204 purge_files_from_job(ua, &jr);
208 client = get_client_resource(ua);
210 purge_files_from_client(ua, client);
214 if (select_media_dbr(ua, &mr)) {
215 purge_files_from_volume(ua, &mr);
221 switch(find_arg_keyword(ua, jobs_keywords)) {
223 client = get_client_resource(ua);
225 purge_jobs_from_client(ua, client);
229 if (select_media_dbr(ua, &mr)) {
230 purge_jobs_from_volume(ua, &mr);
236 if (select_media_dbr(ua, &mr)) {
237 purge_jobs_from_volume(ua, &mr);
243 switch (do_keyword_prompt(ua, _("Choose item to purge"), keywords)) {
245 client = get_client_resource(ua);
247 purge_files_from_client(ua, client);
251 client = get_client_resource(ua);
253 purge_jobs_from_client(ua, client);
257 if (select_media_dbr(ua, &mr)) {
258 purge_jobs_from_volume(ua, &mr);
266 * Purge File records from the database. For any Job which
267 * is older than the retention period, we unconditionally delete
268 * all File records for that Job. This is simple enough that no
269 * temporary tables are needed. We simply make an in memory list of
270 * the JobIds meeting the prune conditions, then delete all File records
271 * pointing to each of those JobIds.
273 int purge_files_from_client(UAContext *ua, CLIENT *client)
275 struct s_file_del_ctx del;
276 char *query = (char *)get_pool_memory(PM_MESSAGE);
280 memset(&cr, 0, sizeof(cr));
281 memset(&del, 0, sizeof(del));
283 strcpy(cr.Name, client->hdr.name);
284 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
288 Mmsg(&query, select_jobsfiles_from_client, cr.ClientId);
290 Dmsg1(050, "select sql=%s\n", query);
292 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
293 bsendmsg(ua, "%s", db_strerror(ua->db));
294 Dmsg0(050, "Count failed\n");
298 if (del.tot_ids == 0) {
299 bsendmsg(ua, _("No Files found for client %s to purge from %s catalog.\n"),
300 client->hdr.name, client->catalog->hdr.name);
304 if (del.tot_ids < MAX_DEL_LIST_LEN) {
305 del.max_ids = del.tot_ids + 1;
307 del.max_ids = MAX_DEL_LIST_LEN;
311 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
313 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
315 for (i=0; i < del.num_ids; i++) {
316 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
317 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
318 db_sql_query(ua->db, query, NULL, (void *)NULL);
320 * Now mark Job as having files purged. This is necessary to
321 * avoid having too many Jobs to process in future prunings. If
322 * we don't do this, the number of JobId's in our in memory list
323 * will grow very large.
325 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", del.JobId[i]);
326 db_sql_query(ua->db, query, NULL, (void *)NULL);
327 Dmsg1(050, "Del sql=%s\n", query);
329 bsendmsg(ua, _("%d Files for client %s purged from %s catalog.\n"), del.num_ids,
330 client->hdr.name, client->catalog->hdr.name);
336 free_pool_memory(query);
343 * Purge Job records from the database. For any Job which
344 * is older than the retention period, we unconditionally delete
345 * it and all File records for that Job. This is simple enough that no
346 * temporary tables are needed. We simply make an in memory list of
347 * the JobIds meeting the prune conditions, then delete the Job,
348 * Files, and JobMedia records in that list.
350 int purge_jobs_from_client(UAContext *ua, CLIENT *client)
352 struct s_job_del_ctx del;
353 char *query = (char *)get_pool_memory(PM_MESSAGE);
357 memset(&cr, 0, sizeof(cr));
358 memset(&del, 0, sizeof(del));
360 bstrncpy(cr.Name, client->hdr.name, sizeof(cr.Name));
361 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
365 Mmsg(&query, select_jobs_from_client, cr.ClientId);
367 Dmsg1(050, "select sql=%s\n", query);
369 if (!db_sql_query(ua->db, query, job_count_handler, (void *)&del)) {
370 bsendmsg(ua, "%s", db_strerror(ua->db));
371 Dmsg0(050, "Count failed\n");
374 if (del.tot_ids == 0) {
375 bsendmsg(ua, _("No Jobs found for client %s to purge from %s catalog.\n"),
376 client->hdr.name, client->catalog->hdr.name);
380 if (del.tot_ids < MAX_DEL_LIST_LEN) {
381 del.max_ids = del.tot_ids + 1;
383 del.max_ids = MAX_DEL_LIST_LEN;
388 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
389 del.PurgedFiles = (char *)malloc(del.max_ids);
391 db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
394 * OK, now we have the list of JobId's to be purged, first check
395 * if the Files have been purged, if not, purge (delete) them.
396 * Then delete the Job entry, and finally and JobMedia records.
398 for (i=0; i < del.num_ids; i++) {
399 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
400 if (!del.PurgedFiles[i]) {
401 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
402 db_sql_query(ua->db, query, NULL, (void *)NULL);
403 Dmsg1(050, "Del sql=%s\n", query);
406 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
407 db_sql_query(ua->db, query, NULL, (void *)NULL);
408 Dmsg1(050, "Del sql=%s\n", query);
410 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
411 db_sql_query(ua->db, query, NULL, (void *)NULL);
412 Dmsg1(050, "Del sql=%s\n", query);
414 bsendmsg(ua, _("%d Jobs for client %s purged from %s catalog.\n"), del.num_ids,
415 client->hdr.name, client->catalog->hdr.name);
421 if (del.PurgedFiles) {
422 free(del.PurgedFiles);
424 free_pool_memory(query);
428 void purge_files_from_job(UAContext *ua, JOB_DBR *jr)
430 char *query = (char *)get_pool_memory(PM_MESSAGE);
432 Mmsg(&query, "DELETE FROM File WHERE JobId=%u", jr->JobId);
433 db_sql_query(ua->db, query, NULL, (void *)NULL);
435 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%u", jr->JobId);
436 db_sql_query(ua->db, query, NULL, (void *)NULL);
438 free_pool_memory(query);
441 void purge_files_from_volume(UAContext *ua, MEDIA_DBR *mr )
442 {} /* ***FIXME*** implement */
445 * Returns: 1 if Volume purged
446 * 0 if Volume not purged
448 int purge_jobs_from_volume(UAContext *ua, MEDIA_DBR *mr)
450 char *query = (char *)get_pool_memory(PM_MESSAGE);
451 struct s_count_ctx cnt;
452 struct s_file_del_ctx del;
456 stat = strcmp(mr->VolStatus, "Append") == 0 ||
457 strcmp(mr->VolStatus, "Full") == 0 ||
458 strcmp(mr->VolStatus, "Used") == 0 ||
459 strcmp(mr->VolStatus, "Error") == 0;
462 bsendmsg(ua, _("Volume \"%s\" has VolStatus \"%s\" and cannot be purged.\n"
463 "The VolStatus must be: Append, Full, Used, or Error to be purged.\n"),
464 mr->VolumeName, mr->VolStatus);
468 memset(&jr, 0, sizeof(jr));
469 memset(&del, 0, sizeof(del));
471 Mmsg(&query, "SELECT count(*) FROM JobMedia WHERE MediaId=%d", mr->MediaId);
472 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
473 bsendmsg(ua, "%s", db_strerror(ua->db));
474 Dmsg0(050, "Count failed\n");
478 if (cnt.count == 0) {
479 bsendmsg(ua, "There are no Jobs associated with Volume %s. Marking it purged.\n",
481 if (!mark_media_purged(ua, mr)) {
482 bsendmsg(ua, "%s", db_strerror(ua->db));
488 if (cnt.count < MAX_DEL_LIST_LEN) {
489 del.max_ids = cnt.count + 1;
491 del.max_ids = MAX_DEL_LIST_LEN;
494 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
496 Mmsg(&query, "SELECT JobId FROM JobMedia WHERE MediaId=%d", mr->MediaId);
497 if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
498 bsendmsg(ua, "%s", db_strerror(ua->db));
499 Dmsg0(050, "Count failed\n");
503 for (i=0; i < del.num_ids; i++) {
504 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
505 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
506 db_sql_query(ua->db, query, NULL, (void *)NULL);
507 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
508 db_sql_query(ua->db, query, NULL, (void *)NULL);
509 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
510 db_sql_query(ua->db, query, NULL, (void *)NULL);
511 Dmsg1(050, "Del sql=%s\n", query);
517 bsendmsg(ua, _("%d File%s on Volume \"%s\" purged from catalog.\n"), del.num_del,
518 del.num_del==1?"":"s", mr->VolumeName);
520 /* If purged, mark it so */
521 if (del.num_ids == del.num_del) {
522 if (!(stat = mark_media_purged(ua, mr))) {
523 bsendmsg(ua, "%s", db_strerror(ua->db));
528 free_pool_memory(query);
533 * IF volume status is Append, Full, Used, or Error, mark it Purged
534 * Purged volumes can then be recycled (if enabled).
536 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
538 if (strcmp(mr->VolStatus, "Append") == 0 ||
539 strcmp(mr->VolStatus, "Full") == 0 ||
540 strcmp(mr->VolStatus, "Used") == 0 ||
541 strcmp(mr->VolStatus, "Error") == 0) {
542 strcpy(mr->VolStatus, "Purged");
543 if (!db_update_media_record(ua->jcr, ua->db, mr)) {
548 bsendmsg(ua, _("Cannot purge Volume with VolStatus=%s\n"), mr->VolStatus);
550 return strcpy(mr->VolStatus, "Purged") == 0;