2 Bacula® - The Network Backup Solution
4 Copyright (C) 2002-2007 Free Software Foundation Europe e.V.
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
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.
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
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.
30 * Bacula Director -- Automatic Recycling of Volumes
31 * Recycles Volumes that have been purged
33 * Kern Sibbald, May MMII
48 static int oldest_handler(void *ctx, int num_fields, char **row)
50 struct s_oldest_ctx *oldest = (struct s_oldest_ctx *)ctx;
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]:"");
60 /* Forward referenced functions */
62 bool find_recycled_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
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);
76 * Look for oldest Purged volume
78 bool recycle_oldest_purged_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
80 struct s_oldest_ctx oldest;
82 POOLMEM *query = get_pool_memory(PM_EMSG);
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";
89 Dmsg0(100, "Enter recycle_oldest_purged_volume\n");
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);
97 Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, "");
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);
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);
117 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
119 Dmsg0(100, "return 0 recycle_oldest_purged_volume end\n");
124 * Recycle the specified volume
126 int recycle_volume(JCR *jcr, MEDIA_DBR *mr)
128 bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
129 mr->VolJobs = mr->VolFiles = mr->VolBlocks = mr->VolErrors = 0;
131 mr->FirstWritten = mr->LastWritten = 0;
133 mr->set_first_written = true;
134 return db_update_media_record(jcr, jcr->db, mr);