2 * Bacula Catalog Database Find record interface routines
4 * Note, generally, these routines are more complicated
5 * that a simple search by name or id. Such simple
8 * Kern Sibbald, December 2000
14 Copyright (C) 2000-2003 Kern Sibbald and John Walker
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation; either version 2 of
19 the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with this program; if not, write to the Free
28 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 /* The following is necessary so that we do not include
35 * the dummy external definition of DB.
37 #define __SQL_C /* indicate that this is sql.c */
42 #if HAVE_MYSQL || HAVE_SQLITE
44 /* -----------------------------------------------------------------------
46 * Generic Routines (or almost generic)
48 * -----------------------------------------------------------------------
51 /* Imported subroutines */
52 extern void print_result(B_DB *mdb);
53 extern int QueryDB(char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
56 * Find job start time. Used to find last full save
57 * for Incremental and Differential saves.
59 * Returns: 0 on failure
60 * 1 on success, jr is unchanged, but stime is set
63 db_find_job_start_time(JCR *jcr, B_DB *mdb, JOB_DBR *jr, POOLMEM **stime)
70 pm_strcpy(stime, "0000-00-00 00:00:00"); /* default */
71 /* If no Id given, we must find corresponding job */
73 /* Differential is since last Full backup */
74 if (jr->Level == L_DIFFERENTIAL) {
76 "SELECT JobId FROM Job WHERE JobStatus='T' AND Type='%c' AND \
77 Level='%c' AND Name='%s' AND ClientId=%d AND FileSetId=%d \
78 ORDER BY StartTime DESC LIMIT 1",
79 jr->Type, L_FULL, jr->Name, jr->ClientId, jr->FileSetId);
80 /* Incremental is since last Full, Incremental, or Differential */
81 } else if (jr->Level == L_INCREMENTAL) {
83 "SELECT JobId FROM Job WHERE JobStatus='T' AND Type='%c' AND \
84 Level IN ('%c','%c','%c') AND Name='%s' AND ClientId=%d \
85 ORDER BY StartTime DESC LIMIT 1",
86 jr->Type, L_INCREMENTAL, L_DIFFERENTIAL, L_FULL, jr->Name,
89 Mmsg1(&mdb->errmsg, _("Unknown level=%d\n"), jr->Level);
93 Dmsg1(100, "Submitting: %s\n", mdb->cmd);
94 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
95 Mmsg1(&mdb->errmsg, _("Query error for start time request: %s\n"), mdb->cmd);
99 if ((row = sql_fetch_row(mdb)) == NULL) {
100 sql_free_result(mdb);
104 JobId = atoi(row[0]);
105 sql_free_result(mdb);
107 JobId = jr->JobId; /* search for particular id */
110 Dmsg1(100, "Submitting: %s\n", mdb->cmd);
111 Mmsg(&mdb->cmd, "SELECT StartTime FROM Job WHERE Job.JobId=%d", JobId);
113 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
114 Mmsg1(&mdb->errmsg, _("Query error for start time request: %s\n"), mdb->cmd);
119 if ((row = sql_fetch_row(mdb)) == NULL) {
120 pm_strcpy(stime, ""); /* set EOS */
121 Mmsg2(&mdb->errmsg, _("No Job found for JobId=%d: %s\n"), JobId, sql_strerror(mdb));
122 sql_free_result(mdb);
126 Dmsg1(100, "Got start time: %s\n", row[0]);
127 pm_strcpy(stime, row[0]);
129 sql_free_result(mdb);
136 * Find JobId of last job that ran. E.g. for
137 * VERIFY_CATALOG we want the JobId of the last INIT.
138 * For VERIFY_VOLUME_TO_CATALOG, we want the JobId of the last Job.
140 * Returns: 1 on success
144 db_find_last_jobid(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
150 if (jr->Level == L_VERIFY_CATALOG) {
152 "SELECT JobId FROM Job WHERE Type='%c' AND Level='%c' AND Name='%s' AND \
153 ClientId=%d ORDER BY StartTime DESC LIMIT 1",
154 JT_VERIFY, L_VERIFY_INIT, jr->Name, jr->ClientId);
155 } else if (jr->Level == L_VERIFY_VOLUME_TO_CATALOG) {
157 "SELECT JobId FROM Job WHERE Type='%c' AND \
158 ClientId=%d ORDER BY StartTime DESC LIMIT 1",
159 JT_BACKUP, jr->ClientId);
161 Mmsg1(&mdb->errmsg, _("Unknown Job level=%c\n"), jr->Level);
166 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
170 if ((row = sql_fetch_row(mdb)) == NULL) {
171 Mmsg1(&mdb->errmsg, _("No Job found for: %s.\n"), mdb->cmd);
172 sql_free_result(mdb);
177 jr->JobId = atoi(row[0]);
178 sql_free_result(mdb);
180 Dmsg1(100, "db_get_last_jobid: got JobId=%d\n", jr->JobId);
181 if (jr->JobId <= 0) {
182 Mmsg1(&mdb->errmsg, _("No Job found for: %s\n"), mdb->cmd);
192 * Find Available Media (Volume) for Pool
194 * Find a Volume for a given PoolId, MediaType, and Status.
196 * Returns: 0 on failure
200 db_find_next_volume(JCR *jcr, B_DB *mdb, int item, MEDIA_DBR *mr)
206 if (item == -1) { /* find oldest volume */
207 /* Find oldest volume */
208 Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
209 VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,\
210 VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,\
211 FirstWritten,LastWritten \
212 FROM Media WHERE PoolId=%d AND MediaType='%s' AND VolStatus IN ('Full',\
213 'Recycle','Purged','Used') \
214 ORDER BY FirstWritten", mr->PoolId, mr->MediaType);
217 /* Find next available volume */
218 Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
219 VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,\
220 VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,\
221 FirstWritten,LastWritten \
222 FROM Media WHERE PoolId=%d AND MediaType='%s' AND VolStatus='%s' \
223 ORDER BY MediaId", mr->PoolId, mr->MediaType, mr->VolStatus);
225 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
230 numrows = sql_num_rows(mdb);
231 if (item > numrows) {
232 Mmsg2(&mdb->errmsg, _("Request for Volume item %d greater than max %d\n"),
238 /* Seek to desired item
239 * Note, we use base 1; SQL uses base 0
241 sql_data_seek(mdb, item-1);
243 if ((row = sql_fetch_row(mdb)) == NULL) {
244 Mmsg1(&mdb->errmsg, _("No Volume record found for item %d.\n"), item);
245 sql_free_result(mdb);
250 /* Return fields in Media Record */
251 mr->MediaId = str_to_int64(row[0]);
252 bstrncpy(mr->VolumeName, row[1], sizeof(mr->VolumeName));
253 mr->VolJobs = str_to_int64(row[2]);
254 mr->VolFiles = str_to_int64(row[3]);
255 mr->VolBlocks = str_to_int64(row[4]);
256 mr->VolBytes = str_to_uint64(row[5]);
257 mr->VolMounts = str_to_int64(row[6]);
258 mr->VolErrors = str_to_int64(row[7]);
259 mr->VolWrites = str_to_int64(row[8]);
260 mr->MaxVolBytes = str_to_uint64(row[9]);
261 mr->VolCapacityBytes = str_to_uint64(row[10]);
262 mr->VolRetention = str_to_uint64(row[11]);
263 mr->VolUseDuration = str_to_uint64(row[12]);
264 mr->MaxVolJobs = str_to_int64(row[13]);
265 mr->MaxVolFiles = str_to_int64(row[14]);
266 mr->Recycle = str_to_int64(row[15]);
267 mr->Slot = str_to_int64(row[16]);
268 bstrncpy(mr->cFirstWritten, row[17]!=NULL?row[17]:"", sizeof(mr->cFirstWritten));
269 mr->FirstWritten = (time_t)str_to_utime(mr->cFirstWritten);
270 bstrncpy(mr->cLastWritten, row[18]!=NULL?row[18]:"", sizeof(mr->cLastWritten));
271 mr->LastWritten = (time_t)str_to_utime(mr->cLastWritten);
273 sql_free_result(mdb);
280 #endif /* HAVE_MYSQL || HAVE_SQLITE */