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 * This routine assumes you want the pruning to be done. All checking
273 * must be done before calling this routine.
275 int prune_files(UAContext *ua, CLIENT *client)
277 struct s_file_del_ctx del;
278 POOLMEM *query = get_pool_memory(PM_MESSAGE);
282 char ed1[50], ed2[50];
285 memset(&cr, 0, sizeof(cr));
286 memset(&del, 0, sizeof(del));
287 strcpy(cr.Name, client->hdr.name);
288 if (!db_create_client_record(ua->db, &cr)) {
293 period = client->FileRetention;
294 now = (btime_t)time(NULL);
296 Mmsg(&query, select_job, edit_uint64(now - period, ed1), cr.ClientId);
298 Dmsg1(050, "select sql=%s\n", query);
300 if (!db_sql_query(ua->db, query, file_count_handler, (void *)&del)) {
302 bsendmsg(ua, "%s", db_strerror(ua->db));
304 Dmsg0(050, "Count failed\n");
308 if (del.tot_ids == 0) {
310 bsendmsg(ua, _("No Files found for client %s to prune from %s catalog.\n"),
311 client->hdr.name, client->catalog->hdr.name);
316 if (del.tot_ids < MAX_DEL_LIST_LEN) {
317 del.max_ids = del.tot_ids + 1;
319 del.max_ids = MAX_DEL_LIST_LEN;
323 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
325 db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
327 for (i=0; i < del.num_ids; i++) {
328 struct s_count_ctx cnt;
329 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
330 Mmsg(&query, "SELECT count(*) FROM File WHERE JobId=%d", del.JobId[i]);
332 db_sql_query(ua->db, query, count_handler, (void *)&cnt);
333 del.tot_ids += cnt.count;
334 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
335 db_sql_query(ua->db, query, NULL, (void *)NULL);
337 * Now mark Job as having files purged. This is necessary to
338 * avoid having too many Jobs to process in future prunings. If
339 * we don't do this, the number of JobId's in our in memory list
340 * will grow very large.
342 Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", del.JobId[i]);
343 db_sql_query(ua->db, query, NULL, (void *)NULL);
344 Dmsg1(050, "Del sql=%s\n", query);
346 edit_uint64_with_commas(del.tot_ids, ed1);
347 edit_uint64_with_commas(del.num_ids, ed2);
348 bsendmsg(ua, _("Pruned %s Files from %s Jobs for client %s from %s catalog.\n"),
349 ed1, ed2, client->hdr.name, client->catalog->hdr.name);
356 free_pool_memory(query);
361 static void drop_temp_tables(UAContext *ua)
364 for (i=0; drop_deltabs[i]; i++) {
365 db_sql_query(ua->db, drop_deltabs[i], NULL, (void *)NULL);
369 static int create_temp_tables(UAContext *ua)
372 /* Create temp tables and indicies */
373 for (i=0; create_deltabs[i]; i++) {
374 if (!db_sql_query(ua->db, create_deltabs[i], NULL, (void *)NULL)) {
375 bsendmsg(ua, "%s", db_strerror(ua->db));
376 Dmsg0(050, "create DelTables table failed\n");
386 * Purging Jobs is a bit more complicated than purging Files
387 * because we delete Job records only if there is a more current
388 * backup of the FileSet. Otherwise, we keep the Job record.
389 * In other words, we never delete the only Job record that
390 * contains a current backup of a FileSet. This prevents the
391 * Volume from being recycled and destroying a current backup.
393 int prune_jobs(UAContext *ua, CLIENT *client)
395 struct s_job_del_ctx del;
396 struct s_count_ctx cnt;
397 char *query = (char *)get_pool_memory(PM_MESSAGE);
404 memset(&cr, 0, sizeof(cr));
405 memset(&del, 0, sizeof(del));
406 strcpy(cr.Name, client->hdr.name);
407 if (!db_create_client_record(ua->db, &cr)) {
412 period = client->JobRetention;
413 now = (btime_t)time(NULL);
415 /* Drop any previous temporary tables still there */
416 drop_temp_tables(ua);
418 /* Create temp tables and indicies */
419 if (!create_temp_tables(ua)) {
424 * Select all files that are older than the JobRetention period
425 * and stuff them into the "DeletionCandidates" table.
427 edit_uint64(now - period, ed1);
428 Mmsg(&query, insert_delcand, ed1, cr.ClientId);
430 if (!db_sql_query(ua->db, query, NULL, (void *)NULL)) {
432 bsendmsg(ua, "%s", db_strerror(ua->db));
434 Dmsg0(050, "insert delcand failed\n");
438 strcpy(query, "SELECT count(*) FROM DelCandidates");
440 Dmsg1(100, "select sql=%s\n", query);
442 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
444 bsendmsg(ua, "%s", db_strerror(ua->db));
446 Dmsg0(050, "Count failed\n");
450 if (cnt.count == 0) {
452 bsendmsg(ua, _("No Jobs found for client %s to prune from %s catalog.\n"),
453 client->hdr.name, client->catalog->hdr.name);
458 if (cnt.count < MAX_DEL_LIST_LEN) {
459 del.max_ids = cnt.count + 1;
461 del.max_ids = MAX_DEL_LIST_LEN;
463 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
464 del.PurgedFiles = (char *)malloc(del.max_ids);
466 Mmsg(&query, select_del, ed1, cr.ClientId);
467 db_sql_query(ua->db, query, job_delete_handler, (void *)&del);
470 * OK, now we have the list of JobId's to be pruned, first check
471 * if the Files have been purged, if not, purge (delete) them.
472 * Then delete the Job entry, and finally and JobMedia records.
474 for (i=0; i < del.num_ids; i++) {
475 Dmsg1(050, "Delete JobId=%d\n", del.JobId[i]);
476 if (!del.PurgedFiles[i]) {
477 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
478 db_sql_query(ua->db, query, NULL, (void *)NULL);
479 Dmsg1(050, "Del sql=%s\n", query);
482 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
483 db_sql_query(ua->db, query, NULL, (void *)NULL);
484 Dmsg1(050, "Del sql=%s\n", query);
486 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
487 db_sql_query(ua->db, query, NULL, (void *)NULL);
488 Dmsg1(050, "Del sql=%s\n", query);
490 bsendmsg(ua, _("Pruned %d %s for client %s from %s catalog.\n"), del.num_ids,
491 del.num_ids==1?_("Job"):_("Jobs"), client->hdr.name, client->catalog->hdr.name);
494 drop_temp_tables(ua);
499 if (del.PurgedFiles) {
500 free(del.PurgedFiles);
502 free_pool_memory(query);
507 * Prune a given Volume
509 int prune_volume(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
511 char *query = (char *)get_pool_memory(PM_MESSAGE);
512 struct s_count_ctx cnt;
513 struct s_file_del_ctx del;
519 memset(&jr, 0, sizeof(jr));
520 memset(&del, 0, sizeof(del));
522 Mmsg(&query, "SELECT count(*) FROM JobMedia WHERE MediaId=%d", mr->MediaId);
523 if (!db_sql_query(ua->db, query, count_handler, (void *)&cnt)) {
524 bsendmsg(ua, "%s", db_strerror(ua->db));
525 Dmsg0(050, "Count failed\n");
529 if (cnt.count == 0) {
531 bsendmsg(ua, "There are no Jobs associated with Volume %s. It is purged.\n",
534 stat = mark_media_purged(ua, mr);
538 if (cnt.count < MAX_DEL_LIST_LEN) {
539 del.max_ids = cnt.count + 1;
541 del.max_ids = MAX_DEL_LIST_LEN;
544 del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
546 /* ***FIXME*** could make this do JobTDate check too */
547 Mmsg(&query, "SELECT JobId FROM JobMedia WHERE MediaId=%d", mr->MediaId);
548 if (!db_sql_query(ua->db, query, file_delete_handler, (void *)&del)) {
550 bsendmsg(ua, "%s", db_strerror(ua->db));
552 Dmsg0(050, "Count failed\n");
556 /* Use Volume Retention to prune Jobs and Files */
557 period = mr->VolRetention;
558 now = (btime_t)time(NULL);
560 Dmsg3(200, "Now=%d period=%d now-period=%d\n", (int)now, (int)period,
562 for (i=0; i < del.num_ids; i++) {
563 jr.JobId = del.JobId[i];
564 if (!db_get_job_record(ua->db, &jr)) {
567 Dmsg2(200, "Looking at %s JobTdate=%d\n", jr.Job, (int)jr.JobTDate);
568 if (jr.JobTDate >= (now - period)) {
571 Dmsg2(200, "Delete JobId=%d Job=%s\n", del.JobId[i], jr.Job);
572 Mmsg(&query, "DELETE FROM File WHERE JobId=%d", del.JobId[i]);
573 db_sql_query(ua->db, query, NULL, (void *)NULL);
574 Mmsg(&query, "DELETE FROM Job WHERE JobId=%d", del.JobId[i]);
575 db_sql_query(ua->db, query, NULL, (void *)NULL);
576 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%d", del.JobId[i]);
577 db_sql_query(ua->db, query, NULL, (void *)NULL);
578 Dmsg1(050, "Del sql=%s\n", query);
584 if (ua->verbose && del.num_del != 0) {
585 bsendmsg(ua, _("Pruned %d Jobs on Volume %s from catalog.\n"), del.num_del,
589 /* If purged, mark it so */
590 if (del.num_ids == del.num_del) {
591 Dmsg0(200, "Volume is purged.\n");
592 stat = mark_media_purged(ua, mr);
597 free_pool_memory(query);
601 static int mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
603 if (strcmp(mr->VolStatus, "Append") == 0 ||
604 strcmp(mr->VolStatus, "Full") == 0) {
605 strcpy(mr->VolStatus, "Purged");
606 if (!db_update_media_record(ua->db, mr)) {
608 bsendmsg(ua, "%s", db_strerror(ua->db));
614 return strcpy(mr->VolStatus, "Purged") == 0;