]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/recycle.c
d960643db66078a78784ea598c37a4836195434e
[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' %s"
87           "ORDER BY LastWritten ASC,MediaId LIMIT 1";
88
89    Dmsg0(100, "Enter recycle_oldest_purged_volume\n");
90    oldest.MediaId = 0;
91    if (InChanger) {
92       char changer[100];
93       bsnprintf(changer, sizeof(changer), "AND InChanger=1 AND StorageId=%s ",
94          edit_int64(mr->StorageId, ed1));
95       Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, changer);
96    } else {
97       Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, "");
98    }
99
100    if (!db_sql_query(jcr->db, query, oldest_handler, (void *)&oldest)) {
101       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
102       Dmsg0(100, "return 0  recycle_oldest_purged_volume query\n");
103       free_pool_memory(query);
104       return false;
105    }
106    free_pool_memory(query);
107    Dmsg1(100, "Oldest mediaid=%d\n", oldest.MediaId);
108    if (oldest.MediaId != 0) {
109       mr->MediaId = oldest.MediaId;
110       if (db_get_media_record(jcr, jcr->db, mr)) {
111          if (recycle_volume(jcr, mr)) {
112             Jmsg(jcr, M_INFO, 0, _("Recycled volume \"%s\"\n"), mr->VolumeName);
113             Dmsg1(100, "return 1  recycle_oldest_purged_volume Vol=%s\n", mr->VolumeName);
114             return true;
115          }
116       }
117       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
118    }
119    Dmsg0(100, "return 0  recycle_oldest_purged_volume end\n");
120    return false;
121 }
122
123 /*
124  * Recycle the specified volume
125  */
126 int recycle_volume(JCR *jcr, MEDIA_DBR *mr)
127 {
128    bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
129    mr->VolJobs = mr->VolFiles = mr->VolBlocks = mr->VolErrors = 0;
130    mr->VolBytes = 1;
131    mr->FirstWritten = mr->LastWritten = 0;
132    mr->RecycleCount++;
133    mr->set_first_written = true;
134    return db_update_media_record(jcr, jcr->db, mr);
135 }