3 * Program to scan a Bacula Volume and compare it with
4 * the catalog and optionally synchronize the catalog
7 * Kern E. Sibbald, December 2001
13 Copyright (C) 2001, 2002 Kern Sibbald and John Walker
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of
18 the License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public
26 License along with this program; if not, write to the Free
27 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 #include "findlib/find.h"
35 #include "cats/cats.h"
37 /* Forward referenced functions */
38 static void do_scan(void);
39 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
40 static int create_file_attributes_record(B_DB *db, JCR *mjcr,
41 char *fname, char *lname, int type,
42 char *ap, DEV_RECORD *rec);
43 static int create_media_record(B_DB *db, MEDIA_DBR *mr, VOLUME_LABEL *vl);
44 static int update_media_record(B_DB *db, MEDIA_DBR *mr);
45 static int create_pool_record(B_DB *db, POOL_DBR *pr);
46 static JCR *create_job_record(B_DB *db, JOB_DBR *mr, SESSION_LABEL *label, DEV_RECORD *rec);
47 static int update_job_record(B_DB *db, JOB_DBR *mr, SESSION_LABEL *elabel,
49 static int create_client_record(B_DB *db, CLIENT_DBR *cr);
50 static int create_fileset_record(B_DB *db, FILESET_DBR *fsr);
51 static int create_jobmedia_record(B_DB *db, JCR *jcr);
52 static JCR *create_jcr(JOB_DBR *jr, DEV_RECORD *rec, uint32_t JobId);
53 static int update_SIG_record(B_DB *db, char *SIGbuf, DEV_RECORD *rec, int type);
56 /* Global variables */
58 static DEVICE *dev = NULL;
60 static JCR *bjcr; /* jcr for bscan */
61 static BSR *bsr = NULL;
62 static struct stat statp;
64 static long record_file_index;
65 static POOLMEM *fname; /* original file name */
66 static POOLMEM *ofile; /* output name with prefix */
67 static POOLMEM *lname; /* link name */
72 static FILESET_DBR fsr;
75 static SESSION_LABEL label;
76 static SESSION_LABEL elabel;
78 static time_t lasttime = 0;
80 static char *db_name = "bacula";
81 static char *db_user = "bacula";
82 static char *db_password = "";
83 static char *wd = NULL;
84 static int update_db = 0;
85 static int update_vol_info = 0;
86 static int list_records = 0;
87 static int ignored_msgs = 0;
89 #define CONFIG_FILE "bacula-sd.conf"
97 "\nVersion: " VERSION " (" BDATE ")\n\n"
98 "Usage: bscan [-d debug_level] <bacula-archive>\n"
99 " -b bootstrap specify a bootstrap file\n"
100 " -c <file> specify configuration file\n"
101 " -dnn set debug level to nn\n"
102 " -m update media info in database\n"
103 " -n name specify the database name (default bacula)\n"
104 " -u user specify database user name (default bacula)\n"
105 " -p password specify database password (default none)\n"
107 " -s synchronize or store in database\n"
109 " -V specify Volume names (separated by |)\n"
110 " -w dir specify working directory (default from conf file)\n"
111 " -? print this message\n\n"));
115 int main (int argc, char *argv[])
118 struct stat stat_buf;
119 char *VolumeName = NULL;
121 my_name_is(argc, argv, "bscan");
122 init_msg(NULL, NULL);
125 while ((ch = getopt(argc, argv, "b:c:d:mn:p:rsu:vw:?")) != -1) {
128 bsr = parse_bsr(NULL, optarg);
131 case 'c': /* specify config file */
132 if (configfile != NULL) {
135 configfile = bstrdup(optarg);
138 case 'd': /* debug level */
139 debug_level = atoi(optarg);
140 if (debug_level <= 0)
157 db_password = optarg;
172 case 'V': /* Volume name */
190 Pmsg0(0, _("Wrong number of arguments: \n"));
194 if (configfile == NULL) {
195 configfile = bstrdup(CONFIG_FILE);
198 parse_config(configfile);
200 me = (STORES *)GetNextRes(R_STORAGE, NULL);
203 Emsg1(M_ERROR_TERM, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
207 /* Check if -w option given, otherwise use resource for working directory */
209 working_directory = wd;
210 } else if (!me->working_directory) {
211 Emsg1(M_ERROR_TERM, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
214 working_directory = me->working_directory;
217 /* Check that working directory is good */
218 if (stat(working_directory, &stat_buf) != 0) {
219 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s not found. Cannot continue.\n"),
222 if (!S_ISDIR(stat_buf.st_mode)) {
223 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s is not a directory. Cannot continue.\n"),
227 bjcr = setup_jcr("bscan", argv[0], bsr, VolumeName);
228 dev = setup_to_access_device(bjcr, 1); /* read device */
233 if ((db=db_init_database(NULL, db_name, db_user, db_password, NULL, 0, NULL)) == NULL) {
234 Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
236 if (!db_open_database(NULL, db)) {
237 Emsg0(M_ERROR_TERM, 0, db_strerror(db));
239 Dmsg0(200, "Database opened\n");
241 Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
251 static void do_scan()
253 fname = get_pool_memory(PM_FNAME);
254 ofile = get_pool_memory(PM_FNAME);
255 lname = get_pool_memory(PM_FNAME);
257 memset(&ar, 0, sizeof(ar));
258 memset(&pr, 0, sizeof(pr));
259 memset(&jr, 0, sizeof(jr));
260 memset(&cr, 0, sizeof(cr));
261 memset(&fsr, 0, sizeof(fsr));
262 memset(&fr, 0, sizeof(fr));
264 detach_jcr_from_device(dev, bjcr);
266 read_records(bjcr, dev, record_cb, mount_next_read_volume);
267 release_device(bjcr, dev);
269 free_pool_memory(fname);
270 free_pool_memory(ofile);
271 free_pool_memory(lname);
275 static void record_cb(JCR *bjcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
280 if (rec->data_len > 0) {
281 mr.VolBytes += rec->data_len + WRITE_RECHDR_LENGTH; /* Accumulate Volume bytes */
284 Pmsg5(000, _("Record: SessId=%u SessTim=%u FileIndex=%d Stream=%d len=%u\n"),
285 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex,
286 rec->Stream, rec->data_len);
289 * Check for Start or End of Session Record
292 if (rec->FileIndex < 0) {
293 int save_update_db = update_db;
296 dump_label_record(dev, rec, 1);
298 switch (rec->FileIndex) {
300 Pmsg0(000, _("Volume is prelabeled. This tape cannot be scanned.\n"));
304 unser_volume_label(dev, rec);
305 /* Check Pool info */
306 strcpy(pr.Name, dev->VolHdr.PoolName);
307 strcpy(pr.PoolType, dev->VolHdr.PoolType);
308 if (db_get_pool_record(bjcr, db, &pr)) {
310 Pmsg1(000, _("Pool record for %s found in DB.\n"), pr.Name);
314 Pmsg1(000, _("VOL_LABEL: Pool record not found for Pool: %s\n"),
317 create_pool_record(db, &pr);
319 if (strcmp(pr.PoolType, dev->VolHdr.PoolType) != 0) {
320 Pmsg2(000, _("VOL_LABEL: PoolType mismatch. DB=%s Vol=%s\n"),
321 pr.PoolType, dev->VolHdr.PoolType);
323 } else if (verbose) {
324 Pmsg1(000, _("Pool type \"%s\" is OK.\n"), pr.PoolType);
327 /* Check Media Info */
328 memset(&mr, 0, sizeof(mr));
329 strcpy(mr.VolumeName, dev->VolHdr.VolName);
330 mr.PoolId = pr.PoolId;
331 if (db_get_media_record(bjcr, db, &mr)) {
333 Pmsg1(000, _("Media record for %s found in DB.\n"), mr.VolumeName);
335 /* Clear out some volume statistics that will be updated */
336 mr.VolJobs = mr.VolFiles = mr.VolBlocks = 0;
337 mr.VolBytes = rec->data_len + 20;
340 Pmsg1(000, _("VOL_LABEL: Media record not found for Volume: %s\n"),
343 strcpy(mr.MediaType, dev->VolHdr.MediaType);
344 create_media_record(db, &mr, &dev->VolHdr);
346 if (strcmp(mr.MediaType, dev->VolHdr.MediaType) != 0) {
347 Pmsg2(000, _("VOL_LABEL: MediaType mismatch. DB=%s Vol=%s\n"),
348 mr.MediaType, dev->VolHdr.MediaType);
350 } else if (verbose) {
351 Pmsg1(000, _("Media type \"%s\" is OK.\n"), mr.MediaType);
353 /* Reset some JCR variables */
354 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
355 mjcr->VolFirstFile = mjcr->FileIndex = 0;
356 mjcr->StartBlock = mjcr->EndBlock = 0;
357 mjcr->StartFile = mjcr->EndFile = 0;
360 Pmsg1(000, _("VOL_LABEL: OK for Volume: %s\n"), mr.VolumeName);
364 if (ignored_msgs > 0) {
365 Pmsg1(000, _("%d \"errors\" ignored before first Start of Session record.\n"),
369 unser_session_label(&label, rec);
370 memset(&jr, 0, sizeof(jr));
371 jr.JobId = label.JobId;
372 if (db_get_job_record(bjcr, db, &jr)) {
373 /* Job record already exists in DB */
374 update_db = 0; /* don't change db in create_job_record */
376 Pmsg1(000, _("SOS_LABEL: Found Job record for JobId: %d\n"), jr.JobId);
379 /* Must create a Job record in DB */
381 Pmsg1(000, _("SOS_LABEL: Job record not found for JobId: %d\n"),
385 /* Create Client record if not already there */
386 strcpy(cr.Name, label.ClientName);
387 create_client_record(db, &cr);
388 jr.ClientId = cr.ClientId;
390 /* process label, if Job record exists don't update db */
391 mjcr = create_job_record(db, &jr, &label, rec);
392 update_db = save_update_db;
394 jr.PoolId = pr.PoolId;
395 /* Set start positions into JCR */
396 if (dev->state & ST_TAPE) {
397 mjcr->StartBlock = dev->block_num;
398 mjcr->StartFile = dev->file;
400 mjcr->StartBlock = (uint32_t)dev->file_addr;
401 mjcr->StartFile = (uint32_t)(dev->file_addr >> 32);
403 mjcr->start_time = jr.StartTime;
404 mjcr->JobLevel = jr.Level;
406 mjcr->client_name = get_pool_memory(PM_FNAME);
407 pm_strcpy(&mjcr->client_name, label.ClientName);
408 mjcr->pool_type = get_pool_memory(PM_FNAME);
409 pm_strcpy(&mjcr->pool_type, label.PoolType);
410 mjcr->fileset_name = get_pool_memory(PM_FNAME);
411 pm_strcpy(&mjcr->fileset_name, label.FileSetName);
412 mjcr->pool_name = get_pool_memory(PM_FNAME);
413 pm_strcpy(&mjcr->pool_name, label.PoolName);
415 if (rec->VolSessionId != jr.VolSessionId) {
416 Pmsg3(000, _("SOS_LABEL: VolSessId mismatch for JobId=%u. DB=%d Vol=%d\n"),
418 jr.VolSessionId, rec->VolSessionId);
421 if (rec->VolSessionTime != jr.VolSessionTime) {
422 Pmsg3(000, _("SOS_LABEL: VolSessTime mismatch for JobId=%u. DB=%d Vol=%d\n"),
424 jr.VolSessionTime, rec->VolSessionTime);
427 if (jr.PoolId != pr.PoolId) {
428 Pmsg3(000, _("SOS_LABEL: PoolId mismatch for JobId=%u. DB=%d Vol=%d\n"),
430 jr.PoolId, pr.PoolId);
435 unser_session_label(&elabel, rec);
437 /* Create FileSet record */
438 strcpy(fsr.FileSet, label.FileSetName);
439 strcpy(fsr.MD5, label.FileSetMD5);
440 create_fileset_record(db, &fsr);
441 jr.FileSetId = fsr.FileSetId;
443 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
445 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
446 rec->VolSessionId, rec->VolSessionTime);
450 /* Do the final update to the Job record */
451 update_job_record(db, &jr, &elabel, rec);
453 mjcr->end_time = jr.EndTime;
454 mjcr->JobStatus = JS_Terminated;
456 /* Create JobMedia record */
457 create_jobmedia_record(db, mjcr);
458 detach_jcr_from_device(dev, mjcr);
464 case EOT_LABEL: /* end of all tapes */
466 * Wiffle through all jobs still open and close
470 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
471 jr.JobId = mjcr->JobId;
472 jr.JobStatus = JS_ErrorTerminated;
473 jr.JobFiles = mjcr->JobFiles;
474 jr.JobBytes = mjcr->JobBytes;
475 jr.VolSessionId = mjcr->VolSessionId;
476 jr.VolSessionTime = mjcr->VolSessionTime;
477 jr.JobTDate = (utime_t)mjcr->start_time;
478 jr.ClientId = mjcr->ClientId;
480 if (!db_update_job_end_record(bjcr, db, &jr)) {
481 Pmsg1(0, _("Could not update job record. ERR=%s\n"), db_strerror(db));
485 mr.VolFiles = rec->File;
486 mr.VolBlocks = rec->Block;
487 mr.VolBytes += mr.VolBlocks * WRITE_BLKHDR_LENGTH; /* approx. */
489 update_media_record(db, &mr);
490 Pmsg3(0, _("End of Volume. VolFiles=%u VolBlocks=%u VolBytes=%s\n"), mr.VolFiles,
491 mr.VolBlocks, edit_uint64_with_commas(mr.VolBytes, ec1));
500 /* File Attributes stream */
501 if (rec->Stream == STREAM_UNIX_ATTRIBUTES || rec->Stream == STREAM_WIN32_ATTRIBUTES) {
504 if (sizeof_pool_memory(fname) < rec->data_len) {
505 fname = realloc_pool_memory(fname, rec->data_len + 1);
507 if (sizeof_pool_memory(lname) < rec->data_len) {
508 lname = realloc_pool_memory(lname, rec->data_len + 1);
514 * An Attributes record consists of:
519 * Link name (if file linked i.e. FT_LNK)
522 sscanf(rec->data, "%ld %d", &record_file_index, &type);
523 if (record_file_index != rec->FileIndex)
524 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
525 rec->FileIndex, record_file_index);
527 while (*ap++ != ' ') /* skip record file index */
529 while (*ap++ != ' ') /* skip type */
531 /* Save filename and position to attributes */
536 *fp = *ap++; /* terminate filename & point to attribs */
538 /* Skip through attributes to link name */
543 strcat(lname, lp); /* "save" link name */
546 decode_stat(ap, &statp, &LinkFI);
547 print_ls_output(fname, lname, type, &statp);
549 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
551 if (mr.VolJobs > 0) {
552 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Attributes record.\n"),
553 rec->VolSessionId, rec->VolSessionTime);
559 fr.JobId = mjcr->JobId;
561 if (db_get_file_attributes_record(bjcr, db, fname, &fr)) {
563 Pmsg1(000, _("File record already exists for: %s\n"), fname);
566 create_file_attributes_record(db, mjcr, fname, lname, type, ap, rec);
571 } else if (rec->Stream == STREAM_FILE_DATA) {
572 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
574 if (mr.VolJobs > 0) {
575 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for File Data record.\n"),
576 rec->VolSessionId, rec->VolSessionTime);
582 mjcr->JobBytes += rec->data_len;
583 free_jcr(mjcr); /* done using JCR */
585 } else if (rec->Stream == STREAM_SPARSE_DATA) {
586 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
588 if (mr.VolJobs > 0) {
589 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Sparse Data record.\n"),
590 rec->VolSessionId, rec->VolSessionTime);
596 mjcr->JobBytes += rec->data_len - sizeof(uint64_t);
597 free_jcr(mjcr); /* done using JCR */
599 } else if (rec->Stream == STREAM_GZIP_DATA) {
600 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
602 if (mr.VolJobs > 0) {
603 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for GZIP Data record.\n"),
604 rec->VolSessionId, rec->VolSessionTime);
610 mjcr->JobBytes += rec->data_len; /* No correct, we should expand it */
611 free_jcr(mjcr); /* done using JCR */
613 } else if (rec->Stream == STREAM_SPARSE_GZIP_DATA) {
614 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
616 if (mr.VolJobs > 0) {
617 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Sparse GZIP Data record.\n"),
618 rec->VolSessionId, rec->VolSessionTime);
624 mjcr->JobBytes += rec->data_len - sizeof(uint64_t); /* No correct, we should expand it */
625 free_jcr(mjcr); /* done using JCR */
628 } else if (rec->Stream == STREAM_MD5_SIGNATURE) {
630 bin_to_base64(MD5buf, (char *)rec->data, 16); /* encode 16 bytes */
632 Pmsg1(000, _("Got MD5 record: %s\n"), MD5buf);
634 update_SIG_record(db, MD5buf, rec, MD5_SIG);
636 } else if (rec->Stream == STREAM_SHA1_SIGNATURE) {
638 bin_to_base64(SIGbuf, (char *)rec->data, 20); /* encode 20 bytes */
640 Pmsg1(000, _("Got SHA1 record: %s\n"), SIGbuf);
642 update_SIG_record(db, SIGbuf, rec, SHA1_SIG);
645 } else if (rec->Stream == STREAM_PROGRAM_NAMES) {
647 Pmsg1(000, _("Got Prog Names Stream: %s\n"), rec->data);
649 } else if (rec->Stream == STREAM_PROGRAM_DATA) {
651 Pmsg0(000, _("Got Prog Data Stream record.\n"));
654 Pmsg2(0, _("Unknown stream type!!! stream=%d data=%s\n"), rec->Stream, rec->data);
660 * Free the Job Control Record if no one is still using it.
661 * Called from main free_jcr() routine in src/lib/jcr.c so
662 * that we can do our Director specific cleanup of the jcr.
664 static void dird_free_jcr(JCR *jcr)
666 Dmsg0(200, "Start dird free_jcr\n");
668 if (jcr->file_bsock) {
669 Dmsg0(200, "Close File bsock\n");
670 bnet_close(jcr->file_bsock);
672 if (jcr->store_bsock) {
673 Dmsg0(200, "Close Store bsock\n");
674 bnet_close(jcr->store_bsock);
676 if (jcr->RestoreBootstrap) {
677 free(jcr->RestoreBootstrap);
679 Dmsg0(200, "End dird free_jcr\n");
683 * We got a File Attributes record on the tape. Now, lookup the Job
684 * record, and then create the attributes record.
686 static int create_file_attributes_record(B_DB *db, JCR *mjcr,
687 char *fname, char *lname, int type,
688 char *ap, DEV_RECORD *rec)
693 ar.ClientId = mjcr->ClientId;
694 ar.JobId = mjcr->JobId;
695 ar.Stream = rec->Stream;
696 ar.FileIndex = rec->FileIndex;
698 if (mjcr->VolFirstFile == 0) {
699 mjcr->VolFirstFile = rec->FileIndex;
701 mjcr->FileIndex = rec->FileIndex;
708 if (!db_create_file_attributes_record(bjcr, db, &ar)) {
709 Pmsg1(0, _("Could not create File Attributes record. ERR=%s\n"), db_strerror(db));
712 mjcr->FileId = ar.FileId;
715 Pmsg1(000, _("Created File record: %s\n"), fname);
721 * For each Volume we see, we create a Medium record
723 static int create_media_record(B_DB *db, MEDIA_DBR *mr, VOLUME_LABEL *vl)
728 strcpy(mr->VolStatus, "Full");
729 mr->VolRetention = 365 * 3600 * 24; /* 1 year */
730 if (vl->VerNum >= 11) {
731 mr->FirstWritten = btime_to_utime(vl->write_btime);
732 mr->LabelDate = btime_to_utime(vl->label_btime);
734 /* DEPRECATED DO NOT USE */
735 dt.julian_day_number = vl->write_date;
736 dt.julian_day_fraction = vl->write_time;
738 mr->FirstWritten = mktime(&tm);
739 dt.julian_day_number = vl->label_date;
740 dt.julian_day_fraction = vl->label_time;
742 mr->LabelDate = mktime(&tm);
744 lasttime = mr->LabelDate;
750 if (!db_create_media_record(bjcr, db, mr)) {
751 Pmsg1(0, _("Could not create media record. ERR=%s\n"), db_strerror(db));
754 if (!db_update_media_record(bjcr, db, mr)) {
755 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
759 Pmsg1(000, _("Created Media record for Volume: %s\n"), mr->VolumeName);
766 * Called at end of media to update it
768 static int update_media_record(B_DB *db, MEDIA_DBR *mr)
770 if (!update_db && !update_vol_info) {
774 mr->LastWritten = lasttime;
775 if (!db_update_media_record(bjcr, db, mr)) {
776 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
780 Pmsg1(000, _("Updated Media record at end of Volume: %s\n"), mr->VolumeName);
787 static int create_pool_record(B_DB *db, POOL_DBR *pr)
791 pr->VolRetention = 355 * 3600 * 24; /* 1 year */
796 if (!db_create_pool_record(bjcr, db, pr)) {
797 Pmsg1(0, _("Could not create pool record. ERR=%s\n"), db_strerror(db));
801 Pmsg1(000, _("Created Pool record for Pool: %s\n"), pr->Name);
809 * Called from SOS to create a client for the current Job
811 static int create_client_record(B_DB *db, CLIENT_DBR *cr)
816 if (!db_create_client_record(bjcr, db, cr)) {
817 Pmsg1(0, _("Could not create Client record. ERR=%s\n"), db_strerror(db));
821 Pmsg1(000, _("Created Client record for Client: %s\n"), cr->Name);
826 static int create_fileset_record(B_DB *db, FILESET_DBR *fsr)
832 if (fsr->MD5[0] == 0) {
833 fsr->MD5[0] = ' '; /* Equivalent to nothing */
836 if (db_get_fileset_record(bjcr, db, fsr)) {
838 Pmsg1(000, _("Fileset \"%s\" already exists.\n"), fsr->FileSet);
841 if (!db_create_fileset_record(bjcr, db, fsr)) {
842 Pmsg2(0, _("Could not create FileSet record \"%s\". ERR=%s\n"),
843 fsr->FileSet, db_strerror(db));
847 Pmsg1(000, _("Created FileSet record \"%s\"\n"), fsr->FileSet);
854 * Simulate the two calls on the database to create
855 * the Job record and to update it when the Job actually
858 static JCR *create_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *label,
865 jr->JobId = label->JobId;
866 jr->Type = label->JobType;
867 jr->Level = label->JobLevel;
868 jr->JobStatus = JS_Created;
869 strcpy(jr->Name, label->JobName);
870 strcpy(jr->Job, label->Job);
871 if (label->VerNum >= 11) {
872 jr->SchedTime = btime_to_unix(label->write_btime);
874 dt.julian_day_number = label->write_date;
875 dt.julian_day_fraction = label->write_time;
877 jr->SchedTime = mktime(&tm);
880 jr->StartTime = jr->SchedTime;
881 jr->JobTDate = (utime_t)jr->SchedTime;
882 jr->VolSessionId = rec->VolSessionId;
883 jr->VolSessionTime = rec->VolSessionTime;
885 /* Now create a JCR as if starting the Job */
886 mjcr = create_jcr(jr, rec, label->JobId);
892 /* This creates the bare essentials */
893 if (!db_create_job_record(bjcr, db, jr)) {
894 Pmsg1(0, _("Could not create JobId record. ERR=%s\n"), db_strerror(db));
898 /* This adds the client, StartTime, JobTDate, ... */
899 if (!db_update_job_start_record(bjcr, db, jr)) {
900 Pmsg1(0, _("Could not update job start record. ERR=%s\n"), db_strerror(db));
903 Pmsg2(000, _("Created new JobId=%u record for original JobId=%u\n"), jr->JobId,
905 mjcr->JobId = jr->JobId; /* set new JobId */
910 * Simulate the database call that updates the Job
911 * at Job termination time.
913 static int update_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *elabel,
920 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
922 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
923 rec->VolSessionId, rec->VolSessionTime);
926 if (elabel->VerNum >= 11) {
927 jr->EndTime = btime_to_unix(elabel->write_btime);
929 dt.julian_day_number = elabel->write_date;
930 dt.julian_day_fraction = elabel->write_time;
932 jr->EndTime = mktime(&tm);
934 lasttime = jr->EndTime;
935 mjcr->end_time = jr->EndTime;
937 jr->JobId = mjcr->JobId;
938 jr->JobStatus = elabel->JobStatus;
939 mjcr->JobStatus = elabel->JobStatus;
940 jr->JobFiles = elabel->JobFiles;
941 jr->JobBytes = elabel->JobBytes;
942 jr->VolSessionId = rec->VolSessionId;
943 jr->VolSessionTime = rec->VolSessionTime;
944 jr->JobTDate = (utime_t)mjcr->start_time;
945 jr->ClientId = mjcr->ClientId;
952 if (!db_update_job_end_record(bjcr, db, jr)) {
953 Pmsg2(0, _("Could not update JobId=%u record. ERR=%s\n"), jr->JobId, db_strerror(db));
958 Pmsg1(000, _("Updated Job termination record for new JobId=%u\n"), jr->JobId);
962 static char term_code[70];
963 char sdt[50], edt[50];
964 char ec1[30], ec2[30], ec3[30];
966 switch (mjcr->JobStatus) {
968 term_msg = _("Backup OK");
971 case JS_ErrorTerminated:
972 term_msg = _("*** Backup Error ***");
975 term_msg = _("Backup Cancelled");
978 term_msg = term_code;
979 sprintf(term_code, _("Job Termination code: %d"), mjcr->JobStatus);
982 bstrftime(sdt, sizeof(sdt), mjcr->start_time);
983 bstrftime(edt, sizeof(edt), mjcr->end_time);
994 Volume Session Id: %d\n\
995 Volume Session Time: %d\n\
996 Last Volume Bytes: %s\n\
997 Termination: %s\n\n"),
1002 job_level_to_str(mjcr->JobLevel),
1006 edit_uint64_with_commas(mjcr->JobFiles, ec1),
1007 edit_uint64_with_commas(mjcr->JobBytes, ec2),
1009 mjcr->VolSessionTime,
1010 edit_uint64_with_commas(mr.VolBytes, ec3),
1017 static int create_jobmedia_record(B_DB *db, JCR *mjcr)
1021 if (dev->state & ST_TAPE) {
1022 mjcr->EndBlock = dev->EndBlock;
1023 mjcr->EndFile = dev->EndFile;
1025 mjcr->EndBlock = (uint32_t)dev->file_addr;
1026 mjcr->EndFile = (uint32_t)(dev->file_addr >> 32);
1029 memset(&jmr, 0, sizeof(jmr));
1030 jmr.JobId = mjcr->JobId;
1031 jmr.MediaId = mr.MediaId;
1032 jmr.FirstIndex = mjcr->VolFirstFile;
1033 jmr.LastIndex = mjcr->FileIndex;
1034 jmr.StartFile = mjcr->StartFile;
1035 jmr.EndFile = mjcr->EndFile;
1036 jmr.StartBlock = mjcr->StartBlock;
1037 jmr.EndBlock = mjcr->EndBlock;
1044 if (!db_create_jobmedia_record(bjcr, db, &jmr)) {
1045 Pmsg1(0, _("Could not create JobMedia record. ERR=%s\n"), db_strerror(db));
1049 Pmsg2(000, _("Created JobMedia record JobId %d, MediaId %d\n"),
1050 jmr.JobId, jmr.MediaId);
1056 * Simulate the database call that updates the MD5/SHA1 record
1058 static int update_SIG_record(B_DB *db, char *SIGbuf, DEV_RECORD *rec, int type)
1062 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
1064 if (mr.VolJobs > 0) {
1065 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for MD5/SHA1 record.\n"),
1066 rec->VolSessionId, rec->VolSessionTime);
1078 if (!db_add_SIG_to_file_record(bjcr, db, mjcr->FileId, SIGbuf, type)) {
1079 Pmsg1(0, _("Could not add MD5/SHA1 to File record. ERR=%s\n"), db_strerror(db));
1084 Pmsg0(000, _("Updated MD5/SHA1 record\n"));
1092 * Create a JCR as if we are really starting the job
1094 static JCR *create_jcr(JOB_DBR *jr, DEV_RECORD *rec, uint32_t JobId)
1098 * Transfer as much as possible to the Job JCR. Most important is
1099 * the JobId and the ClientId.
1101 jobjcr = new_jcr(sizeof(JCR), dird_free_jcr);
1102 jobjcr->JobType = jr->Type;
1103 jobjcr->JobLevel = jr->Level;
1104 jobjcr->JobStatus = jr->JobStatus;
1105 strcpy(jobjcr->Job, jr->Job);
1106 jobjcr->JobId = JobId; /* this is JobId on tape */
1107 jobjcr->sched_time = jr->SchedTime;
1108 jobjcr->start_time = jr->StartTime;
1109 jobjcr->VolSessionId = rec->VolSessionId;
1110 jobjcr->VolSessionTime = rec->VolSessionTime;
1111 jobjcr->ClientId = jr->ClientId;
1112 attach_jcr_to_device(dev, jobjcr);
1116 /* Dummies to replace askdir.c */
1117 int dir_get_volume_info(JCR *jcr, int writing) { return 1;}
1118 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
1119 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
1120 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
1121 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
1122 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
1123 int dir_send_job_status(JCR *jcr) {return 1;}
1126 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
1129 * We are at the end of reading a tape. Now, we simulate handling
1130 * the end of writing a tape by wiffling through the attached
1131 * jcrs creating jobmedia records.
1133 Dmsg1(100, "Walk attached jcrs. Volume=%s\n", dev->VolCatInfo.VolCatName);
1134 for (JCR *mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
1136 Pmsg1(000, _("Create JobMedia for Job %s\n"), mjcr->Job);
1138 if (dev->state & ST_TAPE) {
1139 mjcr->EndBlock = dev->EndBlock;
1140 mjcr->EndFile = dev->EndFile;
1142 mjcr->EndBlock = (uint32_t)dev->file_addr;
1143 mjcr->StartBlock = (uint32_t)(dev->file_addr >> 32);
1145 if (!create_jobmedia_record(db, mjcr)) {
1146 Pmsg2(000, _("Could not create JobMedia record for Volume=%s Job=%s\n"),
1147 dev->VolCatInfo.VolCatName, mjcr->Job);
1151 fprintf(stderr, _("Mount Volume %s on device %s and press return when ready: "),
1152 jcr->VolumeName, dev_name(dev));