3 * Bacula Director -- Automatic Recycling of Volumes
4 * Recycles Volumes that have been purged
6 * Kern Sibbald, May MMII
12 Copyright (C) 2002-2006 Kern Sibbald
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License
16 version 2 as amended with additional clauses defined in the
17 file LICENSE in the main source directory.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 the file LICENSE for additional details.
35 static int oldest_handler(void *ctx, int num_fields, char **row)
37 struct s_oldest_ctx *oldest = (struct s_oldest_ctx *)ctx;
40 oldest->MediaId = str_to_int64(row[0]);
41 bstrncpy(oldest->LastWritten, row[1]?row[1]:"", sizeof(oldest->LastWritten));
42 Dmsg1(100, "New oldest %s\n", row[1]?row[1]:"");
47 /* Forward referenced functions */
49 bool find_recycled_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
51 bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
52 if (db_find_next_volume(jcr, jcr->db, 1, InChanger, mr)) {
53 jcr->MediaId = mr->MediaId;
54 Dmsg1(20, "Find_next_vol MediaId=%u\n", jcr->MediaId);
55 pm_strcpy(jcr->VolumeName, mr->VolumeName);
63 * Look for oldest Purged volume
65 bool recycle_oldest_purged_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
67 struct s_oldest_ctx oldest;
69 POOLMEM *query = get_pool_memory(PM_EMSG);
71 "SELECT MediaId,LastWritten FROM Media "
72 "WHERE PoolId=%s AND Recycle=1 AND VolStatus='Purged' "
73 "AND Enabled=1 AND MediaType='%s' %s"
74 "ORDER BY LastWritten ASC,MediaId LIMIT 1";
76 Dmsg0(100, "Enter recycle_oldest_purged_volume\n");
80 bsnprintf(changer, sizeof(changer), "AND InChanger=1 AND StorageId=%s ",
81 edit_int64(mr->StorageId, ed1));
82 Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, changer);
84 Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, "");
87 if (!db_sql_query(jcr->db, query, oldest_handler, (void *)&oldest)) {
88 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
89 Dmsg0(100, "return 0 recycle_oldest_purged_volume query\n");
90 free_pool_memory(query);
93 free_pool_memory(query);
94 Dmsg1(100, "Oldest mediaid=%d\n", oldest.MediaId);
95 if (oldest.MediaId != 0) {
96 mr->MediaId = oldest.MediaId;
97 if (db_get_media_record(jcr, jcr->db, mr)) {
98 if (recycle_volume(jcr, mr)) {
99 Jmsg(jcr, M_INFO, 0, _("Recycled volume \"%s\"\n"), mr->VolumeName);
100 Dmsg1(100, "return 1 recycle_oldest_purged_volume Vol=%s\n", mr->VolumeName);
104 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
106 Dmsg0(100, "return 0 recycle_oldest_purged_volume end\n");
111 * Recycle the specified volume
113 int recycle_volume(JCR *jcr, MEDIA_DBR *mr)
115 bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
116 mr->VolJobs = mr->VolFiles = mr->VolBlocks = mr->VolErrors = 0;
118 mr->FirstWritten = mr->LastWritten = 0;
119 mr->set_first_written = true;
120 return db_update_media_record(jcr, jcr->db, mr);