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_MD5_record(B_DB *db, char *MD5buf, DEV_RECORD *rec);
56 /* Global variables */
57 static DEVICE *dev = NULL;
59 static JCR *bjcr; /* jcr for bscan */
60 static BSR *bsr = NULL;
61 static struct stat statp;
63 static long record_file_index;
64 static POOLMEM *fname; /* original file name */
65 static POOLMEM *ofile; /* output name with prefix */
66 static POOLMEM *lname; /* link name */
71 static FILESET_DBR fsr;
74 static SESSION_LABEL label;
75 static SESSION_LABEL elabel;
77 static time_t lasttime = 0;
79 static char *db_name = "bacula";
80 static char *db_user = "bacula";
81 static char *db_password = "";
82 static char *wd = "/tmp";
83 static int verbose = 0;
84 static int update_db = 0;
85 static int update_vol_info = 0;
86 static int list_records = 0;
88 #define CONFIG_FILE "bacula-sd.conf"
95 "\nVersion: " VERSION " (" DATE ")\n\n"
96 "Usage: bscan [-d debug_level] <bacula-archive>\n"
97 " -b bootstrap specify a bootstrap file\n"
98 " -c <file> specify configuration file\n"
99 " -dnn set debug level to nn\n"
100 " -m update media info in database\n"
101 " -n name specify the database name (default bacula)\n"
102 " -u user specify database user name (default bacula)\n"
103 " -p password specify database password (default none)\n"
105 " -s synchronize or store in database\n"
107 " -w dir specify working directory (default /tmp)\n"
108 " -? print this message\n\n"));
112 int main (int argc, char *argv[])
116 my_name_is(argc, argv, "bscan");
117 init_msg(NULL, NULL);
120 while ((ch = getopt(argc, argv, "b:c:d:mn:p:rsu:vw:?")) != -1) {
123 bsr = parse_bsr(NULL, optarg);
126 case 'c': /* specify config file */
127 if (configfile != NULL) {
130 configfile = bstrdup(optarg);
133 case 'd': /* debug level */
134 debug_level = atoi(optarg);
135 if (debug_level <= 0)
152 db_password = optarg;
181 Pmsg0(0, _("Wrong number of arguments: \n"));
185 working_directory = wd;
187 if (configfile == NULL) {
188 configfile = bstrdup(CONFIG_FILE);
191 parse_config(configfile);
193 bjcr = setup_jcr("bscan", argv[0], bsr);
194 dev = setup_to_access_device(bjcr, 1); /* read device */
199 if ((db=db_init_database(NULL, db_name, db_user, db_password)) == NULL) {
200 Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
202 if (!db_open_database(db)) {
203 Emsg0(M_ERROR_TERM, 0, db_strerror(db));
205 Dmsg0(200, "Database opened\n");
207 Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
217 static void do_scan()
219 fname = get_pool_memory(PM_FNAME);
220 ofile = get_pool_memory(PM_FNAME);
221 lname = get_pool_memory(PM_FNAME);
223 memset(&ar, 0, sizeof(ar));
224 memset(&pr, 0, sizeof(pr));
225 memset(&jr, 0, sizeof(jr));
226 memset(&cr, 0, sizeof(cr));
227 memset(&fsr, 0, sizeof(fsr));
228 memset(&fr, 0, sizeof(fr));
230 detach_jcr_from_device(dev, bjcr);
232 read_records(bjcr, dev, record_cb, mount_next_read_volume);
233 release_device(bjcr, dev);
235 free_pool_memory(fname);
236 free_pool_memory(ofile);
237 free_pool_memory(lname);
241 static void record_cb(JCR *bjcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
246 if (rec->data_len > 0) {
247 mr.VolBytes += rec->data_len + WRITE_RECHDR_LENGTH; /* Accumulate Volume bytes */
250 Pmsg5(000, _("Record: SessId=%u SessTim=%u FileIndex=%d Stream=%d len=%u\n"),
251 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex,
252 rec->Stream, rec->data_len);
255 * Check for Start or End of Session Record
258 if (rec->FileIndex < 0) {
259 int save_update_db = update_db;
262 dump_label_record(dev, rec, 1);
264 switch (rec->FileIndex) {
266 Pmsg0(000, "Volume is prelabeled. This tape cannot be scanned.\n");
270 unser_volume_label(dev, rec);
271 /* Check Pool info */
272 strcpy(pr.Name, dev->VolHdr.PoolName);
273 strcpy(pr.PoolType, dev->VolHdr.PoolType);
274 if (db_get_pool_record(db, &pr)) {
276 Pmsg1(000, "Pool record for %s found in DB.\n", pr.Name);
280 Pmsg1(000, "VOL_LABEL: Pool record not found for Pool: %s\n",
283 create_pool_record(db, &pr);
285 if (strcmp(pr.PoolType, dev->VolHdr.PoolType) != 0) {
286 Pmsg2(000, "VOL_LABEL: PoolType mismatch. DB=%s Vol=%s\n",
287 pr.PoolType, dev->VolHdr.PoolType);
289 } else if (verbose) {
290 Pmsg1(000, "Pool type \"%s\" is OK.\n", pr.PoolType);
293 /* Check Media Info */
294 memset(&mr, 0, sizeof(mr));
295 strcpy(mr.VolumeName, dev->VolHdr.VolName);
296 mr.PoolId = pr.PoolId;
297 if (db_get_media_record(db, &mr)) {
299 Pmsg1(000, "Media record for %s found in DB.\n", mr.VolumeName);
301 /* Clear out some volume statistics that will be updated */
302 mr.VolJobs = mr.VolFiles = mr.VolBlocks = 0;
303 mr.VolBytes = rec->data_len + 20;
306 Pmsg1(000, "VOL_LABEL: Media record not found for Volume: %s\n",
309 strcpy(mr.MediaType, dev->VolHdr.MediaType);
310 create_media_record(db, &mr, &dev->VolHdr);
312 if (strcmp(mr.MediaType, dev->VolHdr.MediaType) != 0) {
313 Pmsg2(000, "VOL_LABEL: MediaType mismatch. DB=%s Vol=%s\n",
314 mr.MediaType, dev->VolHdr.MediaType);
316 } else if (verbose) {
317 Pmsg1(000, "Media type \"%s\" is OK.\n", mr.MediaType);
319 /* Reset some JCR variables */
320 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
321 mjcr->VolFirstFile = mjcr->FileIndex = 0;
322 mjcr->StartBlock = mjcr->EndBlock = 0;
323 mjcr->StartFile = mjcr->EndFile = 0;
326 Pmsg1(000, "VOL_LABEL: OK for Volume: %s\n", mr.VolumeName);
331 unser_session_label(&label, rec);
332 memset(&jr, 0, sizeof(jr));
333 jr.JobId = label.JobId;
334 if (db_get_job_record(db, &jr)) {
335 /* Job record already exists in DB */
336 update_db = 0; /* don't change db in create_job_record */
338 Pmsg1(000, _("SOS_LABEL: Found Job record for JobId: %d\n"), jr.JobId);
341 /* Must create a Job record in DB */
343 Pmsg1(000, "SOS_LABEL: Job record not found for JobId: %d\n",
347 /* Create Client record if not already there */
348 strcpy(cr.Name, label.ClientName);
349 create_client_record(db, &cr);
350 jr.ClientId = cr.ClientId;
352 /* process label, if Job record exists don't update db */
353 mjcr = create_job_record(db, &jr, &label, rec);
354 update_db = save_update_db;
356 jr.PoolId = pr.PoolId;
357 /* Set start positions into JCR */
358 if (dev->state & ST_TAPE) {
359 mjcr->StartBlock = dev->block_num;
360 mjcr->StartFile = dev->file;
362 mjcr->StartBlock = (uint32_t)dev->file_addr;
363 mjcr->StartFile = (uint32_t)(dev->file_addr >> 32);
365 mjcr->start_time = jr.StartTime;
366 mjcr->JobLevel = jr.Level;
368 mjcr->client_name = get_pool_memory(PM_FNAME);
369 pm_strcpy(&mjcr->client_name, label.ClientName);
370 mjcr->pool_type = get_pool_memory(PM_FNAME);
371 pm_strcpy(&mjcr->pool_type, label.PoolType);
372 mjcr->fileset_name = get_pool_memory(PM_FNAME);
373 pm_strcpy(&mjcr->fileset_name, label.FileSetName);
374 mjcr->pool_name = get_pool_memory(PM_FNAME);
375 pm_strcpy(&mjcr->pool_name, label.PoolName);
377 if (rec->VolSessionId != jr.VolSessionId) {
378 Pmsg3(000, "SOS_LABEL: VolSessId mismatch for JobId=%u. DB=%d Vol=%d\n",
380 jr.VolSessionId, rec->VolSessionId);
383 if (rec->VolSessionTime != jr.VolSessionTime) {
384 Pmsg3(000, "SOS_LABEL: VolSessTime mismatch for JobId=%u. DB=%d Vol=%d\n",
386 jr.VolSessionTime, rec->VolSessionTime);
389 if (jr.PoolId != pr.PoolId) {
390 Pmsg3(000, "SOS_LABEL: PoolId mismatch for JobId=%u. DB=%d Vol=%d\n",
392 jr.PoolId, pr.PoolId);
397 unser_session_label(&elabel, rec);
399 /* Create FileSet record */
400 strcpy(fsr.FileSet, label.FileSetName);
401 strcpy(fsr.MD5, label.FileSetMD5);
402 create_fileset_record(db, &fsr);
403 jr.FileSetId = fsr.FileSetId;
406 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
408 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
409 rec->VolSessionId, rec->VolSessionTime);
413 /* Do the final update to the Job record */
414 update_job_record(db, &jr, &elabel, rec);
416 mjcr->end_time = jr.EndTime;
417 mjcr->JobStatus = JS_Terminated;
419 /* Create JobMedia record */
420 create_jobmedia_record(db, mjcr);
421 detach_jcr_from_device(dev, mjcr);
427 case EOT_LABEL: /* end of all tapes */
429 * Wiffle through all jobs still open and close
433 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
434 jr.JobId = mjcr->JobId;
435 jr.JobStatus = JS_ErrorTerminated;
436 jr.JobFiles = mjcr->JobFiles;
437 jr.JobBytes = mjcr->JobBytes;
438 jr.VolSessionId = mjcr->VolSessionId;
439 jr.VolSessionTime = mjcr->VolSessionTime;
440 jr.JobTDate = (utime_t)mjcr->start_time;
441 jr.ClientId = mjcr->ClientId;
443 if (!db_update_job_end_record(db, &jr)) {
444 Pmsg1(0, _("Could not update job record. ERR=%s\n"), db_strerror(db));
448 mr.VolFiles = rec->File;
449 mr.VolBlocks = rec->Block;
450 mr.VolBytes += mr.VolBlocks * WRITE_BLKHDR_LENGTH; /* approx. */
452 update_media_record(db, &mr);
453 Pmsg3(0, _("End of Volume. VolFiles=%u VolBlocks=%u VolBytes=%s\n"), mr.VolFiles,
454 mr.VolBlocks, edit_uint64_with_commas(mr.VolBytes, ec1));
463 /* File Attributes stream */
464 if (rec->Stream == STREAM_UNIX_ATTRIBUTES || rec->Stream == STREAM_WIN32_ATTRIBUTES) {
467 if (sizeof_pool_memory(fname) < rec->data_len) {
468 fname = realloc_pool_memory(fname, rec->data_len + 1);
470 if (sizeof_pool_memory(lname) < rec->data_len) {
471 lname = realloc_pool_memory(lname, rec->data_len + 1);
477 * An Attributes record consists of:
482 * Link name (if file linked i.e. FT_LNK)
485 sscanf(rec->data, "%ld %d", &record_file_index, &type);
486 if (record_file_index != rec->FileIndex)
487 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
488 rec->FileIndex, record_file_index);
490 while (*ap++ != ' ') /* skip record file index */
492 while (*ap++ != ' ') /* skip type */
494 /* Save filename and position to attributes */
499 *fp = *ap++; /* terminate filename & point to attribs */
501 /* Skip through attributes to link name */
506 strcat(lname, lp); /* "save" link name */
510 decode_stat(ap, &statp);
511 print_ls_output(fname, lname, type, &statp);
513 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
515 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Attributes record.\n"),
516 rec->VolSessionId, rec->VolSessionTime);
519 fr.JobId = mjcr->JobId;
521 if (db_get_file_attributes_record(db, fname, &fr)) {
523 Pmsg1(000, _("File record already exists for: %s\n"), fname);
526 create_file_attributes_record(db, mjcr, fname, lname, type, ap, rec);
531 } else if (rec->Stream == STREAM_FILE_DATA) {
532 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
534 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Attributes record.\n"),
535 rec->VolSessionId, rec->VolSessionTime);
538 mjcr->JobBytes += rec->data_len;
539 free_jcr(mjcr); /* done using JCR */
541 } else if (rec->Stream == STREAM_SPARSE_DATA) {
542 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
544 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Attributes record.\n"),
545 rec->VolSessionId, rec->VolSessionTime);
548 mjcr->JobBytes += rec->data_len - sizeof(uint64_t);
549 free_jcr(mjcr); /* done using JCR */
551 } else if (rec->Stream == STREAM_GZIP_DATA) {
552 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
554 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Attributes record.\n"),
555 rec->VolSessionId, rec->VolSessionTime);
558 mjcr->JobBytes += rec->data_len; /* No correct, we should expand it */
559 free_jcr(mjcr); /* done using JCR */
561 } else if (rec->Stream == STREAM_SPARSE_GZIP_DATA) {
562 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
564 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Attributes record.\n"),
565 rec->VolSessionId, rec->VolSessionTime);
568 mjcr->JobBytes += rec->data_len - sizeof(uint64_t); /* No correct, we should expand it */
569 free_jcr(mjcr); /* done using JCR */
572 } else if (rec->Stream == STREAM_MD5_SIGNATURE) {
574 bin_to_base64(MD5buf, (char *)rec->data, 16); /* encode 16 bytes */
576 Pmsg1(000, _("Got MD5 record: %s\n"), MD5buf);
578 update_MD5_record(db, MD5buf, rec);
580 } else if (rec->Stream == STREAM_PROGRAM_NAMES) {
582 Pmsg1(000, _("Got Prog Names Stream: %s\n"), rec->data);
584 } else if (rec->Stream == STREAM_PROGRAM_DATA) {
586 Pmsg0(000, _("Got Prog Data Stream record.\n"));
589 Pmsg2(0, _("Unknown stream type!!! stream=%d data=%s\n"), rec->Stream, rec->data);
595 * Free the Job Control Record if no one is still using it.
596 * Called from main free_jcr() routine in src/lib/jcr.c so
597 * that we can do our Director specific cleanup of the jcr.
599 static void dird_free_jcr(JCR *jcr)
601 Dmsg0(200, "Start dird free_jcr\n");
603 if (jcr->file_bsock) {
604 Dmsg0(200, "Close File bsock\n");
605 bnet_close(jcr->file_bsock);
607 if (jcr->store_bsock) {
608 Dmsg0(200, "Close Store bsock\n");
609 bnet_close(jcr->store_bsock);
611 if (jcr->RestoreBootstrap) {
612 free(jcr->RestoreBootstrap);
614 Dmsg0(200, "End dird free_jcr\n");
618 * We got a File Attributes record on the tape. Now, lookup the Job
619 * record, and then create the attributes record.
621 static int create_file_attributes_record(B_DB *db, JCR *mjcr,
622 char *fname, char *lname, int type,
623 char *ap, DEV_RECORD *rec)
628 ar.ClientId = mjcr->ClientId;
629 ar.JobId = mjcr->JobId;
630 ar.Stream = rec->Stream;
631 ar.FileIndex = rec->FileIndex;
633 if (mjcr->VolFirstFile == 0) {
634 mjcr->VolFirstFile = rec->FileIndex;
636 mjcr->FileIndex = rec->FileIndex;
643 if (!db_create_file_attributes_record(db, &ar)) {
644 Pmsg1(0, _("Could not create File Attributes record. ERR=%s\n"), db_strerror(db));
647 mjcr->FileId = ar.FileId;
650 Pmsg1(000, _("Created File record: %s\n"), fname);
656 * For each Volume we see, we create a Medium record
658 static int create_media_record(B_DB *db, MEDIA_DBR *mr, VOLUME_LABEL *vl)
663 strcpy(mr->VolStatus, "Full");
664 mr->VolRetention = 365 * 3600 * 24; /* 1 year */
665 if (vl->VerNum >= 11) {
666 mr->FirstWritten = btime_to_utime(vl->write_btime);
667 mr->LabelDate = btime_to_utime(vl->label_btime);
669 /* DEPRECATED DO NOT USE */
670 dt.julian_day_number = vl->write_date;
671 dt.julian_day_fraction = vl->write_time;
673 mr->FirstWritten = mktime(&tm);
674 dt.julian_day_number = vl->label_date;
675 dt.julian_day_fraction = vl->label_time;
677 mr->LabelDate = mktime(&tm);
679 lasttime = mr->LabelDate;
685 if (!db_create_media_record(db, mr)) {
686 Pmsg1(0, _("Could not create media record. ERR=%s\n"), db_strerror(db));
689 if (!db_update_media_record(db, mr)) {
690 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
694 Pmsg1(000, _("Created Media record for Volume: %s\n"), mr->VolumeName);
701 * Called at end of media to update it
703 static int update_media_record(B_DB *db, MEDIA_DBR *mr)
705 if (!update_db && !update_vol_info) {
709 mr->LastWritten = lasttime;
710 if (!db_update_media_record(db, mr)) {
711 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
715 Pmsg1(000, _("Updated Media record at end of Volume: %s\n"), mr->VolumeName);
722 static int create_pool_record(B_DB *db, POOL_DBR *pr)
726 pr->VolRetention = 355 * 3600 * 24; /* 1 year */
731 if (!db_create_pool_record(db, pr)) {
732 Pmsg1(0, _("Could not create pool record. ERR=%s\n"), db_strerror(db));
736 Pmsg1(000, _("Created Pool record for Pool: %s\n"), pr->Name);
744 * Called from SOS to create a client for the current Job
746 static int create_client_record(B_DB *db, CLIENT_DBR *cr)
751 if (!db_create_client_record(db, cr)) {
752 Pmsg1(0, _("Could not create Client record. ERR=%s\n"), db_strerror(db));
756 Pmsg1(000, _("Created Client record for Client: %s\n"), cr->Name);
761 static int create_fileset_record(B_DB *db, FILESET_DBR *fsr)
767 if (fsr->MD5[0] == 0) {
768 fsr->MD5[0] = ' '; /* Equivalent to nothing */
771 if (db_get_fileset_record(db, fsr)) {
773 Pmsg1(000, _("Fileset \"%s\" already exists.\n"), fsr->FileSet);
776 if (!db_create_fileset_record(db, fsr)) {
777 Pmsg2(0, _("Could not create FileSet record \"%s\". ERR=%s\n"),
778 fsr->FileSet, db_strerror(db));
782 Pmsg1(000, _("Created FileSet record \"%s\"\n"), fsr->FileSet);
789 * Simulate the two calls on the database to create
790 * the Job record and to update it when the Job actually
793 static JCR *create_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *label,
800 jr->JobId = label->JobId;
801 jr->Type = label->JobType;
802 jr->Level = label->JobLevel;
803 jr->JobStatus = JS_Created;
804 strcpy(jr->Name, label->JobName);
805 strcpy(jr->Job, label->Job);
806 if (label->VerNum >= 11) {
807 jr->SchedTime = btime_to_unix(label->write_btime);
809 dt.julian_day_number = label->write_date;
810 dt.julian_day_fraction = label->write_time;
812 jr->SchedTime = mktime(&tm);
815 jr->StartTime = jr->SchedTime;
816 jr->JobTDate = (utime_t)jr->SchedTime;
817 jr->VolSessionId = rec->VolSessionId;
818 jr->VolSessionTime = rec->VolSessionTime;
820 /* Now create a JCR as if starting the Job */
821 mjcr = create_jcr(jr, rec, label->JobId);
827 /* This creates the bare essentials */
828 if (!db_create_job_record(db, jr)) {
829 Pmsg1(0, _("Could not create JobId record. ERR=%s\n"), db_strerror(db));
833 /* This adds the client, StartTime, JobTDate, ... */
834 if (!db_update_job_start_record(db, jr)) {
835 Pmsg1(0, _("Could not update job start record. ERR=%s\n"), db_strerror(db));
839 Pmsg2(000, _("Created new JobId=%u record for original JobId=%u\n"), jr->JobId,
842 mjcr->JobId = jr->JobId; /* set new JobId */
847 * Simulate the database call that updates the Job
848 * at Job termination time.
850 static int update_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *elabel,
857 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
859 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
860 rec->VolSessionId, rec->VolSessionTime);
863 if (elabel->VerNum >= 11) {
864 jr->EndTime = btime_to_unix(elabel->write_btime);
866 dt.julian_day_number = elabel->write_date;
867 dt.julian_day_fraction = elabel->write_time;
869 jr->EndTime = mktime(&tm);
871 lasttime = jr->EndTime;
872 mjcr->end_time = jr->EndTime;
874 jr->JobId = mjcr->JobId;
875 jr->JobStatus = elabel->JobStatus;
876 mjcr->JobStatus = elabel->JobStatus;
877 jr->JobFiles = elabel->JobFiles;
878 jr->JobBytes = elabel->JobBytes;
879 jr->VolSessionId = rec->VolSessionId;
880 jr->VolSessionTime = rec->VolSessionTime;
881 jr->JobTDate = (utime_t)mjcr->start_time;
882 jr->ClientId = mjcr->ClientId;
889 if (!db_update_job_end_record(db, jr)) {
890 Pmsg2(0, _("Could not update JobId=%u record. ERR=%s\n"), jr->JobId, db_strerror(db));
895 Pmsg1(000, _("Updated Job termination record for new JobId=%u\n"), jr->JobId);
899 static char term_code[70];
900 char sdt[50], edt[50];
901 char ec1[30], ec2[30], ec3[30];
903 switch (mjcr->JobStatus) {
905 term_msg = _("Backup OK");
908 case JS_ErrorTerminated:
909 term_msg = _("*** Backup Error ***");
912 term_msg = _("Backup Cancelled");
915 term_msg = term_code;
916 sprintf(term_code, _("Job Termination code: %d"), mjcr->JobStatus);
919 bstrftime(sdt, sizeof(sdt), mjcr->start_time);
920 bstrftime(edt, sizeof(edt), mjcr->end_time);
931 Volume Session Id: %d\n\
932 Volume Session Time: %d\n\
933 Last Volume Bytes: %s\n\
934 Termination: %s\n\n"),
939 job_level_to_str(mjcr->JobLevel),
943 edit_uint64_with_commas(mjcr->JobFiles, ec1),
944 edit_uint64_with_commas(mjcr->JobBytes, ec2),
946 mjcr->VolSessionTime,
947 edit_uint64_with_commas(mr.VolBytes, ec3),
954 static int create_jobmedia_record(B_DB *db, JCR *mjcr)
958 if (dev->state & ST_TAPE) {
959 mjcr->EndBlock = dev->block_num;
960 mjcr->EndFile = dev->file;
962 mjcr->EndBlock = (uint32_t)dev->file_addr;
963 mjcr->EndFile = (uint32_t)(dev->file_addr >> 32);
966 memset(&jmr, 0, sizeof(jmr));
967 jmr.JobId = mjcr->JobId;
968 jmr.MediaId = mr.MediaId;
969 jmr.FirstIndex = mjcr->VolFirstFile;
970 jmr.LastIndex = mjcr->FileIndex;
971 jmr.StartFile = mjcr->StartFile;
972 jmr.EndFile = mjcr->EndFile;
973 jmr.StartBlock = mjcr->StartBlock;
974 jmr.EndBlock = mjcr->EndBlock;
981 if (!db_create_jobmedia_record(db, &jmr)) {
982 Pmsg1(0, _("Could not create JobMedia record. ERR=%s\n"), db_strerror(db));
986 Pmsg2(000, _("Created JobMedia record JobId %d, MediaId %d\n"),
987 jmr.JobId, jmr.MediaId);
993 * Simulate the database call that updates the MD5 record
995 static int update_MD5_record(B_DB *db, char *MD5buf, DEV_RECORD *rec)
999 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
1001 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
1002 rec->VolSessionId, rec->VolSessionTime);
1011 if (!db_add_MD5_to_file_record(db, mjcr->FileId, MD5buf)) {
1012 Pmsg1(0, _("Could not add MD5 to File record. ERR=%s\n"), db_strerror(db));
1017 Pmsg0(000, _("Updated MD5 record\n"));
1025 * Create a JCR as if we are really starting the job
1027 static JCR *create_jcr(JOB_DBR *jr, DEV_RECORD *rec, uint32_t JobId)
1031 * Transfer as much as possible to the Job JCR. Most important is
1032 * the JobId and the ClientId.
1034 jobjcr = new_jcr(sizeof(JCR), dird_free_jcr);
1035 jobjcr->JobType = jr->Type;
1036 jobjcr->JobLevel = jr->Level;
1037 jobjcr->JobStatus = jr->JobStatus;
1038 strcpy(jobjcr->Job, jr->Job);
1039 jobjcr->JobId = JobId; /* this is JobId on tape */
1040 jobjcr->sched_time = jr->SchedTime;
1041 jobjcr->start_time = jr->StartTime;
1042 jobjcr->VolSessionId = rec->VolSessionId;
1043 jobjcr->VolSessionTime = rec->VolSessionTime;
1044 jobjcr->ClientId = jr->ClientId;
1045 attach_jcr_to_device(dev, jobjcr);
1049 /* Dummies to replace askdir.c */
1050 int dir_get_volume_info(JCR *jcr, int writing) { return 1;}
1051 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
1052 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
1053 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
1054 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
1055 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
1056 int dir_send_job_status(JCR *jcr) {return 1;}
1059 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
1062 * We are at the end of reading a tape. Now, we simulate handling
1063 * the end of writing a tape by wiffling through the attached
1064 * jcrs creating jobmedia records.
1066 Dmsg1(100, "Walk attached jcrs. Volume=%s\n", dev->VolCatInfo.VolCatName);
1067 for (JCR *mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
1069 Pmsg1(000, "create JobMedia for Job %s\n", mjcr->Job);
1071 if (dev->state & ST_TAPE) {
1072 mjcr->EndBlock = dev->block_num;
1073 mjcr->EndFile = dev->file;
1075 mjcr->EndBlock = (uint32_t)dev->file_addr;
1076 mjcr->StartBlock = (uint32_t)(dev->file_addr >> 32);
1078 if (!create_jobmedia_record(db, mjcr)) {
1079 Pmsg2(000, _("Could not create JobMedia record for Volume=%s Job=%s\n"),
1080 dev->VolCatInfo.VolCatName, mjcr->Job);
1084 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
1085 jcr->VolumeName, dev_name(dev));