3 * Bacula Director -- Automatic Recycling of Volumes
4 * Recycles Volumes that have been purged
6 * Kern Sibbald, May MMII
11 Bacula® - The Network Backup Solution
13 Copyright (C) 2002-2006 Free Software Foundation Europe e.V.
15 The main author of Bacula is Kern Sibbald, with contributions from
16 many others, a complete list can be found in the file AUTHORS.
17 This program is Free Software; you can redistribute it and/or
18 modify it under the terms of version two of the GNU General Public
19 License as published by the Free Software Foundation plus additions
20 that are listed in the file LICENSE.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 Bacula® is a registered trademark of John Walker.
33 The licensor of Bacula is the Free Software Foundation Europe
34 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
35 Switzerland, email:ftf@fsfeurope.org.
47 static int oldest_handler(void *ctx, int num_fields, char **row)
49 struct s_oldest_ctx *oldest = (struct s_oldest_ctx *)ctx;
52 oldest->MediaId = str_to_int64(row[0]);
53 bstrncpy(oldest->LastWritten, row[1]?row[1]:"", sizeof(oldest->LastWritten));
54 Dmsg1(100, "New oldest %s\n", row[1]?row[1]:"");
59 /* Forward referenced functions */
61 bool find_recycled_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
63 bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
64 if (db_find_next_volume(jcr, jcr->db, 1, InChanger, mr)) {
65 jcr->MediaId = mr->MediaId;
66 Dmsg1(20, "Find_next_vol MediaId=%u\n", jcr->MediaId);
67 pm_strcpy(jcr->VolumeName, mr->VolumeName);
75 * Look for oldest Purged volume
77 bool recycle_oldest_purged_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
79 struct s_oldest_ctx oldest;
81 POOLMEM *query = get_pool_memory(PM_EMSG);
83 "SELECT MediaId,LastWritten FROM Media "
84 "WHERE PoolId=%s AND Recycle=1 AND VolStatus='Purged' "
85 "AND Enabled=1 AND MediaType='%s' %s"
86 "ORDER BY LastWritten ASC,MediaId LIMIT 1";
88 Dmsg0(100, "Enter recycle_oldest_purged_volume\n");
92 bsnprintf(changer, sizeof(changer), "AND InChanger=1 AND StorageId=%s ",
93 edit_int64(mr->StorageId, ed1));
94 Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, changer);
96 Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, "");
99 if (!db_sql_query(jcr->db, query, oldest_handler, (void *)&oldest)) {
100 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
101 Dmsg0(100, "return 0 recycle_oldest_purged_volume query\n");
102 free_pool_memory(query);
105 free_pool_memory(query);
106 Dmsg1(100, "Oldest mediaid=%d\n", oldest.MediaId);
107 if (oldest.MediaId != 0) {
108 mr->MediaId = oldest.MediaId;
109 if (db_get_media_record(jcr, jcr->db, mr)) {
110 if (recycle_volume(jcr, mr)) {
111 Jmsg(jcr, M_INFO, 0, _("Recycled volume \"%s\"\n"), mr->VolumeName);
112 Dmsg1(100, "return 1 recycle_oldest_purged_volume Vol=%s\n", mr->VolumeName);
116 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
118 Dmsg0(100, "return 0 recycle_oldest_purged_volume end\n");
123 * Recycle the specified volume
125 int recycle_volume(JCR *jcr, MEDIA_DBR *mr)
127 bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
128 mr->VolJobs = mr->VolFiles = mr->VolBlocks = mr->VolErrors = 0;
130 mr->FirstWritten = mr->LastWritten = 0;
132 mr->set_first_written = true;
133 return db_update_media_record(jcr, jcr->db, mr);