2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Bacula Catalog Database Update record interface routines
31 * Kern Sibbald, March 2000
33 * Version $Id: sql_update.c 8478 2009-02-18 20:11:55Z kerns $
36 /* The following is necessary so that we do not include
37 * the dummy external definition of DB.
39 #define __SQL_C /* indicate that this is sql.c */
44 #if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI
46 /* -----------------------------------------------------------------------
48 * Generic Routines (or almost generic)
50 * -----------------------------------------------------------------------
53 /* -----------------------------------------------------------------------
55 * Generic Routines (or almost generic)
57 * -----------------------------------------------------------------------
59 /* Update the attributes record by adding the file digest */
61 db_add_digest_to_file_record(JCR *jcr, B_DB *mdb, FileId_t FileId, char *digest,
68 Mmsg(mdb->cmd, "UPDATE File SET MD5='%s' WHERE FileId=%s", digest,
69 edit_int64(FileId, ed1));
70 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
75 /* Mark the file record as being visited during database
76 * verify compare. Stuff JobId into the MarkId field
78 int db_mark_file_record(JCR *jcr, B_DB *mdb, FileId_t FileId, JobId_t JobId)
81 char ed1[50], ed2[50];
84 Mmsg(mdb->cmd, "UPDATE File SET MarkId=%s WHERE FileId=%s",
85 edit_int64(JobId, ed1), edit_int64(FileId, ed2));
86 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
92 * Update the Job record at start of Job
94 * Returns: false on failure
98 db_update_job_start_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
100 char dt[MAX_TIME_LENGTH];
105 char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50];
107 stime = jr->StartTime;
108 (void)localtime_r(&stime, &tm);
109 strftime(dt, sizeof(dt), "%Y-%m-%d %H:%M:%S", &tm);
110 JobTDate = (btime_t)stime;
113 Mmsg(mdb->cmd, "UPDATE Job SET JobStatus='%c',Level='%c',StartTime='%s',"
114 "ClientId=%s,JobTDate=%s,PoolId=%s,FileSetId=%s WHERE JobId=%s",
115 (char)(jcr->JobStatus),
116 (char)(jr->JobLevel), dt,
117 edit_int64(jr->ClientId, ed1),
118 edit_uint64(JobTDate, ed2),
119 edit_int64(jr->PoolId, ed3),
120 edit_int64(jr->FileSetId, ed4),
121 edit_int64(jr->JobId, ed5));
123 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
130 * Update Long term statistics with all jobs that were run before
134 db_update_stats(JCR *jcr, B_DB *mdb, utime_t age)
137 utime_t now = (utime_t)time(NULL);
138 edit_uint64(now - age, ed1);
140 Mmsg(mdb->cmd, fill_jobhisto, ed1);
141 QUERY_DB(jcr, mdb, mdb->cmd); /* TODO: get a message ? */
142 return sql_affected_rows(mdb);
146 * Update the Job record at end of Job
148 * Returns: 0 on failure
152 db_update_job_end_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
154 char dt[MAX_TIME_LENGTH];
155 char rdt[MAX_TIME_LENGTH];
159 char ed1[30], ed2[30], ed3[50], ed4[50];
163 if (jr->PriorJobId) {
164 bstrncpy(PriorJobId, edit_int64(jr->PriorJobId, ed1), sizeof(PriorJobId));
166 bstrncpy(PriorJobId, "0", sizeof(PriorJobId));
170 (void)localtime_r(&ttime, &tm);
171 strftime(dt, sizeof(dt), "%Y-%m-%d %H:%M:%S", &tm);
173 if (jr->RealEndTime == 0) {
174 jr->RealEndTime = jr->EndTime;
176 ttime = jr->RealEndTime;
177 (void)localtime_r(&ttime, &tm);
178 strftime(rdt, sizeof(rdt), "%Y-%m-%d %H:%M:%S", &tm);
184 "UPDATE Job SET JobStatus='%c',EndTime='%s',"
185 "ClientId=%u,JobBytes=%s,ReadBytes=%s,JobFiles=%u,JobErrors=%u,VolSessionId=%u,"
186 "VolSessionTime=%u,PoolId=%u,FileSetId=%u,JobTDate=%s,"
187 "RealEndTime='%s',PriorJobId=%s,HasBase=%u,PurgedFiles=%u WHERE JobId=%s",
188 (char)(jr->JobStatus), dt, jr->ClientId, edit_uint64(jr->JobBytes, ed1),
189 edit_uint64(jr->ReadBytes, ed4),
190 jr->JobFiles, jr->JobErrors, jr->VolSessionId, jr->VolSessionTime,
191 jr->PoolId, jr->FileSetId, edit_uint64(JobTDate, ed2),
192 rdt, PriorJobId, jr->HasBase, jr->PurgedFiles,
193 edit_int64(jr->JobId, ed3));
195 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
202 * Update Client record
203 * Returns: 0 on failure
207 db_update_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
210 char ed1[50], ed2[50];
214 memcpy(&tcr, cr, sizeof(tcr));
215 if (!db_create_client_record(jcr, mdb, &tcr)) {
221 "UPDATE Client SET AutoPrune=%d,FileRetention=%s,JobRetention=%s,"
222 "Uname='%s' WHERE Name='%s'",
224 edit_uint64(cr->FileRetention, ed1),
225 edit_uint64(cr->JobRetention, ed2),
226 cr->Uname, cr->Name);
228 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
235 * Update Counters record
236 * Returns: 0 on failure
239 int db_update_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
244 "UPDATE Counters SET \"MinValue\"=%d,\"MaxValue\"=%d,CurrentValue=%d,"
245 "WrapCounter='%s' WHERE Counter='%s'",
246 cr->MinValue, cr->MaxValue, cr->CurrentValue,
247 cr->WrapCounter, cr->Counter);
249 int stat = UPDATE_DB(jcr, mdb, mdb->cmd);
255 int db_update_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
258 char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50], ed6[50];
261 Mmsg(mdb->cmd, "SELECT count(*) from Media WHERE PoolId=%s",
262 edit_int64(pr->PoolId, ed4));
263 pr->NumVols = get_sql_record_max(jcr, mdb);
264 Dmsg1(400, "NumVols=%d\n", pr->NumVols);
267 "UPDATE Pool SET NumVols=%u,MaxVols=%u,UseOnce=%d,UseCatalog=%d,"
268 "AcceptAnyVolume=%d,VolRetention='%s',VolUseDuration='%s',"
269 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,Recycle=%d,"
270 "AutoPrune=%d,LabelType=%d,LabelFormat='%s',RecyclePoolId=%s,"
271 "ScratchPoolId=%s,ActionOnPurge=%d WHERE PoolId=%s",
272 pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
273 pr->AcceptAnyVolume, edit_uint64(pr->VolRetention, ed1),
274 edit_uint64(pr->VolUseDuration, ed2),
275 pr->MaxVolJobs, pr->MaxVolFiles,
276 edit_uint64(pr->MaxVolBytes, ed3),
277 pr->Recycle, pr->AutoPrune, pr->LabelType,
278 pr->LabelFormat, edit_int64(pr->RecyclePoolId,ed5),
279 edit_int64(pr->ScratchPoolId,ed6),
282 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
288 db_update_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *sr)
293 Mmsg(mdb->cmd, "UPDATE Storage SET AutoChanger=%d WHERE StorageId=%s",
294 sr->AutoChanger, edit_int64(sr->StorageId, ed1));
296 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
303 * Update the Media Record at end of Session
305 * Returns: 0 on failure
309 db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
311 char dt[MAX_TIME_LENGTH];
315 char ed1[50], ed2[50], ed3[50], ed4[50];
316 char ed5[50], ed6[50], ed7[50], ed8[50];
317 char ed9[50], ed10[50], ed11[50];
320 Dmsg1(100, "update_media: FirstWritten=%d\n", mr->FirstWritten);
322 if (mr->set_first_written) {
323 Dmsg1(400, "Set FirstWritten Vol=%s\n", mr->VolumeName);
324 ttime = mr->FirstWritten;
325 (void)localtime_r(&ttime, &tm);
326 strftime(dt, sizeof(dt), "%Y-%m-%d %H:%M:%S", &tm);
327 Mmsg(mdb->cmd, "UPDATE Media SET FirstWritten='%s'"
328 " WHERE VolumeName='%s'", dt, mr->VolumeName);
329 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
330 Dmsg1(400, "Firstwritten=%d\n", mr->FirstWritten);
333 /* Label just done? */
334 if (mr->set_label_date) {
335 ttime = mr->LabelDate;
339 (void)localtime_r(&ttime, &tm);
340 strftime(dt, sizeof(dt), "%Y-%m-%d %H:%M:%S", &tm);
341 Mmsg(mdb->cmd, "UPDATE Media SET LabelDate='%s' "
342 "WHERE VolumeName='%s'", dt, mr->VolumeName);
343 UPDATE_DB(jcr, mdb, mdb->cmd);
346 if (mr->LastWritten != 0) {
347 ttime = mr->LastWritten;
348 (void)localtime_r(&ttime, &tm);
349 strftime(dt, sizeof(dt), "%Y-%m-%d %H:%M:%S", &tm);
350 Mmsg(mdb->cmd, "UPDATE Media Set LastWritten='%s' "
351 "WHERE VolumeName='%s'", dt, mr->VolumeName);
352 UPDATE_DB(jcr, mdb, mdb->cmd);
355 /* sanity checks for #1066 */
356 if (mr->VolReadTime < 0) {
359 if (mr->VolWriteTime < 0) {
360 mr->VolWriteTime = 0;
363 Mmsg(mdb->cmd, "UPDATE Media SET VolJobs=%u,"
364 "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
365 "VolWrites=%u,MaxVolBytes=%s,VolStatus='%s',"
366 "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s,VolParts=%d,"
367 "LabelType=%d,StorageId=%s,PoolId=%s,VolRetention=%s,VolUseDuration=%s,"
368 "MaxVolJobs=%d,MaxVolFiles=%d,Enabled=%d,LocationId=%s,"
369 "ScratchPoolId=%s,RecyclePoolId=%s,RecycleCount=%d,Recycle=%d,ActionOnPurge=%d"
370 " WHERE VolumeName='%s'",
371 mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
372 mr->VolMounts, mr->VolErrors, mr->VolWrites,
373 edit_uint64(mr->MaxVolBytes, ed2),
374 mr->VolStatus, mr->Slot, mr->InChanger,
375 edit_int64(mr->VolReadTime, ed3),
376 edit_int64(mr->VolWriteTime, ed4),
379 edit_int64(mr->StorageId, ed5),
380 edit_int64(mr->PoolId, ed6),
381 edit_uint64(mr->VolRetention, ed7),
382 edit_uint64(mr->VolUseDuration, ed8),
383 mr->MaxVolJobs, mr->MaxVolFiles,
384 mr->Enabled, edit_uint64(mr->LocationId, ed9),
385 edit_uint64(mr->ScratchPoolId, ed10),
386 edit_uint64(mr->RecyclePoolId, ed11),
387 mr->RecycleCount,mr->Recycle, mr->ActionOnPurge,
390 Dmsg1(400, "%s\n", mdb->cmd);
392 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
394 /* Make sure InChanger is 0 for any record having the same Slot */
395 db_make_inchanger_unique(jcr, mdb, mr);
402 * Update the Media Record Default values from Pool
404 * Returns: 0 on failure
408 db_update_media_defaults(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
411 char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50];
415 if (mr->VolumeName[0]) {
416 Mmsg(mdb->cmd, "UPDATE Media SET "
417 "ActionOnPurge=%d, Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
418 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,RecyclePoolId=%s"
419 " WHERE VolumeName='%s'",
420 mr->ActionOnPurge, mr->Recycle,edit_uint64(mr->VolRetention, ed1),
421 edit_uint64(mr->VolUseDuration, ed2),
422 mr->MaxVolJobs, mr->MaxVolFiles,
423 edit_uint64(mr->MaxVolBytes, ed3),
424 edit_uint64(mr->RecyclePoolId, ed4),
427 Mmsg(mdb->cmd, "UPDATE Media SET "
428 "ActionOnPurge=%d, Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
429 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,RecyclePoolId=%s"
431 mr->ActionOnPurge, mr->Recycle,edit_uint64(mr->VolRetention, ed1),
432 edit_uint64(mr->VolUseDuration, ed2),
433 mr->MaxVolJobs, mr->MaxVolFiles,
434 edit_uint64(mr->MaxVolBytes, ed3),
435 edit_int64(mr->RecyclePoolId, ed4),
436 edit_int64(mr->PoolId, ed5));
439 Dmsg1(400, "%s\n", mdb->cmd);
441 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
449 * If we have a non-zero InChanger, ensure that no other Media
450 * record has InChanger set on the same Slot.
452 * This routine assumes the database is already locked.
455 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
457 char ed1[50], ed2[50];
458 if (mr->InChanger != 0 && mr->Slot != 0 && mr->StorageId != 0) {
460 if (mr->MediaId != 0) {
461 Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0, Slot=0 WHERE "
462 "Slot=%d AND StorageId=%s AND MediaId!=%s",
464 edit_int64(mr->StorageId, ed1), edit_int64(mr->MediaId, ed2));
466 } else if (*mr->VolumeName) {
467 Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0, Slot=0 WHERE "
468 "Slot=%d AND StorageId=%s AND VolumeName!='%s'",
470 edit_int64(mr->StorageId, ed1), mr->VolumeName);
472 } else { /* used by ua_label to reset all volume with this slot */
473 Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0, Slot=0 WHERE "
474 "Slot=%d AND StorageId=%s",
476 edit_int64(mr->StorageId, ed1), mr->VolumeName);
478 Dmsg1(100, "%s\n", mdb->cmd);
479 UPDATE_DB(jcr, mdb, mdb->cmd);
483 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL || HAVE_INGRES */