]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/recycle.c
- Implement first cut of Migration.
[bacula/bacula] / bacula / src / dird / recycle.c
1 /*
2  *
3  *   Bacula Director -- Automatic Recycling of Volumes
4  *      Recycles Volumes that have been purged
5  *
6  *     Kern Sibbald, May MMII
7  *
8  *   Version $Id$
9  */
10
11 /*
12    Copyright (C) 2002-2005 Kern Sibbald
13
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.
18
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.
23
24  */
25
26 #include "bacula.h"
27 #include "dird.h"
28 #include "ua.h"
29
30 struct s_oldest_ctx {
31    uint32_t MediaId;
32    char LastWritten[30];
33 };
34
35 static int oldest_handler(void *ctx, int num_fields, char **row)
36 {
37    struct s_oldest_ctx *oldest = (struct s_oldest_ctx *)ctx;
38
39    if (row[0]) {
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]:"");
43    }
44    return 1;
45 }
46
47 /* Forward referenced functions */
48
49 int find_recycled_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
50 {
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);
56       return 1;
57    }
58    return 0;
59 }
60
61
62 /*
63  *   Look for oldest Purged volume
64  */
65 int recycle_oldest_purged_volume(JCR *jcr, bool InChanger, MEDIA_DBR *mr)
66 {
67    struct s_oldest_ctx oldest;
68    char ed1[50];
69    POOLMEM *query = get_pool_memory(PM_EMSG);
70    const char *select =
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";
75
76    Dmsg0(100, "Enter recycle_oldest_purged_volume\n");
77    oldest.MediaId = 0;
78    if (InChanger) {
79       char changer[100];
80       bsnprintf(changer, sizeof(changer), "AND InChanger=1 AND StorageId=%s",
81          mr->StorageId);
82       Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, changer);
83    } else {
84       Mmsg(query, select, edit_int64(mr->PoolId, ed1), mr->MediaType, "");
85    }
86
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);
91       return 0;
92    }
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);
101             return 1;
102          }
103       }
104       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
105    }
106    Dmsg0(100, "return 0  recycle_oldest_purged_volume end\n");
107    return 0;
108 }
109
110 /*
111  * Recycle the specified volume
112  */
113 int recycle_volume(JCR *jcr, MEDIA_DBR *mr)
114 {
115    bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
116    mr->VolJobs = mr->VolFiles = mr->VolBlocks = mr->VolErrors = 0;
117    mr->VolBytes = 1;
118    mr->FirstWritten = mr->LastWritten = 0;
119    mr->set_first_written = true;
120    return db_update_media_record(jcr, jcr->db, mr);
121 }