]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/autoprune.c
ebl Fix #1173 where prune_volume() returns a volume from the scratch.
[bacula/bacula] / bacula / src / dird / autoprune.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2002-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *
30  *   Bacula Director -- Automatic Pruning
31  *      Applies retention periods
32  *
33  *     Kern Sibbald, May MMII
34  *
35  *   Version $Id$
36  */
37
38 #include "bacula.h"
39 #include "dird.h"
40 #include "ua.h"
41
42 /* Forward referenced functions */
43
44
45 /*
46  * Auto Prune Jobs and Files. This is called at the end of every
47  *   Job.  We do not prune volumes here.
48  */
49 void do_autoprune(JCR *jcr)
50 {
51    UAContext *ua;
52    CLIENT *client;
53    bool pruned;
54
55    if (!jcr->client) {                /* temp -- remove me */
56       return;
57    }
58
59    ua = new_ua_context(jcr);
60    client = jcr->client;
61
62    if (jcr->job->PruneJobs || jcr->client->AutoPrune) {
63       Jmsg(jcr, M_INFO, 0, _("Begin pruning Jobs.\n"));
64       prune_jobs(ua, client, jcr->get_JobType());
65       pruned = true;
66    } else {
67       pruned = false;
68    }
69
70    if (jcr->job->PruneFiles || jcr->client->AutoPrune) {
71       Jmsg(jcr, M_INFO, 0, _("Begin pruning Files.\n"));
72       prune_files(ua, client);
73       pruned = true;
74    }
75    if (pruned) {
76       Jmsg(jcr, M_INFO, 0, _("End auto prune.\n\n"));
77    }
78    free_ua_context(ua);
79    return;
80 }
81
82 /*
83  * Prune at least one Volume in current Pool. This is called from
84  *   catreq.c => next_vol.c when the Storage daemon is asking for another
85  *   volume and no appendable volumes are available.
86  *
87  *  Return: false if nothing pruned
88  *          true if pruned, and mr is set to pruned volume
89  */
90 bool prune_volumes(JCR *jcr, bool InChanger, MEDIA_DBR *mr) 
91 {
92    int count;
93    int i;
94    dbid_list ids;
95    struct del_ctx prune_list;
96    POOL_MEM query(PM_MESSAGE);
97    UAContext *ua;
98    bool ok = false;
99    char ed1[50], ed2[100], ed3[50];
100    POOL_DBR spr;
101
102    Dmsg1(050, "Prune volumes PoolId=%d\n", jcr->jr.PoolId);
103    if (!jcr->job->PruneVolumes && !jcr->pool->AutoPrune) {
104       Dmsg0(100, "AutoPrune not set in Pool.\n");
105       return 0;
106    }
107
108    memset(&prune_list, 0, sizeof(prune_list));
109    prune_list.max_ids = 10000;
110    prune_list.JobId = (JobId_t *)malloc(sizeof(JobId_t) * prune_list.max_ids);
111
112    ua = new_ua_context(jcr);
113
114    db_lock(jcr->db);
115
116    /* Edit PoolId */
117    edit_int64(mr->PoolId, ed1);
118    /*
119     * Get Pool record for Scratch Pool
120     */
121    memset(&spr, 0, sizeof(spr));
122    bstrncpy(spr.Name, "Scratch", sizeof(spr.Name));
123    if (db_get_pool_record(jcr, jcr->db, &spr)) {
124       edit_int64(spr.PoolId, ed2);
125       bstrncat(ed2, ",", sizeof(ed2));
126    } else {
127       ed2[0] = 0;
128    }
129    Dmsg1(050, "Scratch pool=%s\n", ed2);
130    /*
131     * ed2 ends up with scratch poolid and current poolid or
132     *   just current poolid if there is no scratch pool 
133     */
134    bstrncat(ed2, ed1, sizeof(ed2));
135
136    /*
137     * Get the List of all media ids in the current Pool or whose
138     *  RecyclePoolId is the current pool or the scratch pool
139     */
140    const char *select = "SELECT DISTINCT MediaId,LastWritten FROM Media WHERE "
141         "(PoolId=%s OR RecyclePoolId IN (%s)) AND MediaType='%s' %s"
142         "ORDER BY LastWritten ASC,MediaId";
143
144    if (InChanger) {
145       char changer[100];
146       /* Ensure it is in this autochanger */
147       bsnprintf(changer, sizeof(changer), "AND InChanger=1 AND StorageId=%s ",
148          edit_int64(mr->StorageId, ed3));
149       Mmsg(query, select, ed1, ed2, mr->MediaType, changer);
150    } else {
151       Mmsg(query, select, ed1, ed2, mr->MediaType, "");
152    }
153
154    Dmsg1(050, "query=%s\n", query.c_str());
155    if (!db_get_query_dbids(ua->jcr, ua->db, query, ids)) {
156       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
157       goto bail_out;
158    }
159
160    Dmsg1(050, "num_ids=%d\n", ids.num_ids);
161
162    /* Visit each Volume and Prune it until we find one that is purged */
163    for (i=0; i<ids.num_ids; i++) {
164       MEDIA_DBR lmr;
165       memset(&lmr, 0, sizeof(lmr));
166       lmr.MediaId = ids.DBId[i];
167       Dmsg1(050, "Get record MediaId=%d\n", (int)lmr.MediaId);
168       if (!db_get_media_record(jcr, jcr->db, &lmr)) {
169          Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
170          continue;
171       }
172       /* Don't prune archived volumes */
173       if (lmr.Enabled == 2) {
174          continue;
175       }
176       /* Prune only Volumes with status "Full", or "Used" */
177       if (strcmp(lmr.VolStatus, "Full")   == 0 ||
178           strcmp(lmr.VolStatus, "Used")   == 0) {
179          Dmsg2(050, "Add prune list MediaId=%d Volume %s\n", (int)lmr.MediaId, lmr.VolumeName);
180          count = get_prune_list_for_volume(ua, &lmr, &prune_list);
181          Dmsg1(050, "Num pruned = %d\n", count);
182          if (count != 0) {
183             purge_job_list_from_catalog(ua, prune_list);
184             prune_list.num_ids = 0;             /* reset count */
185          }
186          ok = is_volume_purged(ua, &lmr);
187
188          /*
189           * Check if this volume is available (InChanger + StorageId)
190           * If not, just skip this volume and try the next one
191           */
192          if (ok && InChanger) {
193             if (!lmr.InChanger || (lmr.StorageId != mr->StorageId)) {
194                ok = false;             /* skip this volume, ie not loadable */
195             }
196          }
197
198          /*
199           * If purged and not moved to another Pool, 
200           *   then we stop pruning and take this volume.
201           */
202          if (ok && lmr.PoolId == mr->PoolId) {
203             Dmsg2(050, "Vol=%s MediaId=%d purged.\n", lmr.VolumeName, (int)lmr.MediaId);
204             memcpy(mr, &lmr, sizeof(lmr));
205             break;                        /* got a volume */
206          }
207          ok = false;                     /* clear OK, in case we fall out */
208       } else {
209          Dmsg2(050, "Nothing pruned MediaId=%d Volume=%s\n", (int)lmr.MediaId, lmr.VolumeName);
210       }
211    }
212
213 bail_out:
214    db_unlock(jcr->db);
215    free_ua_context(ua);
216    if (prune_list.JobId) {
217       free(prune_list.JobId);
218    }
219    return ok;
220 }