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 */
40 #define MAX_DEL_LIST_LEN 1000000
42 /* Imported variables */
43 extern char *select_job;
44 extern char *drop_deltabs[];
45 extern char *create_deltabs[];
46 extern char *insert_delcand;
47 extern char *select_backup_del;
48 extern char *select_verify_del;
49 extern char *select_restore_del;
50 extern char *cnt_File;
51 extern char *del_File;
52 extern char *upd_Purged;
53 extern char *cnt_DelCand;
55 extern char *del_JobMedia;
56 extern char *cnt_JobMedia;
57 extern char *sel_JobMedia;
60 /* In memory list of JobIds */
61 struct s_file_del_ctx {
63 int num_ids; /* ids stored */
64 int max_ids; /* size of array */
65 int num_del; /* number deleted */
66 int tot_ids; /* total to process */
69 struct s_job_del_ctx {
70 JobId_t *JobId; /* array of JobIds */
71 char *PurgedFiles; /* Array of PurgedFile flags */
72 int num_ids; /* ids stored */
73 int max_ids; /* size of array */
74 int num_del; /* number deleted */
75 int tot_ids; /* total to process */
84 * Called here to count entries to be deleted
86 static int count_handler(void *ctx, int num_fields, char **row)
88 struct s_count_ctx *cnt = (struct s_count_ctx *)ctx;
91 cnt->count = atoi(row[0]);
100 * Called here to count the number of Jobs to be pruned
102 static int file_count_handler(void *ctx, int num_fields, char **row)
104 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
111 * Called here to make in memory list of JobIds to be
112 * deleted and the associated PurgedFiles flag.
113 * The in memory list will then be transversed
114 * to issue the SQL DELETE commands. Note, the list
115 * is allowed to get to MAX_DEL_LIST_LEN to limit the
116 * maximum malloc'ed memory.
118 static int job_delete_handler(void *ctx, int num_fields, char **row)
120 struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
122 if (del->num_ids == MAX_DEL_LIST_LEN) {
125 if (del->num_ids == del->max_ids) {
126 del->max_ids = (del->max_ids * 3) / 2;
127 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) * del->max_ids);
128 del->PurgedFiles = (char *)brealloc(del->PurgedFiles, del->max_ids);
130 del->JobId[del->num_ids] = (JobId_t)str_to_int64(row[0]);
131 del->PurgedFiles[del->num_ids++] = (char)str_to_int64(row[0]);
135 static int file_delete_handler(void *ctx, int num_fields, char **row)
137 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
139 if (del->num_ids == MAX_DEL_LIST_LEN) {
142 if (del->num_ids == del->max_ids) {
143 del->max_ids = (del->max_ids * 3) / 2;
144 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) *
147 del->JobId[del->num_ids++] = (JobId_t)str_to_int64(row[0]);
152 * Prune records from database
154 * prune files (from) client=xxx
155 * prune jobs (from) client=xxx
158 int prunecmd(UAContext *ua, char *cmd)
165 static char *keywords[] = {
175 /* First search args */
176 kw = find_arg_keyword(ua, keywords);
177 if (kw < 0 || kw > 2) {
178 /* no args, so ask user */
179 kw = do_keyword_prompt(ua, _("Choose item to prune"), keywords);
183 case 0: /* prune files */
184 client = get_client_resource(ua);
185 if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
188 prune_files(ua, client);
190 case 1: /* prune jobs */
191 client = get_client_resource(ua);
192 if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
195 /* ****FIXME**** allow user to select JobType */
196 prune_jobs(ua, client, JT_BACKUP);
198 case 2: /* prune volume */
199 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
202 if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
205 prune_volume(ua, &mr);
215 * Prune File records from the database. For any Job which
216 * is older than the retention period, we unconditionally delete
217 * all File records for that Job. This is simple enough that no
218 * temporary tables are needed. We simply make an in memory list of
219 * the JobIds meeting the prune conditions, then delete all File records
220 * pointing to each of those JobIds.
222 * This routine assumes you want the pruning to be done. All checking
223 * must be done before calling this routine.
225 int prune_files(UAContext *ua, CLIENT *client)
227 struct s_file_del_ctx del;
228 POOLMEM *query = get_pool_memory(PM_MESSAGE);
232 char ed1[50], ed2[50];
235 memset(&cr, 0, sizeof(cr));
236 memset(&del, 0, sizeof(del));
237 bstrncpy(cr.Name, client->hdr.name, sizeof(cr.Name));
238 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
243 period = client->FileRetention;
244 now = (utime_t)time(NULL);
246 /* Select Jobs -- for counting */
247 Mmsg(&query, select_job, edit_uint64(now - period, ed1), cr.ClientId);
248 Dmsg1(050, "select sql=%s\n", query);
249 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
251 bsendmsg(ua, "%s", db_strerror(ua->db));
253 Dmsg0(050, "Count failed\n");
257 if (del.tot_ids == 0) {
259 bsendmsg(ua, _("No Files found to prune.\n"));
264 if (del.tot_ids < MAX_DEL_LIST_LEN) {
265 del.max_ids = del.tot_ids + 1;
267 del.max_ids = MAX_DEL_LIST_LEN;
271 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
273 /* Now process same set but making a delete list */
274 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
276 for (i=0; i < del.num_ids; i++) {
277 struct s_count_ctx cnt;
278 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
279 Mmsg(&query, cnt_File, del.JobId[i]);
281 db_sql_query(ua->db, query, count_handler, (void *)&cnt);
282 del.tot_ids += cnt.count;
283 Mmsg(&query, del_File, del.JobId[i]);
284 db_sql_query(ua->db, query, NULL, (void *)NULL);
286 * Now mark Job as having files purged. This is necessary to
287 * avoid having too many Jobs to process in future prunings. If
288 * we don't do this, the number of JobId's in our in memory list
289 * could grow very large.
291 Mmsg(&query, upd_Purged, del.JobId[i]);
292 db_sql_query(ua->db, query, NULL, (void *)NULL);
293 Dmsg1(050, "Del sql=%s\n", query);
295 edit_uint64_with_commas(del.tot_ids, ed1);
296 edit_uint64_with_commas(del.num_ids, ed2);
297 bsendmsg(ua, _("Pruned %s Files from %s Jobs for client %s from catalog.\n"),
298 ed1, ed2, client->hdr.name);
305 free_pool_memory(query);
310 static void drop_temp_tables(UAContext *ua)
313 for (i=0; drop_deltabs[i]; i++) {
314 db_sql_query(ua->db, drop_deltabs[i], NULL, (void *)NULL);
318 static int create_temp_tables(UAContext *ua)
321 /* Create temp tables and indicies */
322 for (i=0; create_deltabs[i]; i++) {
323 if (!db_sql_query(ua->db, create_deltabs[i], NULL, (void *)NULL)) {
324 bsendmsg(ua, "%s", db_strerror(ua->db));
325 Dmsg0(050, "create DelTables table failed\n");
335 * Purging Jobs is a bit more complicated than purging Files
336 * because we delete Job records only if there is a more current
337 * backup of the FileSet. Otherwise, we keep the Job record.
338 * In other words, we never delete the only Job record that
339 * contains a current backup of a FileSet. This prevents the
340 * Volume from being recycled and destroying a current backup.
342 * For Verify Jobs, we do not delete the last InitCatalog.
344 * For Restore Jobs there are no restrictions.
346 int prune_jobs(UAContext *ua, CLIENT *client, int JobType)
348 struct s_job_del_ctx del;
349 struct s_count_ctx cnt;
350 char *query = (char *)get_pool_memory(PM_MESSAGE);
357 memset(&cr, 0, sizeof(cr));
358 memset(&del, 0, sizeof(del));
359 bstrncpy(cr.Name, client->hdr.name, sizeof(cr.Name));
360 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
365 period = client->JobRetention;
366 now = (utime_t)time(NULL);
368 /* Drop any previous temporary tables still there */
369 drop_temp_tables(ua);
371 /* Create temp tables and indicies */
372 if (!create_temp_tables(ua)) {
377 * Select all files that are older than the JobRetention period
378 * and stuff them into the "DeletionCandidates" table.
380 edit_uint64(now - period, ed1);
381 Mmsg(&query, insert_delcand, (char)JobType, ed1, cr.ClientId);
382 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
384 bsendmsg(ua, "%s", db_strerror(ua->db));
386 Dmsg0(050, "insert delcand failed\n");
390 /* Count Files to be deleted */
391 pm_strcpy(&query, cnt_DelCand);
392 Dmsg1(100, "select sql=%s\n", query);
394 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
395 bsendmsg(ua, "%s", db_strerror(ua->db));
396 Dmsg0(050, "Count failed\n");
400 if (cnt.count == 0) {
402 bsendmsg(ua, _("No Jobs found to prune.\n"));
407 if (cnt.count < MAX_DEL_LIST_LEN) {
408 del.max_ids = cnt.count + 1;
410 del.max_ids = MAX_DEL_LIST_LEN;
412 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
413 del.PurgedFiles = (char *)malloc(del.max_ids);
418 Mmsg(&query, select_backup_del, ed1, ed1, cr.ClientId);
421 Mmsg(&query, select_restore_del, ed1, cr.ClientId);
424 Mmsg(&query, select_verify_del, ed1, cr.ClientId);
427 if (!db_sql_query(ua->db, query, job_delete_handler, (void *)&del)) {
428 bsendmsg(ua, "%s", db_strerror(ua->db));
432 * OK, now we have the list of JobId's to be pruned, first check
433 * if the Files have been purged, if not, purge (delete) them.
434 * Then delete the Job entry, and finally and JobMedia records.
436 for (i=0; i < del.num_ids; i++) {
437 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
438 if (!del.PurgedFiles[i]) {
439 Mmsg(&query, del_File, del.JobId[i]);
440 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
441 bsendmsg(ua, "%s", db_strerror(ua->db));
443 Dmsg1(050, "Del sql=%s\n", query);
446 Mmsg(&query, del_Job, del.JobId[i]);
447 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
448 bsendmsg(ua, "%s", db_strerror(ua->db));
450 Dmsg1(050, "Del sql=%s\n", query);
452 Mmsg(&query, del_JobMedia, del.JobId[i]);
453 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
454 bsendmsg(ua, "%s", db_strerror(ua->db));
456 Dmsg1(050, "Del sql=%s\n", query);
458 bsendmsg(ua, _("Pruned %d %s for client %s from catalog.\n"), del.num_ids,
459 del.num_ids==1?_("Job"):_("Jobs"), client->hdr.name);
462 drop_temp_tables(ua);
467 if (del.PurgedFiles) {
468 free(del.PurgedFiles);
470 free_pool_memory(query);
475 * Prune a given Volume
477 int prune_volume(UAContext *ua, MEDIA_DBR *mr)
479 char *query = (char *)get_pool_memory(PM_MESSAGE);
480 struct s_count_ctx cnt;
481 struct s_file_del_ctx del;
487 memset(&jr, 0, sizeof(jr));
488 memset(&del, 0, sizeof(del));
491 * Find out how many Jobs remain on this Volume by
492 * counting the JobMedia records.
495 Mmsg(&query, cnt_JobMedia, mr->MediaId);
496 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
497 bsendmsg(ua, "%s", db_strerror(ua->db));
498 Dmsg0(050, "Count failed\n");
502 if (cnt.count == 0) {
504 bsendmsg(ua, "There are no Jobs associated with Volume %s. Marking it purged.\n",
507 stat = mark_media_purged(ua, mr);
511 if (cnt.count < MAX_DEL_LIST_LEN) {
512 del.max_ids = cnt.count + 1;
514 del.max_ids = MAX_DEL_LIST_LEN;
518 * Now get a list of JobIds for Jobs written to this Volume
519 * Could optimize here by adding JobTDate > (now - period).
521 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
522 Mmsg(&query, sel_JobMedia, mr->MediaId);
523 if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
525 bsendmsg(ua, "%s", db_strerror(ua->db));
527 Dmsg0(050, "Count failed\n");
531 /* Use Volume Retention to prune Jobs and their Files */
532 period = mr->VolRetention;
533 now = (utime_t)time(NULL);
535 Dmsg3(200, "Now=%d period=%d now-period=%d\n", (int)now, (int)period,
538 for (i=0; i < del.num_ids; i++) {
539 jr.JobId = del.JobId[i];
540 if (!db_get_job_record(ua->jcr, ua->db, &jr)) {
543 Dmsg2(200, "Looking at %s JobTdate=%d\n", jr.Job, (int)jr.JobTDate);
544 if (jr.JobTDate >= (now - period)) {
547 Dmsg2(200, "Delete JobId=%d Job=%s\n", del.JobId[i], jr.Job);
548 Mmsg(&query, del_File, del.JobId[i]);
549 db_sql_query(ua->db, query, NULL, (void *)NULL);
550 Mmsg(&query, del_Job, del.JobId[i]);
551 db_sql_query(ua->db, query, NULL, (void *)NULL);
552 Mmsg(&query, del_JobMedia, del.JobId[i]);
553 db_sql_query(ua->db, query, NULL, (void *)NULL);
554 Dmsg1(050, "Del sql=%s\n", query);
560 if (ua->verbose && del.num_del != 0) {
561 bsendmsg(ua, _("Pruned %d %s on Volume %s from catalog.\n"), del.num_del,
562 del.num_del == 1 ? "Job" : "Jobs", mr->VolumeName);
565 /* If purged, mark it so */
566 if (del.num_ids == del.num_del) {
567 Dmsg0(200, "Volume is purged.\n");
568 stat = mark_media_purged(ua, mr);
573 free_pool_memory(query);