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)
169 static char *keywords[] = {
175 static char *files_keywords[] = {
182 static char *jobs_keywords[] = {
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"));
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 = select_client_resource(ua);
209 purge_files_from_client(ua, client);
212 if (select_pool_and_media_dbr(ua, &pr, &mr)) {
213 purge_files_from_volume(ua, &mr);
219 switch(find_arg_keyword(ua, jobs_keywords)) {
221 client = select_client_resource(ua);
222 purge_jobs_from_client(ua, client);
225 if (select_pool_and_media_dbr(ua, &pr, &mr)) {
226 purge_jobs_from_volume(ua, &mr);
232 if (select_pool_and_media_dbr(ua, &pr, &mr)) {
233 purge_jobs_from_volume(ua, &mr);
239 switch (do_keyword_prompt(ua, _("Choose item to purge"), keywords)) {
241 client = select_client_resource(ua);
245 purge_files_from_client(ua, client);
248 client = select_client_resource(ua);
252 purge_jobs_from_client(ua, client);
255 if (select_pool_and_media_dbr(ua, &pr, &mr)) {
256 purge_jobs_from_volume(ua, &mr);
264 * Prune File records from the database. For any Job which
265 * is older than the retention period, we unconditionally delete
266 * all File records for that Job. This is simple enough that no
267 * temporary tables are needed. We simply make an in memory list of
268 * the JobIds meeting the prune conditions, then delete all File records
269 * pointing to each of those JobIds.
271 int purge_files_from_client(UAContext *ua, CLIENT *client)
273 struct s_file_del_ctx del;
274 char *query = (char *)get_pool_memory(PM_MESSAGE);
278 memset(&cr, 0, sizeof(cr));
279 memset(&del, 0, sizeof(del));
281 strcpy(cr.Name, client->hdr.name);
282 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
286 Mmsg(&query, select_jobsfiles_from_client, cr.ClientId);
288 Dmsg1(050, "select sql=%s\n", query);
290 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
291 bsendmsg(ua, "%s", db_strerror(ua->db));
292 Dmsg0(050, "Count failed\n");
296 if (del.tot_ids == 0) {
297 bsendmsg(ua, _("No Files found for client %s to purge from %s catalog.\n"),
298 client->hdr.name, client->catalog->hdr.name);
302 if (del.tot_ids < MAX_DEL_LIST_LEN) {
303 del.max_ids = del.tot_ids + 1;
305 del.max_ids = MAX_DEL_LIST_LEN;
309 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
311 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
313 for (i=0; i < del.num_ids; i++) {
314 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
315 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
316 db_sql_query(ua->db, query, NULL, (void *)NULL);
318 * Now mark Job as having files purged. This is necessary to
319 * avoid having too many Jobs to process in future prunings. If
320 * we don't do this, the number of JobId's in our in memory list
321 * will grow very large.
323 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", del.JobId[i]);
324 db_sql_query(ua->db, query, NULL, (void *)NULL);
325 Dmsg1(050, "Del sql=%s\n", query);
327 bsendmsg(ua, _("%d Files for client %s purged from %s catalog.\n"), del.num_ids,
328 client->hdr.name, client->catalog->hdr.name);
334 free_pool_memory(query);
341 * Purging Jobs is a bit more complicated than purging Files
342 * because we delete Job records only if there is a more current
343 * backup of the FileSet. Otherwise, we keep the Job record.
344 * In other words, we never delete the only Job record that
345 * contains a current backup of a FileSet. This prevents the
346 * Volume from being recycled and destroying a current backup.
348 int purge_jobs_from_client(UAContext *ua, CLIENT *client)
350 struct s_job_del_ctx del;
351 char *query = (char *)get_pool_memory(PM_MESSAGE);
355 memset(&cr, 0, sizeof(cr));
356 memset(&del, 0, sizeof(del));
358 strcpy(cr.Name, client->hdr.name);
359 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
363 Mmsg(&query, select_jobs_from_client, cr.ClientId);
365 Dmsg1(050, "select sql=%s\n", query);
367 if (!db_sql_query(ua->db, query, job_count_handler, (void *)&del)) {
368 bsendmsg(ua, "%s", db_strerror(ua->db));
369 Dmsg0(050, "Count failed\n");
372 if (del.tot_ids == 0) {
373 bsendmsg(ua, _("No Jobs found for client %s to purge from %s catalog.\n"),
374 client->hdr.name, client->catalog->hdr.name);
378 if (del.tot_ids < MAX_DEL_LIST_LEN) {
379 del.max_ids = del.tot_ids + 1;
381 del.max_ids = MAX_DEL_LIST_LEN;
386 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
387 del.PurgedFiles = (char *)malloc(del.max_ids);
389 db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
392 * OK, now we have the list of JobId's to be purged, first check
393 * if the Files have been purged, if not, purge (delete) them.
394 * Then delete the Job entry, and finally and JobMedia records.
396 for (i=0; i < del.num_ids; i++) {
397 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
398 if (!del.PurgedFiles[i]) {
399 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
400 db_sql_query(ua->db, query, NULL, (void *)NULL);
401 Dmsg1(050, "Del sql=%s\n", query);
404 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
405 db_sql_query(ua->db, query, NULL, (void *)NULL);
406 Dmsg1(050, "Del sql=%s\n", query);
408 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
409 db_sql_query(ua->db, query, NULL, (void *)NULL);
410 Dmsg1(050, "Del sql=%s\n", query);
412 bsendmsg(ua, _("%d Jobs for client %s purged from %s catalog.\n"), del.num_ids,
413 client->hdr.name, client->catalog->hdr.name);
419 if (del.PurgedFiles) {
420 free(del.PurgedFiles);
422 free_pool_memory(query);
426 void purge_files_from_job(UAContext *ua, JOB_DBR *jr)
428 char *query = (char *)get_pool_memory(PM_MESSAGE);
430 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", jr->JobId);
431 db_sql_query(ua->db, query, NULL, (void *)NULL);
433 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", jr->JobId);
434 db_sql_query(ua->db, query, NULL, (void *)NULL);
436 free_pool_memory(query);
439 void purge_files_from_volume(UAContext *ua, MEDIA_DBR *mr )
440 {} /* ***FIXME*** implement */
443 * Returns: 1 if Volume purged
444 * 0 if Volume not purged
446 int purge_jobs_from_volume(UAContext *ua, MEDIA_DBR *mr)
448 char *query = (char *)get_pool_memory(PM_MESSAGE);
449 struct s_count_ctx cnt;
450 struct s_file_del_ctx del;
454 memset(&jr, 0, sizeof(jr));
455 memset(&del, 0, sizeof(del));
457 Mmsg(&query, "SELECT count(*) FROM JobMedia WHERE MediaId=%d", mr->MediaId);
458 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
459 bsendmsg(ua, "%s", db_strerror(ua->db));
460 Dmsg0(050, "Count failed\n");
464 if (cnt.count == 0) {
465 bsendmsg(ua, "There are no Jobs associated with Volume %s. Marking it purged.\n",
467 if (!mark_media_purged(ua, mr)) {
468 bsendmsg(ua, "%s", db_strerror(ua->db));
474 if (cnt.count < MAX_DEL_LIST_LEN) {
475 del.max_ids = cnt.count + 1;
477 del.max_ids = MAX_DEL_LIST_LEN;
480 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
482 Mmsg(&query, "SELECT JobId FROM JobMedia WHERE MediaId=%d", mr->MediaId);
483 if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
484 bsendmsg(ua, "%s", db_strerror(ua->db));
485 Dmsg0(050, "Count failed\n");
489 for (i=0; i < del.num_ids; i++) {
490 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
491 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
492 db_sql_query(ua->db, query, NULL, (void *)NULL);
493 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
494 db_sql_query(ua->db, query, NULL, (void *)NULL);
495 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
496 db_sql_query(ua->db, query, NULL, (void *)NULL);
497 Dmsg1(050, "Del sql=%s\n", query);
503 bsendmsg(ua, _("%d File%s on Volume \"%s\" purged from catalog.\n"), del.num_del,
504 del.num_del==1?"":"s", mr->VolumeName);
506 /* If purged, mark it so */
507 if (del.num_ids == del.num_del) {
508 if (!(stat = mark_media_purged(ua, mr))) {
509 bsendmsg(ua, "%s", db_strerror(ua->db));
514 free_pool_memory(query);
519 * IF volume status is Append, Full, Used, or Error, mark it Purged
520 * Purged volumes can then be recycled (if enabled).
522 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
524 if (strcmp(mr->VolStatus, "Append") == 0 ||
525 strcmp(mr->VolStatus, "Full") == 0 ||
526 strcmp(mr->VolStatus, "Used") == 0 ||
527 strcmp(mr->VolStatus, "Error") == 0) {
528 strcpy(mr->VolStatus, "Purged");
529 if (!db_update_media_record(ua->jcr, ua->db, mr)) {
534 bsendmsg(ua, _("Cannot purge Volume with VolStatus=%s\n"), mr->VolStatus);
536 return strcpy(mr->VolStatus, "Purged") == 0;