]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/recycle.c
ebl update
[bacula/bacula] / bacula / src / dird / recycle.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 John Walker.
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 Recycling of Volumes
31  *      Recycles Volumes that have been purged
32  *
33  *     Kern Sibbald, May MMII
34  *
35  *   Version $Id$
36  */
37
38
39 #include "bacula.h"
40 #include "dird.h"
41 #include "ua.h"
42
43 struct s_oldest_ctx {
44    uint32_t MediaId;
45    char LastWritten[30];
46 };
47
48 static int oldest_handler(void *ctx, int num_fields, char **row)
49 {
50    struct s_oldest_ctx *oldest = (struct s_oldest_ctx *)ctx;
51
52    if (row[0]) {
53       oldest->MediaId = str_to_int64(row[0]);
54       bstrncpy(oldest->LastWritten, row[1]?row[1]:"", sizeof(oldest->LastWritten));
55       Dmsg1(100, "New oldest %s\n", row[1]?row[1]:"");
56    }
57    return 1;
58 }
59
60 /* Forward referenced functions */
61
62 bool find_recycled_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
63 {
64    bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
65    if (db_find_next_volume(jcr, jcr->db, 1, InChanger, mr)) {
66       jcr->MediaId = mr->MediaId;
67       Dmsg1(20, "Find_next_vol MediaId=%u\n", jcr->MediaId);
68       pm_strcpy(jcr->VolumeName, mr->VolumeName);
69       return true;
70    }
71    return false;
72 }
73
74
75 /*
76  *   Look for oldest Purged volume
77  */
78 bool recycle_oldest_purged_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
79 {
80    struct s_oldest_ctx oldest;
81    char ed1[50];
82    POOLMEM *query = get_pool_memory(PM_EMSG);
83    const char *select =
84           "SELECT MediaId,LastWritten FROM Media "
85           "WHERE PoolId=%s AND Recycle=1 AND VolStatus='Purged' "
86           "AND Enabled=1 AND MediaType='%s' "
87           "ORDER BY LastWritten ASC,MediaId LIMIT 1";
88
89    Dmsg0(100, "Enter recycle_oldest_purged_volume\n");
90    oldest.MediaId = 0;
91    Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType);
92
93    if (!db_sql_query(jcr->db, query, oldest_handler, (void *)&oldest)) {
94       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
95       Dmsg0(100, "return 0  recycle_oldest_purged_volume query\n");
96       free_pool_memory(query);
97       return false;
98    }
99    free_pool_memory(query);
100    Dmsg1(100, "Oldest mediaid=%d\n", oldest.MediaId);
101    if (oldest.MediaId != 0) {
102       mr->MediaId = oldest.MediaId;
103       if (db_get_media_record(jcr, jcr->db, mr)) {
104          if (recycle_volume(jcr, mr)) {
105             Jmsg(jcr, M_INFO, 0, _("Recycled volume \"%s\"\n"), mr->VolumeName);
106             Dmsg1(100, "return 1  recycle_oldest_purged_volume Vol=%s\n", mr->VolumeName);
107             return true;
108          }
109       }
110       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
111    }
112    Dmsg0(100, "return 0  recycle_oldest_purged_volume end\n");
113    return false;
114 }
115
116 /*
117  * Recycle the specified volume
118  */
119 int recycle_volume(JCR *jcr, MEDIA_DBR *mr)
120 {
121    bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
122    mr->VolJobs = mr->VolFiles = mr->VolBlocks = mr->VolErrors = 0;
123    mr->VolBytes = 1;
124    mr->FirstWritten = mr->LastWritten = 0;
125    mr->RecycleCount++;
126    mr->set_first_written = true;
127    return db_update_media_record(jcr, jcr->db, mr);
128 }