3 * Bacula Director -- Automatic Pruning
4 * Applies retention periods
6 * Kern Sibbald, May 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 */
38 * The pruning code was written to be referenced by the
39 * User Agent (i.e. the console), so to properly access it and
40 * to ensure that the Job gets the proper output, we create
41 * a User Agent context. This is a sort of mini-kludge.
43 void create_ua_context(JCR *jcr, UAContext *ua)
45 memset(ua, 0, sizeof(UAContext));
48 ua->cmd = get_pool_memory(PM_FNAME);
49 ua->args = get_pool_memory(PM_FNAME);
53 void free_ua_context(UAContext *ua)
56 free_pool_memory(ua->cmd);
59 free_pool_memory(ua->args);
69 * Auto Prune Jobs and Files. This is called at the end of every
70 * Job. We do not prune volumes here.
72 int do_autoprune(JCR *jcr)
78 if (!jcr->client) { /* temp -- remove me */
82 create_ua_context(jcr, &ua);
86 if (jcr->job->PruneJobs || jcr->client->AutoPrune) {
87 Jmsg(jcr, M_INFO, 0, _("Begin pruning Jobs.\n"));
88 prune_jobs(&ua, client, jcr->JobType);
94 if (jcr->job->PruneFiles || jcr->client->AutoPrune) {
95 Jmsg(jcr, M_INFO, 0, _("Begin pruning Files.\n"));
96 prune_files(&ua, client);
100 Jmsg(jcr, M_INFO, 0, _("End auto prune.\n\n"));
103 free_ua_context(&ua);
108 * Prune all volumes in current Pool. This is called from
109 * catreq.c when the Storage daemon is asking for another
110 * volume and no appendable volumes are available.
113 * number of Volumes Purged
115 int prune_volumes(JCR *jcr)
119 uint32_t *ids = NULL;
125 if (!jcr->job->PruneVolumes && !jcr->pool->AutoPrune) {
126 Dmsg0(100, "AutoPrune not set in Pool.\n");
129 memset(&mr, 0, sizeof(mr));
130 memset(&pr, 0, sizeof(pr));
131 create_ua_context(jcr, &ua);
135 /* Get the Pool Record and a list of Media Id's in the Pool */
136 pr.PoolId = jcr->PoolId;
137 if (!db_get_pool_record(jcr, jcr->db, &pr) || !db_get_media_ids(jcr, jcr->db, &num_ids, &ids)) {
138 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
142 /* Visit each Volume and Prune it */
143 for (i=0; i<num_ids; i++) {
145 if (!db_get_media_record(jcr, jcr->db, &mr)) {
146 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
149 /* Prune only Volumes from current Pool */
150 if (pr.PoolId != mr.PoolId) {
153 /* Prune only Volumes with status "Full", "Used", or "Append" */
154 if (strcmp(mr.VolStatus, "Full") == 0 ||
155 strcmp(mr.VolStatus, "Append") == 0 ||
156 strcmp(mr.VolStatus, "Used") == 0) {
157 Dmsg1(200, "Prune Volume %s\n", mr.VolumeName);
158 stat += prune_volume(&ua, &pr, &mr);
159 Dmsg1(200, "Num pruned = %d\n", stat);
165 free_ua_context(&ua);