3 * Bacula Director -- User Agent Database prune Command
4 * Applies retention periods
6 * Kern Sibbald, February MMII
12 Copyright (C) 2002-2005 Kern Sibbald
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 *select_admin_del;
51 extern char *cnt_File;
52 extern char *del_File;
53 extern char *upd_Purged;
54 extern char *cnt_DelCand;
56 extern char *del_JobMedia;
57 extern char *cnt_JobMedia;
58 extern char *sel_JobMedia;
61 /* In memory list of JobIds */
62 struct s_file_del_ctx {
64 int num_ids; /* ids stored */
65 int max_ids; /* size of array */
66 int num_del; /* number deleted */
67 int tot_ids; /* total to process */
70 struct s_job_del_ctx {
71 JobId_t *JobId; /* array of JobIds */
72 char *PurgedFiles; /* Array of PurgedFile flags */
73 int num_ids; /* ids stored */
74 int max_ids; /* size of array */
75 int num_del; /* number deleted */
76 int tot_ids; /* total to process */
85 * Called here to count entries to be deleted
87 static int count_handler(void *ctx, int num_fields, char **row)
89 struct s_count_ctx *cnt = (struct s_count_ctx *)ctx;
92 cnt->count = str_to_int64(row[0]);
101 * Called here to count the number of Jobs to be pruned
103 static int file_count_handler(void *ctx, int num_fields, char **row)
105 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
112 * Called here to make in memory list of JobIds to be
113 * deleted and the associated PurgedFiles flag.
114 * The in memory list will then be transversed
115 * to issue the SQL DELETE commands. Note, the list
116 * is allowed to get to MAX_DEL_LIST_LEN to limit the
117 * maximum malloc'ed memory.
119 static int job_delete_handler(void *ctx, int num_fields, char **row)
121 struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
123 if (del->num_ids == MAX_DEL_LIST_LEN) {
126 if (del->num_ids == del->max_ids) {
127 del->max_ids = (del->max_ids * 3) / 2;
128 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) * del->max_ids);
129 del->PurgedFiles = (char *)brealloc(del->PurgedFiles, del->max_ids);
131 del->JobId[del->num_ids] = (JobId_t)str_to_int64(row[0]);
132 del->PurgedFiles[del->num_ids++] = (char)str_to_int64(row[0]);
136 static int file_delete_handler(void *ctx, int num_fields, char **row)
138 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
140 if (del->num_ids == MAX_DEL_LIST_LEN) {
143 if (del->num_ids == del->max_ids) {
144 del->max_ids = (del->max_ids * 3) / 2;
145 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) *
148 del->JobId[del->num_ids++] = (JobId_t)str_to_int64(row[0]);
153 * Prune records from database
155 * prune files (from) client=xxx
156 * prune jobs (from) client=xxx
159 int prunecmd(UAContext *ua, const char *cmd)
166 static const char *keywords[] = {
176 /* First search args */
177 kw = find_arg_keyword(ua, keywords);
178 if (kw < 0 || kw > 2) {
179 /* no args, so ask user */
180 kw = do_keyword_prompt(ua, _("Choose item to prune"), keywords);
184 case 0: /* prune files */
185 client = get_client_resource(ua);
186 if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
189 prune_files(ua, client);
191 case 1: /* prune jobs */
192 client = get_client_resource(ua);
193 if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
196 /* ****FIXME**** allow user to select JobType */
197 prune_jobs(ua, client, JT_BACKUP);
199 case 2: /* prune volume */
200 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
203 if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
206 prune_volume(ua, &mr);
216 * Prune File records from the database. For any Job which
217 * is older than the retention period, we unconditionally delete
218 * all File records for that Job. This is simple enough that no
219 * temporary tables are needed. We simply make an in memory list of
220 * the JobIds meeting the prune conditions, then delete all File records
221 * pointing to each of those JobIds.
223 * This routine assumes you want the pruning to be done. All checking
224 * must be done before calling this routine.
226 int prune_files(UAContext *ua, CLIENT *client)
228 struct s_file_del_ctx del;
229 POOLMEM *query = get_pool_memory(PM_MESSAGE);
233 char ed1[50], ed2[50];
236 memset(&cr, 0, sizeof(cr));
237 memset(&del, 0, sizeof(del));
238 bstrncpy(cr.Name, client->hdr.name, sizeof(cr.Name));
239 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
244 period = client->FileRetention;
245 now = (utime_t)time(NULL);
247 /* Select Jobs -- for counting */
248 Mmsg(query, select_job, edit_uint64(now - period, ed1), cr.ClientId);
249 Dmsg1(050, "select sql=%s\n", query);
250 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
252 bsendmsg(ua, "%s", db_strerror(ua->db));
254 Dmsg0(050, "Count failed\n");
258 if (del.tot_ids == 0) {
260 bsendmsg(ua, _("No Files found to prune.\n"));
265 if (del.tot_ids < MAX_DEL_LIST_LEN) {
266 del.max_ids = del.tot_ids + 1;
268 del.max_ids = MAX_DEL_LIST_LEN;
272 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
274 /* Now process same set but making a delete list */
275 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
277 for (i=0; i < del.num_ids; i++) {
278 struct s_count_ctx cnt;
279 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
280 Mmsg(query, cnt_File, del.JobId[i]);
282 db_sql_query(ua->db, query, count_handler, (void *)&cnt);
283 del.tot_ids += cnt.count;
284 Mmsg(query, del_File, del.JobId[i]);
285 db_sql_query(ua->db, query, NULL, (void *)NULL);
287 * Now mark Job as having files purged. This is necessary to
288 * avoid having too many Jobs to process in future prunings. If
289 * we don't do this, the number of JobId's in our in memory list
290 * could grow very large.
292 Mmsg(query, upd_Purged, del.JobId[i]);
293 db_sql_query(ua->db, query, NULL, (void *)NULL);
294 Dmsg1(050, "Del sql=%s\n", query);
296 edit_uint64_with_commas(del.tot_ids, ed1);
297 edit_uint64_with_commas(del.num_ids, ed2);
298 bsendmsg(ua, _("Pruned %s Files from %s Jobs for client %s from catalog.\n"),
299 ed1, ed2, client->hdr.name);
306 free_pool_memory(query);
311 static void drop_temp_tables(UAContext *ua)
314 for (i=0; drop_deltabs[i]; i++) {
315 db_sql_query(ua->db, drop_deltabs[i], NULL, (void *)NULL);
319 static int create_temp_tables(UAContext *ua)
322 /* Create temp tables and indicies */
323 for (i=0; create_deltabs[i]; i++) {
324 if (!db_sql_query(ua->db, create_deltabs[i], NULL, (void *)NULL)) {
325 bsendmsg(ua, "%s", db_strerror(ua->db));
326 Dmsg0(050, "create DelTables table failed\n");
336 * Purging Jobs is a bit more complicated than purging Files
337 * because we delete Job records only if there is a more current
338 * backup of the FileSet. Otherwise, we keep the Job record.
339 * In other words, we never delete the only Job record that
340 * contains a current backup of a FileSet. This prevents the
341 * Volume from being recycled and destroying a current backup.
343 * For Verify Jobs, we do not delete the last InitCatalog.
345 * For Restore Jobs there are no restrictions.
347 int prune_jobs(UAContext *ua, CLIENT *client, int JobType)
349 struct s_job_del_ctx del;
350 struct s_count_ctx cnt;
351 POOLMEM *query = (char *)get_pool_memory(PM_MESSAGE);
358 memset(&cr, 0, sizeof(cr));
359 memset(&del, 0, sizeof(del));
360 bstrncpy(cr.Name, client->hdr.name, sizeof(cr.Name));
361 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
366 period = client->JobRetention;
367 now = (utime_t)time(NULL);
369 /* Drop any previous temporary tables still there */
370 drop_temp_tables(ua);
372 /* Create temp tables and indicies */
373 if (!create_temp_tables(ua)) {
378 * Select all files that are older than the JobRetention period
379 * and stuff them into the "DeletionCandidates" table.
381 edit_uint64(now - period, ed1);
382 Mmsg(query, insert_delcand, (char)JobType, ed1, cr.ClientId);
383 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
385 bsendmsg(ua, "%s", db_strerror(ua->db));
387 Dmsg0(050, "insert delcand failed\n");
391 /* Count Files to be deleted */
392 pm_strcpy(query, cnt_DelCand);
393 Dmsg1(100, "select sql=%s\n", query);
395 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
396 bsendmsg(ua, "%s", db_strerror(ua->db));
397 Dmsg0(050, "Count failed\n");
401 if (cnt.count == 0) {
403 bsendmsg(ua, _("No Jobs found to prune.\n"));
408 if (cnt.count < MAX_DEL_LIST_LEN) {
409 del.max_ids = cnt.count + 1;
411 del.max_ids = MAX_DEL_LIST_LEN;
413 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
414 del.PurgedFiles = (char *)malloc(del.max_ids);
418 Mmsg(query, select_backup_del, ed1, ed1, cr.ClientId);
421 Mmsg(query, select_restore_del, ed1, ed1, cr.ClientId);
424 Mmsg(query, select_verify_del, ed1, ed1, cr.ClientId);
427 Mmsg(query, select_admin_del, ed1, ed1, cr.ClientId);
430 if (!db_sql_query(ua->db, query, job_delete_handler, (void *)&del)) {
431 bsendmsg(ua, "%s", db_strerror(ua->db));
435 * OK, now we have the list of JobId's to be pruned, first check
436 * if the Files have been purged, if not, purge (delete) them.
437 * Then delete the Job entry, and finally and JobMedia records.
439 for (i=0; i < del.num_ids; i++) {
440 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
441 if (!del.PurgedFiles[i]) {
442 Mmsg(query, del_File, del.JobId[i]);
443 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
444 bsendmsg(ua, "%s", db_strerror(ua->db));
446 Dmsg1(050, "Del sql=%s\n", query);
449 Mmsg(query, del_Job, del.JobId[i]);
450 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
451 bsendmsg(ua, "%s", db_strerror(ua->db));
453 Dmsg1(050, "Del sql=%s\n", query);
455 Mmsg(query, del_JobMedia, del.JobId[i]);
456 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
457 bsendmsg(ua, "%s", db_strerror(ua->db));
459 Dmsg1(050, "Del sql=%s\n", query);
461 bsendmsg(ua, _("Pruned %d %s for client %s from catalog.\n"), del.num_ids,
462 del.num_ids==1?_("Job"):_("Jobs"), client->hdr.name);
465 drop_temp_tables(ua);
470 if (del.PurgedFiles) {
471 free(del.PurgedFiles);
473 free_pool_memory(query);
478 * Prune a given Volume
480 int prune_volume(UAContext *ua, MEDIA_DBR *mr)
482 POOLMEM *query = (char *)get_pool_memory(PM_MESSAGE);
483 struct s_count_ctx cnt;
484 struct s_file_del_ctx del;
490 memset(&jr, 0, sizeof(jr));
491 memset(&del, 0, sizeof(del));
494 * Find out how many Jobs remain on this Volume by
495 * counting the JobMedia records.
498 Mmsg(query, cnt_JobMedia, mr->MediaId);
499 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
500 bsendmsg(ua, "%s", db_strerror(ua->db));
501 Dmsg0(050, "Count failed\n");
505 if (cnt.count == 0) {
506 if (strcmp(mr->VolStatus, "Purged") != 0 && verbose) {
507 bsendmsg(ua, "There are no Jobs associated with Volume \"%s\". Marking it purged.\n",
510 stat = mark_media_purged(ua, mr);
514 if (cnt.count < MAX_DEL_LIST_LEN) {
515 del.max_ids = cnt.count + 1;
517 del.max_ids = MAX_DEL_LIST_LEN;
521 * Now get a list of JobIds for Jobs written to this Volume
522 * Could optimize here by adding JobTDate > (now - period).
524 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
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 their Files */
535 period = mr->VolRetention;
536 now = (utime_t)time(NULL);
538 Dmsg3(200, "Now=%d period=%d now-period=%d\n", (int)now, (int)period,
541 for (i=0; i < del.num_ids; i++) {
542 jr.JobId = del.JobId[i];
543 if (!db_get_job_record(ua->jcr, ua->db, &jr)) {
546 Dmsg2(200, "Looking at %s JobTdate=%d\n", jr.Job, (int)jr.JobTDate);
547 if (jr.JobTDate >= (now - period)) {
550 Dmsg2(200, "Delete JobId=%d Job=%s\n", del.JobId[i], jr.Job);
551 Mmsg(query, del_File, del.JobId[i]);
552 db_sql_query(ua->db, query, NULL, (void *)NULL);
553 Mmsg(query, del_Job, del.JobId[i]);
554 db_sql_query(ua->db, query, NULL, (void *)NULL);
555 Mmsg(query, del_JobMedia, del.JobId[i]);
556 db_sql_query(ua->db, query, NULL, (void *)NULL);
557 Dmsg1(050, "Del sql=%s\n", query);
563 if (ua->verbose && del.num_del != 0) {
564 bsendmsg(ua, _("Pruned %d %s on Volume \"%s\" from catalog.\n"), del.num_del,
565 del.num_del == 1 ? "Job" : "Jobs", mr->VolumeName);
568 /* If purged, mark it so */
569 if (del.num_ids == del.num_del) {
570 Dmsg0(200, "Volume is purged.\n");
571 stat = mark_media_purged(ua, mr);
576 free_pool_memory(query);