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-2004 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 bool 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 bool 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 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
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 const char *db_name = "bacula";
84 static const char *db_user = "bacula";
85 static const char *db_password = "";
86 static const char *db_host = NULL;
87 static const char *wd = NULL;
88 static bool update_db = false;
89 static bool update_vol_info = false;
90 static bool list_records = false;
91 static int ignored_msgs = 0;
93 static int num_jobs = 0;
94 static int num_pools = 0;
95 static int num_media = 0;
96 static int num_files = 0;
98 #define CONFIG_FILE "bacula-sd.conf"
100 bool forge_on = false;
106 "\nVersion: " VERSION " (" BDATE ")\n\n"
107 "Usage: bscan [ options ] <bacula-archive>\n"
108 " -b bootstrap specify a bootstrap file\n"
109 " -c <file> specify configuration file\n"
110 " -d <nn> set debug level to nn\n"
111 " -m update media info in database\n"
112 " -n <name> specify the database name (default bacula)\n"
113 " -u <user> specify database user name (default bacula)\n"
114 " -P <password specify database password (default none)\n"
115 " -h <host> specify database host (default NULL)\n"
116 " -p proceed inspite of I/O errors\n"
118 " -s synchronize or store in database\n"
120 " -V <Volumes> specify Volume names (separated by |)\n"
121 " -w <dir> specify working directory (default from conf file)\n"
122 " -? print this message\n\n"));
126 int main (int argc, char *argv[])
129 struct stat stat_buf;
130 char *VolumeName = NULL;
132 my_name_is(argc, argv, "bscan");
133 init_msg(NULL, NULL);
136 while ((ch = getopt(argc, argv, "b:c:d:h:mn:pP:rsu:vV:w:?")) != -1) {
139 bsr = parse_bsr(NULL, optarg);
142 case 'c': /* specify config file */
143 if (configfile != NULL) {
146 configfile = bstrdup(optarg);
149 case 'd': /* debug level */
150 debug_level = atoi(optarg);
151 if (debug_level <= 0)
160 update_vol_info = true;
172 db_password = optarg;
191 case 'V': /* Volume name */
209 Pmsg0(0, _("Wrong number of arguments: \n"));
213 if (configfile == NULL) {
214 configfile = bstrdup(CONFIG_FILE);
217 parse_config(configfile);
219 me = (STORES *)GetNextRes(R_STORAGE, NULL);
222 Emsg1(M_ERROR_TERM, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
226 /* Check if -w option given, otherwise use resource for working directory */
228 working_directory = wd;
229 } else if (!me->working_directory) {
230 Emsg1(M_ERROR_TERM, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
233 working_directory = me->working_directory;
236 /* Check that working directory is good */
237 if (stat(working_directory, &stat_buf) != 0) {
238 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s not found. Cannot continue.\n"),
241 if (!S_ISDIR(stat_buf.st_mode)) {
242 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s is not a directory. Cannot continue.\n"),
246 bjcr = setup_jcr("bscan", argv[0], bsr, VolumeName);
247 dev = setup_to_access_device(bjcr, 1); /* read device */
252 if ((db=db_init_database(NULL, db_name, db_user, db_password, db_host, 0, NULL)) == NULL) {
253 Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
255 if (!db_open_database(NULL, db)) {
256 Emsg0(M_ERROR_TERM, 0, db_strerror(db));
258 Dmsg0(200, "Database opened\n");
260 Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
264 printf("Records %sadded to catalog:\n%7d Media\n%7d Pool\n%7d Job\n%7d File\n",
265 update_db?"":"would have been ",
266 num_media, num_pools, num_jobs, num_files);
273 * We are at the end of reading a tape. Now, we simulate handling
274 * the end of writing a tape by wiffling through the attached
275 * jcrs creating jobmedia records.
277 static bool bscan_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
279 Dmsg1(100, "Walk attached jcrs. Volume=%s\n", dev->VolCatInfo.VolCatName);
280 for (JCR *mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
281 DCR *dcr = mjcr->dcr;
283 Pmsg1(000, _("Create JobMedia for Job %s\n"), mjcr->Job);
285 if (dev->state & ST_TAPE) {
286 dcr->EndBlock = dev->EndBlock;
287 dcr->EndFile = dev->EndFile;
289 dcr->EndBlock = (uint32_t)dev->file_addr;
290 dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
292 if (!create_jobmedia_record(db, mjcr)) {
293 Pmsg2(000, _("Could not create JobMedia record for Volume=%s Job=%s\n"),
294 dev->VolCatInfo.VolCatName, mjcr->Job);
297 /* Now let common read routine get up next tape. Note,
298 * we call mount_next... with bscan's jcr because that is where we
299 * have the Volume list, but we get attached.
301 bool stat = mount_next_read_volume(jcr, dev, block);
302 /* we must once more detach ourselves (attached by mount_next ...) */
303 detach_jcr_from_device(dev, jcr); /* detach bscan jcr */
307 static void do_scan()
311 memset(&ar, 0, sizeof(ar));
312 memset(&pr, 0, sizeof(pr));
313 memset(&jr, 0, sizeof(jr));
314 memset(&cr, 0, sizeof(cr));
315 memset(&fsr, 0, sizeof(fsr));
316 memset(&fr, 0, sizeof(fr));
318 /* Detach bscan's jcr as we are not a real Job on the tape */
319 detach_jcr_from_device(dev, bjcr);
321 read_records(bjcr->dcr, record_cb, bscan_mount_next_read_volume);
322 release_device(bjcr);
329 * Returns: true if OK
332 static bool record_cb(JCR *bjcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
338 if (rec->data_len > 0) {
339 mr.VolBytes += rec->data_len + WRITE_RECHDR_LENGTH; /* Accumulate Volume bytes */
342 Pmsg5(000, _("Record: SessId=%u SessTim=%u FileIndex=%d Stream=%d len=%u\n"),
343 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex,
344 rec->Stream, rec->data_len);
347 * Check for Start or End of Session Record
350 if (rec->FileIndex < 0) {
351 bool save_update_db = update_db;
354 dump_label_record(dev, rec, 1);
356 switch (rec->FileIndex) {
358 Pmsg0(000, _("Volume is prelabeled. This tape cannot be scanned.\n"));
363 unser_volume_label(dev, rec);
364 /* Check Pool info */
365 bstrncpy(pr.Name, dev->VolHdr.PoolName, sizeof(pr.Name));
366 bstrncpy(pr.PoolType, dev->VolHdr.PoolType, sizeof(pr.PoolType));
368 if (db_get_pool_record(bjcr, db, &pr)) {
370 Pmsg1(000, _("Pool record for %s found in DB.\n"), pr.Name);
374 Pmsg1(000, _("VOL_LABEL: Pool record not found for Pool: %s\n"),
377 create_pool_record(db, &pr);
379 if (strcmp(pr.PoolType, dev->VolHdr.PoolType) != 0) {
380 Pmsg2(000, _("VOL_LABEL: PoolType mismatch. DB=%s Vol=%s\n"),
381 pr.PoolType, dev->VolHdr.PoolType);
383 } else if (verbose) {
384 Pmsg1(000, _("Pool type \"%s\" is OK.\n"), pr.PoolType);
387 /* Check Media Info */
388 memset(&mr, 0, sizeof(mr));
389 bstrncpy(mr.VolumeName, dev->VolHdr.VolName, sizeof(mr.VolumeName));
390 mr.PoolId = pr.PoolId;
392 if (db_get_media_record(bjcr, db, &mr)) {
394 Pmsg1(000, _("Media record for %s found in DB.\n"), mr.VolumeName);
396 /* Clear out some volume statistics that will be updated */
397 mr.VolJobs = mr.VolFiles = mr.VolBlocks = 0;
398 mr.VolBytes = rec->data_len + 20;
401 Pmsg1(000, _("VOL_LABEL: Media record not found for Volume: %s\n"),
404 bstrncpy(mr.MediaType, dev->VolHdr.MediaType, sizeof(mr.MediaType));
405 create_media_record(db, &mr, &dev->VolHdr);
407 if (strcmp(mr.MediaType, dev->VolHdr.MediaType) != 0) {
408 Pmsg2(000, _("VOL_LABEL: MediaType mismatch. DB=%s Vol=%s\n"),
409 mr.MediaType, dev->VolHdr.MediaType);
410 return true; /* ignore error */
411 } else if (verbose) {
412 Pmsg1(000, _("Media type \"%s\" is OK.\n"), mr.MediaType);
414 /* Reset some JCR variables */
415 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
417 dcr->VolFirstIndex = dcr->FileIndex = 0;
418 dcr->StartBlock = dcr->EndBlock = 0;
419 dcr->StartFile = dcr->EndFile = 0;
422 Pmsg1(000, _("VOL_LABEL: OK for Volume: %s\n"), mr.VolumeName);
428 if (ignored_msgs > 0) {
429 Pmsg1(000, _("%d \"errors\" ignored before first Start of Session record.\n"),
433 unser_session_label(&label, rec);
434 memset(&jr, 0, sizeof(jr));
435 jr.JobId = label.JobId;
436 if (db_get_job_record(bjcr, db, &jr)) {
437 /* Job record already exists in DB */
438 update_db = false; /* don't change db in create_job_record */
440 Pmsg1(000, _("SOS_LABEL: Found Job record for JobId: %d\n"), jr.JobId);
443 /* Must create a Job record in DB */
445 Pmsg1(000, _("SOS_LABEL: Job record not found for JobId: %d\n"),
449 /* Create Client record if not already there */
450 bstrncpy(cr.Name, label.ClientName, sizeof(cr.Name));
451 create_client_record(db, &cr);
452 jr.ClientId = cr.ClientId;
454 /* process label, if Job record exists don't update db */
455 mjcr = create_job_record(db, &jr, &label, rec);
457 update_db = save_update_db;
459 jr.PoolId = pr.PoolId;
460 /* Set start positions into JCR */
461 if (dev->state & ST_TAPE) {
463 * Note, we have already advanced past current block,
464 * so the correct number is block_num - 1
466 dcr->StartBlock = dev->block_num - 1;
467 dcr->StartFile = dev->file;
469 dcr->StartBlock = (uint32_t)dev->file_addr;
470 dcr->StartFile = (uint32_t)(dev->file_addr >> 32);
472 mjcr->start_time = jr.StartTime;
473 mjcr->JobLevel = jr.JobLevel;
475 mjcr->client_name = get_pool_memory(PM_FNAME);
476 pm_strcpy(&mjcr->client_name, label.ClientName);
477 mjcr->pool_type = get_pool_memory(PM_FNAME);
478 pm_strcpy(&mjcr->pool_type, label.PoolType);
479 mjcr->fileset_name = get_pool_memory(PM_FNAME);
480 pm_strcpy(&mjcr->fileset_name, label.FileSetName);
481 mjcr->pool_name = get_pool_memory(PM_FNAME);
482 pm_strcpy(&mjcr->pool_name, label.PoolName);
484 if (rec->VolSessionId != jr.VolSessionId) {
485 Pmsg3(000, _("SOS_LABEL: VolSessId mismatch for JobId=%u. DB=%d Vol=%d\n"),
487 jr.VolSessionId, rec->VolSessionId);
488 return true; /* ignore error */
490 if (rec->VolSessionTime != jr.VolSessionTime) {
491 Pmsg3(000, _("SOS_LABEL: VolSessTime mismatch for JobId=%u. DB=%d Vol=%d\n"),
493 jr.VolSessionTime, rec->VolSessionTime);
494 return true; /* ignore error */
496 if (jr.PoolId != pr.PoolId) {
497 Pmsg3(000, _("SOS_LABEL: PoolId mismatch for JobId=%u. DB=%d Vol=%d\n"),
499 jr.PoolId, pr.PoolId);
500 return true; /* ignore error */
505 unser_session_label(&elabel, rec);
507 /* Create FileSet record */
508 bstrncpy(fsr.FileSet, label.FileSetName, sizeof(fsr.FileSet));
509 bstrncpy(fsr.MD5, label.FileSetMD5, sizeof(fsr.MD5));
510 create_fileset_record(db, &fsr);
511 jr.FileSetId = fsr.FileSetId;
513 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
515 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
516 rec->VolSessionId, rec->VolSessionTime);
520 /* Do the final update to the Job record */
521 update_job_record(db, &jr, &elabel, rec);
523 mjcr->end_time = jr.EndTime;
524 mjcr->JobStatus = JS_Terminated;
526 /* Create JobMedia record */
527 create_jobmedia_record(db, mjcr);
528 detach_jcr_from_device(dev, mjcr);
536 case EOT_LABEL: /* end of all tapes */
538 * Wiffle through all jobs still open and close
542 mjcr=next_attached_jcr(dev, NULL);
545 jr.JobId = mjcr->JobId;
546 jr.JobStatus = JS_ErrorTerminated;
547 jr.JobFiles = mjcr->JobFiles;
548 jr.JobBytes = mjcr->JobBytes;
549 jr.VolSessionId = mjcr->VolSessionId;
550 jr.VolSessionTime = mjcr->VolSessionTime;
551 jr.JobTDate = (utime_t)mjcr->start_time;
552 jr.ClientId = mjcr->ClientId;
553 if (!db_update_job_end_record(bjcr, db, &jr)) {
554 Pmsg1(0, _("Could not update job record. ERR=%s\n"), db_strerror(db));
556 njcr = mjcr->next_dev;
561 mr.VolFiles = rec->File;
562 mr.VolBlocks = rec->Block;
563 mr.VolBytes += mr.VolBlocks * WRITE_BLKHDR_LENGTH; /* approx. */
565 update_media_record(db, &mr);
566 Pmsg3(0, _("End of all Volumes. VolFiles=%u VolBlocks=%u VolBytes=%s\n"), mr.VolFiles,
567 mr.VolBlocks, edit_uint64_with_commas(mr.VolBytes, ec1));
575 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
577 if (mr.VolJobs > 0) {
578 Pmsg2(000, _("Could not find Job for SessId=%d SessTime=%d record.\n"),
579 rec->VolSessionId, rec->VolSessionTime);
586 if (dcr->VolFirstIndex == 0) {
587 dcr->VolFirstIndex = block->FirstIndex;
590 /* File Attributes stream */
591 switch (rec->Stream) {
592 case STREAM_UNIX_ATTRIBUTES:
593 case STREAM_UNIX_ATTRIBUTES_EX:
595 if (!unpack_attributes_record(bjcr, rec->Stream, rec->data, attr)) {
596 Emsg0(M_ERROR_TERM, 0, _("Cannot continue.\n"));
599 if (attr->file_index != rec->FileIndex) {
600 Emsg2(M_ERROR_TERM, 0, _("Record header file index %ld not equal record index %ld\n"),
601 rec->FileIndex, attr->file_index);
605 decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
606 build_attr_output_fnames(bjcr, attr);
607 print_ls_output(bjcr, attr);
609 fr.JobId = mjcr->JobId;
612 if (verbose && (num_files & 0x7FFF) == 0) {
613 char ed1[30], ed2[30], ed3[30], ed4[30];
614 Pmsg4(000, _("%s file records. At file:blk=%s:%s bytes=%s\n"),
615 edit_uint64_with_commas(num_files, ed1),
616 edit_uint64_with_commas(rec->File, ed2),
617 edit_uint64_with_commas(rec->Block, ed3),
618 edit_uint64_with_commas(mr.VolBytes, ed4));
620 if (db_get_file_attributes_record(bjcr, db, attr->fname, NULL, &fr)) {
622 Pmsg1(000, _("File record already exists for: %s\n"), attr->fname);
625 create_file_attributes_record(db, mjcr, attr->fname, attr->lname,
626 attr->type, attr->attr, rec);
632 case STREAM_WIN32_DATA:
633 case STREAM_FILE_DATA:
634 case STREAM_SPARSE_DATA:
635 mjcr->JobBytes += rec->data_len;
636 if (rec->Stream == STREAM_SPARSE_DATA) {
637 mjcr->JobBytes -= sizeof(uint64_t);
640 free_jcr(mjcr); /* done using JCR */
643 case STREAM_GZIP_DATA:
644 mjcr->JobBytes += rec->data_len; /* No correct, we should expand it */
645 free_jcr(mjcr); /* done using JCR */
648 case STREAM_SPARSE_GZIP_DATA:
649 mjcr->JobBytes += rec->data_len - sizeof(uint64_t); /* No correct, we should expand it */
650 free_jcr(mjcr); /* done using JCR */
653 /* Win32 GZIP stream */
654 case STREAM_WIN32_GZIP_DATA:
655 mjcr->JobBytes += rec->data_len;
656 free_jcr(mjcr); /* done using JCR */
659 case STREAM_MD5_SIGNATURE:
661 bin_to_base64(MD5buf, (char *)rec->data, 16); /* encode 16 bytes */
663 Pmsg1(000, _("Got MD5 record: %s\n"), MD5buf);
665 update_SIG_record(db, MD5buf, rec, MD5_SIG);
668 case STREAM_SHA1_SIGNATURE:
670 bin_to_base64(SIGbuf, (char *)rec->data, 20); /* encode 20 bytes */
672 Pmsg1(000, _("Got SHA1 record: %s\n"), SIGbuf);
674 update_SIG_record(db, SIGbuf, rec, SHA1_SIG);
678 case STREAM_PROGRAM_NAMES:
680 Pmsg1(000, _("Got Prog Names Stream: %s\n"), rec->data);
684 case STREAM_PROGRAM_DATA:
686 Pmsg0(000, _("Got Prog Data Stream record.\n"));
690 Pmsg2(0, _("Unknown stream type!!! stream=%d data=%s\n"), rec->Stream, rec->data);
697 * Free the Job Control Record if no one is still using it.
698 * Called from main free_jcr() routine in src/lib/jcr.c so
699 * that we can do our Director specific cleanup of the jcr.
701 static void bscan_free_jcr(JCR *jcr)
703 Dmsg0(200, "Start dird free_jcr\n");
705 if (jcr->file_bsock) {
706 Dmsg0(200, "Close File bsock\n");
707 bnet_close(jcr->file_bsock);
709 if (jcr->store_bsock) {
710 Dmsg0(200, "Close Store bsock\n");
711 bnet_close(jcr->store_bsock);
713 if (jcr->RestoreBootstrap) {
714 free(jcr->RestoreBootstrap);
720 Dmsg0(200, "End dird free_jcr\n");
724 * We got a File Attributes record on the tape. Now, lookup the Job
725 * record, and then create the attributes record.
727 static int create_file_attributes_record(B_DB *db, JCR *mjcr,
728 char *fname, char *lname, int type,
729 char *ap, DEV_RECORD *rec)
731 DCR *dcr = mjcr->dcr;
734 ar.ClientId = mjcr->ClientId;
735 ar.JobId = mjcr->JobId;
736 ar.Stream = rec->Stream;
737 ar.FileIndex = rec->FileIndex;
739 if (dcr->VolFirstIndex == 0) {
740 dcr->VolFirstIndex = rec->FileIndex;
742 dcr->FileIndex = rec->FileIndex;
749 if (!db_create_file_attributes_record(bjcr, db, &ar)) {
750 Pmsg1(0, _("Could not create File Attributes record. ERR=%s\n"), db_strerror(db));
753 mjcr->FileId = ar.FileId;
756 Pmsg1(000, _("Created File record: %s\n"), fname);
762 * For each Volume we see, we create a Medium record
764 static int create_media_record(B_DB *db, MEDIA_DBR *mr, VOLUME_LABEL *vl)
769 /* We mark Vols as Archive to keep them from being re-written */
770 bstrncpy(mr->VolStatus, "Archive", sizeof(mr->VolStatus));
771 mr->VolRetention = 365 * 3600 * 24; /* 1 year */
772 if (vl->VerNum >= 11) {
773 mr->FirstWritten = btime_to_utime(vl->write_btime);
774 mr->LabelDate = btime_to_utime(vl->label_btime);
776 /* DEPRECATED DO NOT USE */
777 dt.julian_day_number = vl->write_date;
778 dt.julian_day_fraction = vl->write_time;
780 mr->FirstWritten = mktime(&tm);
781 dt.julian_day_number = vl->label_date;
782 dt.julian_day_fraction = vl->label_time;
784 mr->LabelDate = mktime(&tm);
786 lasttime = mr->LabelDate;
792 if (!db_create_media_record(bjcr, db, mr)) {
793 Pmsg1(0, _("Could not create media record. ERR=%s\n"), db_strerror(db));
796 if (!db_update_media_record(bjcr, db, mr)) {
797 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
801 Pmsg1(000, _("Created Media record for Volume: %s\n"), mr->VolumeName);
808 * Called at end of media to update it
810 static bool update_media_record(B_DB *db, MEDIA_DBR *mr)
812 if (!update_db && !update_vol_info) {
816 mr->LastWritten = lasttime;
817 if (!db_update_media_record(bjcr, db, mr)) {
818 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
822 Pmsg1(000, _("Updated Media record at end of Volume: %s\n"), mr->VolumeName);
829 static int create_pool_record(B_DB *db, POOL_DBR *pr)
833 pr->VolRetention = 355 * 3600 * 24; /* 1 year */
838 if (!db_create_pool_record(bjcr, db, pr)) {
839 Pmsg1(0, _("Could not create pool record. ERR=%s\n"), db_strerror(db));
843 Pmsg1(000, _("Created Pool record for Pool: %s\n"), pr->Name);
851 * Called from SOS to create a client for the current Job
853 static int create_client_record(B_DB *db, CLIENT_DBR *cr)
858 if (!db_create_client_record(bjcr, db, cr)) {
859 Pmsg1(0, _("Could not create Client record. ERR=%s\n"), db_strerror(db));
863 Pmsg1(000, _("Created Client record for Client: %s\n"), cr->Name);
868 static int create_fileset_record(B_DB *db, FILESET_DBR *fsr)
874 if (fsr->MD5[0] == 0) {
875 fsr->MD5[0] = ' '; /* Equivalent to nothing */
878 if (db_get_fileset_record(bjcr, db, fsr)) {
880 Pmsg1(000, _("Fileset \"%s\" already exists.\n"), fsr->FileSet);
883 if (!db_create_fileset_record(bjcr, db, fsr)) {
884 Pmsg2(0, _("Could not create FileSet record \"%s\". ERR=%s\n"),
885 fsr->FileSet, db_strerror(db));
889 Pmsg1(000, _("Created FileSet record \"%s\"\n"), fsr->FileSet);
896 * Simulate the two calls on the database to create
897 * the Job record and to update it when the Job actually
900 static JCR *create_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *label,
907 jr->JobId = label->JobId;
908 jr->JobType = label->JobType;
909 jr->JobLevel = label->JobLevel;
910 jr->JobStatus = JS_Created;
911 bstrncpy(jr->Name, label->JobName, sizeof(jr->Name));
912 bstrncpy(jr->Job, label->Job, sizeof(jr->Job));
913 if (label->VerNum >= 11) {
914 jr->SchedTime = btime_to_unix(label->write_btime);
916 dt.julian_day_number = label->write_date;
917 dt.julian_day_fraction = label->write_time;
919 jr->SchedTime = mktime(&tm);
922 jr->StartTime = jr->SchedTime;
923 jr->JobTDate = (utime_t)jr->SchedTime;
924 jr->VolSessionId = rec->VolSessionId;
925 jr->VolSessionTime = rec->VolSessionTime;
927 /* Now create a JCR as if starting the Job */
928 mjcr = create_jcr(jr, rec, label->JobId);
934 /* This creates the bare essentials */
935 if (!db_create_job_record(bjcr, db, jr)) {
936 Pmsg1(0, _("Could not create JobId record. ERR=%s\n"), db_strerror(db));
940 /* This adds the client, StartTime, JobTDate, ... */
941 if (!db_update_job_start_record(bjcr, db, jr)) {
942 Pmsg1(0, _("Could not update job start record. ERR=%s\n"), db_strerror(db));
945 Pmsg2(000, _("Created new JobId=%u record for original JobId=%u\n"), jr->JobId,
947 mjcr->JobId = jr->JobId; /* set new JobId */
952 * Simulate the database call that updates the Job
953 * at Job termination time.
955 static int update_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *elabel,
962 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
964 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
965 rec->VolSessionId, rec->VolSessionTime);
968 if (elabel->VerNum >= 11) {
969 jr->EndTime = btime_to_unix(elabel->write_btime);
971 dt.julian_day_number = elabel->write_date;
972 dt.julian_day_fraction = elabel->write_time;
974 jr->EndTime = mktime(&tm);
976 lasttime = jr->EndTime;
977 mjcr->end_time = jr->EndTime;
979 jr->JobId = mjcr->JobId;
980 jr->JobStatus = elabel->JobStatus;
981 mjcr->JobStatus = elabel->JobStatus;
982 jr->JobFiles = elabel->JobFiles;
983 jr->JobBytes = elabel->JobBytes;
984 jr->VolSessionId = rec->VolSessionId;
985 jr->VolSessionTime = rec->VolSessionTime;
986 jr->JobTDate = (utime_t)mjcr->start_time;
987 jr->ClientId = mjcr->ClientId;
994 if (!db_update_job_end_record(bjcr, db, jr)) {
995 Pmsg2(0, _("Could not update JobId=%u record. ERR=%s\n"), jr->JobId, db_strerror(db));
1000 Pmsg1(000, _("Updated Job termination record for new JobId=%u\n"), jr->JobId);
1003 const char *term_msg;
1004 static char term_code[70];
1005 char sdt[50], edt[50];
1006 char ec1[30], ec2[30], ec3[30];
1008 switch (mjcr->JobStatus) {
1010 term_msg = _("Backup OK");
1013 case JS_ErrorTerminated:
1014 term_msg = _("*** Backup Error ***");
1017 term_msg = _("Backup Canceled");
1020 term_msg = term_code;
1021 sprintf(term_code, _("Job Termination code: %d"), mjcr->JobStatus);
1024 bstrftime(sdt, sizeof(sdt), mjcr->start_time);
1025 bstrftime(edt, sizeof(edt), mjcr->end_time);
1026 Pmsg14(000, _("%s\n\
1034 Files Written: %s\n\
1035 Bytes Written: %s\n\
1036 Volume Session Id: %d\n\
1037 Volume Session Time: %d\n\
1038 Last Volume Bytes: %s\n\
1039 Termination: %s\n\n"),
1044 job_level_to_str(mjcr->JobLevel),
1048 edit_uint64_with_commas(mjcr->JobFiles, ec1),
1049 edit_uint64_with_commas(mjcr->JobBytes, ec2),
1051 mjcr->VolSessionTime,
1052 edit_uint64_with_commas(mr.VolBytes, ec3),
1059 static int create_jobmedia_record(B_DB *db, JCR *mjcr)
1062 DCR *dcr = mjcr->dcr;
1064 if (dev->state & ST_TAPE) {
1065 dcr->EndBlock = dev->EndBlock;
1066 dcr->EndFile = dev->EndFile;
1068 dcr->EndBlock = (uint32_t)dev->file_addr;
1069 dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
1072 memset(&jmr, 0, sizeof(jmr));
1073 jmr.JobId = mjcr->JobId;
1074 jmr.MediaId = mr.MediaId;
1075 jmr.FirstIndex = dcr->VolFirstIndex;
1076 jmr.LastIndex = dcr->FileIndex;
1077 jmr.StartFile = dcr->StartFile;
1078 jmr.EndFile = dcr->EndFile;
1079 jmr.StartBlock = dcr->StartBlock;
1080 jmr.EndBlock = dcr->EndBlock;
1087 if (!db_create_jobmedia_record(bjcr, db, &jmr)) {
1088 Pmsg1(0, _("Could not create JobMedia record. ERR=%s\n"), db_strerror(db));
1092 Pmsg2(000, _("Created JobMedia record JobId %d, MediaId %d\n"),
1093 jmr.JobId, jmr.MediaId);
1099 * Simulate the database call that updates the MD5/SHA1 record
1101 static int update_SIG_record(B_DB *db, char *SIGbuf, DEV_RECORD *rec, int type)
1105 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
1107 if (mr.VolJobs > 0) {
1108 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for MD5/SHA1 record.\n"),
1109 rec->VolSessionId, rec->VolSessionTime);
1121 if (!db_add_SIG_to_file_record(bjcr, db, mjcr->FileId, SIGbuf, type)) {
1122 Pmsg1(0, _("Could not add MD5/SHA1 to File record. ERR=%s\n"), db_strerror(db));
1127 Pmsg0(000, _("Updated MD5/SHA1 record\n"));
1135 * Create a JCR as if we are really starting the job
1137 static JCR *create_jcr(JOB_DBR *jr, DEV_RECORD *rec, uint32_t JobId)
1141 * Transfer as much as possible to the Job JCR. Most important is
1142 * the JobId and the ClientId.
1144 jobjcr = new_jcr(sizeof(JCR), bscan_free_jcr);
1145 jobjcr->JobType = jr->JobType;
1146 jobjcr->JobLevel = jr->JobLevel;
1147 jobjcr->JobStatus = jr->JobStatus;
1148 bstrncpy(jobjcr->Job, jr->Job, sizeof(jobjcr->Job));
1149 jobjcr->JobId = JobId; /* this is JobId on tape */
1150 jobjcr->sched_time = jr->SchedTime;
1151 jobjcr->start_time = jr->StartTime;
1152 jobjcr->VolSessionId = rec->VolSessionId;
1153 jobjcr->VolSessionTime = rec->VolSessionTime;
1154 jobjcr->ClientId = jr->ClientId;
1155 attach_jcr_to_device(dev, jobjcr);
1156 new_dcr(jobjcr, dev);
1160 /* Dummies to replace askdir.c */
1161 bool dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing) { return 1;}
1162 bool dir_find_next_appendable_volume(JCR *jcr) { return 1;}
1163 bool dir_update_volume_info(JCR *jcr, bool relabel) { return 1; }
1164 bool dir_create_jobmedia_record(JCR *jcr) { return 1; }
1165 bool dir_ask_sysop_to_create_appendable_volume(JCR *jcr) { return 1; }
1166 bool dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
1167 bool dir_send_job_status(JCR *jcr) {return 1;}
1170 bool dir_ask_sysop_to_mount_volume(JCR *jcr)
1172 DEVICE *dev = jcr->dcr->dev;
1173 fprintf(stderr, _("Mount Volume \"%s\" on device \"%s\" and press return when ready: "),
1174 jcr->VolumeName, dev_name(dev));