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 /* Forward referenced functions */
36 int prune_files(UAContext *ua, CLIENT *client);
37 int prune_jobs(UAContext *ua, CLIENT *client);
38 int prune_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr);
39 static int mark_media_purged(UAContext *ua, MEDIA_DBR *mr);
42 #define MAX_DEL_LIST_LEN 1000000
45 * Select JobIds for File deletion.
47 static char *select_job =
48 "SELECT JobId from Job "
49 "WHERE JobTDate < %s "
54 * List of SQL commands terminated by NULL for deleting
55 * temporary tables and indicies
57 static char *drop_deltabs[] = {
58 "DROP TABLE DelCandidates",
63 * List of SQL commands to create temp table and indicies
65 static char *create_deltabs[] = {
66 "CREATE TABLE DelCandidates ("
67 "JobId INTEGER UNSIGNED NOT NULL, "
68 "PurgedFiles TINYINT, "
69 "FileSetId INTEGER UNSIGNED)",
70 "CREATE INDEX DelInx1 ON DelCandidates (JobId)",
75 * Fill candidates table with all Files subject to being deleted
77 static char *insert_delcand =
78 "INSERT INTO DelCandidates "
79 "SELECT JobId, PurgedFiles, FileSetId FROM Job "
80 "WHERE JobTDate < %s "
84 * Select files from the DelCandidates table that have a
85 * more recent backup -- i.e. are not the only backup.
86 * This is the list of files to delete.
88 static char *select_del =
89 "SELECT DelCandidates.JobId "
90 "FROM Job,DelCandidates "
91 "WHERE Job.JobTDate >= %s "
92 "AND Job.ClientId=%d "
94 "AND Job.JobStatus='T' "
95 "AND Job.FileSetId=DelCandidates.FileSetId";
97 /* In memory list of JobIds */
98 struct s_file_del_ctx {
100 int num_ids; /* ids stored */
101 int max_ids; /* size of array */
102 int num_del; /* number deleted */
103 int tot_ids; /* total to process */
106 struct s_job_del_ctx {
107 JobId_t *JobId; /* array of JobIds */
108 char *PurgedFiles; /* Array of PurgedFile flags */
109 int num_ids; /* ids stored */
110 int max_ids; /* size of array */
111 int num_del; /* number deleted */
112 int tot_ids; /* total to process */
121 * Called here to count entries to be deleted
123 static int count_handler(void *ctx, int num_fields, char **row)
125 struct s_count_ctx *cnt = (struct s_count_ctx *)ctx;
128 cnt->count = atoi(row[0]);
137 * Called here to count the number of Jobs to be pruned
139 static int file_count_handler(void *ctx, int num_fields, char **row)
141 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
148 * Called here to make in memory list of JobIds to be
149 * deleted and the associated PurgedFiles flag.
150 * The in memory list will then be transversed
151 * to issue the SQL DELETE commands. Note, the list
152 * is allowed to get to MAX_DEL_LIST_LEN to limit the
153 * maximum malloc'ed memory.
155 static int job_delete_handler(void *ctx, int num_fields, char **row)
157 struct s_job_del_ctx *del = (struct s_job_del_ctx *)ctx;
159 if (del->num_ids == MAX_DEL_LIST_LEN) {
162 if (del->num_ids == del->max_ids) {
163 del->max_ids = (del->max_ids * 3) / 2;
164 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) * del->max_ids);
165 del->PurgedFiles = (char *)brealloc(del->PurgedFiles, del->max_ids);
167 del->JobId[del->num_ids] = (JobId_t)strtod(row[0], NULL);
168 del->PurgedFiles[del->num_ids++] = (char)atoi(row[0]);
172 static int file_delete_handler(void *ctx, int num_fields, char **row)
174 struct s_file_del_ctx *del = (struct s_file_del_ctx *)ctx;
176 if (del->num_ids == MAX_DEL_LIST_LEN) {
179 if (del->num_ids == del->max_ids) {
180 del->max_ids = (del->max_ids * 3) / 2;
181 del->JobId = (JobId_t *)brealloc(del->JobId, sizeof(JobId_t) *
184 del->JobId[del->num_ids++] = (JobId_t)strtod(row[0], NULL);
189 * Prune records from database
191 * prune files (from) client=xxx
192 * prune jobs (from) client=xxx
195 int prunecmd(UAContext *ua, char *cmd)
201 static char *keywords[] = {
209 switch (find_arg_keyword(ua, keywords)) {
211 client = select_client_resource(ua);
212 if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
215 prune_files(ua, client);
218 client = select_client_resource(ua);
219 if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
222 prune_jobs(ua, client);
225 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
228 if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
231 prune_volume(ua, &pr, &mr);
236 switch (do_keyword_prompt(ua, _("Choose item to prune"), keywords)) {
238 client = select_client_resource(ua);
239 if (!client || !confirm_retention(ua, &client->FileRetention, "File")) {
242 prune_files(ua, client);
245 client = select_client_resource(ua);
246 if (!client || !confirm_retention(ua, &client->JobRetention, "Job")) {
249 prune_jobs(ua, client);
252 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
255 if (!confirm_retention(ua, &mr.VolRetention, "Volume")) {
258 prune_volume(ua, &pr, &mr);
265 * Prune File records from the database. For any Job which
266 * is older than the retention period, we unconditionally delete
267 * all File records for that Job. This is simple enough that no
268 * temporary tables are needed. We simply make an in memory list of
269 * the JobIds meeting the prune conditions, then delete all File records
270 * pointing to each of those JobIds.
272 int prune_files(UAContext *ua, CLIENT *client)
274 struct s_file_del_ctx del;
275 char *query = (char *)get_pool_memory(PM_MESSAGE);
279 char ed1[50], ed2[50];
281 memset(&cr, 0, sizeof(cr));
282 memset(&del, 0, sizeof(del));
283 strcpy(cr.Name, client->hdr.name);
284 if (!db_create_client_record(ua->db, &cr)) {
288 period = client->FileRetention;
289 now = (btime_t)time(NULL);
291 Mmsg(&query, select_job, edit_uint64(now - period, ed1), cr.ClientId);
293 Dmsg1(050, "select sql=%s\n", query);
295 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
297 bsendmsg(ua, "%s", db_strerror(ua->db));
299 Dmsg0(050, "Count failed\n");
303 if (del.tot_ids == 0) {
305 bsendmsg(ua, _("No Files found for client %s to prune from %s catalog.\n"),
306 client->hdr.name, client->catalog->hdr.name);
311 if (del.tot_ids < MAX_DEL_LIST_LEN) {
312 del.max_ids = del.tot_ids + 1;
314 del.max_ids = MAX_DEL_LIST_LEN;
318 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
320 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
322 for (i=0; i < del.num_ids; i++) {
323 struct s_count_ctx cnt;
324 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
325 Mmsg(&query, "SELECT count(*) FROM File WHERE JobId=%d", del.JobId[i]);
327 db_sql_query(ua->db, query, count_handler, (void *)&cnt);
328 del.tot_ids += cnt.count;
329 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
330 db_sql_query(ua->db, query, NULL, (void *)NULL);
332 * Now mark Job as having files purged. This is necessary to
333 * avoid having too many Jobs to process in future prunings. If
334 * we don't do this, the number of JobId's in our in memory list
335 * will grow very large.
337 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", del.JobId[i]);
338 db_sql_query(ua->db, query, NULL, (void *)NULL);
339 Dmsg1(050, "Del sql=%s\n", query);
341 edit_uint64_with_commas(del.tot_ids, ed1);
342 edit_uint64_with_commas(del.num_ids, ed2);
343 bsendmsg(ua, _("Pruned %s Files from %s Jobs for client %s from %s catalog.\n"),
344 ed1, ed2, client->hdr.name, client->catalog->hdr.name);
350 free_pool_memory(query);
355 static void drop_temp_tables(UAContext *ua)
358 for (i=0; drop_deltabs[i]; i++) {
359 db_sql_query(ua->db, drop_deltabs[i], NULL, (void *)NULL);
363 static int create_temp_tables(UAContext *ua)
366 /* Create temp tables and indicies */
367 for (i=0; create_deltabs[i]; i++) {
368 if (!db_sql_query(ua->db, create_deltabs[i], NULL, (void *)NULL)) {
369 bsendmsg(ua, "%s", db_strerror(ua->db));
370 Dmsg0(050, "create DelTables table failed\n");
380 * Purging Jobs is a bit more complicated than purging Files
381 * because we delete Job records only if there is a more current
382 * backup of the FileSet. Otherwise, we keep the Job record.
383 * In other words, we never delete the only Job record that
384 * contains a current backup of a FileSet. This prevents the
385 * Volume from being recycled and destroying a current backup.
387 int prune_jobs(UAContext *ua, CLIENT *client)
389 struct s_job_del_ctx del;
390 struct s_count_ctx cnt;
391 char *query = (char *)get_pool_memory(PM_MESSAGE);
397 memset(&cr, 0, sizeof(cr));
398 memset(&del, 0, sizeof(del));
399 strcpy(cr.Name, client->hdr.name);
400 if (!db_create_client_record(ua->db, &cr)) {
404 period = client->JobRetention;
405 now = (btime_t)time(NULL);
407 /* Drop any previous temporary tables still there */
408 drop_temp_tables(ua);
410 /* Create temp tables and indicies */
411 if (!create_temp_tables(ua)) {
416 * Select all files that are older than the JobRetention period
417 * and stuff them into the "DeletionCandidates" table.
419 edit_uint64(now - period, ed1);
420 Mmsg(&query, insert_delcand, ed1, cr.ClientId);
422 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
424 bsendmsg(ua, "%s", db_strerror(ua->db));
426 Dmsg0(050, "insert delcand failed\n");
430 strcpy(query, "SELECT count(*) FROM DelCandidates");
432 Dmsg1(100, "select sql=%s\n", query);
434 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
436 bsendmsg(ua, "%s", db_strerror(ua->db));
438 Dmsg0(050, "Count failed\n");
442 if (cnt.count == 0) {
444 bsendmsg(ua, _("No Jobs for client %s found to prune from %s catalog.\n"),
445 client->hdr.name, client->catalog->hdr.name);
450 if (cnt.count < MAX_DEL_LIST_LEN) {
451 del.max_ids = cnt.count + 1;
453 del.max_ids = MAX_DEL_LIST_LEN;
455 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
456 del.PurgedFiles = (char *)malloc(del.max_ids);
458 Mmsg(&query, select_del, ed1, cr.ClientId);
459 db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
462 * OK, now we have the list of JobId's to be pruned, first check
463 * if the Files have been purged, if not, purge (delete) them.
464 * Then delete the Job entry, and finally and JobMedia records.
466 for (i=0; i < del.num_ids; i++) {
467 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
468 if (!del.PurgedFiles[i]) {
469 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
470 db_sql_query(ua->db, query, NULL, (void *)NULL);
471 Dmsg1(050, "Del sql=%s\n", query);
474 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
475 db_sql_query(ua->db, query, NULL, (void *)NULL);
476 Dmsg1(050, "Del sql=%s\n", query);
478 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
479 db_sql_query(ua->db, query, NULL, (void *)NULL);
480 Dmsg1(050, "Del sql=%s\n", query);
482 bsendmsg(ua, _("Pruned %d Jobs for client %s from %s catalog.\n"), del.num_ids,
483 client->hdr.name, client->catalog->hdr.name);
486 drop_temp_tables(ua);
490 if (del.PurgedFiles) {
491 free(del.PurgedFiles);
493 free_pool_memory(query);
500 int prune_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
502 char *query = (char *)get_pool_memory(PM_MESSAGE);
503 struct s_count_ctx cnt;
504 struct s_file_del_ctx del;
509 memset(&jr, 0, sizeof(jr));
510 memset(&del, 0, sizeof(del));
512 Mmsg(&query, "SELECT count(*) FROM JobMedia WHERE MediaId=%d", mr->MediaId);
513 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
514 bsendmsg(ua, "%s", db_strerror(ua->db));
515 Dmsg0(050, "Count failed\n");
519 if (cnt.count == 0) {
521 bsendmsg(ua, "There are no Jobs associated with Volume %s. It is purged.\n",
524 if (!mark_media_purged(ua, mr)) {
530 if (cnt.count < MAX_DEL_LIST_LEN) {
531 del.max_ids = cnt.count + 1;
533 del.max_ids = MAX_DEL_LIST_LEN;
536 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
538 Mmsg(&query, "SELECT JobId FROM JobMedia WHERE MediaId=%d", mr->MediaId);
539 if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
541 bsendmsg(ua, "%s", db_strerror(ua->db));
543 Dmsg0(050, "Count failed\n");
547 /* Use Volume Retention to purge Jobs and Files */
548 period = mr->VolRetention;
549 now = (btime_t)time(NULL);
551 for (i=0; i < del.num_ids; i++) {
552 jr.JobId = del.JobId[i];
553 if (!db_get_job_record(ua->db, &jr)) {
556 if (jr.JobTDate >= (now - period)) {
559 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
560 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
561 db_sql_query(ua->db, query, NULL, (void *)NULL);
562 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
563 db_sql_query(ua->db, query, NULL, (void *)NULL);
564 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
565 db_sql_query(ua->db, query, NULL, (void *)NULL);
566 Dmsg1(050, "Del sql=%s\n", query);
572 bsendmsg(ua, _("Pruned %d Jobs on Volume %s from catalog.\n"), del.num_del,
575 /* If purged, mark it so */
576 if (del.num_ids == del.num_del) {
577 mark_media_purged(ua, mr);
581 free_pool_memory(query);
585 static int mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
587 if (strcmp(mr->VolStatus, "Append") == 0 ||
588 strcmp(mr->VolStatus, "Full") == 0) {
589 strcpy(mr->VolStatus, "Purged");
590 if (!db_update_media_record(ua->db, mr)) {
592 bsendmsg(ua, "%s", db_strerror(ua->db));