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 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr);
46 #define MAX_DEL_LIST_LEN 1000000
49 static char *select_jobsfiles_from_client =
50 "SELECT JobId FROM Job "
54 static 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 = atoi(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)strtod(row[0], NULL);
136 del->PurgedFiles[del->num_ids++] = (char)atoi(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)strtod(row[0], NULL);
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, char *cmd)
170 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, &pr, &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, &pr, &mr);
233 switch (do_keyword_prompt(ua, _("Choose item to purge"), keywords)) {
235 client = select_client_resource(ua);
239 purge_files_from_client(ua, client);
242 client = select_client_resource(ua);
246 purge_jobs_from_client(ua, client);
253 * Prune File records from the database. For any Job which
254 * is older than the retention period, we unconditionally delete
255 * all File records for that Job. This is simple enough that no
256 * temporary tables are needed. We simply make an in memory list of
257 * the JobIds meeting the prune conditions, then delete all File records
258 * pointing to each of those JobIds.
260 int purge_files_from_client(UAContext *ua, CLIENT *client)
262 struct s_file_del_ctx del;
263 char *query = (char *)get_pool_memory(PM_MESSAGE);
267 memset(&cr, 0, sizeof(cr));
268 memset(&del, 0, sizeof(del));
270 strcpy(cr.Name, client->hdr.name);
271 if (!db_create_client_record(ua->db, &cr)) {
275 Mmsg(&query, select_jobsfiles_from_client, cr.ClientId);
277 Dmsg1(050, "select sql=%s\n", query);
279 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
280 bsendmsg(ua, "%s", db_strerror(ua->db));
281 Dmsg0(050, "Count failed\n");
285 if (del.tot_ids == 0) {
286 bsendmsg(ua, _("No Files found for client %s to purge from %s catalog.\n"),
287 client->hdr.name, client->catalog->hdr.name);
291 if (del.tot_ids < MAX_DEL_LIST_LEN) {
292 del.max_ids = del.tot_ids + 1;
294 del.max_ids = MAX_DEL_LIST_LEN;
298 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
300 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
302 for (i=0; i < del.num_ids; i++) {
303 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
304 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
305 db_sql_query(ua->db, query, NULL, (void *)NULL);
307 * Now mark Job as having files purged. This is necessary to
308 * avoid having too many Jobs to process in future prunings. If
309 * we don't do this, the number of JobId's in our in memory list
310 * will grow very large.
312 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", del.JobId[i]);
313 db_sql_query(ua->db, query, NULL, (void *)NULL);
314 Dmsg1(050, "Del sql=%s\n", query);
316 bsendmsg(ua, _("%d Files for client %s purged from %s catalog.\n"), del.num_ids,
317 client->hdr.name, client->catalog->hdr.name);
323 free_pool_memory(query);
330 * Purging Jobs is a bit more complicated than purging Files
331 * because we delete Job records only if there is a more current
332 * backup of the FileSet. Otherwise, we keep the Job record.
333 * In other words, we never delete the only Job record that
334 * contains a current backup of a FileSet. This prevents the
335 * Volume from being recycled and destroying a current backup.
337 int purge_jobs_from_client(UAContext *ua, CLIENT *client)
339 struct s_job_del_ctx del;
340 char *query = (char *)get_pool_memory(PM_MESSAGE);
344 memset(&cr, 0, sizeof(cr));
345 memset(&del, 0, sizeof(del));
347 strcpy(cr.Name, client->hdr.name);
348 if (!db_create_client_record(ua->db, &cr)) {
352 Mmsg(&query, select_jobs_from_client, cr.ClientId);
354 Dmsg1(050, "select sql=%s\n", query);
356 if (!db_sql_query(ua->db, query, job_count_handler, (void *)&del)) {
357 bsendmsg(ua, "%s", db_strerror(ua->db));
358 Dmsg0(050, "Count failed\n");
361 if (del.tot_ids == 0) {
362 bsendmsg(ua, _("No Jobs found for client %s to purge from %s catalog.\n"),
363 client->hdr.name, client->catalog->hdr.name);
367 if (del.tot_ids < MAX_DEL_LIST_LEN) {
368 del.max_ids = del.tot_ids + 1;
370 del.max_ids = MAX_DEL_LIST_LEN;
375 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
376 del.PurgedFiles = (char *)malloc(del.max_ids);
378 db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
381 * OK, now we have the list of JobId's to be purged, first check
382 * if the Files have been purged, if not, purge (delete) them.
383 * Then delete the Job entry, and finally and JobMedia records.
385 for (i=0; i < del.num_ids; i++) {
386 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
387 if (!del.PurgedFiles[i]) {
388 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
389 db_sql_query(ua->db, query, NULL, (void *)NULL);
390 Dmsg1(050, "Del sql=%s\n", query);
393 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
394 db_sql_query(ua->db, query, NULL, (void *)NULL);
395 Dmsg1(050, "Del sql=%s\n", query);
397 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
398 db_sql_query(ua->db, query, NULL, (void *)NULL);
399 Dmsg1(050, "Del sql=%s\n", query);
401 bsendmsg(ua, _("%d Jobs for client %s purged from %s catalog.\n"), del.num_ids,
402 client->hdr.name, client->catalog->hdr.name);
408 if (del.PurgedFiles) {
409 free(del.PurgedFiles);
411 free_pool_memory(query);
415 void purge_files_from_job(UAContext *ua, JOB_DBR *jr)
417 char *query = (char *)get_pool_memory(PM_MESSAGE);
419 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", jr->JobId);
420 db_sql_query(ua->db, query, NULL, (void *)NULL);
422 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", jr->JobId);
423 db_sql_query(ua->db, query, NULL, (void *)NULL);
425 free_pool_memory(query);
428 void purge_files_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr )
429 {} /* ***FIXME*** implement */
431 void purge_jobs_from_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
433 char *query = (char *)get_pool_memory(PM_MESSAGE);
434 struct s_count_ctx cnt;
435 struct s_file_del_ctx del;
439 memset(&jr, 0, sizeof(jr));
440 memset(&del, 0, sizeof(del));
442 Mmsg(&query, "SELECT count(*) FROM JobMedia WHERE MediaId=%d", mr->MediaId);
443 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
444 bsendmsg(ua, "%s", db_strerror(ua->db));
445 Dmsg0(050, "Count failed\n");
449 if (cnt.count == 0) {
450 bsendmsg(ua, "There are no Jobs associated with Volume %s. Marking it purged.\n",
452 if (!mark_media_purged(ua, mr)) {
458 if (cnt.count < MAX_DEL_LIST_LEN) {
459 del.max_ids = cnt.count + 1;
461 del.max_ids = MAX_DEL_LIST_LEN;
464 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
466 Mmsg(&query, "SELECT JobId FROM JobMedia WHERE MediaId=%d", mr->MediaId);
467 if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
468 bsendmsg(ua, "%s", db_strerror(ua->db));
469 Dmsg0(050, "Count failed\n");
473 for (i=0; i < del.num_ids; i++) {
474 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
475 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
476 db_sql_query(ua->db, query, NULL, (void *)NULL);
477 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
478 db_sql_query(ua->db, query, NULL, (void *)NULL);
479 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
480 db_sql_query(ua->db, query, NULL, (void *)NULL);
481 Dmsg1(050, "Del sql=%s\n", query);
487 bsendmsg(ua, _("%d Files for Volume %s purged from catalog.\n"), del.num_del,
490 /* If purged, mark it so */
491 if (del.num_ids == del.num_del) {
492 mark_media_purged(ua, mr);
496 free_pool_memory(query);
500 * IF volume status is Append, Full, or Used, mark it Purged
501 * Purged volumes can then be recycled (if enabled).
503 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
505 if (strcmp(mr->VolStatus, "Append") == 0 ||
506 strcmp(mr->VolStatus, "Full") == 0 ||
507 strcmp(mr->VolStatus, "Used") == 0) {
508 strcpy(mr->VolStatus, "Purged");
509 if (!db_update_media_record(ua->db, mr)) {
511 bsendmsg(ua, "%s", db_strerror(ua->db));
517 return strcpy(mr->VolStatus, "Purged") == 0;