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-2005 Kern Sibbald
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 static int purge_files_from_client(UAContext *ua, CLIENT *client);
39 static int purge_jobs_from_client(UAContext *ua, CLIENT *client);
41 void purge_files_from_volume(UAContext *ua, MEDIA_DBR *mr );
42 int purge_jobs_from_volume(UAContext *ua, MEDIA_DBR *mr);
43 void purge_files_from_job(UAContext *ua, JOB_DBR *jr);
44 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr);
46 #define MAX_DEL_LIST_LEN 1000000
49 static const char *select_jobsfiles_from_client =
50 "SELECT JobId FROM Job "
54 static const char *select_jobs_from_client =
55 "SELECT JobId, PurgedFiles FROM Job "
59 /* In memory list of JobIds */
60 struct s_file_del_ctx {
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 */
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 */
82 * Called here to count entries to be deleted
84 static int count_handler(void *ctx, int num_fields, char **row)
86 struct s_count_ctx *cnt = (struct s_count_ctx *)ctx;
89 cnt->count = str_to_int64(row[0]);
97 * Called here to count entries to be deleted
99 static int file_count_handler(void *ctx, int num_fields, char **row)
101 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
107 static int job_count_handler(void *ctx, int num_fields, char **row)
109 struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
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.
123 static int job_delete_handler(void *ctx, int num_fields, char **row)
125 struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
127 if (del->num_ids == MAX_DEL_LIST_LEN) {
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);
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]);
140 static int file_delete_handler(void *ctx, int num_fields, char **row)
142 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
144 if (del->num_ids == MAX_DEL_LIST_LEN) {
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) *
152 del->JobId[del->num_ids++] = (JobId_t)str_to_int64(row[0]);
157 * Purge records from database
159 * Purge Files (from) [Job|JobId|Client|Volume]
160 * Purge Jobs (from) [Client|Volume]
162 * N.B. Not all above is implemented yet.
164 int purgecmd(UAContext *ua, const char *cmd)
170 static const char *keywords[] = {
176 static const char *files_keywords[] = {
183 static const char *jobs_keywords[] = {
189 "\nThis command is can be DANGEROUS!!!\n\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 without regard\n"
193 "for retention periods. Normally you should use the\n"
194 "PRUNE command, which respects retention periods.\n"));
199 switch (find_arg_keyword(ua, keywords)) {
202 switch(find_arg_keyword(ua, files_keywords)) {
205 if (get_job_dbr(ua, &jr)) {
206 purge_files_from_job(ua, &jr);
210 client = get_client_resource(ua);
212 purge_files_from_client(ua, client);
216 if (select_media_dbr(ua, &mr)) {
217 purge_files_from_volume(ua, &mr);
223 switch(find_arg_keyword(ua, jobs_keywords)) {
225 client = get_client_resource(ua);
227 purge_jobs_from_client(ua, client);
231 if (select_media_dbr(ua, &mr)) {
232 purge_jobs_from_volume(ua, &mr);
238 while ((i=find_arg(ua, _("volume"))) >= 0) {
239 if (select_media_dbr(ua, &mr)) {
240 purge_jobs_from_volume(ua, &mr);
242 *ua->argk[i] = 0; /* zap keyword already seen */
249 switch (do_keyword_prompt(ua, _("Choose item to purge"), keywords)) {
251 client = get_client_resource(ua);
253 purge_files_from_client(ua, client);
257 client = get_client_resource(ua);
259 purge_jobs_from_client(ua, client);
263 if (select_media_dbr(ua, &mr)) {
264 purge_jobs_from_volume(ua, &mr);
272 * Purge File records from the database. For any Job which
273 * is older than the retention period, we unconditionally delete
274 * all File records for that Job. This is simple enough that no
275 * temporary tables are needed. We simply make an in memory list of
276 * the JobIds meeting the prune conditions, then delete all File records
277 * pointing to each of those JobIds.
279 static int purge_files_from_client(UAContext *ua, CLIENT *client)
281 struct s_file_del_ctx del;
282 char *query = (char *)get_pool_memory(PM_MESSAGE);
286 memset(&cr, 0, sizeof(cr));
287 memset(&del, 0, sizeof(del));
289 bstrncpy(cr.Name, client->hdr.name, sizeof(cr.Name));
290 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
293 bsendmsg(ua, _("Begin purging files for Client \"%s\"\n"), cr.Name);
294 Mmsg(query, select_jobsfiles_from_client, cr.ClientId);
296 Dmsg1(050, "select sql=%s\n", query);
298 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
299 bsendmsg(ua, "%s", db_strerror(ua->db));
300 Dmsg0(050, "Count failed\n");
304 if (del.tot_ids == 0) {
305 bsendmsg(ua, _("No Files found for client %s to purge from %s catalog.\n"),
306 client->hdr.name, client->catalog->hdr.name);
310 if (del.tot_ids < MAX_DEL_LIST_LEN) {
311 del.max_ids = del.tot_ids + 1;
313 del.max_ids = MAX_DEL_LIST_LEN;
317 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
319 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
321 for (i=0; i < del.num_ids; i++) {
322 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
323 Mmsg(query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
324 db_sql_query(ua->db, query, NULL, (void *)NULL);
326 * Now mark Job as having files purged. This is necessary to
327 * avoid having too many Jobs to process in future prunings. If
328 * we don't do this, the number of JobId's in our in memory list
329 * will grow very large.
331 Mmsg(query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", del.JobId[i]);
332 db_sql_query(ua->db, query, NULL, (void *)NULL);
333 Dmsg1(050, "Del sql=%s\n", query);
335 bsendmsg(ua, _("%d Files for client \"%s\" purged from %s catalog.\n"), del.num_ids,
336 client->hdr.name, client->catalog->hdr.name);
342 free_pool_memory(query);
349 * Purge Job records from the database. For any Job which
350 * is older than the retention period, we unconditionally delete
351 * it and all File records for that Job. This is simple enough that no
352 * temporary tables are needed. We simply make an in memory list of
353 * the JobIds meeting the prune conditions, then delete the Job,
354 * Files, and JobMedia records in that list.
356 static int purge_jobs_from_client(UAContext *ua, CLIENT *client)
358 struct s_job_del_ctx del;
359 char *query = (char *)get_pool_memory(PM_MESSAGE);
363 memset(&cr, 0, sizeof(cr));
364 memset(&del, 0, sizeof(del));
366 bstrncpy(cr.Name, client->hdr.name, sizeof(cr.Name));
367 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
371 bsendmsg(ua, _("Begin purging jobs from Client \"%s\"\n"), cr.Name);
372 Mmsg(query, select_jobs_from_client, cr.ClientId);
374 Dmsg1(050, "select sql=%s\n", query);
376 if (!db_sql_query(ua->db, query, job_count_handler, (void *)&del)) {
377 bsendmsg(ua, "%s", db_strerror(ua->db));
378 Dmsg0(050, "Count failed\n");
381 if (del.tot_ids == 0) {
382 bsendmsg(ua, _("No Jobs found for client %s to purge from %s catalog.\n"),
383 client->hdr.name, client->catalog->hdr.name);
387 if (del.tot_ids < MAX_DEL_LIST_LEN) {
388 del.max_ids = del.tot_ids + 1;
390 del.max_ids = MAX_DEL_LIST_LEN;
395 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
396 del.PurgedFiles = (char *)malloc(del.max_ids);
398 db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
401 * OK, now we have the list of JobId's to be purged, first check
402 * if the Files have been purged, if not, purge (delete) them.
403 * Then delete the Job entry, and finally and JobMedia records.
405 for (i=0; i < del.num_ids; i++) {
406 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
407 if (!del.PurgedFiles[i]) {
408 Mmsg(query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
409 db_sql_query(ua->db, query, NULL, (void *)NULL);
410 Dmsg1(050, "Del sql=%s\n", query);
413 Mmsg(query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
414 db_sql_query(ua->db, query, NULL, (void *)NULL);
415 Dmsg1(050, "Del sql=%s\n", query);
417 Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
418 db_sql_query(ua->db, query, NULL, (void *)NULL);
419 Dmsg1(050, "Del sql=%s\n", query);
421 bsendmsg(ua, _("%d Jobs for client %s purged from %s catalog.\n"), del.num_ids,
422 client->hdr.name, client->catalog->hdr.name);
428 if (del.PurgedFiles) {
429 free(del.PurgedFiles);
431 free_pool_memory(query);
435 void purge_files_from_job(UAContext *ua, JOB_DBR *jr)
437 char *query = (char *)get_pool_memory(PM_MESSAGE);
440 Mmsg(query, "DELETE FROM File WHERE JobId=%s", edit_int64(jr->JobId,ed1));
441 db_sql_query(ua->db, query, NULL, (void *)NULL);
443 Mmsg(query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%s", edit_int64(jr->JobId,ed1));
444 db_sql_query(ua->db, query, NULL, (void *)NULL);
446 free_pool_memory(query);
449 void purge_files_from_volume(UAContext *ua, MEDIA_DBR *mr )
450 {} /* ***FIXME*** implement */
453 * Returns: 1 if Volume purged
454 * 0 if Volume not purged
456 int purge_jobs_from_volume(UAContext *ua, MEDIA_DBR *mr)
458 char *query = (char *)get_pool_memory(PM_MESSAGE);
459 struct s_count_ctx cnt;
460 struct s_file_del_ctx del;
464 stat = strcmp(mr->VolStatus, "Append") == 0 ||
465 strcmp(mr->VolStatus, "Full") == 0 ||
466 strcmp(mr->VolStatus, "Used") == 0 ||
467 strcmp(mr->VolStatus, "Error") == 0;
470 bsendmsg(ua, _("Volume \"%s\" has VolStatus \"%s\" and cannot be purged.\n"
471 "The VolStatus must be: Append, Full, Used, or Error to be purged.\n"),
472 mr->VolumeName, mr->VolStatus);
476 memset(&jr, 0, sizeof(jr));
477 memset(&del, 0, sizeof(del));
479 Mmsg(query, "SELECT count(*) FROM JobMedia WHERE MediaId=%d", mr->MediaId);
480 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
481 bsendmsg(ua, "%s", db_strerror(ua->db));
482 Dmsg0(050, "Count failed\n");
486 if (cnt.count == 0) {
487 bsendmsg(ua, "There are no Jobs associated with Volume \"%s\". Marking it purged.\n",
489 if (!mark_media_purged(ua, mr)) {
490 bsendmsg(ua, "%s", db_strerror(ua->db));
496 if (cnt.count < MAX_DEL_LIST_LEN) {
497 del.max_ids = cnt.count + 1;
499 del.max_ids = MAX_DEL_LIST_LEN;
503 * Check if he wants to purge a single jobid
505 i = find_arg_with_value(ua, "jobid");
507 del.JobId = (JobId_t *)malloc(sizeof(JobId_t));
509 del.JobId[0] = str_to_int64(ua->argv[i]);
514 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
516 Mmsg(query, "SELECT JobId FROM JobMedia WHERE MediaId=%d", mr->MediaId);
517 if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
518 bsendmsg(ua, "%s", db_strerror(ua->db));
519 Dmsg0(050, "Count failed\n");
524 for (i=0; i < del.num_ids; i++) {
525 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
526 Mmsg(query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
527 db_sql_query(ua->db, query, NULL, (void *)NULL);
528 Mmsg(query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
529 db_sql_query(ua->db, query, NULL, (void *)NULL);
530 Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
531 db_sql_query(ua->db, query, NULL, (void *)NULL);
532 Dmsg1(050, "Del sql=%s\n", query);
538 bsendmsg(ua, _("%d File%s on Volume \"%s\" purged from catalog.\n"), del.num_del,
539 del.num_del==1?"":"s", mr->VolumeName);
541 /* If purged, mark it so */
543 Mmsg(query, "SELECT count(*) FROM JobMedia WHERE MediaId=%d", mr->MediaId);
544 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
545 bsendmsg(ua, "%s", db_strerror(ua->db));
546 Dmsg0(050, "Count failed\n");
550 if (cnt.count == 0) {
551 bsendmsg(ua, "There are no more Jobs associated with Volume \"%s\". Marking it purged.\n",
553 if (!(stat = mark_media_purged(ua, mr))) {
554 bsendmsg(ua, "%s", db_strerror(ua->db));
560 free_pool_memory(query);
565 * IF volume status is Append, Full, Used, or Error, mark it Purged
566 * Purged volumes can then be recycled (if enabled).
568 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
570 if (strcmp(mr->VolStatus, "Append") == 0 ||
571 strcmp(mr->VolStatus, "Full") == 0 ||
572 strcmp(mr->VolStatus, "Used") == 0 ||
573 strcmp(mr->VolStatus, "Error") == 0) {
574 bstrncpy(mr->VolStatus, "Purged", sizeof(mr->VolStatus));
575 if (!db_update_media_record(ua->jcr, ua->db, mr)) {
580 bsendmsg(ua, _("Cannot purge Volume with VolStatus=%s\n"), mr->VolStatus);
582 return strcpy(mr->VolStatus, "Purged") == 0;