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 || HAVE_POSTGRESQL
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 if JobId specified, otherwise
57 * find last full save for Incremental and Differential saves.
59 * StartTime is returned in stime
61 * Returns: 0 on failure
62 * 1 on success, jr is unchanged, but stime is set
65 db_find_job_start_time(JCR *jcr, B_DB *mdb, JOB_DBR *jr, POOLMEM **stime)
71 pm_strcpy(stime, "0000-00-00 00:00:00"); /* default */
72 /* If no Id given, we must find corresponding job */
74 /* Differential is since last Full backup */
76 "SELECT StartTime FROM Job WHERE JobStatus='T' AND Type='%c' AND "
77 "Level='%c' AND Name='%s' AND ClientId=%u AND FileSetId=%u "
78 "ORDER BY StartTime DESC LIMIT 1",
79 jr->Type, L_FULL, jr->Name, jr->ClientId, jr->FileSetId);
81 if (jr->Level == L_DIFFERENTIAL) {
82 /* SQL cmd for Differential backup already edited above */
84 /* Incremental is since last Full, Incremental, or Differential */
85 } else if (jr->Level == L_INCREMENTAL) {
87 * For an Incremental job, we must first ensure
88 * that a Full backup was done (cmd edited above)
89 * then we do a second look to find the most recent
92 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
93 Mmsg2(&mdb->errmsg, _("Query error for start time request: ERR=%s\nCMD=%s\n"),
94 sql_strerror(mdb), mdb->cmd);
98 if ((row = sql_fetch_row(mdb)) == NULL) {
100 Mmsg(&mdb->errmsg, _("No prior Full backup Job record found.\n"));
104 sql_free_result(mdb);
105 /* Now edit SQL command for Incremental Job */
107 "SELECT StartTime FROM Job WHERE JobStatus='T' AND Type='%c' AND "
108 "Level IN ('%c','%c','%c') AND Name='%s' AND ClientId=%u "
109 "AND FileSetId=%u ORDER BY StartTime DESC LIMIT 1",
110 jr->Type, L_INCREMENTAL, L_DIFFERENTIAL, L_FULL, jr->Name,
111 jr->ClientId, jr->FileSetId);
113 Mmsg1(&mdb->errmsg, _("Unknown level=%d\n"), jr->Level);
118 Dmsg1(100, "Submitting: %s\n", mdb->cmd);
119 Mmsg(&mdb->cmd, "SELECT StartTime FROM Job WHERE Job.JobId=%u", jr->JobId);
122 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
123 pm_strcpy(stime, ""); /* set EOS */
124 Mmsg2(&mdb->errmsg, _("Query error for start time request: ERR=%s\nCMD=%s\n"),
125 sql_strerror(mdb), mdb->cmd);
130 if ((row = sql_fetch_row(mdb)) == NULL) {
131 Mmsg1(&mdb->errmsg, _("No Job record found: ERR=%s\n"), sql_strerror(mdb));
132 sql_free_result(mdb);
136 Dmsg1(100, "Got start time: %s\n", row[0]);
137 pm_strcpy(stime, row[0]);
139 sql_free_result(mdb);
146 * Find JobId of last job that ran. E.g. for
147 * VERIFY_CATALOG we want the JobId of the last INIT.
148 * For VERIFY_VOLUME_TO_CATALOG, we want the JobId of the last Job.
150 * Returns: 1 on success
154 db_find_last_jobid(JCR *jcr, B_DB *mdb, char *Name, JOB_DBR *jr)
160 if (jr->Level == L_VERIFY_CATALOG) {
162 "SELECT JobId FROM Job WHERE Type='V' AND Level='%c' AND "
163 " JobStatus='T' AND Name='%s' AND "
164 "ClientId=%u ORDER BY StartTime DESC LIMIT 1",
165 L_VERIFY_INIT, jr->Name, jr->ClientId);
166 } else if (jr->Level == L_VERIFY_VOLUME_TO_CATALOG ||
167 jr->Level == L_VERIFY_DISK_TO_CATALOG) {
170 "SELECT JobId FROM Job WHERE Type='B' AND JobStatus='T' AND "
171 "Name='%s' ORDER BY StartTime DESC LIMIT 1", Name);
174 "SELECT JobId FROM Job WHERE Type='B' AND JobStatus='T' AND "
175 "ClientId=%u ORDER BY StartTime DESC LIMIT 1", jr->ClientId);
178 Mmsg1(&mdb->errmsg, _("Unknown Job level=%c\n"), jr->Level);
182 Dmsg1(100, "Query: %s\n", mdb->cmd);
183 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
187 if ((row = sql_fetch_row(mdb)) == NULL) {
188 Mmsg1(&mdb->errmsg, _("No Job found for: %s.\n"), mdb->cmd);
189 sql_free_result(mdb);
194 jr->JobId = atoi(row[0]);
195 sql_free_result(mdb);
197 Dmsg1(100, "db_get_last_jobid: got JobId=%d\n", jr->JobId);
198 if (jr->JobId <= 0) {
199 Mmsg1(&mdb->errmsg, _("No Job found for: %s\n"), mdb->cmd);
209 * Find Available Media (Volume) for Pool
211 * Find a Volume for a given PoolId, MediaType, and Status.
213 * Returns: 0 on failure
217 db_find_next_volume(JCR *jcr, B_DB *mdb, int item, bool InChanger, MEDIA_DBR *mr)
223 if (item == -1) { /* find oldest volume */
224 /* Find oldest volume */
225 Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,"
226 "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
227 "VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,"
228 "FirstWritten,LastWritten,VolStatus "
229 "FROM Media WHERE PoolId=%u AND MediaType='%s' AND VolStatus IN ('Full',"
230 "'Recycle','Purged','Used','Append') "
231 "ORDER BY LastWritten LIMIT 1", mr->PoolId, mr->MediaType);
234 /* Find next available volume */
236 Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,"
237 "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
238 "VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,"
239 "FirstWritten,LastWritten,VolStatus "
240 "FROM Media WHERE PoolId=%u AND MediaType='%s' AND VolStatus='%s' "
241 "AND InChanger=1 ORDER BY LastWritten,MediaId",
242 mr->PoolId, mr->MediaType, mr->VolStatus);
244 Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,"
245 "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
246 "VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,"
247 "FirstWritten,LastWritten,VolStatus "
248 "FROM Media WHERE PoolId=%u AND MediaType='%s' AND VolStatus='%s' "
249 "ORDER BY LastWritten,MediaId",
250 mr->PoolId, mr->MediaType, mr->VolStatus);
253 if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
258 numrows = sql_num_rows(mdb);
259 if (item > numrows) {
260 Mmsg2(&mdb->errmsg, _("Request for Volume item %d greater than max %d\n"),
266 /* Seek to desired item
267 * Note, we use base 1; SQL uses base 0
269 sql_data_seek(mdb, item-1);
271 if ((row = sql_fetch_row(mdb)) == NULL) {
272 Mmsg1(&mdb->errmsg, _("No Volume record found for item %d.\n"), item);
273 sql_free_result(mdb);
278 /* Return fields in Media Record */
279 mr->MediaId = str_to_int64(row[0]);
280 bstrncpy(mr->VolumeName, row[1], sizeof(mr->VolumeName));
281 mr->VolJobs = str_to_int64(row[2]);
282 mr->VolFiles = str_to_int64(row[3]);
283 mr->VolBlocks = str_to_int64(row[4]);
284 mr->VolBytes = str_to_uint64(row[5]);
285 mr->VolMounts = str_to_int64(row[6]);
286 mr->VolErrors = str_to_int64(row[7]);
287 mr->VolWrites = str_to_int64(row[8]);
288 mr->MaxVolBytes = str_to_uint64(row[9]);
289 mr->VolCapacityBytes = str_to_uint64(row[10]);
290 mr->VolRetention = str_to_uint64(row[11]);
291 mr->VolUseDuration = str_to_uint64(row[12]);
292 mr->MaxVolJobs = str_to_int64(row[13]);
293 mr->MaxVolFiles = str_to_int64(row[14]);
294 mr->Recycle = str_to_int64(row[15]);
295 mr->Slot = str_to_int64(row[16]);
296 bstrncpy(mr->cFirstWritten, row[17]!=NULL?row[17]:"", sizeof(mr->cFirstWritten));
297 mr->FirstWritten = (time_t)str_to_utime(mr->cFirstWritten);
298 bstrncpy(mr->cLastWritten, row[18]!=NULL?row[18]:"", sizeof(mr->cLastWritten));
299 mr->LastWritten = (time_t)str_to_utime(mr->cLastWritten);
300 bstrncpy(mr->VolStatus, row[19], sizeof(mr->VolStatus));
302 sql_free_result(mdb);
309 #endif /* HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL */