3 * Bacula Director -- User Agent Database Purge Command
5 * Purges Files from specific JobIds
7 * Purges Jobs from Volumes
9 * Kern Sibbald, February MMII
13 Copyright (C) 2002 Kern Sibbald and John Walker
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.
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.
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,
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 static 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)strtod(row[0], NULL);
135 del->PurgedFiles[del->num_ids++] = (char)atoi(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)strtod(row[0], NULL);
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[] = {
174 static char *files_keywords[] = {
181 static char *jobs_keywords[] = {
187 "This command is DANGEROUS!!!\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. Normally you\n"
191 "should use the PRUNE command instead.\n"));
196 switch (find_arg_keyword(ua, keywords)) {
199 switch(find_arg_keyword(ua, files_keywords)) {
202 if (get_job_dbr(ua, &jr)) {
203 purge_files_from_job(ua, &jr);
207 client = select_client_resource(ua);
208 purge_files_from_client(ua, client);
211 if (select_pool_and_media_dbr(ua, &pr, &mr)) {
212 purge_files_from_volume(ua, &pr, &mr);
218 switch(find_arg_keyword(ua, jobs_keywords)) {
220 client = select_client_resource(ua);
221 purge_jobs_from_client(ua, client);
224 if (select_pool_and_media_dbr(ua, &pr, &mr)) {
225 purge_jobs_from_volume(ua, &pr, &mr);
232 switch (do_keyword_prompt(ua, _("Choose item to purge"), keywords)) {
234 client = select_client_resource(ua);
238 purge_files_from_client(ua, client);
241 client = select_client_resource(ua);
245 purge_jobs_from_client(ua, client);
252 * Prune File records from the database. For any Job which
253 * is older than the retention period, we unconditionally delete
254 * all File records for that Job. This is simple enough that no
255 * temporary tables are needed. We simply make an in memory list of
256 * the JobIds meeting the prune conditions, then delete all File records
257 * pointing to each of those JobIds.
259 int purge_files_from_client(UAContext *ua, CLIENT *client)
261 struct s_file_del_ctx del;
262 char *query = (char *)get_pool_memory(PM_MESSAGE);
266 memset(&cr, 0, sizeof(cr));
267 memset(&del, 0, sizeof(del));
269 strcpy(cr.Name, client->hdr.name);
270 if (!db_create_client_record(ua->db, &cr)) {
274 Mmsg(&query, select_jobsfiles_from_client, cr.ClientId);
276 Dmsg1(050, "select sql=%s\n", query);
278 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
279 bsendmsg(ua, "%s", db_strerror(ua->db));
280 Dmsg0(050, "Count failed\n");
284 if (del.tot_ids == 0) {
285 bsendmsg(ua, _("No Files found for client %s to purge from %s catalog.\n"),
286 client->hdr.name, client->catalog->hdr.name);
290 if (del.tot_ids < MAX_DEL_LIST_LEN) {
291 del.max_ids = del.tot_ids + 1;
293 del.max_ids = MAX_DEL_LIST_LEN;
297 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
299 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
301 for (i=0; i < del.num_ids; i++) {
302 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
303 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
304 db_sql_query(ua->db, query, NULL, (void *)NULL);
306 * Now mark Job as having files purged. This is necessary to
307 * avoid having too many Jobs to process in future prunings. If
308 * we don't do this, the number of JobId's in our in memory list
309 * will grow very large.
311 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", del.JobId[i]);
312 db_sql_query(ua->db, query, NULL, (void *)NULL);
313 Dmsg1(050, "Del sql=%s\n", query);
315 bsendmsg(ua, _("%d Files for client %s purged from %s catalog.\n"), del.num_ids,
316 client->hdr.name, client->catalog->hdr.name);
322 free_pool_memory(query);
329 * Purging Jobs is a bit more complicated than purging Files
330 * because we delete Job records only if there is a more current
331 * backup of the FileSet. Otherwise, we keep the Job record.
332 * In other words, we never delete the only Job record that
333 * contains a current backup of a FileSet. This prevents the
334 * Volume from being recycled and destroying a current backup.
336 int purge_jobs_from_client(UAContext *ua, CLIENT *client)
338 struct s_job_del_ctx del;
339 char *query = (char *)get_pool_memory(PM_MESSAGE);
343 memset(&cr, 0, sizeof(cr));
344 memset(&del, 0, sizeof(del));
346 strcpy(cr.Name, client->hdr.name);
347 if (!db_create_client_record(ua->db, &cr)) {
351 Mmsg(&query, select_jobs_from_client, cr.ClientId);
353 Dmsg1(050, "select sql=%s\n", query);
355 if (!db_sql_query(ua->db, query, job_count_handler, (void *)&del)) {
356 bsendmsg(ua, "%s", db_strerror(ua->db));
357 Dmsg0(050, "Count failed\n");
360 if (del.tot_ids == 0) {
361 bsendmsg(ua, _("No Jobs found for client %s to purge from %s catalog.\n"),
362 client->hdr.name, client->catalog->hdr.name);
366 if (del.tot_ids < MAX_DEL_LIST_LEN) {
367 del.max_ids = del.tot_ids + 1;
369 del.max_ids = MAX_DEL_LIST_LEN;
374 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
375 del.PurgedFiles = (char *)malloc(del.max_ids);
377 db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
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.
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);
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);
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);
400 bsendmsg(ua, _("%d Jobs for client %s purged from %s catalog.\n"), del.num_ids,
401 client->hdr.name, client->catalog->hdr.name);
407 if (del.PurgedFiles) {
408 free(del.PurgedFiles);
410 free_pool_memory(query);
414 void purge_files_from_job(UAContext *ua, JOB_DBR *jr)
416 char *query = (char *)get_pool_memory(PM_MESSAGE);
418 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", jr->JobId);
419 db_sql_query(ua->db, query, NULL, (void *)NULL);
421 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", jr->JobId);
422 db_sql_query(ua->db, query, NULL, (void *)NULL);
424 free_pool_memory(query);
427 void purge_files_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr )
428 {} /* ***FIXME*** implement */
430 void purge_jobs_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
432 char *query = (char *)get_pool_memory(PM_MESSAGE);
433 struct s_count_ctx cnt;
434 struct s_file_del_ctx del;
438 memset(&jr, 0, sizeof(jr));
439 memset(&del, 0, sizeof(del));
441 Mmsg(&query, "SELECT count(*) FROM JobMedia WHERE MediaId=%d", mr->MediaId);
442 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
443 bsendmsg(ua, "%s", db_strerror(ua->db));
444 Dmsg0(050, "Count failed\n");
448 if (cnt.count == 0) {
449 bsendmsg(ua, "There are no Jobs associated with Volume %s. It is purged.\n",
451 if (!mark_media_purged(ua, mr)) {
457 if (cnt.count < MAX_DEL_LIST_LEN) {
458 del.max_ids = cnt.count + 1;
460 del.max_ids = MAX_DEL_LIST_LEN;
463 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
465 Mmsg(&query, "SELECT JobId FROM JobMedia WHERE MediaId=%d", mr->MediaId);
466 if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
467 bsendmsg(ua, "%s", db_strerror(ua->db));
468 Dmsg0(050, "Count failed\n");
472 for (i=0; i < del.num_ids; i++) {
473 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
474 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
475 db_sql_query(ua->db, query, NULL, (void *)NULL);
476 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
477 db_sql_query(ua->db, query, NULL, (void *)NULL);
478 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
479 db_sql_query(ua->db, query, NULL, (void *)NULL);
480 Dmsg1(050, "Del sql=%s\n", query);
486 bsendmsg(ua, _("%d Files for Volume %s purged from catalog.\n"), del.num_del,
489 /* If purged, mark it so */
490 if (del.num_ids == del.num_del) {
491 mark_media_purged(ua, mr);
495 free_pool_memory(query);
498 static int mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
500 if (strcmp(mr->VolStatus, "Append") == 0 ||
501 strcmp(mr->VolStatus, "Full") == 0) {
502 strcpy(mr->VolStatus, "Purged");
503 if (!db_update_media_record(ua->db, mr)) {
504 bsendmsg(ua, "%s", db_strerror(ua->db));