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 /* Imported functions */
36 int mark_media_purged(UAContext *ua, MEDIA_DBR *mr);
38 /* Forward referenced functions */
39 int prune_files(UAContext *ua, CLIENT *client);
40 int prune_jobs(UAContext *ua, CLIENT *client, int JobType);
41 int prune_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr);
44 #define MAX_DEL_LIST_LEN 1000000
46 /* Imported variables */
47 extern char *select_job;
48 extern char *drop_deltabs[];
49 extern char *create_deltabs[];
50 extern char *insert_delcand;
51 extern char *select_backup_del;
52 extern char *select_verify_del;
53 extern char *select_restore_del;
54 extern char *cnt_File;
55 extern char *del_File;
56 extern char *upd_Purged;
57 extern char *cnt_DelCand;
59 extern char *del_JobMedia;
60 extern char *cnt_JobMedia;
61 extern char *sel_JobMedia;
64 /* In memory list of JobIds */
65 struct s_file_del_ctx {
67 int num_ids; /* ids stored */
68 int max_ids; /* size of array */
69 int num_del; /* number deleted */
70 int tot_ids; /* total to process */
73 struct s_job_del_ctx {
74 JobId_t *JobId; /* array of JobIds */
75 char *PurgedFiles; /* Array of PurgedFile flags */
76 int num_ids; /* ids stored */
77 int max_ids; /* size of array */
78 int num_del; /* number deleted */
79 int tot_ids; /* total to process */
88 * Called here to count entries to be deleted
90 static int count_handler(void *ctx, int num_fields, char **row)
92 struct s_count_ctx *cnt = (struct s_count_ctx *)ctx;
95 cnt->count = atoi(row[0]);
104 * Called here to count the number of Jobs to be pruned
106 static int file_count_handler(void *ctx, int num_fields, char **row)
108 struct s_file_del_ctx *del = (struct s_file_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)str_to_int64(row[0]);
135 del->PurgedFiles[del->num_ids++] = (char)str_to_int64(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)str_to_int64(row[0]);
156 * Prune records from database
158 * prune files (from) client=xxx
159 * prune jobs (from) client=xxx
162 int prunecmd(UAContext *ua, char *cmd)
168 static char *keywords[] = {
176 switch (find_arg_keyword(ua, keywords)) {
178 client = select_client_resource(ua);
179 if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
182 prune_files(ua, client);
185 client = select_client_resource(ua);
186 if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
189 /* ****FIXME**** allow user to select JobType */
190 prune_jobs(ua, client, JT_BACKUP);
193 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
196 if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
199 prune_volume(ua, &pr, &mr);
204 switch (do_keyword_prompt(ua, _("Choose item to prune"), keywords)) {
206 client = select_client_resource(ua);
207 if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
210 prune_files(ua, client);
213 client = select_client_resource(ua);
214 if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
217 /* ****FIXME**** allow user to select JobType */
218 prune_jobs(ua, client, JT_BACKUP);
221 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
224 if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
227 prune_volume(ua, &pr, &mr);
234 * Prune File records from the database. For any Job which
235 * is older than the retention period, we unconditionally delete
236 * all File records for that Job. This is simple enough that no
237 * temporary tables are needed. We simply make an in memory list of
238 * the JobIds meeting the prune conditions, then delete all File records
239 * pointing to each of those JobIds.
241 * This routine assumes you want the pruning to be done. All checking
242 * must be done before calling this routine.
244 int prune_files(UAContext *ua, CLIENT *client)
246 struct s_file_del_ctx del;
247 POOLMEM *query = get_pool_memory(PM_MESSAGE);
251 char ed1[50], ed2[50];
254 memset(&cr, 0, sizeof(cr));
255 memset(&del, 0, sizeof(del));
256 strcpy(cr.Name, client->hdr.name);
257 if (!db_create_client_record(ua->db, &cr)) {
262 period = client->FileRetention;
263 now = (utime_t)time(NULL);
265 /* Select Jobs -- for counting */
266 Mmsg(&query, select_job, edit_uint64(now - period, ed1), cr.ClientId);
267 Dmsg1(050, "select sql=%s\n", query);
268 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
270 bsendmsg(ua, "%s", db_strerror(ua->db));
272 Dmsg0(050, "Count failed\n");
276 if (del.tot_ids == 0) {
278 bsendmsg(ua, _("No Files found to prune.\n"));
283 if (del.tot_ids < MAX_DEL_LIST_LEN) {
284 del.max_ids = del.tot_ids + 1;
286 del.max_ids = MAX_DEL_LIST_LEN;
290 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
292 /* Now process same set but making delete list */
293 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
295 for (i=0; i < del.num_ids; i++) {
296 struct s_count_ctx cnt;
297 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
298 Mmsg(&query, cnt_File, del.JobId[i]);
300 db_sql_query(ua->db, query, count_handler, (void *)&cnt);
301 del.tot_ids += cnt.count;
302 Mmsg(&query, del_File, del.JobId[i]);
303 db_sql_query(ua->db, query, NULL, (void *)NULL);
305 * Now mark Job as having files purged. This is necessary to
306 * avoid having too many Jobs to process in future prunings. If
307 * we don't do this, the number of JobId's in our in memory list
308 * will grow very large.
310 Mmsg(&query, upd_Purged, del.JobId[i]);
311 db_sql_query(ua->db, query, NULL, (void *)NULL);
312 Dmsg1(050, "Del sql=%s\n", query);
314 edit_uint64_with_commas(del.tot_ids, ed1);
315 edit_uint64_with_commas(del.num_ids, ed2);
316 bsendmsg(ua, _("Pruned %s Files from %s Jobs for client %s from catalog.\n"),
317 ed1, ed2, client->hdr.name);
324 free_pool_memory(query);
329 static void drop_temp_tables(UAContext *ua)
332 for (i=0; drop_deltabs[i]; i++) {
333 db_sql_query(ua->db, drop_deltabs[i], NULL, (void *)NULL);
337 static int create_temp_tables(UAContext *ua)
340 /* Create temp tables and indicies */
341 for (i=0; create_deltabs[i]; i++) {
342 if (!db_sql_query(ua->db, create_deltabs[i], NULL, (void *)NULL)) {
343 bsendmsg(ua, "%s", db_strerror(ua->db));
344 Dmsg0(050, "create DelTables table failed\n");
354 * Purging Jobs is a bit more complicated than purging Files
355 * because we delete Job records only if there is a more current
356 * backup of the FileSet. Otherwise, we keep the Job record.
357 * In other words, we never delete the only Job record that
358 * contains a current backup of a FileSet. This prevents the
359 * Volume from being recycled and destroying a current backup.
361 * For Verify Jobs, we do not delete the last InitCatalog.
363 * For Restore Jobs there are no restrictions.
365 int prune_jobs(UAContext *ua, CLIENT *client, int JobType)
367 struct s_job_del_ctx del;
368 struct s_count_ctx cnt;
369 char *query = (char *)get_pool_memory(PM_MESSAGE);
376 memset(&cr, 0, sizeof(cr));
377 memset(&del, 0, sizeof(del));
378 strcpy(cr.Name, client->hdr.name);
379 if (!db_create_client_record(ua->db, &cr)) {
384 period = client->JobRetention;
385 now = (utime_t)time(NULL);
387 /* Drop any previous temporary tables still there */
388 drop_temp_tables(ua);
390 /* Create temp tables and indicies */
391 if (!create_temp_tables(ua)) {
396 * Select all files that are older than the JobRetention period
397 * and stuff them into the "DeletionCandidates" table.
399 edit_uint64(now - period, ed1);
400 Mmsg(&query, insert_delcand, (char)JobType, ed1, cr.ClientId);
401 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
403 bsendmsg(ua, "%s", db_strerror(ua->db));
405 Dmsg0(050, "insert delcand failed\n");
409 /* Count Files to be deleted */
410 strcpy(query, cnt_DelCand);
411 Dmsg1(100, "select sql=%s\n", query);
412 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
414 bsendmsg(ua, "%s", db_strerror(ua->db));
416 Dmsg0(050, "Count failed\n");
420 if (cnt.count == 0) {
422 bsendmsg(ua, _("No Jobs found to prune.\n"));
427 if (cnt.count < MAX_DEL_LIST_LEN) {
428 del.max_ids = cnt.count + 1;
430 del.max_ids = MAX_DEL_LIST_LEN;
432 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
433 del.PurgedFiles = (char *)malloc(del.max_ids);
438 Mmsg(&query, select_backup_del, ed1, cr.ClientId);
441 Mmsg(&query, select_restore_del, ed1, cr.ClientId);
444 Mmsg(&query, select_verify_del, ed1, cr.ClientId);
447 db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
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 db_sql_query(ua->db, query, NULL, (void *)NULL);
459 Dmsg1(050, "Del sql=%s\n", query);
462 Mmsg(&query, del_Job, del.JobId[i]);
463 db_sql_query(ua->db, query, NULL, (void *)NULL);
464 Dmsg1(050, "Del sql=%s\n", query);
466 Mmsg(&query, del_JobMedia, del.JobId[i]);
467 db_sql_query(ua->db, query, NULL, (void *)NULL);
468 Dmsg1(050, "Del sql=%s\n", query);
470 bsendmsg(ua, _("Pruned %d %s for client %s from catalog.\n"), del.num_ids,
471 del.num_ids==1?_("Job"):_("Jobs"), client->hdr.name);
474 drop_temp_tables(ua);
479 if (del.PurgedFiles) {
480 free(del.PurgedFiles);
482 free_pool_memory(query);
487 * Prune a given Volume
489 int prune_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
491 char *query = (char *)get_pool_memory(PM_MESSAGE);
492 struct s_count_ctx cnt;
493 struct s_file_del_ctx del;
499 memset(&jr, 0, sizeof(jr));
500 memset(&del, 0, sizeof(del));
502 Mmsg(&query, cnt_JobMedia, mr->MediaId);
503 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
504 bsendmsg(ua, "%s", db_strerror(ua->db));
505 Dmsg0(050, "Count failed\n");
509 if (cnt.count == 0) {
511 bsendmsg(ua, "There are no Jobs associated with Volume %s. Marking it purged.\n",
514 stat = mark_media_purged(ua, mr);
518 if (cnt.count < MAX_DEL_LIST_LEN) {
519 del.max_ids = cnt.count + 1;
521 del.max_ids = MAX_DEL_LIST_LEN;
524 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
526 /* ***FIXME*** could make this do JobTDate check too */
527 Mmsg(&query, sel_JobMedia, mr->MediaId);
528 if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
530 bsendmsg(ua, "%s", db_strerror(ua->db));
532 Dmsg0(050, "Count failed\n");
536 /* Use Volume Retention to prune Jobs and Files */
537 period = mr->VolRetention;
538 now = (utime_t)time(NULL);
540 Dmsg3(200, "Now=%d period=%d now-period=%d\n", (int)now, (int)period,
542 for (i=0; i < del.num_ids; i++) {
543 jr.JobId = del.JobId[i];
544 if (!db_get_job_record(ua->db, &jr)) {
547 Dmsg2(200, "Looking at %s JobTdate=%d\n", jr.Job, (int)jr.JobTDate);
548 if (jr.JobTDate >= (now - period)) {
551 Dmsg2(200, "Delete JobId=%d Job=%s\n", del.JobId[i], jr.Job);
552 Mmsg(&query, del_File, del.JobId[i]);
553 db_sql_query(ua->db, query, NULL, (void *)NULL);
554 Mmsg(&query, del_Job, del.JobId[i]);
555 db_sql_query(ua->db, query, NULL, (void *)NULL);
556 Mmsg(&query, del_JobMedia, del.JobId[i]);
557 db_sql_query(ua->db, query, NULL, (void *)NULL);
558 Dmsg1(050, "Del sql=%s\n", query);
564 if (ua->verbose && del.num_del != 0) {
565 bsendmsg(ua, _("Pruned %d %s on Volume %s from catalog.\n"), del.num_del,
566 del.num_del == 1 ? "Job" : "Jobs", mr->VolumeName);
569 /* If purged, mark it so */
570 if (del.num_ids == del.num_del) {
571 Dmsg0(200, "Volume is purged.\n");
572 stat = mark_media_purged(ua, mr);
577 free_pool_memory(query);