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 int 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 */
66 static DEVICE *dev = NULL;
68 static JCR *bjcr; /* jcr for bscan */
69 static BSR *bsr = NULL;
74 static FILESET_DBR fsr;
77 static SESSION_LABEL label;
78 static SESSION_LABEL elabel;
81 static time_t lasttime = 0;
83 static char *db_name = "bacula";
84 static char *db_user = "bacula";
85 static char *db_password = "";
86 static char *db_host = NULL;
87 static char *wd = NULL;
88 static int update_db = 0;
89 static int update_vol_info = 0;
90 static int list_records = 0;
91 static int ignored_msgs = 0;
93 #define CONFIG_FILE "bacula-sd.conf"
101 "\nVersion: " VERSION " (" BDATE ")\n\n"
102 "Usage: bscan [-d debug_level] <bacula-archive>\n"
103 " -b bootstrap specify a bootstrap file\n"
104 " -c <file> specify configuration file\n"
105 " -d <nn> set debug level to nn\n"
106 " -m update media info in database\n"
107 " -n <name> specify the database name (default bacula)\n"
108 " -u <user> specify database user name (default bacula)\n"
109 " -p <password specify database password (default none)\n"
110 " -h <host> specify database host (default NULL)\n"
112 " -s synchronize or store in database\n"
114 " -V <Volumes> specify Volume names (separated by |)\n"
115 " -w <dir> specify working directory (default from conf file)\n"
116 " -? print this message\n\n"));
120 int main (int argc, char *argv[])
123 struct stat stat_buf;
124 char *VolumeName = NULL;
126 my_name_is(argc, argv, "bscan");
127 init_msg(NULL, NULL);
130 while ((ch = getopt(argc, argv, "b:c:d:h:mn:p:rsu:vV:w:?")) != -1) {
133 bsr = parse_bsr(NULL, optarg);
136 case 'c': /* specify config file */
137 if (configfile != NULL) {
140 configfile = bstrdup(optarg);
143 case 'd': /* debug level */
144 debug_level = atoi(optarg);
145 if (debug_level <= 0)
166 db_password = optarg;
181 case 'V': /* Volume name */
199 Pmsg0(0, _("Wrong number of arguments: \n"));
203 if (configfile == NULL) {
204 configfile = bstrdup(CONFIG_FILE);
207 parse_config(configfile);
209 me = (STORES *)GetNextRes(R_STORAGE, NULL);
212 Emsg1(M_ERROR_TERM, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
216 /* Check if -w option given, otherwise use resource for working directory */
218 working_directory = wd;
219 } else if (!me->working_directory) {
220 Emsg1(M_ERROR_TERM, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
223 working_directory = me->working_directory;
226 /* Check that working directory is good */
227 if (stat(working_directory, &stat_buf) != 0) {
228 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s not found. Cannot continue.\n"),
231 if (!S_ISDIR(stat_buf.st_mode)) {
232 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s is not a directory. Cannot continue.\n"),
236 bjcr = setup_jcr("bscan", argv[0], bsr, VolumeName);
237 dev = setup_to_access_device(bjcr, 1); /* read device */
242 if ((db=db_init_database(NULL, db_name, db_user, db_password, db_host, 0, NULL)) == NULL) {
243 Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
245 if (!db_open_database(NULL, db)) {
246 Emsg0(M_ERROR_TERM, 0, db_strerror(db));
248 Dmsg0(200, "Database opened\n");
250 Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
260 * We are at the end of reading a tape. Now, we simulate handling
261 * the end of writing a tape by wiffling through the attached
262 * jcrs creating jobmedia records.
264 static int bscan_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
266 Dmsg1(100, "Walk attached jcrs. Volume=%s\n", dev->VolCatInfo.VolCatName);
267 for (JCR *mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
269 Pmsg1(000, _("Create JobMedia for Job %s\n"), mjcr->Job);
271 if (dev->state & ST_TAPE) {
272 mjcr->EndBlock = dev->EndBlock;
273 mjcr->EndFile = dev->EndFile;
275 mjcr->EndBlock = (uint32_t)dev->file_addr;
276 mjcr->StartBlock = (uint32_t)(dev->file_addr >> 32);
278 if (!create_jobmedia_record(db, mjcr)) {
279 Pmsg2(000, _("Could not create JobMedia record for Volume=%s Job=%s\n"),
280 dev->VolCatInfo.VolCatName, mjcr->Job);
283 /* Now let common read routine get up next tape. Note,
284 * we call mount_next... with bscan's jcr because that is where we
285 * have the Volume list, but we get attached.
287 int stat = mount_next_read_volume(jcr, dev, block);
288 /* we must once more detach ourselves (attached by mount_next ...) */
289 detach_jcr_from_device(dev, jcr); /* detach bscan jcr */
293 static void do_scan()
297 memset(&ar, 0, sizeof(ar));
298 memset(&pr, 0, sizeof(pr));
299 memset(&jr, 0, sizeof(jr));
300 memset(&cr, 0, sizeof(cr));
301 memset(&fsr, 0, sizeof(fsr));
302 memset(&fr, 0, sizeof(fr));
304 /* Detach bscan's jcr as we are not a real Job on the tape */
305 detach_jcr_from_device(dev, bjcr);
307 read_records(bjcr, dev, record_cb, bscan_mount_next_read_volume);
308 release_device(bjcr, dev);
314 static int record_cb(JCR *bjcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
319 if (rec->data_len > 0) {
320 mr.VolBytes += rec->data_len + WRITE_RECHDR_LENGTH; /* Accumulate Volume bytes */
323 Pmsg5(000, _("Record: SessId=%u SessTim=%u FileIndex=%d Stream=%d len=%u\n"),
324 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex,
325 rec->Stream, rec->data_len);
328 * Check for Start or End of Session Record
331 if (rec->FileIndex < 0) {
332 int save_update_db = update_db;
335 dump_label_record(dev, rec, 1);
337 switch (rec->FileIndex) {
339 Pmsg0(000, _("Volume is prelabeled. This tape cannot be scanned.\n"));
344 unser_volume_label(dev, rec);
345 /* Check Pool info */
346 bstrncpy(pr.Name, dev->VolHdr.PoolName, sizeof(pr.Name));
347 bstrncpy(pr.PoolType, dev->VolHdr.PoolType, sizeof(pr.PoolType));
348 if (db_get_pool_record(bjcr, db, &pr)) {
350 Pmsg1(000, _("Pool record for %s found in DB.\n"), pr.Name);
354 Pmsg1(000, _("VOL_LABEL: Pool record not found for Pool: %s\n"),
357 create_pool_record(db, &pr);
359 if (strcmp(pr.PoolType, dev->VolHdr.PoolType) != 0) {
360 Pmsg2(000, _("VOL_LABEL: PoolType mismatch. DB=%s Vol=%s\n"),
361 pr.PoolType, dev->VolHdr.PoolType);
363 } else if (verbose) {
364 Pmsg1(000, _("Pool type \"%s\" is OK.\n"), pr.PoolType);
367 /* Check Media Info */
368 memset(&mr, 0, sizeof(mr));
369 bstrncpy(mr.VolumeName, dev->VolHdr.VolName, sizeof(mr.VolumeName));
370 mr.PoolId = pr.PoolId;
371 if (db_get_media_record(bjcr, db, &mr)) {
373 Pmsg1(000, _("Media record for %s found in DB.\n"), mr.VolumeName);
375 /* Clear out some volume statistics that will be updated */
376 mr.VolJobs = mr.VolFiles = mr.VolBlocks = 0;
377 mr.VolBytes = rec->data_len + 20;
380 Pmsg1(000, _("VOL_LABEL: Media record not found for Volume: %s\n"),
383 bstrncpy(mr.MediaType, dev->VolHdr.MediaType, sizeof(mr.MediaType));
384 create_media_record(db, &mr, &dev->VolHdr);
386 if (strcmp(mr.MediaType, dev->VolHdr.MediaType) != 0) {
387 Pmsg2(000, _("VOL_LABEL: MediaType mismatch. DB=%s Vol=%s\n"),
388 mr.MediaType, dev->VolHdr.MediaType);
390 } else if (verbose) {
391 Pmsg1(000, _("Media type \"%s\" is OK.\n"), mr.MediaType);
393 /* Reset some JCR variables */
394 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
395 mjcr->VolFirstIndex = mjcr->FileIndex = 0;
396 mjcr->StartBlock = mjcr->EndBlock = 0;
397 mjcr->StartFile = mjcr->EndFile = 0;
400 Pmsg1(000, _("VOL_LABEL: OK for Volume: %s\n"), mr.VolumeName);
405 if (ignored_msgs > 0) {
406 Pmsg1(000, _("%d \"errors\" ignored before first Start of Session record.\n"),
410 unser_session_label(&label, rec);
411 memset(&jr, 0, sizeof(jr));
412 jr.JobId = label.JobId;
413 if (db_get_job_record(bjcr, db, &jr)) {
414 /* Job record already exists in DB */
415 update_db = 0; /* don't change db in create_job_record */
417 Pmsg1(000, _("SOS_LABEL: Found Job record for JobId: %d\n"), jr.JobId);
420 /* Must create a Job record in DB */
422 Pmsg1(000, _("SOS_LABEL: Job record not found for JobId: %d\n"),
426 /* Create Client record if not already there */
427 bstrncpy(cr.Name, label.ClientName, sizeof(cr.Name));
428 create_client_record(db, &cr);
429 jr.ClientId = cr.ClientId;
431 /* process label, if Job record exists don't update db */
432 mjcr = create_job_record(db, &jr, &label, rec);
433 update_db = save_update_db;
435 jr.PoolId = pr.PoolId;
436 /* Set start positions into JCR */
437 if (dev->state & ST_TAPE) {
438 mjcr->StartBlock = dev->block_num;
439 mjcr->StartFile = dev->file;
441 mjcr->StartBlock = (uint32_t)dev->file_addr;
442 mjcr->StartFile = (uint32_t)(dev->file_addr >> 32);
444 mjcr->start_time = jr.StartTime;
445 mjcr->JobLevel = jr.Level;
447 mjcr->client_name = get_pool_memory(PM_FNAME);
448 pm_strcpy(&mjcr->client_name, label.ClientName);
449 mjcr->pool_type = get_pool_memory(PM_FNAME);
450 pm_strcpy(&mjcr->pool_type, label.PoolType);
451 mjcr->fileset_name = get_pool_memory(PM_FNAME);
452 pm_strcpy(&mjcr->fileset_name, label.FileSetName);
453 mjcr->pool_name = get_pool_memory(PM_FNAME);
454 pm_strcpy(&mjcr->pool_name, label.PoolName);
456 if (rec->VolSessionId != jr.VolSessionId) {
457 Pmsg3(000, _("SOS_LABEL: VolSessId mismatch for JobId=%u. DB=%d Vol=%d\n"),
459 jr.VolSessionId, rec->VolSessionId);
462 if (rec->VolSessionTime != jr.VolSessionTime) {
463 Pmsg3(000, _("SOS_LABEL: VolSessTime mismatch for JobId=%u. DB=%d Vol=%d\n"),
465 jr.VolSessionTime, rec->VolSessionTime);
468 if (jr.PoolId != pr.PoolId) {
469 Pmsg3(000, _("SOS_LABEL: PoolId mismatch for JobId=%u. DB=%d Vol=%d\n"),
471 jr.PoolId, pr.PoolId);
477 unser_session_label(&elabel, rec);
479 /* Create FileSet record */
480 bstrncpy(fsr.FileSet, label.FileSetName, sizeof(fsr.FileSet));
481 bstrncpy(fsr.MD5, label.FileSetMD5, sizeof(fsr.MD5));
482 create_fileset_record(db, &fsr);
483 jr.FileSetId = fsr.FileSetId;
485 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
487 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
488 rec->VolSessionId, rec->VolSessionTime);
492 /* Do the final update to the Job record */
493 update_job_record(db, &jr, &elabel, rec);
495 mjcr->end_time = jr.EndTime;
496 mjcr->JobStatus = JS_Terminated;
498 /* Create JobMedia record */
499 create_jobmedia_record(db, mjcr);
500 detach_jcr_from_device(dev, mjcr);
508 case EOT_LABEL: /* end of all tapes */
510 * Wiffle through all jobs still open and close
514 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
515 jr.JobId = mjcr->JobId;
516 jr.JobStatus = JS_ErrorTerminated;
517 jr.JobFiles = mjcr->JobFiles;
518 jr.JobBytes = mjcr->JobBytes;
519 jr.VolSessionId = mjcr->VolSessionId;
520 jr.VolSessionTime = mjcr->VolSessionTime;
521 jr.JobTDate = (utime_t)mjcr->start_time;
522 jr.ClientId = mjcr->ClientId;
524 if (!db_update_job_end_record(bjcr, db, &jr)) {
525 Pmsg1(0, _("Could not update job record. ERR=%s\n"), db_strerror(db));
529 mr.VolFiles = rec->File;
530 mr.VolBlocks = rec->Block;
531 mr.VolBytes += mr.VolBlocks * WRITE_BLKHDR_LENGTH; /* approx. */
533 update_media_record(db, &mr);
534 Pmsg3(0, _("End of all Volumes. VolFiles=%u VolBlocks=%u VolBytes=%s\n"), mr.VolFiles,
535 mr.VolBlocks, edit_uint64_with_commas(mr.VolBytes, ec1));
543 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
545 if (mr.VolJobs > 0) {
546 Pmsg2(000, _("Could not find Job for SessId=%d SessTime=%d record.\n"),
547 rec->VolSessionId, rec->VolSessionTime);
553 if (mjcr->VolFirstIndex == 0) {
554 mjcr->VolFirstIndex = block->FirstIndex;
557 /* File Attributes stream */
558 switch (rec->Stream) {
559 case STREAM_UNIX_ATTRIBUTES:
560 case STREAM_UNIX_ATTRIBUTES_EX:
562 if (!unpack_attributes_record(bjcr, rec->Stream, rec->data, attr)) {
563 Emsg0(M_ERROR_TERM, 0, _("Cannot continue.\n"));
566 if (attr->file_index != rec->FileIndex) {
567 Emsg2(M_ERROR_TERM, 0, _("Record header file index %ld not equal record index %ld\n"),
568 rec->FileIndex, attr->file_index);
572 decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
573 build_attr_output_fnames(bjcr, attr);
574 print_ls_output(bjcr, attr);
576 fr.JobId = mjcr->JobId;
578 if (db_get_file_attributes_record(bjcr, db, attr->fname, NULL, &fr)) {
580 Pmsg1(000, _("File record already exists for: %s\n"), attr->fname);
583 create_file_attributes_record(db, mjcr, attr->fname, attr->lname,
584 attr->type, attr->attr, rec);
590 case STREAM_WIN32_DATA:
591 case STREAM_FILE_DATA:
592 case STREAM_SPARSE_DATA:
593 mjcr->JobBytes += rec->data_len;
594 if (rec->Stream == STREAM_SPARSE_DATA) {
595 mjcr->JobBytes -= sizeof(uint64_t);
598 free_jcr(mjcr); /* done using JCR */
601 case STREAM_GZIP_DATA:
602 mjcr->JobBytes += rec->data_len; /* No correct, we should expand it */
603 free_jcr(mjcr); /* done using JCR */
606 case STREAM_SPARSE_GZIP_DATA:
607 mjcr->JobBytes += rec->data_len - sizeof(uint64_t); /* No correct, we should expand it */
608 free_jcr(mjcr); /* done using JCR */
611 /* Win32 GZIP stream */
612 case STREAM_WIN32_GZIP_DATA:
613 mjcr->JobBytes += rec->data_len;
614 free_jcr(mjcr); /* done using JCR */
617 case STREAM_MD5_SIGNATURE:
619 bin_to_base64(MD5buf, (char *)rec->data, 16); /* encode 16 bytes */
621 Pmsg1(000, _("Got MD5 record: %s\n"), MD5buf);
623 update_SIG_record(db, MD5buf, rec, MD5_SIG);
626 case STREAM_SHA1_SIGNATURE:
628 bin_to_base64(SIGbuf, (char *)rec->data, 20); /* encode 20 bytes */
630 Pmsg1(000, _("Got SHA1 record: %s\n"), SIGbuf);
632 update_SIG_record(db, SIGbuf, rec, SHA1_SIG);
636 case STREAM_PROGRAM_NAMES:
638 Pmsg1(000, _("Got Prog Names Stream: %s\n"), rec->data);
642 case STREAM_PROGRAM_DATA:
644 Pmsg0(000, _("Got Prog Data Stream record.\n"));
648 Pmsg2(0, _("Unknown stream type!!! stream=%d data=%s\n"), rec->Stream, rec->data);
655 * Free the Job Control Record if no one is still using it.
656 * Called from main free_jcr() routine in src/lib/jcr.c so
657 * that we can do our Director specific cleanup of the jcr.
659 static void dird_free_jcr(JCR *jcr)
661 Dmsg0(200, "Start dird free_jcr\n");
663 if (jcr->file_bsock) {
664 Dmsg0(200, "Close File bsock\n");
665 bnet_close(jcr->file_bsock);
667 if (jcr->store_bsock) {
668 Dmsg0(200, "Close Store bsock\n");
669 bnet_close(jcr->store_bsock);
671 if (jcr->RestoreBootstrap) {
672 free(jcr->RestoreBootstrap);
674 Dmsg0(200, "End dird free_jcr\n");
678 * We got a File Attributes record on the tape. Now, lookup the Job
679 * record, and then create the attributes record.
681 static int create_file_attributes_record(B_DB *db, JCR *mjcr,
682 char *fname, char *lname, int type,
683 char *ap, DEV_RECORD *rec)
688 ar.ClientId = mjcr->ClientId;
689 ar.JobId = mjcr->JobId;
690 ar.Stream = rec->Stream;
691 ar.FileIndex = rec->FileIndex;
693 if (mjcr->VolFirstIndex == 0) {
694 mjcr->VolFirstIndex = rec->FileIndex;
696 mjcr->FileIndex = rec->FileIndex;
703 if (!db_create_file_attributes_record(bjcr, db, &ar)) {
704 Pmsg1(0, _("Could not create File Attributes record. ERR=%s\n"), db_strerror(db));
707 mjcr->FileId = ar.FileId;
710 Pmsg1(000, _("Created File record: %s\n"), fname);
716 * For each Volume we see, we create a Medium record
718 static int create_media_record(B_DB *db, MEDIA_DBR *mr, VOLUME_LABEL *vl)
723 /* We mark Vols as Archive to keep them from being re-written */
724 bstrncpy(mr->VolStatus, "Archive", sizeof(mr->VolStatus));
725 mr->VolRetention = 365 * 3600 * 24; /* 1 year */
726 if (vl->VerNum >= 11) {
727 mr->FirstWritten = btime_to_utime(vl->write_btime);
728 mr->LabelDate = btime_to_utime(vl->label_btime);
730 /* DEPRECATED DO NOT USE */
731 dt.julian_day_number = vl->write_date;
732 dt.julian_day_fraction = vl->write_time;
734 mr->FirstWritten = mktime(&tm);
735 dt.julian_day_number = vl->label_date;
736 dt.julian_day_fraction = vl->label_time;
738 mr->LabelDate = mktime(&tm);
740 lasttime = mr->LabelDate;
746 if (!db_create_media_record(bjcr, db, mr)) {
747 Pmsg1(0, _("Could not create media record. ERR=%s\n"), db_strerror(db));
750 if (!db_update_media_record(bjcr, db, mr)) {
751 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
755 Pmsg1(000, _("Created Media record for Volume: %s\n"), mr->VolumeName);
762 * Called at end of media to update it
764 static int update_media_record(B_DB *db, MEDIA_DBR *mr)
766 if (!update_db && !update_vol_info) {
770 mr->LastWritten = lasttime;
771 if (!db_update_media_record(bjcr, db, mr)) {
772 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
776 Pmsg1(000, _("Updated Media record at end of Volume: %s\n"), mr->VolumeName);
783 static int create_pool_record(B_DB *db, POOL_DBR *pr)
787 pr->VolRetention = 355 * 3600 * 24; /* 1 year */
792 if (!db_create_pool_record(bjcr, db, pr)) {
793 Pmsg1(0, _("Could not create pool record. ERR=%s\n"), db_strerror(db));
797 Pmsg1(000, _("Created Pool record for Pool: %s\n"), pr->Name);
805 * Called from SOS to create a client for the current Job
807 static int create_client_record(B_DB *db, CLIENT_DBR *cr)
812 if (!db_create_client_record(bjcr, db, cr)) {
813 Pmsg1(0, _("Could not create Client record. ERR=%s\n"), db_strerror(db));
817 Pmsg1(000, _("Created Client record for Client: %s\n"), cr->Name);
822 static int create_fileset_record(B_DB *db, FILESET_DBR *fsr)
828 if (fsr->MD5[0] == 0) {
829 fsr->MD5[0] = ' '; /* Equivalent to nothing */
832 if (db_get_fileset_record(bjcr, db, fsr)) {
834 Pmsg1(000, _("Fileset \"%s\" already exists.\n"), fsr->FileSet);
837 if (!db_create_fileset_record(bjcr, db, fsr)) {
838 Pmsg2(0, _("Could not create FileSet record \"%s\". ERR=%s\n"),
839 fsr->FileSet, db_strerror(db));
843 Pmsg1(000, _("Created FileSet record \"%s\"\n"), fsr->FileSet);
850 * Simulate the two calls on the database to create
851 * the Job record and to update it when the Job actually
854 static JCR *create_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *label,
861 jr->JobId = label->JobId;
862 jr->Type = label->JobType;
863 jr->Level = label->JobLevel;
864 jr->JobStatus = JS_Created;
865 bstrncpy(jr->Name, label->JobName, sizeof(jr->Name));
866 bstrncpy(jr->Job, label->Job, sizeof(jr->Job));
867 if (label->VerNum >= 11) {
868 jr->SchedTime = btime_to_unix(label->write_btime);
870 dt.julian_day_number = label->write_date;
871 dt.julian_day_fraction = label->write_time;
873 jr->SchedTime = mktime(&tm);
876 jr->StartTime = jr->SchedTime;
877 jr->JobTDate = (utime_t)jr->SchedTime;
878 jr->VolSessionId = rec->VolSessionId;
879 jr->VolSessionTime = rec->VolSessionTime;
881 /* Now create a JCR as if starting the Job */
882 mjcr = create_jcr(jr, rec, label->JobId);
888 /* This creates the bare essentials */
889 if (!db_create_job_record(bjcr, db, jr)) {
890 Pmsg1(0, _("Could not create JobId record. ERR=%s\n"), db_strerror(db));
894 /* This adds the client, StartTime, JobTDate, ... */
895 if (!db_update_job_start_record(bjcr, db, jr)) {
896 Pmsg1(0, _("Could not update job start record. ERR=%s\n"), db_strerror(db));
899 Pmsg2(000, _("Created new JobId=%u record for original JobId=%u\n"), jr->JobId,
901 mjcr->JobId = jr->JobId; /* set new JobId */
906 * Simulate the database call that updates the Job
907 * at Job termination time.
909 static int update_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *elabel,
916 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
918 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
919 rec->VolSessionId, rec->VolSessionTime);
922 if (elabel->VerNum >= 11) {
923 jr->EndTime = btime_to_unix(elabel->write_btime);
925 dt.julian_day_number = elabel->write_date;
926 dt.julian_day_fraction = elabel->write_time;
928 jr->EndTime = mktime(&tm);
930 lasttime = jr->EndTime;
931 mjcr->end_time = jr->EndTime;
933 jr->JobId = mjcr->JobId;
934 jr->JobStatus = elabel->JobStatus;
935 mjcr->JobStatus = elabel->JobStatus;
936 jr->JobFiles = elabel->JobFiles;
937 jr->JobBytes = elabel->JobBytes;
938 jr->VolSessionId = rec->VolSessionId;
939 jr->VolSessionTime = rec->VolSessionTime;
940 jr->JobTDate = (utime_t)mjcr->start_time;
941 jr->ClientId = mjcr->ClientId;
948 if (!db_update_job_end_record(bjcr, db, jr)) {
949 Pmsg2(0, _("Could not update JobId=%u record. ERR=%s\n"), jr->JobId, db_strerror(db));
954 Pmsg1(000, _("Updated Job termination record for new JobId=%u\n"), jr->JobId);
958 static char term_code[70];
959 char sdt[50], edt[50];
960 char ec1[30], ec2[30], ec3[30];
962 switch (mjcr->JobStatus) {
964 term_msg = _("Backup OK");
967 case JS_ErrorTerminated:
968 term_msg = _("*** Backup Error ***");
971 term_msg = _("Backup Canceled");
974 term_msg = term_code;
975 sprintf(term_code, _("Job Termination code: %d"), mjcr->JobStatus);
978 bstrftime(sdt, sizeof(sdt), mjcr->start_time);
979 bstrftime(edt, sizeof(edt), mjcr->end_time);
990 Volume Session Id: %d\n\
991 Volume Session Time: %d\n\
992 Last Volume Bytes: %s\n\
993 Termination: %s\n\n"),
998 job_level_to_str(mjcr->JobLevel),
1002 edit_uint64_with_commas(mjcr->JobFiles, ec1),
1003 edit_uint64_with_commas(mjcr->JobBytes, ec2),
1005 mjcr->VolSessionTime,
1006 edit_uint64_with_commas(mr.VolBytes, ec3),
1013 static int create_jobmedia_record(B_DB *db, JCR *mjcr)
1017 if (dev->state & ST_TAPE) {
1018 mjcr->EndBlock = dev->EndBlock;
1019 mjcr->EndFile = dev->EndFile;
1021 mjcr->EndBlock = (uint32_t)dev->file_addr;
1022 mjcr->EndFile = (uint32_t)(dev->file_addr >> 32);
1025 memset(&jmr, 0, sizeof(jmr));
1026 jmr.JobId = mjcr->JobId;
1027 jmr.MediaId = mr.MediaId;
1028 jmr.FirstIndex = mjcr->VolFirstIndex;
1029 jmr.LastIndex = mjcr->FileIndex;
1030 jmr.StartFile = mjcr->StartFile;
1031 jmr.EndFile = mjcr->EndFile;
1032 jmr.StartBlock = mjcr->StartBlock;
1033 jmr.EndBlock = mjcr->EndBlock;
1040 if (!db_create_jobmedia_record(bjcr, db, &jmr)) {
1041 Pmsg1(0, _("Could not create JobMedia record. ERR=%s\n"), db_strerror(db));
1045 Pmsg2(000, _("Created JobMedia record JobId %d, MediaId %d\n"),
1046 jmr.JobId, jmr.MediaId);
1052 * Simulate the database call that updates the MD5/SHA1 record
1054 static int update_SIG_record(B_DB *db, char *SIGbuf, DEV_RECORD *rec, int type)
1058 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
1060 if (mr.VolJobs > 0) {
1061 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for MD5/SHA1 record.\n"),
1062 rec->VolSessionId, rec->VolSessionTime);
1074 if (!db_add_SIG_to_file_record(bjcr, db, mjcr->FileId, SIGbuf, type)) {
1075 Pmsg1(0, _("Could not add MD5/SHA1 to File record. ERR=%s\n"), db_strerror(db));
1080 Pmsg0(000, _("Updated MD5/SHA1 record\n"));
1088 * Create a JCR as if we are really starting the job
1090 static JCR *create_jcr(JOB_DBR *jr, DEV_RECORD *rec, uint32_t JobId)
1094 * Transfer as much as possible to the Job JCR. Most important is
1095 * the JobId and the ClientId.
1097 jobjcr = new_jcr(sizeof(JCR), dird_free_jcr);
1098 jobjcr->JobType = jr->Type;
1099 jobjcr->JobLevel = jr->Level;
1100 jobjcr->JobStatus = jr->JobStatus;
1101 bstrncpy(jobjcr->Job, jr->Job, sizeof(jobjcr->Job));
1102 jobjcr->JobId = JobId; /* this is JobId on tape */
1103 jobjcr->sched_time = jr->SchedTime;
1104 jobjcr->start_time = jr->StartTime;
1105 jobjcr->VolSessionId = rec->VolSessionId;
1106 jobjcr->VolSessionTime = rec->VolSessionTime;
1107 jobjcr->ClientId = jr->ClientId;
1108 attach_jcr_to_device(dev, jobjcr);
1112 /* Dummies to replace askdir.c */
1113 int dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing) { return 1;}
1114 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
1115 int dir_update_volume_info(JCR *jcr, DEVICE *dev, int relabel) { return 1; }
1116 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
1117 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
1118 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
1119 int dir_send_job_status(JCR *jcr) {return 1;}
1122 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
1124 fprintf(stderr, _("Mount Volume %s on device %s and press return when ready: "),
1125 jcr->VolumeName, dev_name(dev));