3 * Bacula Director -- Automatic Recycling of Volumes
4 * Recycles Volumes that have been purged
6 * Kern Sibbald, May MMII
12 Copyright (C) 2002-2005 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 int 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 int 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 MediaType='%s' %s"
74 "ORDER BY LastWritten ASC,MediaId LIMIT 1";
76 Dmsg0(100, "Enter recycle_oldest_purged_volume\n");
79 Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType,
82 Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, "");
85 if (!db_sql_query(jcr->db, query, oldest_handler, (void *)&oldest)) {
86 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
87 Dmsg0(100, "return 0 recycle_oldest_purged_volume query\n");
88 free_pool_memory(query);
91 free_pool_memory(query);
92 Dmsg1(100, "Oldest mediaid=%d\n", oldest.MediaId);
93 if (oldest.MediaId != 0) {
94 mr->MediaId = oldest.MediaId;
95 if (db_get_media_record(jcr, jcr->db, mr)) {
96 if (recycle_volume(jcr, mr)) {
97 Jmsg(jcr, M_INFO, 0, _("Recycled volume \"%s\"\n"), mr->VolumeName);
98 Dmsg1(100, "return 1 recycle_oldest_purged_volume Vol=%s\n", mr->VolumeName);
102 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
104 Dmsg0(100, "return 0 recycle_oldest_purged_volume end\n");
109 * Recycle the specified volume
111 int recycle_volume(JCR *jcr, MEDIA_DBR *mr)
113 bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
114 mr->VolJobs = mr->VolFiles = mr->VolBlocks = mr->VolErrors = 0;
116 mr->FirstWritten = mr->LastWritten = 0;
117 mr->set_first_written = true;
118 return db_update_media_record(jcr, jcr->db, mr);