]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_find.c
Fix JobLevel for UA started Admin jobs; add -v to startup scripts
[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-2003 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
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(char *file, int line, void *jcr, B_DB *db, char *select_cmd);
54
55 /*
56  * Find job start time. Used to find last full save
57  * for Incremental and Differential saves.
58  *      
59  * Returns: 0 on failure
60  *          1 on success, jr is unchanged, but stime is set
61  */
62 int
63 db_find_job_start_time(void *jcr, B_DB *mdb, JOB_DBR *jr, POOLMEM **stime)
64 {
65    SQL_ROW row;
66    int JobId;
67
68    pm_strcpy(stime, "0000-00-00 00:00:00");   /* default */
69    db_lock(mdb);
70    /* If no Id given, we must find corresponding job */
71    if (jr->JobId == 0) {
72       /* Differential is since last Full backup */
73       if (jr->Level == L_DIFFERENTIAL) {
74          Mmsg(&mdb->cmd, 
75 "SELECT JobId FROM Job WHERE JobStatus='T' AND Type='%c' AND \
76 Level='%c' AND Name='%s' AND ClientId=%d AND FileSetId=%d \
77 ORDER BY StartTime DESC LIMIT 1",
78            jr->Type, L_FULL, jr->Name, jr->ClientId, jr->FileSetId);
79       /* Incremental is since last Full, Incremental, or Differential */
80       } else if (jr->Level == L_INCREMENTAL) {
81          Mmsg(&mdb->cmd, 
82 "SELECT JobId FROM Job WHERE JobStatus='T' AND Type='%c' AND \
83 Level IN ('%c','%c','%c') AND Name='%s' AND ClientId=%d \
84 ORDER BY StartTime DESC LIMIT 1",
85            jr->Type, L_INCREMENTAL, L_DIFFERENTIAL, L_FULL, jr->Name,
86            jr->ClientId);
87       } else {
88          Mmsg1(&mdb->errmsg, _("Unknown level=%d\n"), jr->Level);
89          db_unlock(mdb);
90          return 0;
91       }
92       Dmsg1(100, "Submitting: %s\n", mdb->cmd);
93       if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
94          Mmsg1(&mdb->errmsg, _("Query error for start time request: %s\n"), mdb->cmd);
95          db_unlock(mdb);
96          return 0;
97       }
98       if ((row = sql_fetch_row(mdb)) == NULL) {
99          sql_free_result(mdb);
100          db_unlock(mdb);
101          return 0;
102       }
103       JobId = atoi(row[0]);
104       sql_free_result(mdb);
105    } else {
106       JobId = jr->JobId;              /* search for particular id */
107    }
108
109    Dmsg1(100, "Submitting: %s\n", mdb->cmd);
110    Mmsg(&mdb->cmd, "SELECT StartTime FROM Job WHERE Job.JobId=%d", JobId);
111
112    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
113       Mmsg1(&mdb->errmsg, _("Query error for start time request: %s\n"), mdb->cmd);
114       db_unlock(mdb);
115       return 0;
116    }
117
118    if ((row = sql_fetch_row(mdb)) == NULL) {
119       pm_strcpy(stime, "");                   /* set EOS */
120       Mmsg2(&mdb->errmsg, _("No Job found for JobId=%d: %s\n"), JobId, sql_strerror(mdb));
121       sql_free_result(mdb);
122       db_unlock(mdb);
123       return 0;
124    }
125    Dmsg1(100, "Got start time: %s\n", row[0]);
126    pm_strcpy(stime, row[0]);
127
128    sql_free_result(mdb);
129
130    db_unlock(mdb);
131    return 1;
132 }
133
134 /* 
135  * Find JobId of last job that ran.  E.g. for
136  *   VERIFY_CATALOG we want the JobId of the last INIT.
137  *   For VERIFY_VOLUME_TO_CATALOG, we want the JobId of the last Job.
138  *
139  * Returns: 1 on success
140  *          0 on failure
141  */
142 int
143 db_find_last_jobid(void *jcr, B_DB *mdb, JOB_DBR *jr)
144 {
145    SQL_ROW row;
146
147    /* Find last full */
148    db_lock(mdb);
149    if (jr->Level == L_VERIFY_CATALOG) {
150       Mmsg(&mdb->cmd, 
151 "SELECT JobId FROM Job WHERE Type='%c' AND Level='%c' AND Name='%s' AND \
152 ClientId=%d ORDER BY StartTime DESC LIMIT 1",
153            JT_VERIFY, L_VERIFY_INIT, jr->Name, jr->ClientId);
154    } else if (jr->Level == L_VERIFY_VOLUME_TO_CATALOG) {
155       Mmsg(&mdb->cmd, 
156 "SELECT JobId FROM Job WHERE Type='%c' AND \
157 ClientId=%d ORDER BY StartTime DESC LIMIT 1",
158            JT_BACKUP, jr->ClientId);
159    } else {
160       Mmsg1(&mdb->errmsg, _("Unknown Job level=%c\n"), jr->Level);
161       db_unlock(mdb);
162       return 0;
163    }
164
165    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
166       db_unlock(mdb);
167       return 0;
168    }
169    if ((row = sql_fetch_row(mdb)) == NULL) {
170       Mmsg1(&mdb->errmsg, _("No Job found for: %s.\n"), mdb->cmd);
171       sql_free_result(mdb);
172       db_unlock(mdb);
173       return 0;
174    }
175
176    jr->JobId = atoi(row[0]);
177    sql_free_result(mdb);
178
179    Dmsg1(100, "db_get_last_jobid: got JobId=%d\n", jr->JobId);
180    if (jr->JobId <= 0) {
181       Mmsg1(&mdb->errmsg, _("No Job found for: %s\n"), mdb->cmd);
182       db_unlock(mdb);
183       return 0;
184    }
185
186    db_unlock(mdb);
187    return 1;
188 }
189
190 /* 
191  * Find Available Media (Volume) for Pool
192  *
193  * Find a Volume for a given PoolId, MediaType, and Status.
194  *
195  * Returns: 0 on failure
196  *          numrows on success
197  */
198 int
199 db_find_next_volume(void *jcr, B_DB *mdb, int item, MEDIA_DBR *mr) 
200 {
201    SQL_ROW row;
202    int numrows;
203
204    db_lock(mdb);
205    if (item == -1) {       /* find oldest volume */
206       /* Find oldest volume */
207       Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
208 VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,\
209 VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,\
210 FirstWritten,LastWritten \
211 FROM Media WHERE PoolId=%d AND MediaType='%s' AND VolStatus IN ('Full',\
212 'Recycle','Purged','Used') \
213 ORDER BY FirstWritten", mr->PoolId, mr->MediaType); 
214      item = 1;
215    } else {
216       /* Find next available volume */
217       Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
218 VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,\
219 VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,Recycle,Slot,\
220 FirstWritten,LastWritten \
221 FROM Media WHERE PoolId=%d AND MediaType='%s' AND VolStatus='%s' \
222 ORDER BY MediaId", mr->PoolId, mr->MediaType, mr->VolStatus); 
223    }
224    if (!QUERY_DB(jcr, mdb, mdb->cmd)) {
225       db_unlock(mdb);
226       return 0;
227    }
228
229    numrows = sql_num_rows(mdb);
230    if (item > numrows) {
231       Mmsg2(&mdb->errmsg, _("Request for Volume item %d greater than max %d\n"),
232          item, numrows);
233       db_unlock(mdb);
234       return 0;
235    }
236    
237    /* Seek to desired item 
238     * Note, we use base 1; SQL uses base 0
239     */
240    sql_data_seek(mdb, item-1);
241
242    if ((row = sql_fetch_row(mdb)) == NULL) {
243       Mmsg1(&mdb->errmsg, _("No Volume record found for item %d.\n"), item);
244       sql_free_result(mdb);
245       db_unlock(mdb);
246       return 0;
247    }
248
249    /* Return fields in Media Record */
250    mr->MediaId = str_to_int64(row[0]);
251    bstrncpy(mr->VolumeName, row[1], sizeof(mr->VolumeName));
252    mr->VolJobs = str_to_int64(row[2]);
253    mr->VolFiles = str_to_int64(row[3]);
254    mr->VolBlocks = str_to_int64(row[4]);
255    mr->VolBytes = str_to_uint64(row[5]);
256    mr->VolMounts = str_to_int64(row[6]);
257    mr->VolErrors = str_to_int64(row[7]);
258    mr->VolWrites = str_to_int64(row[8]);
259    mr->MaxVolBytes = str_to_uint64(row[9]);
260    mr->VolCapacityBytes = str_to_uint64(row[10]);
261    mr->VolRetention = str_to_uint64(row[11]);
262    mr->VolUseDuration = str_to_uint64(row[12]);
263    mr->MaxVolJobs = str_to_int64(row[13]);
264    mr->MaxVolFiles = str_to_int64(row[14]);
265    mr->Recycle = str_to_int64(row[15]);
266    mr->Slot = str_to_int64(row[16]);
267    bstrncpy(mr->cFirstWritten, row[17]!=NULL?row[17]:"", sizeof(mr->cFirstWritten));
268    mr->FirstWritten = (time_t)str_to_utime(mr->cFirstWritten);
269    bstrncpy(mr->cLastWritten, row[18]!=NULL?row[18]:"", sizeof(mr->cLastWritten));
270    mr->LastWritten = (time_t)str_to_utime(mr->cLastWritten);
271
272    sql_free_result(mdb);
273
274    db_unlock(mdb);
275    return numrows;
276 }
277
278
279 #endif /* HAVE_MYSQL || HAVE_SQLITE */