]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/autoprune.c
Add var command
[bacula/bacula] / bacula / src / dird / autoprune.c
1 /*
2  *
3  *   Bacula Director -- Automatic Pruning 
4  *      Applies retention periods
5  *
6  *     Kern Sibbald, May MMII
7  *
8  *   Version $Id$
9  */
10
11 /*
12    Copyright (C) 2002 Kern Sibbald and John Walker
13
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.
18
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.
23
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,
27    MA 02111-1307, USA.
28
29  */
30
31 #include "bacula.h"
32 #include "dird.h"
33 #include "ua.h"
34
35 /* Forward referenced functions */
36
37
38 /*
39  * Auto Prune Jobs and Files. This is called at the end of every
40  *   Job.  We do not prune volumes here.
41  */
42 int do_autoprune(JCR *jcr)
43 {
44    UAContext *ua;
45    CLIENT *client;
46    bool pruned;
47
48    if (!jcr->client) {                /* temp -- remove me */
49       return 1;
50    }
51
52    ua = new_ua_context(jcr);
53
54    client = jcr->client;
55
56    if (jcr->job->PruneJobs || jcr->client->AutoPrune) {
57       Jmsg(jcr, M_INFO, 0, _("Begin pruning Jobs.\n"));
58       prune_jobs(ua, client, jcr->JobType);
59       pruned = true;
60    } else {
61       pruned = false;
62    }
63   
64    if (jcr->job->PruneFiles || jcr->client->AutoPrune) {
65       Jmsg(jcr, M_INFO, 0, _("Begin pruning Files.\n"));
66       prune_files(ua, client);
67       pruned = true;
68    }
69    if (pruned) {
70       Jmsg(jcr, M_INFO, 0, _("End auto prune.\n\n"));
71    }
72
73    free_ua_context(ua);
74    return 1;    
75 }
76
77 /*
78  * Prune all volumes in current Pool. This is called from
79  *   catreq.c when the Storage daemon is asking for another
80  *   volume and no appendable volumes are available.
81  *
82  *  Return 0: on error
83  *         number of Volumes Purged
84  */
85 int prune_volumes(JCR *jcr)
86 {
87    int stat = 0;
88    int i;
89    uint32_t *ids = NULL;
90    int num_ids = 0;
91    MEDIA_DBR mr;
92    POOL_DBR pr;
93    UAContext *ua;
94
95    if (!jcr->job->PruneVolumes && !jcr->pool->AutoPrune) {
96       Dmsg0(100, "AutoPrune not set in Pool.\n");
97       return 0;
98    }
99    memset(&mr, 0, sizeof(mr));
100    memset(&pr, 0, sizeof(pr));
101    ua = new_ua_context(jcr);
102
103    db_lock(jcr->db);
104
105    /* Get the Pool Record and a list of Media Id's in the Pool */
106    pr.PoolId = jcr->PoolId;
107    if (!db_get_pool_record(jcr, jcr->db, &pr) || !db_get_media_ids(jcr, jcr->db, &num_ids, &ids)) {
108       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
109       goto bail_out;
110    }
111
112    /* Visit each Volume and Prune it */
113    for (i=0; i<num_ids; i++) {
114       mr.MediaId = ids[i];
115       if (!db_get_media_record(jcr, jcr->db, &mr)) {
116          Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
117          continue;
118       }
119       /* Prune only Volumes from current Pool */
120       if (pr.PoolId != mr.PoolId) {
121          continue;
122       }
123       /* Prune only Volumes with status "Full", "Used", or "Append" */
124       if (strcmp(mr.VolStatus, "Full")   == 0 || 
125           strcmp(mr.VolStatus, "Append") == 0 ||
126           strcmp(mr.VolStatus, "Used")   == 0) {
127          Dmsg1(200, "Prune Volume %s\n", mr.VolumeName);
128          stat += prune_volume(ua, &pr, &mr); 
129          Dmsg1(200, "Num pruned = %d\n", stat);
130       }
131    }   
132
133 bail_out:
134    db_unlock(jcr->db);
135    free_ua_context(ua);
136    if (ids) {
137       free(ids);
138    }
139    return stat;
140 }