]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_find.c
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[bacula/bacula] / bacula / src / cats / sql_find.c
1 /*
2  * Bacula Catalog Database Find record interface routines
3  *
4  *  Note, generally, these routines are more complicated
5  *        that a simple search by name or id. Such simple
6  *        request are in get.c
7  *
8  *    Kern Sibbald, December 2000
9  *
10  *    Version $Id$
11  */
12
13 /*
14    Copyright (C) 2000-2004 Kern Sibbald and John Walker
15
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.
20
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.
25
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,
29    MA 02111-1307, USA.
30
31  */
32
33
34 /* The following is necessary so that we do not include
35  * the dummy external definition of DB.
36  */
37 #define __SQL_C                       /* indicate that this is sql.c */
38
39 #include "bacula.h"
40 #include "cats.h"
41
42 #if    HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL
43
44 /* -----------------------------------------------------------------------
45  *
46  *   Generic Routines (or almost generic)
47  *
48  * -----------------------------------------------------------------------
49  */
50
51 /* Imported subroutines */
52 extern void print_result(B_DB *mdb);
53 extern int QueryDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
54
55 /*
56  * Find job start time if JobId specified, otherwise
57  * find last full save for Incremental and Differential saves.
58  *
59  *  StartTime is returned in stime
60  *
61  * Returns: 0 on failure
62  *          1 on success, jr is unchanged, but stime is set
63  */
64 int
65 db_find_job_start_time(JCR *jcr, B_DB *mdb, JOB_DBR *jr, POOLMEM **stime)
66 {
67    SQL_ROW row;
68
69    db_lock(mdb);
70
71    pm_strcpy(stime, "0000-00-00 00:00:00");   /* default */
72    /* If no Id given, we must find corresponding job */
73    if (jr->JobId == 0) {
74       /* Differential is since last Full backup */
75          Mmsg(mdb->cmd,
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->JobType, L_FULL, jr->Name, jr->ClientId, jr->FileSetId);
80
81       if (jr->JobLevel == L_DIFFERENTIAL) {
82          /* SQL cmd for Differential backup already edited above */
83
84       /* Incremental is since last Full, Incremental, or Differential */
85       } else if (jr->JobLevel == L_INCREMENTAL) {
86          /*
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
90           *  backup
91           */
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);
95             db_unlock(mdb);
96             return 0;
97          }
98          if ((row = sql_fetch_row(mdb)) == NULL) {
99             sql_free_result(mdb);
100             Mmsg(mdb->errmsg, _("No prior Full backup Job record found.\n"));
101             db_unlock(mdb);
102             return 0;
103          }
104          sql_free_result(mdb);
105          /* Now edit SQL command for Incremental Job */
106          Mmsg(mdb->cmd,
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->JobType, L_INCREMENTAL, L_DIFFERENTIAL, L_FULL, jr->Name,
111             jr->ClientId, jr->FileSetId);
112       } else {
113          Mmsg1(&mdb->errmsg, _("Unknown level=%d\n"), jr->JobLevel);
114          db_unlock(mdb);
115          return 0;
116       }
117    } else {
118       Dmsg1(100, "Submitting: %s\n", mdb->cmd);
119       Mmsg(mdb->cmd, "SELECT StartTime FROM Job WHERE Job.JobId=%u", jr->JobId);
120    }
121
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);
126       db_unlock(mdb);
127       return 0;
128    }
129
130    if ((row = sql_fetch_row(mdb)) == NULL) {
131       Mmsg2(&mdb->errmsg, _("No Job record found: ERR=%s\nCMD=%s\n"),
132          sql_strerror(mdb),  mdb->cmd);
133       sql_free_result(mdb);
134       db_unlock(mdb);
135       return 0;
136    }
137    Dmsg1(100, "Got start time: %s\n", row[0]);
138    pm_strcpy(stime, row[0]);
139
140    sql_free_result(mdb);
141
142    db_unlock(mdb);
143    return 1;
144 }
145
146 /*
147  * Find last failed job since given start-time
148  *   it must be either Full or Diff.
149  *
150  * Returns: false on failure
151  *          true  on success, jr is unchanged and stime unchanged
152  *                level returned in JobLevel
153  */
154 bool
155 db_find_failed_job_since(JCR *jcr, B_DB *mdb, JOB_DBR *jr, POOLMEM *stime, int &JobLevel)
156 {
157    SQL_ROW row;
158
159    db_lock(mdb);
160    /* Differential is since last Full backup */
161    Mmsg(mdb->cmd,
162 "SELECT Level FROM Job WHERE JobStatus!='T' AND Type='%c' AND "
163 "Level IN ('%c','%c') AND Name='%s' AND ClientId=%u "
164 "AND FileSetId=%u AND StartTime>'%s' "
165 "ORDER BY StartTime DESC LIMIT 1",
166          jr->JobType, L_FULL, L_DIFFERENTIAL, jr->Name,
167          jr->ClientId, jr->FileSetId, stime);
168
169    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
170       db_unlock(mdb);
171       return false;
172    }
173
174    if ((row = sql_fetch_row(mdb)) == NULL) {
175       sql_free_result(mdb);
176       db_unlock(mdb);
177       return false;
178    }
179    JobLevel = (int)*row[0];
180    sql_free_result(mdb);
181
182    db_unlock(mdb);
183    return true;
184 }
185
186
187 /*
188  * Find JobId of last job that ran.  E.g. for
189  *   VERIFY_CATALOG we want the JobId of the last INIT.
190  *   For VERIFY_VOLUME_TO_CATALOG, we want the JobId of the last Job.
191  *
192  * Returns: 1 on success
193  *          0 on failure
194  */
195 int
196 db_find_last_jobid(JCR *jcr, B_DB *mdb, const char *Name, JOB_DBR *jr)
197 {
198    SQL_ROW row;
199
200    /* Find last full */
201    db_lock(mdb);
202    if (jr->JobLevel == L_VERIFY_CATALOG) {
203       Mmsg(mdb->cmd,
204 "SELECT JobId FROM Job WHERE Type='V' AND Level='%c' AND "
205 " JobStatus='T' AND Name='%s' AND "
206 "ClientId=%u ORDER BY StartTime DESC LIMIT 1",
207            L_VERIFY_INIT, jr->Name, jr->ClientId);
208    } else if (jr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG ||
209               jr->JobLevel == L_VERIFY_DISK_TO_CATALOG) {
210       if (Name) {
211          Mmsg(mdb->cmd,
212 "SELECT JobId FROM Job WHERE Type='B' AND JobStatus='T' AND "
213 "Name='%s' ORDER BY StartTime DESC LIMIT 1", Name);
214       } else {
215          Mmsg(mdb->cmd,
216 "SELECT JobId FROM Job WHERE Type='B' AND JobStatus='T' AND "
217 "ClientId=%u ORDER BY StartTime DESC LIMIT 1", jr->ClientId);
218       }
219    } else {
220       Mmsg1(&mdb->errmsg, _("Unknown Job level=%c\n"), jr->JobLevel);
221       db_unlock(mdb);
222       return 0;
223    }
224    Dmsg1(100, "Query: %s\n", mdb->cmd);
225    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
226       db_unlock(mdb);
227       return 0;
228    }
229    if ((row = sql_fetch_row(mdb)) == NULL) {
230       Mmsg1(&mdb->errmsg, _("No Job found for: %s.\n"), mdb->cmd);
231       sql_free_result(mdb);
232       db_unlock(mdb);
233       return 0;
234    }
235
236    jr->JobId = atoi(row[0]);
237    sql_free_result(mdb);
238
239    Dmsg1(100, "db_get_last_jobid: got JobId=%d\n", jr->JobId);
240    if (jr->JobId <= 0) {
241       Mmsg1(&mdb->errmsg, _("No Job found for: %s\n"), mdb->cmd);
242       db_unlock(mdb);
243       return 0;
244    }
245
246    db_unlock(mdb);
247    return 1;
248 }
249
250 /*
251  * Find Available Media (Volume) for Pool
252  *
253  * Find a Volume for a given PoolId, MediaType, and Status.
254  *
255  * Returns: 0 on failure
256  *          numrows on success
257  */
258 int
259 db_find_next_volume(JCR *jcr, B_DB *mdb, int item, bool InChanger, MEDIA_DBR *mr)
260 {
261    SQL_ROW row;
262    int numrows;
263    const char *changer, *order;
264
265    db_lock(mdb);
266    if (item == -1) {       /* find oldest volume */
267       /* Find oldest volume */
268       Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,"
269           "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
270           "VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,"
271           "FirstWritten,LastWritten,VolStatus,InChanger "
272           "FROM Media WHERE PoolId=%u AND MediaType='%s' AND VolStatus IN ('Full',"
273           "'Recycle','Purged','Used','Append') "
274           "ORDER BY LastWritten LIMIT 1", mr->PoolId, mr->MediaType);
275      item = 1;
276    } else {
277       /* Find next available volume */
278       if (InChanger) {
279          changer = "AND InChanger=1";
280       } else {
281          changer = "";
282       }
283       if (strcmp(mr->VolStatus, "Recycled") == 0 ||
284           strcmp(mr->VolStatus, "Purged") == 0) {
285          order = "ORDER BY LastWritten ASC,MediaId";  /* take oldest */
286       } else {
287          order = "ORDER BY LastWritten IS NULL,LastWritten DESC,MediaId";   /* take most recently written */
288       }
289       Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,"
290           "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
291           "VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,"
292           "FirstWritten,LastWritten,VolStatus,InChanger "
293           "FROM Media WHERE PoolId=%u AND MediaType='%s' AND VolStatus='%s' "
294           "%s "
295           "%s LIMIT %d",
296           mr->PoolId, mr->MediaType, mr->VolStatus, changer, order, item);
297    }
298    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
299       db_unlock(mdb);
300       return 0;
301    }
302
303    numrows = sql_num_rows(mdb);
304    if (item > numrows) {
305       Mmsg2(&mdb->errmsg, _("Request for Volume item %d greater than max %d\n"),
306          item, numrows);
307       db_unlock(mdb);
308       return 0;
309    }
310
311    /* Seek to desired item
312     * Note, we use base 1; SQL uses base 0
313     */
314    sql_data_seek(mdb, item-1);
315
316    if ((row = sql_fetch_row(mdb)) == NULL) {
317       Mmsg1(&mdb->errmsg, _("No Volume record found for item %d.\n"), item);
318       sql_free_result(mdb);
319       db_unlock(mdb);
320       return 0;
321    }
322
323    /* Return fields in Media Record */
324    mr->MediaId = str_to_int64(row[0]);
325    bstrncpy(mr->VolumeName, row[1], sizeof(mr->VolumeName));
326    mr->VolJobs = str_to_int64(row[2]);
327    mr->VolFiles = str_to_int64(row[3]);
328    mr->VolBlocks = str_to_int64(row[4]);
329    mr->VolBytes = str_to_uint64(row[5]);
330    mr->VolMounts = str_to_int64(row[6]);
331    mr->VolErrors = str_to_int64(row[7]);
332    mr->VolWrites = str_to_int64(row[8]);
333    mr->MaxVolBytes = str_to_uint64(row[9]);
334    mr->VolCapacityBytes = str_to_uint64(row[10]);
335    mr->VolRetention = str_to_uint64(row[11]);
336    mr->VolUseDuration = str_to_uint64(row[12]);
337    mr->MaxVolJobs = str_to_int64(row[13]);
338    mr->MaxVolFiles = str_to_int64(row[14]);
339    mr->Recycle = str_to_int64(row[15]);
340    mr->Slot = str_to_int64(row[16]);
341    bstrncpy(mr->cFirstWritten, row[17]!=NULL?row[17]:"", sizeof(mr->cFirstWritten));
342    mr->FirstWritten = (time_t)str_to_utime(mr->cFirstWritten);
343    bstrncpy(mr->cLastWritten, row[18]!=NULL?row[18]:"", sizeof(mr->cLastWritten));
344    mr->LastWritten = (time_t)str_to_utime(mr->cLastWritten);
345    bstrncpy(mr->VolStatus, row[19], sizeof(mr->VolStatus));
346    mr->InChanger = str_to_int64(row[20]);
347    sql_free_result(mdb);
348
349    db_unlock(mdb);
350    return numrows;
351 }
352
353
354 #endif /* HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL */