3 * Bacula Director -- User Agent Database prune Command
4 * Applies retention periods
6 * Kern Sibbald, February MMII
12 Copyright (C) 2002-2003 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,
34 /* Imported functions */
35 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr);
37 /* Forward referenced functions */
38 int prune_files(UAContext *ua, CLIENT *client);
39 int prune_jobs(UAContext *ua, CLIENT *client, int JobType);
40 int prune_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr);
43 #define MAX_DEL_LIST_LEN 1000000
45 /* Imported variables */
46 extern char *select_job;
47 extern char *drop_deltabs[];
48 extern char *create_deltabs[];
49 extern char *insert_delcand;
50 extern char *select_backup_del;
51 extern char *select_verify_del;
52 extern char *select_restore_del;
53 extern char *cnt_File;
54 extern char *del_File;
55 extern char *upd_Purged;
56 extern char *cnt_DelCand;
58 extern char *del_JobMedia;
59 extern char *cnt_JobMedia;
60 extern char *sel_JobMedia;
63 /* In memory list of JobIds */
64 struct s_file_del_ctx {
66 int num_ids; /* ids stored */
67 int max_ids; /* size of array */
68 int num_del; /* number deleted */
69 int tot_ids; /* total to process */
72 struct s_job_del_ctx {
73 JobId_t *JobId; /* array of JobIds */
74 char *PurgedFiles; /* Array of PurgedFile flags */
75 int num_ids; /* ids stored */
76 int max_ids; /* size of array */
77 int num_del; /* number deleted */
78 int tot_ids; /* total to process */
87 * Called here to count entries to be deleted
89 static int count_handler(void *ctx, int num_fields, char **row)
91 struct s_count_ctx *cnt = (struct s_count_ctx *)ctx;
94 cnt->count = atoi(row[0]);
103 * Called here to count the number of Jobs to be pruned
105 static int file_count_handler(void *ctx, int num_fields, char **row)
107 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
114 * Called here to make in memory list of JobIds to be
115 * deleted and the associated PurgedFiles flag.
116 * The in memory list will then be transversed
117 * to issue the SQL DELETE commands. Note, the list
118 * is allowed to get to MAX_DEL_LIST_LEN to limit the
119 * maximum malloc'ed memory.
121 static int job_delete_handler(void *ctx, int num_fields, char **row)
123 struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
125 if (del->num_ids == MAX_DEL_LIST_LEN) {
128 if (del->num_ids == del->max_ids) {
129 del->max_ids = (del->max_ids * 3) / 2;
130 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) * del->max_ids);
131 del->PurgedFiles = (char *)brealloc(del->PurgedFiles, del->max_ids);
133 del->JobId[del->num_ids] = (JobId_t)str_to_int64(row[0]);
134 del->PurgedFiles[del->num_ids++] = (char)str_to_int64(row[0]);
138 static int file_delete_handler(void *ctx, int num_fields, char **row)
140 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
142 if (del->num_ids == MAX_DEL_LIST_LEN) {
145 if (del->num_ids == del->max_ids) {
146 del->max_ids = (del->max_ids * 3) / 2;
147 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) *
150 del->JobId[del->num_ids++] = (JobId_t)str_to_int64(row[0]);
155 * Prune records from database
157 * prune files (from) client=xxx
158 * prune jobs (from) client=xxx
161 int prunecmd(UAContext *ua, char *cmd)
167 static char *keywords[] = {
175 switch (find_arg_keyword(ua, keywords)) {
177 client = select_client_resource(ua);
178 if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
181 prune_files(ua, client);
184 client = select_client_resource(ua);
185 if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
188 /* ****FIXME**** allow user to select JobType */
189 prune_jobs(ua, client, JT_BACKUP);
192 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
195 if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
198 prune_volume(ua, &pr, &mr);
203 switch (do_keyword_prompt(ua, _("Choose item to prune"), keywords)) {
205 client = select_client_resource(ua);
206 if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
209 prune_files(ua, client);
212 client = select_client_resource(ua);
213 if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
216 /* ****FIXME**** allow user to select JobType */
217 prune_jobs(ua, client, JT_BACKUP);
220 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
223 if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
226 prune_volume(ua, &pr, &mr);
233 * Prune File records from the database. For any Job which
234 * is older than the retention period, we unconditionally delete
235 * all File records for that Job. This is simple enough that no
236 * temporary tables are needed. We simply make an in memory list of
237 * the JobIds meeting the prune conditions, then delete all File records
238 * pointing to each of those JobIds.
240 * This routine assumes you want the pruning to be done. All checking
241 * must be done before calling this routine.
243 int prune_files(UAContext *ua, CLIENT *client)
245 struct s_file_del_ctx del;
246 POOLMEM *query = get_pool_memory(PM_MESSAGE);
250 char ed1[50], ed2[50];
253 memset(&cr, 0, sizeof(cr));
254 memset(&del, 0, sizeof(del));
255 strcpy(cr.Name, client->hdr.name);
256 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
261 period = client->FileRetention;
262 now = (utime_t)time(NULL);
264 /* Select Jobs -- for counting */
265 Mmsg(&query, select_job, edit_uint64(now - period, ed1), cr.ClientId);
266 Dmsg1(050, "select sql=%s\n", query);
267 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
269 bsendmsg(ua, "%s", db_strerror(ua->db));
271 Dmsg0(050, "Count failed\n");
275 if (del.tot_ids == 0) {
277 bsendmsg(ua, _("No Files found to prune.\n"));
282 if (del.tot_ids < MAX_DEL_LIST_LEN) {
283 del.max_ids = del.tot_ids + 1;
285 del.max_ids = MAX_DEL_LIST_LEN;
289 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
291 /* Now process same set but making delete list */
292 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
294 for (i=0; i < del.num_ids; i++) {
295 struct s_count_ctx cnt;
296 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
297 Mmsg(&query, cnt_File, del.JobId[i]);
299 db_sql_query(ua->db, query, count_handler, (void *)&cnt);
300 del.tot_ids += cnt.count;
301 Mmsg(&query, del_File, del.JobId[i]);
302 db_sql_query(ua->db, query, NULL, (void *)NULL);
304 * Now mark Job as having files purged. This is necessary to
305 * avoid having too many Jobs to process in future prunings. If
306 * we don't do this, the number of JobId's in our in memory list
307 * will grow very large.
309 Mmsg(&query, upd_Purged, del.JobId[i]);
310 db_sql_query(ua->db, query, NULL, (void *)NULL);
311 Dmsg1(050, "Del sql=%s\n", query);
313 edit_uint64_with_commas(del.tot_ids, ed1);
314 edit_uint64_with_commas(del.num_ids, ed2);
315 bsendmsg(ua, _("Pruned %s Files from %s Jobs for client %s from catalog.\n"),
316 ed1, ed2, client->hdr.name);
323 free_pool_memory(query);
328 static void drop_temp_tables(UAContext *ua)
331 for (i=0; drop_deltabs[i]; i++) {
332 db_sql_query(ua->db, drop_deltabs[i], NULL, (void *)NULL);
336 static int create_temp_tables(UAContext *ua)
339 /* Create temp tables and indicies */
340 for (i=0; create_deltabs[i]; i++) {
341 if (!db_sql_query(ua->db, create_deltabs[i], NULL, (void *)NULL)) {
342 bsendmsg(ua, "%s", db_strerror(ua->db));
343 Dmsg0(050, "create DelTables table failed\n");
353 * Purging Jobs is a bit more complicated than purging Files
354 * because we delete Job records only if there is a more current
355 * backup of the FileSet. Otherwise, we keep the Job record.
356 * In other words, we never delete the only Job record that
357 * contains a current backup of a FileSet. This prevents the
358 * Volume from being recycled and destroying a current backup.
360 * For Verify Jobs, we do not delete the last InitCatalog.
362 * For Restore Jobs there are no restrictions.
364 int prune_jobs(UAContext *ua, CLIENT *client, int JobType)
366 struct s_job_del_ctx del;
367 struct s_count_ctx cnt;
368 char *query = (char *)get_pool_memory(PM_MESSAGE);
375 memset(&cr, 0, sizeof(cr));
376 memset(&del, 0, sizeof(del));
377 strcpy(cr.Name, client->hdr.name);
378 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
383 period = client->JobRetention;
384 now = (utime_t)time(NULL);
386 /* Drop any previous temporary tables still there */
387 drop_temp_tables(ua);
389 /* Create temp tables and indicies */
390 if (!create_temp_tables(ua)) {
395 * Select all files that are older than the JobRetention period
396 * and stuff them into the "DeletionCandidates" table.
398 edit_uint64(now - period, ed1);
399 Mmsg(&query, insert_delcand, (char)JobType, ed1, cr.ClientId);
400 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
402 bsendmsg(ua, "%s", db_strerror(ua->db));
404 Dmsg0(050, "insert delcand failed\n");
408 /* Count Files to be deleted */
409 strcpy(query, cnt_DelCand);
410 Dmsg1(100, "select sql=%s\n", query);
412 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
413 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 if (!db_sql_query(ua->db, query, job_delete_handler, (void *)&del)) {
446 bsendmsg(ua, "%s", db_strerror(ua->db));
450 * OK, now we have the list of JobId's to be pruned, first check
451 * if the Files have been purged, if not, purge (delete) them.
452 * Then delete the Job entry, and finally and JobMedia records.
454 for (i=0; i < del.num_ids; i++) {
455 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
456 if (!del.PurgedFiles[i]) {
457 Mmsg(&query, del_File, del.JobId[i]);
458 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
459 bsendmsg(ua, "%s", db_strerror(ua->db));
461 Dmsg1(050, "Del sql=%s\n", query);
464 Mmsg(&query, del_Job, del.JobId[i]);
465 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
466 bsendmsg(ua, "%s", db_strerror(ua->db));
468 Dmsg1(050, "Del sql=%s\n", query);
470 Mmsg(&query, del_JobMedia, del.JobId[i]);
471 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
472 bsendmsg(ua, "%s", db_strerror(ua->db));
474 Dmsg1(050, "Del sql=%s\n", query);
476 bsendmsg(ua, _("Pruned %d %s for client %s from catalog.\n"), del.num_ids,
477 del.num_ids==1?_("Job"):_("Jobs"), client->hdr.name);
480 drop_temp_tables(ua);
485 if (del.PurgedFiles) {
486 free(del.PurgedFiles);
488 free_pool_memory(query);
493 * Prune a given Volume
495 int prune_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
497 char *query = (char *)get_pool_memory(PM_MESSAGE);
498 struct s_count_ctx cnt;
499 struct s_file_del_ctx del;
505 memset(&jr, 0, sizeof(jr));
506 memset(&del, 0, sizeof(del));
508 Mmsg(&query, cnt_JobMedia, mr->MediaId);
509 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
510 bsendmsg(ua, "%s", db_strerror(ua->db));
511 Dmsg0(050, "Count failed\n");
515 if (cnt.count == 0) {
517 bsendmsg(ua, "There are no Jobs associated with Volume %s. Marking it purged.\n",
520 stat = mark_media_purged(ua, mr);
524 if (cnt.count < MAX_DEL_LIST_LEN) {
525 del.max_ids = cnt.count + 1;
527 del.max_ids = MAX_DEL_LIST_LEN;
530 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
532 /* ***FIXME*** could make this do JobTDate check too */
533 Mmsg(&query, sel_JobMedia, mr->MediaId);
534 if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
536 bsendmsg(ua, "%s", db_strerror(ua->db));
538 Dmsg0(050, "Count failed\n");
542 /* Use Volume Retention to prune Jobs and Files */
543 period = mr->VolRetention;
544 now = (utime_t)time(NULL);
546 Dmsg3(200, "Now=%d period=%d now-period=%d\n", (int)now, (int)period,
548 for (i=0; i < del.num_ids; i++) {
549 jr.JobId = del.JobId[i];
550 if (!db_get_job_record(ua->jcr, ua->db, &jr)) {
553 Dmsg2(200, "Looking at %s JobTdate=%d\n", jr.Job, (int)jr.JobTDate);
554 if (jr.JobTDate >= (now - period)) {
557 Dmsg2(200, "Delete JobId=%d Job=%s\n", del.JobId[i], jr.Job);
558 Mmsg(&query, del_File, del.JobId[i]);
559 db_sql_query(ua->db, query, NULL, (void *)NULL);
560 Mmsg(&query, del_Job, del.JobId[i]);
561 db_sql_query(ua->db, query, NULL, (void *)NULL);
562 Mmsg(&query, del_JobMedia, del.JobId[i]);
563 db_sql_query(ua->db, query, NULL, (void *)NULL);
564 Dmsg1(050, "Del sql=%s\n", query);
570 if (ua->verbose && del.num_del != 0) {
571 bsendmsg(ua, _("Pruned %d %s on Volume %s from catalog.\n"), del.num_del,
572 del.num_del == 1 ? "Job" : "Jobs", mr->VolumeName);
575 /* If purged, mark it so */
576 if (del.num_ids == del.num_del) {
577 Dmsg0(200, "Volume is purged.\n");
578 stat = mark_media_purged(ua, mr);
583 free_pool_memory(query);