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 verbose = 0;
85 static int update_db = 0;
86 static int update_vol_info = 0;
87 static int list_records = 0;
88 static int ignored_msgs = 0;
90 #define CONFIG_FILE "bacula-sd.conf"
98 "\nVersion: " VERSION " (" DATE ")\n\n"
99 "Usage: bscan [-d debug_level] <bacula-archive>\n"
100 " -b bootstrap specify a bootstrap file\n"
101 " -c <file> specify configuration file\n"
102 " -dnn set debug level to nn\n"
103 " -m update media info in database\n"
104 " -n name specify the database name (default bacula)\n"
105 " -u user specify database user name (default bacula)\n"
106 " -p password specify database password (default none)\n"
108 " -s synchronize or store in database\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;
120 my_name_is(argc, argv, "bscan");
121 init_msg(NULL, NULL);
124 while ((ch = getopt(argc, argv, "b:c:d:mn:p:rsu:vw:?")) != -1) {
127 bsr = parse_bsr(NULL, optarg);
130 case 'c': /* specify config file */
131 if (configfile != NULL) {
134 configfile = bstrdup(optarg);
137 case 'd': /* debug level */
138 debug_level = atoi(optarg);
139 if (debug_level <= 0)
156 db_password = optarg;
185 Pmsg0(0, _("Wrong number of arguments: \n"));
189 if (configfile == NULL) {
190 configfile = bstrdup(CONFIG_FILE);
193 parse_config(configfile);
195 me = (STORES *)GetNextRes(R_STORAGE, NULL);
198 Emsg1(M_ERROR_TERM, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
202 /* Check if -w option given, otherwise use resource for working directory */
204 working_directory = wd;
205 } else if (!me->working_directory) {
206 Emsg1(M_ERROR_TERM, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
209 working_directory = me->working_directory;
212 /* Check that working directory is good */
213 if (stat(working_directory, &stat_buf) != 0) {
214 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s not found. Cannot continue.\n"),
217 if (!S_ISDIR(stat_buf.st_mode)) {
218 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s is not a directory. Cannot continue.\n"),
222 bjcr = setup_jcr("bscan", argv[0], bsr);
223 dev = setup_to_access_device(bjcr, 1); /* read device */
228 if ((db=db_init_database(NULL, db_name, db_user, db_password)) == NULL) {
229 Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
231 if (!db_open_database(NULL, db)) {
232 Emsg0(M_ERROR_TERM, 0, db_strerror(db));
234 Dmsg0(200, "Database opened\n");
236 Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
246 static void do_scan()
248 fname = get_pool_memory(PM_FNAME);
249 ofile = get_pool_memory(PM_FNAME);
250 lname = get_pool_memory(PM_FNAME);
252 memset(&ar, 0, sizeof(ar));
253 memset(&pr, 0, sizeof(pr));
254 memset(&jr, 0, sizeof(jr));
255 memset(&cr, 0, sizeof(cr));
256 memset(&fsr, 0, sizeof(fsr));
257 memset(&fr, 0, sizeof(fr));
259 detach_jcr_from_device(dev, bjcr);
261 read_records(bjcr, dev, record_cb, mount_next_read_volume);
262 release_device(bjcr, dev);
264 free_pool_memory(fname);
265 free_pool_memory(ofile);
266 free_pool_memory(lname);
270 static void record_cb(JCR *bjcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
275 if (rec->data_len > 0) {
276 mr.VolBytes += rec->data_len + WRITE_RECHDR_LENGTH; /* Accumulate Volume bytes */
279 Pmsg5(000, _("Record: SessId=%u SessTim=%u FileIndex=%d Stream=%d len=%u\n"),
280 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex,
281 rec->Stream, rec->data_len);
284 * Check for Start or End of Session Record
287 if (rec->FileIndex < 0) {
288 int save_update_db = update_db;
291 dump_label_record(dev, rec, 1);
293 switch (rec->FileIndex) {
295 Pmsg0(000, _("Volume is prelabeled. This tape cannot be scanned.\n"));
299 unser_volume_label(dev, rec);
300 /* Check Pool info */
301 strcpy(pr.Name, dev->VolHdr.PoolName);
302 strcpy(pr.PoolType, dev->VolHdr.PoolType);
303 if (db_get_pool_record(bjcr, db, &pr)) {
305 Pmsg1(000, _("Pool record for %s found in DB.\n"), pr.Name);
309 Pmsg1(000, _("VOL_LABEL: Pool record not found for Pool: %s\n"),
312 create_pool_record(db, &pr);
314 if (strcmp(pr.PoolType, dev->VolHdr.PoolType) != 0) {
315 Pmsg2(000, _("VOL_LABEL: PoolType mismatch. DB=%s Vol=%s\n"),
316 pr.PoolType, dev->VolHdr.PoolType);
318 } else if (verbose) {
319 Pmsg1(000, _("Pool type \"%s\" is OK.\n"), pr.PoolType);
322 /* Check Media Info */
323 memset(&mr, 0, sizeof(mr));
324 strcpy(mr.VolumeName, dev->VolHdr.VolName);
325 mr.PoolId = pr.PoolId;
326 if (db_get_media_record(bjcr, db, &mr)) {
328 Pmsg1(000, _("Media record for %s found in DB.\n"), mr.VolumeName);
330 /* Clear out some volume statistics that will be updated */
331 mr.VolJobs = mr.VolFiles = mr.VolBlocks = 0;
332 mr.VolBytes = rec->data_len + 20;
335 Pmsg1(000, _("VOL_LABEL: Media record not found for Volume: %s\n"),
338 strcpy(mr.MediaType, dev->VolHdr.MediaType);
339 create_media_record(db, &mr, &dev->VolHdr);
341 if (strcmp(mr.MediaType, dev->VolHdr.MediaType) != 0) {
342 Pmsg2(000, _("VOL_LABEL: MediaType mismatch. DB=%s Vol=%s\n"),
343 mr.MediaType, dev->VolHdr.MediaType);
345 } else if (verbose) {
346 Pmsg1(000, _("Media type \"%s\" is OK.\n"), mr.MediaType);
348 /* Reset some JCR variables */
349 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
350 mjcr->VolFirstFile = mjcr->FileIndex = 0;
351 mjcr->StartBlock = mjcr->EndBlock = 0;
352 mjcr->StartFile = mjcr->EndFile = 0;
355 Pmsg1(000, _("VOL_LABEL: OK for Volume: %s\n"), mr.VolumeName);
359 if (ignored_msgs > 0) {
360 Pmsg1(000, _("%d \"errors\" ignored before first Start of Session record.\n"),
364 unser_session_label(&label, rec);
365 memset(&jr, 0, sizeof(jr));
366 jr.JobId = label.JobId;
367 if (db_get_job_record(bjcr, db, &jr)) {
368 /* Job record already exists in DB */
369 update_db = 0; /* don't change db in create_job_record */
371 Pmsg1(000, _("SOS_LABEL: Found Job record for JobId: %d\n"), jr.JobId);
374 /* Must create a Job record in DB */
376 Pmsg1(000, _("SOS_LABEL: Job record not found for JobId: %d\n"),
380 /* Create Client record if not already there */
381 strcpy(cr.Name, label.ClientName);
382 create_client_record(db, &cr);
383 jr.ClientId = cr.ClientId;
385 /* process label, if Job record exists don't update db */
386 mjcr = create_job_record(db, &jr, &label, rec);
387 update_db = save_update_db;
389 jr.PoolId = pr.PoolId;
390 /* Set start positions into JCR */
391 if (dev->state & ST_TAPE) {
392 mjcr->StartBlock = dev->block_num;
393 mjcr->StartFile = dev->file;
395 mjcr->StartBlock = (uint32_t)dev->file_addr;
396 mjcr->StartFile = (uint32_t)(dev->file_addr >> 32);
398 mjcr->start_time = jr.StartTime;
399 mjcr->JobLevel = jr.Level;
401 mjcr->client_name = get_pool_memory(PM_FNAME);
402 pm_strcpy(&mjcr->client_name, label.ClientName);
403 mjcr->pool_type = get_pool_memory(PM_FNAME);
404 pm_strcpy(&mjcr->pool_type, label.PoolType);
405 mjcr->fileset_name = get_pool_memory(PM_FNAME);
406 pm_strcpy(&mjcr->fileset_name, label.FileSetName);
407 mjcr->pool_name = get_pool_memory(PM_FNAME);
408 pm_strcpy(&mjcr->pool_name, label.PoolName);
410 if (rec->VolSessionId != jr.VolSessionId) {
411 Pmsg3(000, _("SOS_LABEL: VolSessId mismatch for JobId=%u. DB=%d Vol=%d\n"),
413 jr.VolSessionId, rec->VolSessionId);
416 if (rec->VolSessionTime != jr.VolSessionTime) {
417 Pmsg3(000, _("SOS_LABEL: VolSessTime mismatch for JobId=%u. DB=%d Vol=%d\n"),
419 jr.VolSessionTime, rec->VolSessionTime);
422 if (jr.PoolId != pr.PoolId) {
423 Pmsg3(000, _("SOS_LABEL: PoolId mismatch for JobId=%u. DB=%d Vol=%d\n"),
425 jr.PoolId, pr.PoolId);
430 unser_session_label(&elabel, rec);
432 /* Create FileSet record */
433 strcpy(fsr.FileSet, label.FileSetName);
434 strcpy(fsr.MD5, label.FileSetMD5);
435 create_fileset_record(db, &fsr);
436 jr.FileSetId = fsr.FileSetId;
438 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
440 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
441 rec->VolSessionId, rec->VolSessionTime);
445 /* Do the final update to the Job record */
446 update_job_record(db, &jr, &elabel, rec);
448 mjcr->end_time = jr.EndTime;
449 mjcr->JobStatus = JS_Terminated;
451 /* Create JobMedia record */
452 create_jobmedia_record(db, mjcr);
453 detach_jcr_from_device(dev, mjcr);
459 case EOT_LABEL: /* end of all tapes */
461 * Wiffle through all jobs still open and close
465 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
466 jr.JobId = mjcr->JobId;
467 jr.JobStatus = JS_ErrorTerminated;
468 jr.JobFiles = mjcr->JobFiles;
469 jr.JobBytes = mjcr->JobBytes;
470 jr.VolSessionId = mjcr->VolSessionId;
471 jr.VolSessionTime = mjcr->VolSessionTime;
472 jr.JobTDate = (utime_t)mjcr->start_time;
473 jr.ClientId = mjcr->ClientId;
475 if (!db_update_job_end_record(bjcr, db, &jr)) {
476 Pmsg1(0, _("Could not update job record. ERR=%s\n"), db_strerror(db));
480 mr.VolFiles = rec->File;
481 mr.VolBlocks = rec->Block;
482 mr.VolBytes += mr.VolBlocks * WRITE_BLKHDR_LENGTH; /* approx. */
484 update_media_record(db, &mr);
485 Pmsg3(0, _("End of Volume. VolFiles=%u VolBlocks=%u VolBytes=%s\n"), mr.VolFiles,
486 mr.VolBlocks, edit_uint64_with_commas(mr.VolBytes, ec1));
495 /* File Attributes stream */
496 if (rec->Stream == STREAM_UNIX_ATTRIBUTES || rec->Stream == STREAM_WIN32_ATTRIBUTES) {
499 if (sizeof_pool_memory(fname) < rec->data_len) {
500 fname = realloc_pool_memory(fname, rec->data_len + 1);
502 if (sizeof_pool_memory(lname) < rec->data_len) {
503 lname = realloc_pool_memory(lname, rec->data_len + 1);
509 * An Attributes record consists of:
514 * Link name (if file linked i.e. FT_LNK)
517 sscanf(rec->data, "%ld %d", &record_file_index, &type);
518 if (record_file_index != rec->FileIndex)
519 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
520 rec->FileIndex, record_file_index);
522 while (*ap++ != ' ') /* skip record file index */
524 while (*ap++ != ' ') /* skip type */
526 /* Save filename and position to attributes */
531 *fp = *ap++; /* terminate filename & point to attribs */
533 /* Skip through attributes to link name */
538 strcat(lname, lp); /* "save" link name */
540 decode_stat(ap, &statp);
541 print_ls_output(fname, lname, type, &statp);
543 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
545 if (mr.VolJobs > 0) {
546 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Attributes record.\n"),
547 rec->VolSessionId, rec->VolSessionTime);
553 fr.JobId = mjcr->JobId;
555 if (db_get_file_attributes_record(bjcr, db, fname, &fr)) {
557 Pmsg1(000, _("File record already exists for: %s\n"), fname);
560 create_file_attributes_record(db, mjcr, fname, lname, type, ap, rec);
565 } else if (rec->Stream == STREAM_FILE_DATA) {
566 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
568 if (mr.VolJobs > 0) {
569 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for File Data record.\n"),
570 rec->VolSessionId, rec->VolSessionTime);
576 mjcr->JobBytes += rec->data_len;
577 free_jcr(mjcr); /* done using JCR */
579 } else if (rec->Stream == STREAM_SPARSE_DATA) {
580 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
582 if (mr.VolJobs > 0) {
583 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Sparse Data record.\n"),
584 rec->VolSessionId, rec->VolSessionTime);
590 mjcr->JobBytes += rec->data_len - sizeof(uint64_t);
591 free_jcr(mjcr); /* done using JCR */
593 } else if (rec->Stream == STREAM_GZIP_DATA) {
594 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
596 if (mr.VolJobs > 0) {
597 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for GZIP Data record.\n"),
598 rec->VolSessionId, rec->VolSessionTime);
604 mjcr->JobBytes += rec->data_len; /* No correct, we should expand it */
605 free_jcr(mjcr); /* done using JCR */
607 } else if (rec->Stream == STREAM_SPARSE_GZIP_DATA) {
608 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
610 if (mr.VolJobs > 0) {
611 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Sparse GZIP Data record.\n"),
612 rec->VolSessionId, rec->VolSessionTime);
618 mjcr->JobBytes += rec->data_len - sizeof(uint64_t); /* No correct, we should expand it */
619 free_jcr(mjcr); /* done using JCR */
622 } else if (rec->Stream == STREAM_MD5_SIGNATURE) {
624 bin_to_base64(MD5buf, (char *)rec->data, 16); /* encode 16 bytes */
626 Pmsg1(000, _("Got MD5 record: %s\n"), MD5buf);
628 update_SIG_record(db, MD5buf, rec, MD5_SIG);
630 } else if (rec->Stream == STREAM_SHA1_SIGNATURE) {
632 bin_to_base64(SIGbuf, (char *)rec->data, 20); /* encode 20 bytes */
634 Pmsg1(000, _("Got SHA1 record: %s\n"), SIGbuf);
636 update_SIG_record(db, SIGbuf, rec, SHA1_SIG);
639 } else if (rec->Stream == STREAM_PROGRAM_NAMES) {
641 Pmsg1(000, _("Got Prog Names Stream: %s\n"), rec->data);
643 } else if (rec->Stream == STREAM_PROGRAM_DATA) {
645 Pmsg0(000, _("Got Prog Data Stream record.\n"));
648 Pmsg2(0, _("Unknown stream type!!! stream=%d data=%s\n"), rec->Stream, rec->data);
654 * Free the Job Control Record if no one is still using it.
655 * Called from main free_jcr() routine in src/lib/jcr.c so
656 * that we can do our Director specific cleanup of the jcr.
658 static void dird_free_jcr(JCR *jcr)
660 Dmsg0(200, "Start dird free_jcr\n");
662 if (jcr->file_bsock) {
663 Dmsg0(200, "Close File bsock\n");
664 bnet_close(jcr->file_bsock);
666 if (jcr->store_bsock) {
667 Dmsg0(200, "Close Store bsock\n");
668 bnet_close(jcr->store_bsock);
670 if (jcr->RestoreBootstrap) {
671 free(jcr->RestoreBootstrap);
673 Dmsg0(200, "End dird free_jcr\n");
677 * We got a File Attributes record on the tape. Now, lookup the Job
678 * record, and then create the attributes record.
680 static int create_file_attributes_record(B_DB *db, JCR *mjcr,
681 char *fname, char *lname, int type,
682 char *ap, DEV_RECORD *rec)
687 ar.ClientId = mjcr->ClientId;
688 ar.JobId = mjcr->JobId;
689 ar.Stream = rec->Stream;
690 ar.FileIndex = rec->FileIndex;
692 if (mjcr->VolFirstFile == 0) {
693 mjcr->VolFirstFile = rec->FileIndex;
695 mjcr->FileIndex = rec->FileIndex;
702 if (!db_create_file_attributes_record(bjcr, db, &ar)) {
703 Pmsg1(0, _("Could not create File Attributes record. ERR=%s\n"), db_strerror(db));
706 mjcr->FileId = ar.FileId;
709 Pmsg1(000, _("Created File record: %s\n"), fname);
715 * For each Volume we see, we create a Medium record
717 static int create_media_record(B_DB *db, MEDIA_DBR *mr, VOLUME_LABEL *vl)
722 strcpy(mr->VolStatus, "Full");
723 mr->VolRetention = 365 * 3600 * 24; /* 1 year */
724 if (vl->VerNum >= 11) {
725 mr->FirstWritten = btime_to_utime(vl->write_btime);
726 mr->LabelDate = btime_to_utime(vl->label_btime);
728 /* DEPRECATED DO NOT USE */
729 dt.julian_day_number = vl->write_date;
730 dt.julian_day_fraction = vl->write_time;
732 mr->FirstWritten = mktime(&tm);
733 dt.julian_day_number = vl->label_date;
734 dt.julian_day_fraction = vl->label_time;
736 mr->LabelDate = mktime(&tm);
738 lasttime = mr->LabelDate;
744 if (!db_create_media_record(bjcr, db, mr)) {
745 Pmsg1(0, _("Could not create media record. ERR=%s\n"), db_strerror(db));
748 if (!db_update_media_record(bjcr, db, mr)) {
749 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
753 Pmsg1(000, _("Created Media record for Volume: %s\n"), mr->VolumeName);
760 * Called at end of media to update it
762 static int update_media_record(B_DB *db, MEDIA_DBR *mr)
764 if (!update_db && !update_vol_info) {
768 mr->LastWritten = lasttime;
769 if (!db_update_media_record(bjcr, db, mr)) {
770 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
774 Pmsg1(000, _("Updated Media record at end of Volume: %s\n"), mr->VolumeName);
781 static int create_pool_record(B_DB *db, POOL_DBR *pr)
785 pr->VolRetention = 355 * 3600 * 24; /* 1 year */
790 if (!db_create_pool_record(bjcr, db, pr)) {
791 Pmsg1(0, _("Could not create pool record. ERR=%s\n"), db_strerror(db));
795 Pmsg1(000, _("Created Pool record for Pool: %s\n"), pr->Name);
803 * Called from SOS to create a client for the current Job
805 static int create_client_record(B_DB *db, CLIENT_DBR *cr)
810 if (!db_create_client_record(bjcr, db, cr)) {
811 Pmsg1(0, _("Could not create Client record. ERR=%s\n"), db_strerror(db));
815 Pmsg1(000, _("Created Client record for Client: %s\n"), cr->Name);
820 static int create_fileset_record(B_DB *db, FILESET_DBR *fsr)
826 if (fsr->MD5[0] == 0) {
827 fsr->MD5[0] = ' '; /* Equivalent to nothing */
830 if (db_get_fileset_record(bjcr, db, fsr)) {
832 Pmsg1(000, _("Fileset \"%s\" already exists.\n"), fsr->FileSet);
835 if (!db_create_fileset_record(bjcr, db, fsr)) {
836 Pmsg2(0, _("Could not create FileSet record \"%s\". ERR=%s\n"),
837 fsr->FileSet, db_strerror(db));
841 Pmsg1(000, _("Created FileSet record \"%s\"\n"), fsr->FileSet);
848 * Simulate the two calls on the database to create
849 * the Job record and to update it when the Job actually
852 static JCR *create_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *label,
859 jr->JobId = label->JobId;
860 jr->Type = label->JobType;
861 jr->Level = label->JobLevel;
862 jr->JobStatus = JS_Created;
863 strcpy(jr->Name, label->JobName);
864 strcpy(jr->Job, label->Job);
865 if (label->VerNum >= 11) {
866 jr->SchedTime = btime_to_unix(label->write_btime);
868 dt.julian_day_number = label->write_date;
869 dt.julian_day_fraction = label->write_time;
871 jr->SchedTime = mktime(&tm);
874 jr->StartTime = jr->SchedTime;
875 jr->JobTDate = (utime_t)jr->SchedTime;
876 jr->VolSessionId = rec->VolSessionId;
877 jr->VolSessionTime = rec->VolSessionTime;
879 /* Now create a JCR as if starting the Job */
880 mjcr = create_jcr(jr, rec, label->JobId);
886 /* This creates the bare essentials */
887 if (!db_create_job_record(bjcr, db, jr)) {
888 Pmsg1(0, _("Could not create JobId record. ERR=%s\n"), db_strerror(db));
892 /* This adds the client, StartTime, JobTDate, ... */
893 if (!db_update_job_start_record(bjcr, db, jr)) {
894 Pmsg1(0, _("Could not update job start record. ERR=%s\n"), db_strerror(db));
897 Pmsg2(000, _("Created new JobId=%u record for original JobId=%u\n"), jr->JobId,
899 mjcr->JobId = jr->JobId; /* set new JobId */
904 * Simulate the database call that updates the Job
905 * at Job termination time.
907 static int update_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *elabel,
914 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
916 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
917 rec->VolSessionId, rec->VolSessionTime);
920 if (elabel->VerNum >= 11) {
921 jr->EndTime = btime_to_unix(elabel->write_btime);
923 dt.julian_day_number = elabel->write_date;
924 dt.julian_day_fraction = elabel->write_time;
926 jr->EndTime = mktime(&tm);
928 lasttime = jr->EndTime;
929 mjcr->end_time = jr->EndTime;
931 jr->JobId = mjcr->JobId;
932 jr->JobStatus = elabel->JobStatus;
933 mjcr->JobStatus = elabel->JobStatus;
934 jr->JobFiles = elabel->JobFiles;
935 jr->JobBytes = elabel->JobBytes;
936 jr->VolSessionId = rec->VolSessionId;
937 jr->VolSessionTime = rec->VolSessionTime;
938 jr->JobTDate = (utime_t)mjcr->start_time;
939 jr->ClientId = mjcr->ClientId;
946 if (!db_update_job_end_record(bjcr, db, jr)) {
947 Pmsg2(0, _("Could not update JobId=%u record. ERR=%s\n"), jr->JobId, db_strerror(db));
952 Pmsg1(000, _("Updated Job termination record for new JobId=%u\n"), jr->JobId);
956 static char term_code[70];
957 char sdt[50], edt[50];
958 char ec1[30], ec2[30], ec3[30];
960 switch (mjcr->JobStatus) {
962 term_msg = _("Backup OK");
965 case JS_ErrorTerminated:
966 term_msg = _("*** Backup Error ***");
969 term_msg = _("Backup Cancelled");
972 term_msg = term_code;
973 sprintf(term_code, _("Job Termination code: %d"), mjcr->JobStatus);
976 bstrftime(sdt, sizeof(sdt), mjcr->start_time);
977 bstrftime(edt, sizeof(edt), mjcr->end_time);
988 Volume Session Id: %d\n\
989 Volume Session Time: %d\n\
990 Last Volume Bytes: %s\n\
991 Termination: %s\n\n"),
996 job_level_to_str(mjcr->JobLevel),
1000 edit_uint64_with_commas(mjcr->JobFiles, ec1),
1001 edit_uint64_with_commas(mjcr->JobBytes, ec2),
1003 mjcr->VolSessionTime,
1004 edit_uint64_with_commas(mr.VolBytes, ec3),
1011 static int create_jobmedia_record(B_DB *db, JCR *mjcr)
1015 if (dev->state & ST_TAPE) {
1016 mjcr->EndBlock = dev->EndBlock;
1017 mjcr->EndFile = dev->EndFile;
1019 mjcr->EndBlock = (uint32_t)dev->file_addr;
1020 mjcr->EndFile = (uint32_t)(dev->file_addr >> 32);
1023 memset(&jmr, 0, sizeof(jmr));
1024 jmr.JobId = mjcr->JobId;
1025 jmr.MediaId = mr.MediaId;
1026 jmr.FirstIndex = mjcr->VolFirstFile;
1027 jmr.LastIndex = mjcr->FileIndex;
1028 jmr.StartFile = mjcr->StartFile;
1029 jmr.EndFile = mjcr->EndFile;
1030 jmr.StartBlock = mjcr->StartBlock;
1031 jmr.EndBlock = mjcr->EndBlock;
1038 if (!db_create_jobmedia_record(bjcr, db, &jmr)) {
1039 Pmsg1(0, _("Could not create JobMedia record. ERR=%s\n"), db_strerror(db));
1043 Pmsg2(000, _("Created JobMedia record JobId %d, MediaId %d\n"),
1044 jmr.JobId, jmr.MediaId);
1050 * Simulate the database call that updates the MD5/SHA1 record
1052 static int update_SIG_record(B_DB *db, char *SIGbuf, DEV_RECORD *rec, int type)
1056 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
1058 if (mr.VolJobs > 0) {
1059 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for MD5/SHA1 record.\n"),
1060 rec->VolSessionId, rec->VolSessionTime);
1072 if (!db_add_SIG_to_file_record(bjcr, db, mjcr->FileId, SIGbuf, type)) {
1073 Pmsg1(0, _("Could not add MD5/SHA1 to File record. ERR=%s\n"), db_strerror(db));
1078 Pmsg0(000, _("Updated MD5/SHA1 record\n"));
1086 * Create a JCR as if we are really starting the job
1088 static JCR *create_jcr(JOB_DBR *jr, DEV_RECORD *rec, uint32_t JobId)
1092 * Transfer as much as possible to the Job JCR. Most important is
1093 * the JobId and the ClientId.
1095 jobjcr = new_jcr(sizeof(JCR), dird_free_jcr);
1096 jobjcr->JobType = jr->Type;
1097 jobjcr->JobLevel = jr->Level;
1098 jobjcr->JobStatus = jr->JobStatus;
1099 strcpy(jobjcr->Job, jr->Job);
1100 jobjcr->JobId = JobId; /* this is JobId on tape */
1101 jobjcr->sched_time = jr->SchedTime;
1102 jobjcr->start_time = jr->StartTime;
1103 jobjcr->VolSessionId = rec->VolSessionId;
1104 jobjcr->VolSessionTime = rec->VolSessionTime;
1105 jobjcr->ClientId = jr->ClientId;
1106 attach_jcr_to_device(dev, jobjcr);
1110 /* Dummies to replace askdir.c */
1111 int dir_get_volume_info(JCR *jcr, int writing) { return 1;}
1112 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
1113 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
1114 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
1115 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
1116 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
1117 int dir_send_job_status(JCR *jcr) {return 1;}
1120 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
1123 * We are at the end of reading a tape. Now, we simulate handling
1124 * the end of writing a tape by wiffling through the attached
1125 * jcrs creating jobmedia records.
1127 Dmsg1(100, "Walk attached jcrs. Volume=%s\n", dev->VolCatInfo.VolCatName);
1128 for (JCR *mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
1130 Pmsg1(000, _("Create JobMedia for Job %s\n"), mjcr->Job);
1132 if (dev->state & ST_TAPE) {
1133 mjcr->EndBlock = dev->EndBlock;
1134 mjcr->EndFile = dev->EndFile;
1136 mjcr->EndBlock = (uint32_t)dev->file_addr;
1137 mjcr->StartBlock = (uint32_t)(dev->file_addr >> 32);
1139 if (!create_jobmedia_record(db, mjcr)) {
1140 Pmsg2(000, _("Could not create JobMedia record for Volume=%s Job=%s\n"),
1141 dev->VolCatInfo.VolCatName, mjcr->Job);
1145 fprintf(stderr, _("Mount Volume %s on device %s and press return when ready: "),
1146 jcr->VolumeName, dev_name(dev));