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