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 */
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 *wd = NULL;
87 static int update_db = 0;
88 static int update_vol_info = 0;
89 static int list_records = 0;
90 static int ignored_msgs = 0;
92 #define CONFIG_FILE "bacula-sd.conf"
100 "\nVersion: " VERSION " (" BDATE ")\n\n"
101 "Usage: bscan [-d debug_level] <bacula-archive>\n"
102 " -b bootstrap specify a bootstrap file\n"
103 " -c <file> specify configuration file\n"
104 " -dnn set debug level to nn\n"
105 " -m update media info in database\n"
106 " -n name specify the database name (default bacula)\n"
107 " -u user specify database user name (default bacula)\n"
108 " -p password specify database password (default none)\n"
110 " -s synchronize or store in database\n"
112 " -V specify Volume names (separated by |)\n"
113 " -w dir specify working directory (default from conf file)\n"
114 " -? print this message\n\n"));
118 int main (int argc, char *argv[])
121 struct stat stat_buf;
122 char *VolumeName = NULL;
124 my_name_is(argc, argv, "bscan");
125 init_msg(NULL, NULL);
128 while ((ch = getopt(argc, argv, "b:c:d:mn:p:rsu:vVw:?")) != -1) {
131 bsr = parse_bsr(NULL, optarg);
134 case 'c': /* specify config file */
135 if (configfile != NULL) {
138 configfile = bstrdup(optarg);
141 case 'd': /* debug level */
142 debug_level = atoi(optarg);
143 if (debug_level <= 0)
160 db_password = optarg;
175 case 'V': /* Volume name */
193 Pmsg0(0, _("Wrong number of arguments: \n"));
197 if (configfile == NULL) {
198 configfile = bstrdup(CONFIG_FILE);
201 parse_config(configfile);
203 me = (STORES *)GetNextRes(R_STORAGE, NULL);
206 Emsg1(M_ERROR_TERM, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
210 /* Check if -w option given, otherwise use resource for working directory */
212 working_directory = wd;
213 } else if (!me->working_directory) {
214 Emsg1(M_ERROR_TERM, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
217 working_directory = me->working_directory;
220 /* Check that working directory is good */
221 if (stat(working_directory, &stat_buf) != 0) {
222 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s not found. Cannot continue.\n"),
225 if (!S_ISDIR(stat_buf.st_mode)) {
226 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s is not a directory. Cannot continue.\n"),
230 bjcr = setup_jcr("bscan", argv[0], bsr, VolumeName);
231 dev = setup_to_access_device(bjcr, 1); /* read device */
236 if ((db=db_init_database(NULL, db_name, db_user, db_password, NULL, 0, NULL)) == NULL) {
237 Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
239 if (!db_open_database(NULL, db)) {
240 Emsg0(M_ERROR_TERM, 0, db_strerror(db));
242 Dmsg0(200, "Database opened\n");
244 Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
254 * We are at the end of reading a tape. Now, we simulate handling
255 * the end of writing a tape by wiffling through the attached
256 * jcrs creating jobmedia records.
258 static int bscan_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
260 Dmsg1(100, "Walk attached jcrs. Volume=%s\n", dev->VolCatInfo.VolCatName);
261 for (JCR *mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
263 Pmsg1(000, _("Create JobMedia for Job %s\n"), mjcr->Job);
265 if (dev->state & ST_TAPE) {
266 mjcr->EndBlock = dev->EndBlock;
267 mjcr->EndFile = dev->EndFile;
269 mjcr->EndBlock = (uint32_t)dev->file_addr;
270 mjcr->StartBlock = (uint32_t)(dev->file_addr >> 32);
272 if (!create_jobmedia_record(db, mjcr)) {
273 Pmsg2(000, _("Could not create JobMedia record for Volume=%s Job=%s\n"),
274 dev->VolCatInfo.VolCatName, mjcr->Job);
277 /* Now let common read routine get up next tape. Note,
278 * we call mount_next... with bscan's jcr because that is where we
279 * have the Volume list, but we get attached.
281 int stat = mount_next_read_volume(jcr, dev, block);
282 /* we must once more detach ourselves (attached by mount_next ...) */
283 detach_jcr_from_device(dev, jcr); /* detach bscan jcr */
287 static void do_scan()
291 memset(&ar, 0, sizeof(ar));
292 memset(&pr, 0, sizeof(pr));
293 memset(&jr, 0, sizeof(jr));
294 memset(&cr, 0, sizeof(cr));
295 memset(&fsr, 0, sizeof(fsr));
296 memset(&fr, 0, sizeof(fr));
298 /* Detach bscan's jcr as we are not a real Job on the tape */
299 detach_jcr_from_device(dev, bjcr);
301 read_records(bjcr, dev, record_cb, bscan_mount_next_read_volume);
302 release_device(bjcr, dev);
308 static void record_cb(JCR *bjcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
313 if (rec->data_len > 0) {
314 mr.VolBytes += rec->data_len + WRITE_RECHDR_LENGTH; /* Accumulate Volume bytes */
317 Pmsg5(000, _("Record: SessId=%u SessTim=%u FileIndex=%d Stream=%d len=%u\n"),
318 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex,
319 rec->Stream, rec->data_len);
322 * Check for Start or End of Session Record
325 if (rec->FileIndex < 0) {
326 int save_update_db = update_db;
329 dump_label_record(dev, rec, 1);
331 switch (rec->FileIndex) {
333 Pmsg0(000, _("Volume is prelabeled. This tape cannot be scanned.\n"));
338 unser_volume_label(dev, rec);
339 /* Check Pool info */
340 strcpy(pr.Name, dev->VolHdr.PoolName);
341 strcpy(pr.PoolType, dev->VolHdr.PoolType);
342 if (db_get_pool_record(bjcr, db, &pr)) {
344 Pmsg1(000, _("Pool record for %s found in DB.\n"), pr.Name);
348 Pmsg1(000, _("VOL_LABEL: Pool record not found for Pool: %s\n"),
351 create_pool_record(db, &pr);
353 if (strcmp(pr.PoolType, dev->VolHdr.PoolType) != 0) {
354 Pmsg2(000, _("VOL_LABEL: PoolType mismatch. DB=%s Vol=%s\n"),
355 pr.PoolType, dev->VolHdr.PoolType);
357 } else if (verbose) {
358 Pmsg1(000, _("Pool type \"%s\" is OK.\n"), pr.PoolType);
361 /* Check Media Info */
362 memset(&mr, 0, sizeof(mr));
363 strcpy(mr.VolumeName, dev->VolHdr.VolName);
364 mr.PoolId = pr.PoolId;
365 if (db_get_media_record(bjcr, db, &mr)) {
367 Pmsg1(000, _("Media record for %s found in DB.\n"), mr.VolumeName);
369 /* Clear out some volume statistics that will be updated */
370 mr.VolJobs = mr.VolFiles = mr.VolBlocks = 0;
371 mr.VolBytes = rec->data_len + 20;
374 Pmsg1(000, _("VOL_LABEL: Media record not found for Volume: %s\n"),
377 strcpy(mr.MediaType, dev->VolHdr.MediaType);
378 create_media_record(db, &mr, &dev->VolHdr);
380 if (strcmp(mr.MediaType, dev->VolHdr.MediaType) != 0) {
381 Pmsg2(000, _("VOL_LABEL: MediaType mismatch. DB=%s Vol=%s\n"),
382 mr.MediaType, dev->VolHdr.MediaType);
384 } else if (verbose) {
385 Pmsg1(000, _("Media type \"%s\" is OK.\n"), mr.MediaType);
387 /* Reset some JCR variables */
388 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
389 mjcr->VolFirstIndex = mjcr->FileIndex = 0;
390 mjcr->StartBlock = mjcr->EndBlock = 0;
391 mjcr->StartFile = mjcr->EndFile = 0;
394 Pmsg1(000, _("VOL_LABEL: OK for Volume: %s\n"), mr.VolumeName);
399 if (ignored_msgs > 0) {
400 Pmsg1(000, _("%d \"errors\" ignored before first Start of Session record.\n"),
404 unser_session_label(&label, rec);
405 memset(&jr, 0, sizeof(jr));
406 jr.JobId = label.JobId;
407 if (db_get_job_record(bjcr, db, &jr)) {
408 /* Job record already exists in DB */
409 update_db = 0; /* don't change db in create_job_record */
411 Pmsg1(000, _("SOS_LABEL: Found Job record for JobId: %d\n"), jr.JobId);
414 /* Must create a Job record in DB */
416 Pmsg1(000, _("SOS_LABEL: Job record not found for JobId: %d\n"),
420 /* Create Client record if not already there */
421 strcpy(cr.Name, label.ClientName);
422 create_client_record(db, &cr);
423 jr.ClientId = cr.ClientId;
425 /* process label, if Job record exists don't update db */
426 mjcr = create_job_record(db, &jr, &label, rec);
427 update_db = save_update_db;
429 jr.PoolId = pr.PoolId;
430 /* Set start positions into JCR */
431 if (dev->state & ST_TAPE) {
432 mjcr->StartBlock = dev->block_num;
433 mjcr->StartFile = dev->file;
435 mjcr->StartBlock = (uint32_t)dev->file_addr;
436 mjcr->StartFile = (uint32_t)(dev->file_addr >> 32);
438 mjcr->start_time = jr.StartTime;
439 mjcr->JobLevel = jr.Level;
441 mjcr->client_name = get_pool_memory(PM_FNAME);
442 pm_strcpy(&mjcr->client_name, label.ClientName);
443 mjcr->pool_type = get_pool_memory(PM_FNAME);
444 pm_strcpy(&mjcr->pool_type, label.PoolType);
445 mjcr->fileset_name = get_pool_memory(PM_FNAME);
446 pm_strcpy(&mjcr->fileset_name, label.FileSetName);
447 mjcr->pool_name = get_pool_memory(PM_FNAME);
448 pm_strcpy(&mjcr->pool_name, label.PoolName);
450 if (rec->VolSessionId != jr.VolSessionId) {
451 Pmsg3(000, _("SOS_LABEL: VolSessId mismatch for JobId=%u. DB=%d Vol=%d\n"),
453 jr.VolSessionId, rec->VolSessionId);
456 if (rec->VolSessionTime != jr.VolSessionTime) {
457 Pmsg3(000, _("SOS_LABEL: VolSessTime mismatch for JobId=%u. DB=%d Vol=%d\n"),
459 jr.VolSessionTime, rec->VolSessionTime);
462 if (jr.PoolId != pr.PoolId) {
463 Pmsg3(000, _("SOS_LABEL: PoolId mismatch for JobId=%u. DB=%d Vol=%d\n"),
465 jr.PoolId, pr.PoolId);
471 unser_session_label(&elabel, rec);
473 /* Create FileSet record */
474 strcpy(fsr.FileSet, label.FileSetName);
475 strcpy(fsr.MD5, label.FileSetMD5);
476 create_fileset_record(db, &fsr);
477 jr.FileSetId = fsr.FileSetId;
479 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
481 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
482 rec->VolSessionId, rec->VolSessionTime);
486 /* Do the final update to the Job record */
487 update_job_record(db, &jr, &elabel, rec);
489 mjcr->end_time = jr.EndTime;
490 mjcr->JobStatus = JS_Terminated;
492 /* Create JobMedia record */
493 create_jobmedia_record(db, mjcr);
494 detach_jcr_from_device(dev, mjcr);
502 case EOT_LABEL: /* end of all tapes */
504 * Wiffle through all jobs still open and close
508 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
509 jr.JobId = mjcr->JobId;
510 jr.JobStatus = JS_ErrorTerminated;
511 jr.JobFiles = mjcr->JobFiles;
512 jr.JobBytes = mjcr->JobBytes;
513 jr.VolSessionId = mjcr->VolSessionId;
514 jr.VolSessionTime = mjcr->VolSessionTime;
515 jr.JobTDate = (utime_t)mjcr->start_time;
516 jr.ClientId = mjcr->ClientId;
518 if (!db_update_job_end_record(bjcr, db, &jr)) {
519 Pmsg1(0, _("Could not update job record. ERR=%s\n"), db_strerror(db));
523 mr.VolFiles = rec->File;
524 mr.VolBlocks = rec->Block;
525 mr.VolBytes += mr.VolBlocks * WRITE_BLKHDR_LENGTH; /* approx. */
527 update_media_record(db, &mr);
528 Pmsg3(0, _("End of all Volumes. VolFiles=%u VolBlocks=%u VolBytes=%s\n"), mr.VolFiles,
529 mr.VolBlocks, edit_uint64_with_commas(mr.VolBytes, ec1));
538 /* File Attributes stream */
539 switch (rec->Stream) {
540 case STREAM_UNIX_ATTRIBUTES:
541 case STREAM_UNIX_ATTRIBUTES_EX:
543 if (!unpack_attributes_record(bjcr, rec->Stream, rec->data, attr)) {
544 Emsg0(M_ERROR_TERM, 0, _("Cannot continue.\n"));
547 if (attr->file_index != rec->FileIndex) {
548 Emsg2(M_ERROR_TERM, 0, _("Record header file index %ld not equal record index %ld\n"),
549 rec->FileIndex, attr->file_index);
554 decode_stat(attr->attr, &attr->statp, &LinkFI);
555 build_attr_output_fnames(bjcr, attr);
556 print_ls_output(bjcr, attr);
558 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
560 if (mr.VolJobs > 0) {
561 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Attributes record.\n"),
562 rec->VolSessionId, rec->VolSessionTime);
568 fr.JobId = mjcr->JobId;
570 if (db_get_file_attributes_record(bjcr, db, attr->fname, &fr)) {
572 Pmsg1(000, _("File record already exists for: %s\n"), attr->fname);
575 create_file_attributes_record(db, mjcr, attr->fname, attr->lname,
576 attr->type, attr->attr, rec);
582 case STREAM_WIN32_DATA:
583 case STREAM_FILE_DATA:
584 case STREAM_SPARSE_DATA:
585 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
587 if (mr.VolJobs > 0) {
588 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for File Data record.\n"),
589 rec->VolSessionId, rec->VolSessionTime);
595 mjcr->JobBytes += rec->data_len;
596 if (rec->Stream == STREAM_SPARSE_DATA) {
597 mjcr->JobBytes -= sizeof(uint64_t);
600 free_jcr(mjcr); /* done using JCR */
603 case STREAM_GZIP_DATA:
604 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
606 if (mr.VolJobs > 0) {
607 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for GZIP Data record.\n"),
608 rec->VolSessionId, rec->VolSessionTime);
614 mjcr->JobBytes += rec->data_len; /* No correct, we should expand it */
615 free_jcr(mjcr); /* done using JCR */
618 case STREAM_SPARSE_GZIP_DATA:
619 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
621 if (mr.VolJobs > 0) {
622 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Sparse GZIP Data record.\n"),
623 rec->VolSessionId, rec->VolSessionTime);
629 mjcr->JobBytes += rec->data_len - sizeof(uint64_t); /* No correct, we should expand it */
630 free_jcr(mjcr); /* done using JCR */
633 /* Win32 GZIP stream */
634 case STREAM_WIN32_GZIP_DATA:
635 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
637 if (mr.VolJobs > 0) {
638 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Win32 GZIP Data record.\n"),
639 rec->VolSessionId, rec->VolSessionTime);
645 mjcr->JobBytes += rec->data_len;
646 free_jcr(mjcr); /* done using JCR */
649 case STREAM_MD5_SIGNATURE:
651 bin_to_base64(MD5buf, (char *)rec->data, 16); /* encode 16 bytes */
653 Pmsg1(000, _("Got MD5 record: %s\n"), MD5buf);
655 update_SIG_record(db, MD5buf, rec, MD5_SIG);
658 case STREAM_SHA1_SIGNATURE:
660 bin_to_base64(SIGbuf, (char *)rec->data, 20); /* encode 20 bytes */
662 Pmsg1(000, _("Got SHA1 record: %s\n"), SIGbuf);
664 update_SIG_record(db, SIGbuf, rec, SHA1_SIG);
668 case STREAM_PROGRAM_NAMES:
670 Pmsg1(000, _("Got Prog Names Stream: %s\n"), rec->data);
674 case STREAM_PROGRAM_DATA:
676 Pmsg0(000, _("Got Prog Data Stream record.\n"));
680 Pmsg2(0, _("Unknown stream type!!! stream=%d data=%s\n"), rec->Stream, rec->data);
687 * Free the Job Control Record if no one is still using it.
688 * Called from main free_jcr() routine in src/lib/jcr.c so
689 * that we can do our Director specific cleanup of the jcr.
691 static void dird_free_jcr(JCR *jcr)
693 Dmsg0(200, "Start dird free_jcr\n");
695 if (jcr->file_bsock) {
696 Dmsg0(200, "Close File bsock\n");
697 bnet_close(jcr->file_bsock);
699 if (jcr->store_bsock) {
700 Dmsg0(200, "Close Store bsock\n");
701 bnet_close(jcr->store_bsock);
703 if (jcr->RestoreBootstrap) {
704 free(jcr->RestoreBootstrap);
706 Dmsg0(200, "End dird free_jcr\n");
710 * We got a File Attributes record on the tape. Now, lookup the Job
711 * record, and then create the attributes record.
713 static int create_file_attributes_record(B_DB *db, JCR *mjcr,
714 char *fname, char *lname, int type,
715 char *ap, DEV_RECORD *rec)
720 ar.ClientId = mjcr->ClientId;
721 ar.JobId = mjcr->JobId;
722 ar.Stream = rec->Stream;
723 ar.FileIndex = rec->FileIndex;
725 if (mjcr->VolFirstIndex == 0) {
726 mjcr->VolFirstIndex = rec->FileIndex;
728 mjcr->FileIndex = rec->FileIndex;
735 if (!db_create_file_attributes_record(bjcr, db, &ar)) {
736 Pmsg1(0, _("Could not create File Attributes record. ERR=%s\n"), db_strerror(db));
739 mjcr->FileId = ar.FileId;
742 Pmsg1(000, _("Created File record: %s\n"), fname);
748 * For each Volume we see, we create a Medium record
750 static int create_media_record(B_DB *db, MEDIA_DBR *mr, VOLUME_LABEL *vl)
755 strcpy(mr->VolStatus, "Full");
756 mr->VolRetention = 365 * 3600 * 24; /* 1 year */
757 if (vl->VerNum >= 11) {
758 mr->FirstWritten = btime_to_utime(vl->write_btime);
759 mr->LabelDate = btime_to_utime(vl->label_btime);
761 /* DEPRECATED DO NOT USE */
762 dt.julian_day_number = vl->write_date;
763 dt.julian_day_fraction = vl->write_time;
765 mr->FirstWritten = mktime(&tm);
766 dt.julian_day_number = vl->label_date;
767 dt.julian_day_fraction = vl->label_time;
769 mr->LabelDate = mktime(&tm);
771 lasttime = mr->LabelDate;
777 if (!db_create_media_record(bjcr, db, mr)) {
778 Pmsg1(0, _("Could not create media record. ERR=%s\n"), db_strerror(db));
781 if (!db_update_media_record(bjcr, db, mr)) {
782 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
786 Pmsg1(000, _("Created Media record for Volume: %s\n"), mr->VolumeName);
793 * Called at end of media to update it
795 static int update_media_record(B_DB *db, MEDIA_DBR *mr)
797 if (!update_db && !update_vol_info) {
801 mr->LastWritten = lasttime;
802 if (!db_update_media_record(bjcr, db, mr)) {
803 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
807 Pmsg1(000, _("Updated Media record at end of Volume: %s\n"), mr->VolumeName);
814 static int create_pool_record(B_DB *db, POOL_DBR *pr)
818 pr->VolRetention = 355 * 3600 * 24; /* 1 year */
823 if (!db_create_pool_record(bjcr, db, pr)) {
824 Pmsg1(0, _("Could not create pool record. ERR=%s\n"), db_strerror(db));
828 Pmsg1(000, _("Created Pool record for Pool: %s\n"), pr->Name);
836 * Called from SOS to create a client for the current Job
838 static int create_client_record(B_DB *db, CLIENT_DBR *cr)
843 if (!db_create_client_record(bjcr, db, cr)) {
844 Pmsg1(0, _("Could not create Client record. ERR=%s\n"), db_strerror(db));
848 Pmsg1(000, _("Created Client record for Client: %s\n"), cr->Name);
853 static int create_fileset_record(B_DB *db, FILESET_DBR *fsr)
859 if (fsr->MD5[0] == 0) {
860 fsr->MD5[0] = ' '; /* Equivalent to nothing */
863 if (db_get_fileset_record(bjcr, db, fsr)) {
865 Pmsg1(000, _("Fileset \"%s\" already exists.\n"), fsr->FileSet);
868 if (!db_create_fileset_record(bjcr, db, fsr)) {
869 Pmsg2(0, _("Could not create FileSet record \"%s\". ERR=%s\n"),
870 fsr->FileSet, db_strerror(db));
874 Pmsg1(000, _("Created FileSet record \"%s\"\n"), fsr->FileSet);
881 * Simulate the two calls on the database to create
882 * the Job record and to update it when the Job actually
885 static JCR *create_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *label,
892 jr->JobId = label->JobId;
893 jr->Type = label->JobType;
894 jr->Level = label->JobLevel;
895 jr->JobStatus = JS_Created;
896 strcpy(jr->Name, label->JobName);
897 strcpy(jr->Job, label->Job);
898 if (label->VerNum >= 11) {
899 jr->SchedTime = btime_to_unix(label->write_btime);
901 dt.julian_day_number = label->write_date;
902 dt.julian_day_fraction = label->write_time;
904 jr->SchedTime = mktime(&tm);
907 jr->StartTime = jr->SchedTime;
908 jr->JobTDate = (utime_t)jr->SchedTime;
909 jr->VolSessionId = rec->VolSessionId;
910 jr->VolSessionTime = rec->VolSessionTime;
912 /* Now create a JCR as if starting the Job */
913 mjcr = create_jcr(jr, rec, label->JobId);
919 /* This creates the bare essentials */
920 if (!db_create_job_record(bjcr, db, jr)) {
921 Pmsg1(0, _("Could not create JobId record. ERR=%s\n"), db_strerror(db));
925 /* This adds the client, StartTime, JobTDate, ... */
926 if (!db_update_job_start_record(bjcr, db, jr)) {
927 Pmsg1(0, _("Could not update job start record. ERR=%s\n"), db_strerror(db));
930 Pmsg2(000, _("Created new JobId=%u record for original JobId=%u\n"), jr->JobId,
932 mjcr->JobId = jr->JobId; /* set new JobId */
937 * Simulate the database call that updates the Job
938 * at Job termination time.
940 static int update_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *elabel,
947 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
949 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
950 rec->VolSessionId, rec->VolSessionTime);
953 if (elabel->VerNum >= 11) {
954 jr->EndTime = btime_to_unix(elabel->write_btime);
956 dt.julian_day_number = elabel->write_date;
957 dt.julian_day_fraction = elabel->write_time;
959 jr->EndTime = mktime(&tm);
961 lasttime = jr->EndTime;
962 mjcr->end_time = jr->EndTime;
964 jr->JobId = mjcr->JobId;
965 jr->JobStatus = elabel->JobStatus;
966 mjcr->JobStatus = elabel->JobStatus;
967 jr->JobFiles = elabel->JobFiles;
968 jr->JobBytes = elabel->JobBytes;
969 jr->VolSessionId = rec->VolSessionId;
970 jr->VolSessionTime = rec->VolSessionTime;
971 jr->JobTDate = (utime_t)mjcr->start_time;
972 jr->ClientId = mjcr->ClientId;
979 if (!db_update_job_end_record(bjcr, db, jr)) {
980 Pmsg2(0, _("Could not update JobId=%u record. ERR=%s\n"), jr->JobId, db_strerror(db));
985 Pmsg1(000, _("Updated Job termination record for new JobId=%u\n"), jr->JobId);
989 static char term_code[70];
990 char sdt[50], edt[50];
991 char ec1[30], ec2[30], ec3[30];
993 switch (mjcr->JobStatus) {
995 term_msg = _("Backup OK");
998 case JS_ErrorTerminated:
999 term_msg = _("*** Backup Error ***");
1002 term_msg = _("Backup Canceled");
1005 term_msg = term_code;
1006 sprintf(term_code, _("Job Termination code: %d"), mjcr->JobStatus);
1009 bstrftime(sdt, sizeof(sdt), mjcr->start_time);
1010 bstrftime(edt, sizeof(edt), mjcr->end_time);
1011 Pmsg14(000, _("%s\n\
1019 Files Written: %s\n\
1020 Bytes Written: %s\n\
1021 Volume Session Id: %d\n\
1022 Volume Session Time: %d\n\
1023 Last Volume Bytes: %s\n\
1024 Termination: %s\n\n"),
1029 job_level_to_str(mjcr->JobLevel),
1033 edit_uint64_with_commas(mjcr->JobFiles, ec1),
1034 edit_uint64_with_commas(mjcr->JobBytes, ec2),
1036 mjcr->VolSessionTime,
1037 edit_uint64_with_commas(mr.VolBytes, ec3),
1044 static int create_jobmedia_record(B_DB *db, JCR *mjcr)
1048 if (dev->state & ST_TAPE) {
1049 mjcr->EndBlock = dev->EndBlock;
1050 mjcr->EndFile = dev->EndFile;
1052 mjcr->EndBlock = (uint32_t)dev->file_addr;
1053 mjcr->EndFile = (uint32_t)(dev->file_addr >> 32);
1056 memset(&jmr, 0, sizeof(jmr));
1057 jmr.JobId = mjcr->JobId;
1058 jmr.MediaId = mr.MediaId;
1059 jmr.FirstIndex = mjcr->VolFirstIndex;
1060 jmr.LastIndex = mjcr->FileIndex;
1061 jmr.StartFile = mjcr->StartFile;
1062 jmr.EndFile = mjcr->EndFile;
1063 jmr.StartBlock = mjcr->StartBlock;
1064 jmr.EndBlock = mjcr->EndBlock;
1071 if (!db_create_jobmedia_record(bjcr, db, &jmr)) {
1072 Pmsg1(0, _("Could not create JobMedia record. ERR=%s\n"), db_strerror(db));
1076 Pmsg2(000, _("Created JobMedia record JobId %d, MediaId %d\n"),
1077 jmr.JobId, jmr.MediaId);
1083 * Simulate the database call that updates the MD5/SHA1 record
1085 static int update_SIG_record(B_DB *db, char *SIGbuf, DEV_RECORD *rec, int type)
1089 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
1091 if (mr.VolJobs > 0) {
1092 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for MD5/SHA1 record.\n"),
1093 rec->VolSessionId, rec->VolSessionTime);
1105 if (!db_add_SIG_to_file_record(bjcr, db, mjcr->FileId, SIGbuf, type)) {
1106 Pmsg1(0, _("Could not add MD5/SHA1 to File record. ERR=%s\n"), db_strerror(db));
1111 Pmsg0(000, _("Updated MD5/SHA1 record\n"));
1119 * Create a JCR as if we are really starting the job
1121 static JCR *create_jcr(JOB_DBR *jr, DEV_RECORD *rec, uint32_t JobId)
1125 * Transfer as much as possible to the Job JCR. Most important is
1126 * the JobId and the ClientId.
1128 jobjcr = new_jcr(sizeof(JCR), dird_free_jcr);
1129 jobjcr->JobType = jr->Type;
1130 jobjcr->JobLevel = jr->Level;
1131 jobjcr->JobStatus = jr->JobStatus;
1132 strcpy(jobjcr->Job, jr->Job);
1133 jobjcr->JobId = JobId; /* this is JobId on tape */
1134 jobjcr->sched_time = jr->SchedTime;
1135 jobjcr->start_time = jr->StartTime;
1136 jobjcr->VolSessionId = rec->VolSessionId;
1137 jobjcr->VolSessionTime = rec->VolSessionTime;
1138 jobjcr->ClientId = jr->ClientId;
1139 attach_jcr_to_device(dev, jobjcr);
1143 /* Dummies to replace askdir.c */
1144 int dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing) { return 1;}
1145 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
1146 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
1147 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
1148 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
1149 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
1150 int dir_send_job_status(JCR *jcr) {return 1;}
1153 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
1155 fprintf(stderr, _("Mount Volume %s on device %s and press return when ready: "),
1156 jcr->VolumeName, dev_name(dev));