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