]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/autoprune.c
Last major change for 1.27 code -- see kes13Nov02
[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 void create_ua_context(JCR *jcr, UAContext *ua)
38 {
39    memset(ua, 0, sizeof(UAContext));
40    ua->jcr = jcr;
41    ua->db = jcr->db;
42    ua->cmd = get_pool_memory(PM_FNAME);
43    ua->args = get_pool_memory(PM_FNAME);
44    ua->verbose = 1;
45 }
46
47 void free_ua_context(UAContext *ua)
48 {
49    if (ua->cmd) {
50       free_pool_memory(ua->cmd);
51    }
52    if (ua->args) {
53       free_pool_memory(ua->args);
54    }
55 }
56
57 /*
58  * Auto Prune Jobs and Files
59  *   Volumes are done separately
60  */
61 int do_autoprune(JCR *jcr)
62 {
63    UAContext ua;
64    CLIENT *client;
65    int pruned;
66
67    if (!jcr->client) {                /* temp -- remove me */
68       return 1;
69    }
70
71    create_ua_context(jcr, &ua);
72
73    client = jcr->client;
74
75    if (jcr->job->PruneJobs || jcr->client->AutoPrune) {
76       Jmsg(jcr, M_INFO, 0, _("Begin pruning Jobs.\n"));
77       prune_jobs(&ua, client, jcr->JobType);
78       pruned = TRUE;
79    } else {
80       pruned = FALSE;
81    }
82   
83    if (jcr->job->PruneFiles || jcr->client->AutoPrune) {
84       Jmsg(jcr, M_INFO, 0, _("Begin pruning Files.\n"));
85       prune_files(&ua, client);
86       pruned = TRUE;
87    }
88    if (pruned) {
89       Jmsg(jcr, M_INFO, 0, _("End auto prune.\n\n"));
90    }
91
92    free_ua_context(&ua);
93    return 1;    
94 }
95
96 /*
97  * Prune all volumes in current Pool.
98  *
99  *  Return 0: on error
100  *         number of Volumes Purged
101  */
102 int prune_volumes(JCR *jcr)
103 {
104    int stat = 0;
105    int i;
106    uint32_t *ids = NULL;
107    int num_ids = 0;
108    MEDIA_DBR mr;
109    POOL_DBR pr;
110    UAContext ua;
111
112    if (!jcr->job->PruneVolumes && !jcr->pool->AutoPrune) {
113       Dmsg0(100, "AutoPrune not set in Pool.\n");
114       return 0;
115    }
116    memset(&mr, 0, sizeof(mr));
117    memset(&pr, 0, sizeof(pr));
118    create_ua_context(jcr, &ua);
119
120    db_lock(jcr->db);
121
122    pr.PoolId = jcr->PoolId;
123    if (!db_get_pool_record(jcr->db, &pr) || !db_get_media_ids(jcr->db, &num_ids, &ids)) {
124       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
125       goto bail_out;
126    }
127
128
129    for (i=0; i<num_ids; i++) {
130       mr.MediaId = ids[i];
131       if (!db_get_media_record(jcr->db, &mr)) {
132          Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
133          continue;
134       }
135       /* Prune only Volumes from current Pool */
136       if (pr.PoolId != mr.PoolId) {
137          continue;
138       }
139       /* Prune only Volumes with status "Full"  or "Used" */
140       if (strcmp(mr.VolStatus, "Full") == 0 || strcmp(mr.VolStatus, "Used") == 0) {
141          Dmsg1(200, "Prune Volume %s\n", mr.VolumeName);
142          stat += prune_volume(&ua, &pr, &mr); 
143          Dmsg1(200, "Num pruned = %d\n", stat);
144       }
145    }   
146
147 bail_out:
148    db_unlock(jcr->db);
149    free_ua_context(&ua);
150    if (ids) {
151       free(ids);
152    }
153    return stat;
154 }