3 * Bacula Director -- User Agent Database prune Command
4 * Applies retention periods
6 * Kern Sibbald, February MMII
12 Copyright (C) 2002 Kern Sibbald and John Walker
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of
17 the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public
25 License along with this program; if not, write to the Free
26 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 /* Forward referenced functions */
36 int prune_files(UAContext *ua, CLIENT *client);
37 int prune_jobs(UAContext *ua, CLIENT *client, int JobType);
38 int prune_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr);
39 static int mark_media_purged(UAContext *ua, MEDIA_DBR *mr);
42 #define MAX_DEL_LIST_LEN 1000000
44 /* Imported variables */
45 extern char *select_job;
46 extern char *drop_deltabs[];
47 extern char *create_deltabs[];
48 extern char *insert_delcand;
49 extern char *select_backup_del;
50 extern char *select_verify_del;
51 extern char *select_restore_del;
52 extern char *cnt_File;
53 extern char *del_File;
54 extern char *upd_Purged;
55 extern char *cnt_DelCand;
57 extern char *del_JobMedia;
58 extern char *cnt_JobMedia;
59 extern char *sel_JobMedia;
62 /* In memory list of JobIds */
63 struct s_file_del_ctx {
65 int num_ids; /* ids stored */
66 int max_ids; /* size of array */
67 int num_del; /* number deleted */
68 int tot_ids; /* total to process */
71 struct s_job_del_ctx {
72 JobId_t *JobId; /* array of JobIds */
73 char *PurgedFiles; /* Array of PurgedFile flags */
74 int num_ids; /* ids stored */
75 int max_ids; /* size of array */
76 int num_del; /* number deleted */
77 int tot_ids; /* total to process */
86 * Called here to count entries to be deleted
88 static int count_handler(void *ctx, int num_fields, char **row)
90 struct s_count_ctx *cnt = (struct s_count_ctx *)ctx;
93 cnt->count = atoi(row[0]);
102 * Called here to count the number of Jobs to be pruned
104 static int file_count_handler(void *ctx, int num_fields, char **row)
106 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
113 * Called here to make in memory list of JobIds to be
114 * deleted and the associated PurgedFiles flag.
115 * The in memory list will then be transversed
116 * to issue the SQL DELETE commands. Note, the list
117 * is allowed to get to MAX_DEL_LIST_LEN to limit the
118 * maximum malloc'ed memory.
120 static int job_delete_handler(void *ctx, int num_fields, char **row)
122 struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
124 if (del->num_ids == MAX_DEL_LIST_LEN) {
127 if (del->num_ids == del->max_ids) {
128 del->max_ids = (del->max_ids * 3) / 2;
129 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) * del->max_ids);
130 del->PurgedFiles = (char *)brealloc(del->PurgedFiles, del->max_ids);
132 del->JobId[del->num_ids] = (JobId_t)strtod(row[0], NULL);
133 del->PurgedFiles[del->num_ids++] = (char)atoi(row[0]);
137 static int file_delete_handler(void *ctx, int num_fields, char **row)
139 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
141 if (del->num_ids == MAX_DEL_LIST_LEN) {
144 if (del->num_ids == del->max_ids) {
145 del->max_ids = (del->max_ids * 3) / 2;
146 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) *
149 del->JobId[del->num_ids++] = (JobId_t)strtod(row[0], NULL);
154 * Prune records from database
156 * prune files (from) client=xxx
157 * prune jobs (from) client=xxx
160 int prunecmd(UAContext *ua, char *cmd)
166 static char *keywords[] = {
174 switch (find_arg_keyword(ua, keywords)) {
176 client = select_client_resource(ua);
177 if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
180 prune_files(ua, client);
183 client = select_client_resource(ua);
184 if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
187 /* ****FIXME**** allow user to select JobType */
188 prune_jobs(ua, client, JT_BACKUP);
191 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
194 if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
197 prune_volume(ua, &pr, &mr);
202 switch (do_keyword_prompt(ua, _("Choose item to prune"), keywords)) {
204 client = select_client_resource(ua);
205 if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
208 prune_files(ua, client);
211 client = select_client_resource(ua);
212 if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
215 /* ****FIXME**** allow user to select JobType */
216 prune_jobs(ua, client, JT_BACKUP);
219 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
222 if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
225 prune_volume(ua, &pr, &mr);
232 * Prune File records from the database. For any Job which
233 * is older than the retention period, we unconditionally delete
234 * all File records for that Job. This is simple enough that no
235 * temporary tables are needed. We simply make an in memory list of
236 * the JobIds meeting the prune conditions, then delete all File records
237 * pointing to each of those JobIds.
239 * This routine assumes you want the pruning to be done. All checking
240 * must be done before calling this routine.
242 int prune_files(UAContext *ua, CLIENT *client)
244 struct s_file_del_ctx del;
245 POOLMEM *query = get_pool_memory(PM_MESSAGE);
249 char ed1[50], ed2[50];
252 memset(&cr, 0, sizeof(cr));
253 memset(&del, 0, sizeof(del));
254 strcpy(cr.Name, client->hdr.name);
255 if (!db_create_client_record(ua->db, &cr)) {
260 period = client->FileRetention;
261 now = (btime_t)time(NULL);
263 /* Select Jobs -- for counting */
264 Mmsg(&query, select_job, edit_uint64(now - period, ed1), cr.ClientId);
265 Dmsg1(050, "select sql=%s\n", query);
266 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
268 bsendmsg(ua, "%s", db_strerror(ua->db));
270 Dmsg0(050, "Count failed\n");
274 if (del.tot_ids == 0) {
276 bsendmsg(ua, _("No Files found to prune.\n"));
281 if (del.tot_ids < MAX_DEL_LIST_LEN) {
282 del.max_ids = del.tot_ids + 1;
284 del.max_ids = MAX_DEL_LIST_LEN;
288 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
290 /* Now process same set but making delete list */
291 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
293 for (i=0; i < del.num_ids; i++) {
294 struct s_count_ctx cnt;
295 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
296 Mmsg(&query, cnt_File, del.JobId[i]);
298 db_sql_query(ua->db, query, count_handler, (void *)&cnt);
299 del.tot_ids += cnt.count;
300 Mmsg(&query, del_File, del.JobId[i]);
301 db_sql_query(ua->db, query, NULL, (void *)NULL);
303 * Now mark Job as having files purged. This is necessary to
304 * avoid having too many Jobs to process in future prunings. If
305 * we don't do this, the number of JobId's in our in memory list
306 * will grow very large.
308 Mmsg(&query, upd_Purged, del.JobId[i]);
309 db_sql_query(ua->db, query, NULL, (void *)NULL);
310 Dmsg1(050, "Del sql=%s\n", query);
312 edit_uint64_with_commas(del.tot_ids, ed1);
313 edit_uint64_with_commas(del.num_ids, ed2);
314 bsendmsg(ua, _("Pruned %s Files from %s Jobs for client %s from %s catalog.\n"),
315 ed1, ed2, client->hdr.name, client->catalog->hdr.name);
322 free_pool_memory(query);
327 static void drop_temp_tables(UAContext *ua)
330 for (i=0; drop_deltabs[i]; i++) {
331 db_sql_query(ua->db, drop_deltabs[i], NULL, (void *)NULL);
335 static int create_temp_tables(UAContext *ua)
338 /* Create temp tables and indicies */
339 for (i=0; create_deltabs[i]; i++) {
340 if (!db_sql_query(ua->db, create_deltabs[i], NULL, (void *)NULL)) {
341 bsendmsg(ua, "%s", db_strerror(ua->db));
342 Dmsg0(050, "create DelTables table failed\n");
352 * Purging Jobs is a bit more complicated than purging Files
353 * because we delete Job records only if there is a more current
354 * backup of the FileSet. Otherwise, we keep the Job record.
355 * In other words, we never delete the only Job record that
356 * contains a current backup of a FileSet. This prevents the
357 * Volume from being recycled and destroying a current backup.
359 * For Verify Jobs, we do not delete the last InitCatalog.
361 * For Restore Jobs there are no restrictions.
363 int prune_jobs(UAContext *ua, CLIENT *client, int JobType)
365 struct s_job_del_ctx del;
366 struct s_count_ctx cnt;
367 char *query = (char *)get_pool_memory(PM_MESSAGE);
374 memset(&cr, 0, sizeof(cr));
375 memset(&del, 0, sizeof(del));
376 strcpy(cr.Name, client->hdr.name);
377 if (!db_create_client_record(ua->db, &cr)) {
382 period = client->JobRetention;
383 now = (btime_t)time(NULL);
385 /* Drop any previous temporary tables still there */
386 drop_temp_tables(ua);
388 /* Create temp tables and indicies */
389 if (!create_temp_tables(ua)) {
394 * Select all files that are older than the JobRetention period
395 * and stuff them into the "DeletionCandidates" table.
397 edit_uint64(now - period, ed1);
398 Mmsg(&query, insert_delcand, ed1, cr.ClientId);
399 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
401 bsendmsg(ua, "%s", db_strerror(ua->db));
403 Dmsg0(050, "insert delcand failed\n");
407 /* Count Files to be deleted */
408 strcpy(query, cnt_DelCand);
409 Dmsg1(100, "select sql=%s\n", query);
410 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
412 bsendmsg(ua, "%s", db_strerror(ua->db));
414 Dmsg0(050, "Count failed\n");
418 if (cnt.count == 0) {
420 bsendmsg(ua, _("No Jobs found to prune.\n"));
425 if (cnt.count < MAX_DEL_LIST_LEN) {
426 del.max_ids = cnt.count + 1;
428 del.max_ids = MAX_DEL_LIST_LEN;
430 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
431 del.PurgedFiles = (char *)malloc(del.max_ids);
436 Mmsg(&query, select_backup_del, ed1, cr.ClientId);
439 Mmsg(&query, select_restore_del, ed1, cr.ClientId);
442 Mmsg(&query, select_verify_del, ed1, cr.ClientId);
445 db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
448 * OK, now we have the list of JobId's to be pruned, first check
449 * if the Files have been purged, if not, purge (delete) them.
450 * Then delete the Job entry, and finally and JobMedia records.
452 for (i=0; i < del.num_ids; i++) {
453 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
454 if (!del.PurgedFiles[i]) {
455 Mmsg(&query, del_File, del.JobId[i]);
456 db_sql_query(ua->db, query, NULL, (void *)NULL);
457 Dmsg1(050, "Del sql=%s\n", query);
460 Mmsg(&query, del_Job, del.JobId[i]);
461 db_sql_query(ua->db, query, NULL, (void *)NULL);
462 Dmsg1(050, "Del sql=%s\n", query);
464 Mmsg(&query, del_JobMedia, del.JobId[i]);
465 db_sql_query(ua->db, query, NULL, (void *)NULL);
466 Dmsg1(050, "Del sql=%s\n", query);
468 bsendmsg(ua, _("Pruned %d %s for client %s from %s catalog.\n"), del.num_ids,
469 del.num_ids==1?_("Job"):_("Jobs"), client->hdr.name, client->catalog->hdr.name);
472 drop_temp_tables(ua);
477 if (del.PurgedFiles) {
478 free(del.PurgedFiles);
480 free_pool_memory(query);
485 * Prune a given Volume
487 int prune_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
489 char *query = (char *)get_pool_memory(PM_MESSAGE);
490 struct s_count_ctx cnt;
491 struct s_file_del_ctx del;
497 memset(&jr, 0, sizeof(jr));
498 memset(&del, 0, sizeof(del));
500 Mmsg(&query, cnt_JobMedia, mr->MediaId);
501 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
502 bsendmsg(ua, "%s", db_strerror(ua->db));
503 Dmsg0(050, "Count failed\n");
507 if (cnt.count == 0) {
509 bsendmsg(ua, "There are no Jobs associated with Volume %s. It is purged.\n",
512 stat = mark_media_purged(ua, mr);
516 if (cnt.count < MAX_DEL_LIST_LEN) {
517 del.max_ids = cnt.count + 1;
519 del.max_ids = MAX_DEL_LIST_LEN;
522 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
524 /* ***FIXME*** could make this do JobTDate check too */
525 Mmsg(&query, sel_JobMedia, mr->MediaId);
526 if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
528 bsendmsg(ua, "%s", db_strerror(ua->db));
530 Dmsg0(050, "Count failed\n");
534 /* Use Volume Retention to prune Jobs and Files */
535 period = mr->VolRetention;
536 now = (btime_t)time(NULL);
538 Dmsg3(200, "Now=%d period=%d now-period=%d\n", (int)now, (int)period,
540 for (i=0; i < del.num_ids; i++) {
541 jr.JobId = del.JobId[i];
542 if (!db_get_job_record(ua->db, &jr)) {
545 Dmsg2(200, "Looking at %s JobTdate=%d\n", jr.Job, (int)jr.JobTDate);
546 if (jr.JobTDate >= (now - period)) {
549 Dmsg2(200, "Delete JobId=%d Job=%s\n", del.JobId[i], jr.Job);
550 Mmsg(&query, del_File, del.JobId[i]);
551 db_sql_query(ua->db, query, NULL, (void *)NULL);
552 Mmsg(&query, del_Job, del.JobId[i]);
553 db_sql_query(ua->db, query, NULL, (void *)NULL);
554 Mmsg(&query, del_JobMedia, del.JobId[i]);
555 db_sql_query(ua->db, query, NULL, (void *)NULL);
556 Dmsg1(050, "Del sql=%s\n", query);
562 if (ua->verbose && del.num_del != 0) {
563 bsendmsg(ua, _("Pruned %d %s on Volume %s from catalog.\n"), del.num_del,
564 del.num_del == 1 ? "Job" : "Jobs", mr->VolumeName);
567 /* If purged, mark it so */
568 if (del.num_ids == del.num_del) {
569 Dmsg0(200, "Volume is purged.\n");
570 stat = mark_media_purged(ua, mr);
575 free_pool_memory(query);
579 static int mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
581 if (strcmp(mr->VolStatus, "Append") == 0 ||
582 strcmp(mr->VolStatus, "Full") == 0) {
583 strcpy(mr->VolStatus, "Purged");
584 if (!db_update_media_record(ua->db, mr)) {
586 bsendmsg(ua, "%s", db_strerror(ua->db));
592 return strcpy(mr->VolStatus, "Purged") == 0;