]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/autoprune.c
Fix bug #1959 input validation on delete of jobs.
[bacula/bacula] / bacula / src / dird / autoprune.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2002-2012 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 three of the GNU Affero 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 Affero 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  */
36
37 #include "bacula.h"
38 #include "dird.h"
39 #include "ua.h"
40
41 /* Forward referenced functions */
42
43
44 /*
45  * Auto Prune Jobs and Files. This is called at the end of every
46  *   Job.  We do not prune volumes here.
47  */
48 void do_autoprune(JCR *jcr)
49 {
50    UAContext *ua;
51    CLIENT *client;
52    POOL *pool;
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    pool = jcr->pool;
62
63    if (jcr->job->PruneJobs || jcr->client->AutoPrune) {
64       prune_jobs(ua, client, pool, jcr->getJobType());
65       pruned = true;
66    } else {
67       pruned = false;
68    }
69
70    if (jcr->job->PruneFiles || jcr->client->AutoPrune) {
71       prune_files(ua, client, pool);
72       pruned = true;
73    }
74    if (pruned) {
75       Jmsg(jcr, M_INFO, 0, _("End auto prune.\n\n"));
76    }
77    free_ua_context(ua);
78    return;
79 }
80
81 /*
82  * Prune at least one Volume in current Pool. This is called from
83  *   catreq.c => next_vol.c when the Storage daemon is asking for another
84  *   volume and no appendable volumes are available.
85  *
86  */
87 void prune_volumes(JCR *jcr, bool InChanger, MEDIA_DBR *mr,
88         STORE *store)
89 {
90    int count;
91    int i;
92    dbid_list ids;
93    struct del_ctx prune_list;
94    POOL_MEM query(PM_MESSAGE);
95    UAContext *ua;
96    char ed1[50], ed2[100], ed3[50];
97    POOL_DBR spr;
98
99    Dmsg1(100, "Prune volumes PoolId=%d\n", jcr->jr.PoolId);
100    if (!jcr->job->PruneVolumes && !jcr->pool->AutoPrune) {
101       Dmsg0(100, "AutoPrune not set in Pool.\n");
102       return;
103    }
104
105    memset(&prune_list, 0, sizeof(prune_list));
106    prune_list.max_ids = 10000;
107    prune_list.JobId = (JobId_t *)malloc(sizeof(JobId_t) * prune_list.max_ids);
108
109    ua = new_ua_context(jcr);
110    db_lock(jcr->db);
111
112    /* Edit PoolId */
113    edit_int64(mr->PoolId, ed1);
114    /*
115     * Get Pool record for Scratch Pool
116     */
117    memset(&spr, 0, sizeof(spr));
118    bstrncpy(spr.Name, "Scratch", sizeof(spr.Name));
119    if (db_get_pool_record(jcr, jcr->db, &spr)) {
120       edit_int64(spr.PoolId, ed2);
121       bstrncat(ed2, ",", sizeof(ed2));
122    } else {
123       ed2[0] = 0;
124    }
125
126    if (mr->ScratchPoolId) {
127       edit_int64(mr->ScratchPoolId, ed3);
128       bstrncat(ed2, ed3, sizeof(ed2));
129       bstrncat(ed2, ",", sizeof(ed2));
130    }
131
132    Dmsg1(100, "Scratch pool(s)=%s\n", ed2);
133    /*
134     * ed2 ends up with scratch poolid and current poolid or
135     *   just current poolid if there is no scratch pool 
136     */
137    bstrncat(ed2, ed1, sizeof(ed2));
138
139    /*
140     * Get the List of all media ids in the current Pool or whose
141     *  RecyclePoolId is the current pool or the scratch pool
142     */
143    const char *select = "SELECT DISTINCT MediaId,LastWritten FROM Media WHERE "
144         "(PoolId=%s OR RecyclePoolId IN (%s)) AND MediaType='%s' %s"
145         "ORDER BY LastWritten ASC,MediaId";
146
147    if (InChanger) {
148       char changer[100];
149       /* Ensure it is in this autochanger */
150       bsnprintf(changer, sizeof(changer), "AND InChanger=1 AND StorageId=%s ",
151          edit_int64(mr->StorageId, ed3));
152       Mmsg(query, select, ed1, ed2, mr->MediaType, changer);
153    } else {
154       Mmsg(query, select, ed1, ed2, mr->MediaType, "");
155    }
156
157    Dmsg1(100, "query=%s\n", query.c_str());
158    if (!db_get_query_dbids(ua->jcr, ua->db, query, ids)) {
159       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
160       goto bail_out;
161    }
162
163    Dmsg1(100, "Volume prune num_ids=%d\n", ids.num_ids);
164
165    /* Visit each Volume and Prune it until we find one that is purged */
166    for (i=0; i<ids.num_ids; i++) {
167       MEDIA_DBR lmr;
168       lmr.MediaId = ids.DBId[i];
169       Dmsg1(100, "Get record MediaId=%d\n", (int)lmr.MediaId);
170       if (!db_get_media_record(jcr, jcr->db, &lmr)) {
171          Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
172          continue;
173       }
174       Dmsg1(100, "Examine vol=%s\n", lmr.VolumeName);
175       /* Don't prune archived volumes */
176       if (lmr.Enabled == 2) {
177          Dmsg1(100, "Vol=%s disabled\n", lmr.VolumeName);
178          continue;
179       }
180       /* Prune only Volumes with status "Full", or "Used" */
181       if (strcmp(lmr.VolStatus, "Full")   == 0 ||
182           strcmp(lmr.VolStatus, "Used")   == 0) {
183          Dmsg2(100, "Add prune list MediaId=%d Volume %s\n", (int)lmr.MediaId, lmr.VolumeName);
184          count = get_prune_list_for_volume(ua, &lmr, &prune_list);
185          Dmsg1(100, "Num pruned = %d\n", count);
186          if (count != 0) {
187             purge_job_list_from_catalog(ua, prune_list);
188             prune_list.num_ids = 0;             /* reset count */
189          }
190          if (!is_volume_purged(ua, &lmr)) {
191             Dmsg1(050, "Vol=%s not pruned\n", lmr.VolumeName);
192             continue;
193          }
194          Dmsg1(050, "Vol=%s is purged\n", lmr.VolumeName);
195
196          /*
197           * Since we are also pruning the Scratch pool, continue
198           *   until and check if this volume is available (InChanger + StorageId)
199           * If not, just skip this volume and try the next one
200           */
201          if (InChanger) {
202             if (!lmr.InChanger || (lmr.StorageId != mr->StorageId)) {
203                Dmsg1(100, "Vol=%s not inchanger or correct StoreId\n", lmr.VolumeName);
204                continue;                  /* skip this volume, ie not loadable */
205             }
206          }
207          if (!lmr.Recycle) {
208             Dmsg1(100, "Vol=%s not recyclable\n", lmr.VolumeName);
209             continue;
210          }
211
212          if (has_volume_expired(jcr, &lmr)) {
213             Dmsg1(100, "Vol=%s has expired\n", lmr.VolumeName);
214             continue;                     /* Volume not usable */
215          }
216
217          /*
218           * If purged and not moved to another Pool, 
219           *   then we stop pruning and take this volume.
220           */
221          if (lmr.PoolId == mr->PoolId) {
222             Dmsg2(100, "Got Vol=%s MediaId=%d purged.\n", lmr.VolumeName, (int)lmr.MediaId);
223             mr->copy(&lmr);
224             set_storageid_in_mr(store, mr);
225             break;                        /* got a volume */
226          }
227       }
228    }
229
230 bail_out:
231    Dmsg0(100, "Leave prune volumes\n");
232    db_unlock(jcr->db);
233    free_ua_context(ua);
234    if (prune_list.JobId) {
235       free(prune_list.JobId);
236    }
237    return;
238 }