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)
168 static char *keywords[] = {
178 /* First search args */
179 kw = find_arg_keyword(ua, keywords);
180 if (kw < 0 || kw > 2) {
181 /* no args, so ask user */
182 kw = do_keyword_prompt(ua, _("Choose item to prune"), keywords);
186 case 0: /* prune files */
187 client = get_client_resource(ua);
188 if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
191 prune_files(ua, client);
193 case 1: /* prune jobs */
194 client = get_client_resource(ua);
195 if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
198 /* ****FIXME**** allow user to select JobType */
199 prune_jobs(ua, client, JT_BACKUP);
201 case 2: /* prune volume */
202 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
205 if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
208 prune_volume(ua, &pr, &mr);
218 * Prune File records from the database. For any Job which
219 * is older than the retention period, we unconditionally delete
220 * all File records for that Job. This is simple enough that no
221 * temporary tables are needed. We simply make an in memory list of
222 * the JobIds meeting the prune conditions, then delete all File records
223 * pointing to each of those JobIds.
225 * This routine assumes you want the pruning to be done. All checking
226 * must be done before calling this routine.
228 int prune_files(UAContext *ua, CLIENT *client)
230 struct s_file_del_ctx del;
231 POOLMEM *query = get_pool_memory(PM_MESSAGE);
235 char ed1[50], ed2[50];
238 memset(&cr, 0, sizeof(cr));
239 memset(&del, 0, sizeof(del));
240 bstrncpy(cr.Name, client->hdr.name, sizeof(cr.Name));
241 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
246 period = client->FileRetention;
247 now = (utime_t)time(NULL);
249 /* Select Jobs -- for counting */
250 Mmsg(&query, select_job, edit_uint64(now - period, ed1), cr.ClientId);
251 Dmsg1(050, "select sql=%s\n", query);
252 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
254 bsendmsg(ua, "%s", db_strerror(ua->db));
256 Dmsg0(050, "Count failed\n");
260 if (del.tot_ids == 0) {
262 bsendmsg(ua, _("No Files found to prune.\n"));
267 if (del.tot_ids < MAX_DEL_LIST_LEN) {
268 del.max_ids = del.tot_ids + 1;
270 del.max_ids = MAX_DEL_LIST_LEN;
274 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
276 /* Now process same set but making delete list */
277 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
279 for (i=0; i < del.num_ids; i++) {
280 struct s_count_ctx cnt;
281 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
282 Mmsg(&query, cnt_File, del.JobId[i]);
284 db_sql_query(ua->db, query, count_handler, (void *)&cnt);
285 del.tot_ids += cnt.count;
286 Mmsg(&query, del_File, del.JobId[i]);
287 db_sql_query(ua->db, query, NULL, (void *)NULL);
289 * Now mark Job as having files purged. This is necessary to
290 * avoid having too many Jobs to process in future prunings. If
291 * we don't do this, the number of JobId's in our in memory list
292 * will grow very large.
294 Mmsg(&query, upd_Purged, del.JobId[i]);
295 db_sql_query(ua->db, query, NULL, (void *)NULL);
296 Dmsg1(050, "Del sql=%s\n", query);
298 edit_uint64_with_commas(del.tot_ids, ed1);
299 edit_uint64_with_commas(del.num_ids, ed2);
300 bsendmsg(ua, _("Pruned %s Files from %s Jobs for client %s from catalog.\n"),
301 ed1, ed2, client->hdr.name);
308 free_pool_memory(query);
313 static void drop_temp_tables(UAContext *ua)
316 for (i=0; drop_deltabs[i]; i++) {
317 db_sql_query(ua->db, drop_deltabs[i], NULL, (void *)NULL);
321 static int create_temp_tables(UAContext *ua)
324 /* Create temp tables and indicies */
325 for (i=0; create_deltabs[i]; i++) {
326 if (!db_sql_query(ua->db, create_deltabs[i], NULL, (void *)NULL)) {
327 bsendmsg(ua, "%s", db_strerror(ua->db));
328 Dmsg0(050, "create DelTables table failed\n");
338 * Purging Jobs is a bit more complicated than purging Files
339 * because we delete Job records only if there is a more current
340 * backup of the FileSet. Otherwise, we keep the Job record.
341 * In other words, we never delete the only Job record that
342 * contains a current backup of a FileSet. This prevents the
343 * Volume from being recycled and destroying a current backup.
345 * For Verify Jobs, we do not delete the last InitCatalog.
347 * For Restore Jobs there are no restrictions.
349 int prune_jobs(UAContext *ua, CLIENT *client, int JobType)
351 struct s_job_del_ctx del;
352 struct s_count_ctx cnt;
353 char *query = (char *)get_pool_memory(PM_MESSAGE);
360 memset(&cr, 0, sizeof(cr));
361 memset(&del, 0, sizeof(del));
362 bstrncpy(cr.Name, client->hdr.name, sizeof(cr.Name));
363 if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
368 period = client->JobRetention;
369 now = (utime_t)time(NULL);
371 /* Drop any previous temporary tables still there */
372 drop_temp_tables(ua);
374 /* Create temp tables and indicies */
375 if (!create_temp_tables(ua)) {
380 * Select all files that are older than the JobRetention period
381 * and stuff them into the "DeletionCandidates" table.
383 edit_uint64(now - period, ed1);
384 Mmsg(&query, insert_delcand, (char)JobType, ed1, cr.ClientId);
385 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
387 bsendmsg(ua, "%s", db_strerror(ua->db));
389 Dmsg0(050, "insert delcand failed\n");
393 /* Count Files to be deleted */
394 pm_strcpy(&query, cnt_DelCand);
395 Dmsg1(100, "select sql=%s\n", query);
397 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
398 bsendmsg(ua, "%s", db_strerror(ua->db));
399 Dmsg0(050, "Count failed\n");
403 if (cnt.count == 0) {
405 bsendmsg(ua, _("No Jobs found to prune.\n"));
410 if (cnt.count < MAX_DEL_LIST_LEN) {
411 del.max_ids = cnt.count + 1;
413 del.max_ids = MAX_DEL_LIST_LEN;
415 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
416 del.PurgedFiles = (char *)malloc(del.max_ids);
421 Mmsg(&query, select_backup_del, ed1, cr.ClientId);
424 Mmsg(&query, select_restore_del, ed1, cr.ClientId);
427 Mmsg(&query, select_verify_del, 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, POOL_DBR *pr, MEDIA_DBR *mr)
482 char *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) {
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);