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 */
60 static DEVICE *dev = NULL;
62 static JCR *bjcr; /* jcr for bscan */
63 static BSR *bsr = NULL;
64 static struct stat statp;
66 static long record_file_index;
67 static POOLMEM *fname; /* original file name */
68 static POOLMEM *ofile; /* output name with prefix */
69 static POOLMEM *lname; /* link name */
74 static FILESET_DBR fsr;
77 static SESSION_LABEL label;
78 static SESSION_LABEL elabel;
80 static time_t lasttime = 0;
82 static char *db_name = "bacula";
83 static char *db_user = "bacula";
84 static char *db_password = "";
85 static char *wd = NULL;
86 static int update_db = 0;
87 static int update_vol_info = 0;
88 static int list_records = 0;
89 static int ignored_msgs = 0;
91 #define CONFIG_FILE "bacula-sd.conf"
99 "\nVersion: " VERSION " (" BDATE ")\n\n"
100 "Usage: bscan [-d debug_level] <bacula-archive>\n"
101 " -b bootstrap specify a bootstrap file\n"
102 " -c <file> specify configuration file\n"
103 " -dnn set debug level to nn\n"
104 " -m update media info in database\n"
105 " -n name specify the database name (default bacula)\n"
106 " -u user specify database user name (default bacula)\n"
107 " -p password specify database password (default none)\n"
109 " -s synchronize or store in database\n"
111 " -V specify Volume names (separated by |)\n"
112 " -w dir specify working directory (default from conf file)\n"
113 " -? print this message\n\n"));
117 int main (int argc, char *argv[])
120 struct stat stat_buf;
121 char *VolumeName = NULL;
123 my_name_is(argc, argv, "bscan");
124 init_msg(NULL, NULL);
127 while ((ch = getopt(argc, argv, "b:c:d:mn:p:rsu:vw:?")) != -1) {
130 bsr = parse_bsr(NULL, optarg);
133 case 'c': /* specify config file */
134 if (configfile != NULL) {
137 configfile = bstrdup(optarg);
140 case 'd': /* debug level */
141 debug_level = atoi(optarg);
142 if (debug_level <= 0)
159 db_password = optarg;
174 case 'V': /* Volume name */
192 Pmsg0(0, _("Wrong number of arguments: \n"));
196 if (configfile == NULL) {
197 configfile = bstrdup(CONFIG_FILE);
200 parse_config(configfile);
202 me = (STORES *)GetNextRes(R_STORAGE, NULL);
205 Emsg1(M_ERROR_TERM, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
209 /* Check if -w option given, otherwise use resource for working directory */
211 working_directory = wd;
212 } else if (!me->working_directory) {
213 Emsg1(M_ERROR_TERM, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
216 working_directory = me->working_directory;
219 /* Check that working directory is good */
220 if (stat(working_directory, &stat_buf) != 0) {
221 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s not found. Cannot continue.\n"),
224 if (!S_ISDIR(stat_buf.st_mode)) {
225 Emsg1(M_ERROR_TERM, 0, _("Working Directory: %s is not a directory. Cannot continue.\n"),
229 bjcr = setup_jcr("bscan", argv[0], bsr, VolumeName);
230 dev = setup_to_access_device(bjcr, 1); /* read device */
235 if ((db=db_init_database(NULL, db_name, db_user, db_password, NULL, 0, NULL)) == NULL) {
236 Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
238 if (!db_open_database(NULL, db)) {
239 Emsg0(M_ERROR_TERM, 0, db_strerror(db));
241 Dmsg0(200, "Database opened\n");
243 Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
253 static void do_scan()
255 fname = get_pool_memory(PM_FNAME);
256 ofile = get_pool_memory(PM_FNAME);
257 lname = get_pool_memory(PM_FNAME);
259 memset(&ar, 0, sizeof(ar));
260 memset(&pr, 0, sizeof(pr));
261 memset(&jr, 0, sizeof(jr));
262 memset(&cr, 0, sizeof(cr));
263 memset(&fsr, 0, sizeof(fsr));
264 memset(&fr, 0, sizeof(fr));
266 detach_jcr_from_device(dev, bjcr);
268 read_records(bjcr, dev, record_cb, mount_next_read_volume);
269 release_device(bjcr, dev);
271 free_pool_memory(fname);
272 free_pool_memory(ofile);
273 free_pool_memory(lname);
277 static void record_cb(JCR *bjcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
282 if (rec->data_len > 0) {
283 mr.VolBytes += rec->data_len + WRITE_RECHDR_LENGTH; /* Accumulate Volume bytes */
286 Pmsg5(000, _("Record: SessId=%u SessTim=%u FileIndex=%d Stream=%d len=%u\n"),
287 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex,
288 rec->Stream, rec->data_len);
291 * Check for Start or End of Session Record
294 if (rec->FileIndex < 0) {
295 int save_update_db = update_db;
298 dump_label_record(dev, rec, 1);
300 switch (rec->FileIndex) {
302 Pmsg0(000, _("Volume is prelabeled. This tape cannot be scanned.\n"));
306 unser_volume_label(dev, rec);
307 /* Check Pool info */
308 strcpy(pr.Name, dev->VolHdr.PoolName);
309 strcpy(pr.PoolType, dev->VolHdr.PoolType);
310 if (db_get_pool_record(bjcr, db, &pr)) {
312 Pmsg1(000, _("Pool record for %s found in DB.\n"), pr.Name);
316 Pmsg1(000, _("VOL_LABEL: Pool record not found for Pool: %s\n"),
319 create_pool_record(db, &pr);
321 if (strcmp(pr.PoolType, dev->VolHdr.PoolType) != 0) {
322 Pmsg2(000, _("VOL_LABEL: PoolType mismatch. DB=%s Vol=%s\n"),
323 pr.PoolType, dev->VolHdr.PoolType);
325 } else if (verbose) {
326 Pmsg1(000, _("Pool type \"%s\" is OK.\n"), pr.PoolType);
329 /* Check Media Info */
330 memset(&mr, 0, sizeof(mr));
331 strcpy(mr.VolumeName, dev->VolHdr.VolName);
332 mr.PoolId = pr.PoolId;
333 if (db_get_media_record(bjcr, db, &mr)) {
335 Pmsg1(000, _("Media record for %s found in DB.\n"), mr.VolumeName);
337 /* Clear out some volume statistics that will be updated */
338 mr.VolJobs = mr.VolFiles = mr.VolBlocks = 0;
339 mr.VolBytes = rec->data_len + 20;
342 Pmsg1(000, _("VOL_LABEL: Media record not found for Volume: %s\n"),
345 strcpy(mr.MediaType, dev->VolHdr.MediaType);
346 create_media_record(db, &mr, &dev->VolHdr);
348 if (strcmp(mr.MediaType, dev->VolHdr.MediaType) != 0) {
349 Pmsg2(000, _("VOL_LABEL: MediaType mismatch. DB=%s Vol=%s\n"),
350 mr.MediaType, dev->VolHdr.MediaType);
352 } else if (verbose) {
353 Pmsg1(000, _("Media type \"%s\" is OK.\n"), mr.MediaType);
355 /* Reset some JCR variables */
356 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
357 mjcr->VolFirstIndex = mjcr->FileIndex = 0;
358 mjcr->StartBlock = mjcr->EndBlock = 0;
359 mjcr->StartFile = mjcr->EndFile = 0;
362 Pmsg1(000, _("VOL_LABEL: OK for Volume: %s\n"), mr.VolumeName);
366 if (ignored_msgs > 0) {
367 Pmsg1(000, _("%d \"errors\" ignored before first Start of Session record.\n"),
371 unser_session_label(&label, rec);
372 memset(&jr, 0, sizeof(jr));
373 jr.JobId = label.JobId;
374 if (db_get_job_record(bjcr, db, &jr)) {
375 /* Job record already exists in DB */
376 update_db = 0; /* don't change db in create_job_record */
378 Pmsg1(000, _("SOS_LABEL: Found Job record for JobId: %d\n"), jr.JobId);
381 /* Must create a Job record in DB */
383 Pmsg1(000, _("SOS_LABEL: Job record not found for JobId: %d\n"),
387 /* Create Client record if not already there */
388 strcpy(cr.Name, label.ClientName);
389 create_client_record(db, &cr);
390 jr.ClientId = cr.ClientId;
392 /* process label, if Job record exists don't update db */
393 mjcr = create_job_record(db, &jr, &label, rec);
394 update_db = save_update_db;
396 jr.PoolId = pr.PoolId;
397 /* Set start positions into JCR */
398 if (dev->state & ST_TAPE) {
399 mjcr->StartBlock = dev->block_num;
400 mjcr->StartFile = dev->file;
402 mjcr->StartBlock = (uint32_t)dev->file_addr;
403 mjcr->StartFile = (uint32_t)(dev->file_addr >> 32);
405 mjcr->start_time = jr.StartTime;
406 mjcr->JobLevel = jr.Level;
408 mjcr->client_name = get_pool_memory(PM_FNAME);
409 pm_strcpy(&mjcr->client_name, label.ClientName);
410 mjcr->pool_type = get_pool_memory(PM_FNAME);
411 pm_strcpy(&mjcr->pool_type, label.PoolType);
412 mjcr->fileset_name = get_pool_memory(PM_FNAME);
413 pm_strcpy(&mjcr->fileset_name, label.FileSetName);
414 mjcr->pool_name = get_pool_memory(PM_FNAME);
415 pm_strcpy(&mjcr->pool_name, label.PoolName);
417 if (rec->VolSessionId != jr.VolSessionId) {
418 Pmsg3(000, _("SOS_LABEL: VolSessId mismatch for JobId=%u. DB=%d Vol=%d\n"),
420 jr.VolSessionId, rec->VolSessionId);
423 if (rec->VolSessionTime != jr.VolSessionTime) {
424 Pmsg3(000, _("SOS_LABEL: VolSessTime mismatch for JobId=%u. DB=%d Vol=%d\n"),
426 jr.VolSessionTime, rec->VolSessionTime);
429 if (jr.PoolId != pr.PoolId) {
430 Pmsg3(000, _("SOS_LABEL: PoolId mismatch for JobId=%u. DB=%d Vol=%d\n"),
432 jr.PoolId, pr.PoolId);
437 unser_session_label(&elabel, rec);
439 /* Create FileSet record */
440 strcpy(fsr.FileSet, label.FileSetName);
441 strcpy(fsr.MD5, label.FileSetMD5);
442 create_fileset_record(db, &fsr);
443 jr.FileSetId = fsr.FileSetId;
445 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
447 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
448 rec->VolSessionId, rec->VolSessionTime);
452 /* Do the final update to the Job record */
453 update_job_record(db, &jr, &elabel, rec);
455 mjcr->end_time = jr.EndTime;
456 mjcr->JobStatus = JS_Terminated;
458 /* Create JobMedia record */
459 create_jobmedia_record(db, mjcr);
460 detach_jcr_from_device(dev, mjcr);
466 case EOT_LABEL: /* end of all tapes */
468 * Wiffle through all jobs still open and close
472 for (mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
473 jr.JobId = mjcr->JobId;
474 jr.JobStatus = JS_ErrorTerminated;
475 jr.JobFiles = mjcr->JobFiles;
476 jr.JobBytes = mjcr->JobBytes;
477 jr.VolSessionId = mjcr->VolSessionId;
478 jr.VolSessionTime = mjcr->VolSessionTime;
479 jr.JobTDate = (utime_t)mjcr->start_time;
480 jr.ClientId = mjcr->ClientId;
482 if (!db_update_job_end_record(bjcr, db, &jr)) {
483 Pmsg1(0, _("Could not update job record. ERR=%s\n"), db_strerror(db));
487 mr.VolFiles = rec->File;
488 mr.VolBlocks = rec->Block;
489 mr.VolBytes += mr.VolBlocks * WRITE_BLKHDR_LENGTH; /* approx. */
491 update_media_record(db, &mr);
492 Pmsg3(0, _("End of Volume. VolFiles=%u VolBlocks=%u VolBytes=%s\n"), mr.VolFiles,
493 mr.VolBlocks, edit_uint64_with_commas(mr.VolBytes, ec1));
502 /* File Attributes stream */
503 if (rec->Stream == STREAM_UNIX_ATTRIBUTES || rec->Stream == STREAM_WIN32_ATTRIBUTES) {
506 if (sizeof_pool_memory(fname) < rec->data_len) {
507 fname = realloc_pool_memory(fname, rec->data_len + 1);
509 if (sizeof_pool_memory(lname) < rec->data_len) {
510 lname = realloc_pool_memory(lname, rec->data_len + 1);
516 * An Attributes record consists of:
521 * Link name (if file linked i.e. FT_LNK)
524 sscanf(rec->data, "%ld %d", &record_file_index, &type);
525 if (record_file_index != rec->FileIndex)
526 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
527 rec->FileIndex, record_file_index);
529 while (*ap++ != ' ') /* skip record file index */
531 while (*ap++ != ' ') /* skip type */
533 /* Save filename and position to attributes */
538 *fp = *ap++; /* terminate filename & point to attribs */
540 /* Skip through attributes to link name */
545 strcat(lname, lp); /* "save" link name */
548 decode_stat(ap, &statp, &LinkFI);
549 print_ls_output(fname, lname, type, &statp);
551 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
553 if (mr.VolJobs > 0) {
554 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Attributes record.\n"),
555 rec->VolSessionId, rec->VolSessionTime);
561 fr.JobId = mjcr->JobId;
563 if (db_get_file_attributes_record(bjcr, db, fname, &fr)) {
565 Pmsg1(000, _("File record already exists for: %s\n"), fname);
568 create_file_attributes_record(db, mjcr, fname, lname, type, ap, rec);
573 } else if (rec->Stream == STREAM_FILE_DATA) {
574 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
576 if (mr.VolJobs > 0) {
577 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for File Data record.\n"),
578 rec->VolSessionId, rec->VolSessionTime);
584 mjcr->JobBytes += rec->data_len;
585 free_jcr(mjcr); /* done using JCR */
587 } else if (rec->Stream == STREAM_SPARSE_DATA) {
588 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
590 if (mr.VolJobs > 0) {
591 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Sparse Data record.\n"),
592 rec->VolSessionId, rec->VolSessionTime);
598 mjcr->JobBytes += rec->data_len - sizeof(uint64_t);
599 free_jcr(mjcr); /* done using JCR */
601 } else if (rec->Stream == STREAM_GZIP_DATA) {
602 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
604 if (mr.VolJobs > 0) {
605 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for GZIP Data record.\n"),
606 rec->VolSessionId, rec->VolSessionTime);
612 mjcr->JobBytes += rec->data_len; /* No correct, we should expand it */
613 free_jcr(mjcr); /* done using JCR */
615 } else if (rec->Stream == STREAM_SPARSE_GZIP_DATA) {
616 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
618 if (mr.VolJobs > 0) {
619 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Sparse GZIP Data record.\n"),
620 rec->VolSessionId, rec->VolSessionTime);
626 mjcr->JobBytes += rec->data_len - sizeof(uint64_t); /* No correct, we should expand it */
627 free_jcr(mjcr); /* done using JCR */
629 /* Win32 Data stream */
630 } else if (rec->Stream == STREAM_WIN32_DATA) {
631 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
633 if (mr.VolJobs > 0) {
634 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Win32 Data record.\n"),
635 rec->VolSessionId, rec->VolSessionTime);
641 mjcr->JobBytes += rec->data_len;
642 free_jcr(mjcr); /* done using JCR */
644 /* Win32 GZIP stream */
645 } else if (rec->Stream == STREAM_WIN32_GZIP_DATA) {
646 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
648 if (mr.VolJobs > 0) {
649 Pmsg2(000, _("Could not find Job SessId=%d SessTime=%d for Win32 GZIP Data record.\n"),
650 rec->VolSessionId, rec->VolSessionTime);
656 mjcr->JobBytes += rec->data_len;
657 free_jcr(mjcr); /* done using JCR */
659 } else if (rec->Stream == 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);
667 } else if (rec->Stream == STREAM_SHA1_SIGNATURE) {
669 bin_to_base64(SIGbuf, (char *)rec->data, 20); /* encode 20 bytes */
671 Pmsg1(000, _("Got SHA1 record: %s\n"), SIGbuf);
673 update_SIG_record(db, SIGbuf, rec, SHA1_SIG);
676 } else if (rec->Stream == STREAM_PROGRAM_NAMES) {
678 Pmsg1(000, _("Got Prog Names Stream: %s\n"), rec->data);
680 } else if (rec->Stream == STREAM_PROGRAM_DATA) {
682 Pmsg0(000, _("Got Prog Data Stream record.\n"));
685 Pmsg2(0, _("Unknown stream type!!! stream=%d data=%s\n"), rec->Stream, rec->data);
691 * Free the Job Control Record if no one is still using it.
692 * Called from main free_jcr() routine in src/lib/jcr.c so
693 * that we can do our Director specific cleanup of the jcr.
695 static void dird_free_jcr(JCR *jcr)
697 Dmsg0(200, "Start dird free_jcr\n");
699 if (jcr->file_bsock) {
700 Dmsg0(200, "Close File bsock\n");
701 bnet_close(jcr->file_bsock);
703 if (jcr->store_bsock) {
704 Dmsg0(200, "Close Store bsock\n");
705 bnet_close(jcr->store_bsock);
707 if (jcr->RestoreBootstrap) {
708 free(jcr->RestoreBootstrap);
710 Dmsg0(200, "End dird free_jcr\n");
714 * We got a File Attributes record on the tape. Now, lookup the Job
715 * record, and then create the attributes record.
717 static int create_file_attributes_record(B_DB *db, JCR *mjcr,
718 char *fname, char *lname, int type,
719 char *ap, DEV_RECORD *rec)
724 ar.ClientId = mjcr->ClientId;
725 ar.JobId = mjcr->JobId;
726 ar.Stream = rec->Stream;
727 ar.FileIndex = rec->FileIndex;
729 if (mjcr->VolFirstIndex == 0) {
730 mjcr->VolFirstIndex = rec->FileIndex;
732 mjcr->FileIndex = rec->FileIndex;
739 if (!db_create_file_attributes_record(bjcr, db, &ar)) {
740 Pmsg1(0, _("Could not create File Attributes record. ERR=%s\n"), db_strerror(db));
743 mjcr->FileId = ar.FileId;
746 Pmsg1(000, _("Created File record: %s\n"), fname);
752 * For each Volume we see, we create a Medium record
754 static int create_media_record(B_DB *db, MEDIA_DBR *mr, VOLUME_LABEL *vl)
759 strcpy(mr->VolStatus, "Full");
760 mr->VolRetention = 365 * 3600 * 24; /* 1 year */
761 if (vl->VerNum >= 11) {
762 mr->FirstWritten = btime_to_utime(vl->write_btime);
763 mr->LabelDate = btime_to_utime(vl->label_btime);
765 /* DEPRECATED DO NOT USE */
766 dt.julian_day_number = vl->write_date;
767 dt.julian_day_fraction = vl->write_time;
769 mr->FirstWritten = mktime(&tm);
770 dt.julian_day_number = vl->label_date;
771 dt.julian_day_fraction = vl->label_time;
773 mr->LabelDate = mktime(&tm);
775 lasttime = mr->LabelDate;
781 if (!db_create_media_record(bjcr, db, mr)) {
782 Pmsg1(0, _("Could not create media record. ERR=%s\n"), db_strerror(db));
785 if (!db_update_media_record(bjcr, db, mr)) {
786 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
790 Pmsg1(000, _("Created Media record for Volume: %s\n"), mr->VolumeName);
797 * Called at end of media to update it
799 static int update_media_record(B_DB *db, MEDIA_DBR *mr)
801 if (!update_db && !update_vol_info) {
805 mr->LastWritten = lasttime;
806 if (!db_update_media_record(bjcr, db, mr)) {
807 Pmsg1(0, _("Could not update media record. ERR=%s\n"), db_strerror(db));
811 Pmsg1(000, _("Updated Media record at end of Volume: %s\n"), mr->VolumeName);
818 static int create_pool_record(B_DB *db, POOL_DBR *pr)
822 pr->VolRetention = 355 * 3600 * 24; /* 1 year */
827 if (!db_create_pool_record(bjcr, db, pr)) {
828 Pmsg1(0, _("Could not create pool record. ERR=%s\n"), db_strerror(db));
832 Pmsg1(000, _("Created Pool record for Pool: %s\n"), pr->Name);
840 * Called from SOS to create a client for the current Job
842 static int create_client_record(B_DB *db, CLIENT_DBR *cr)
847 if (!db_create_client_record(bjcr, db, cr)) {
848 Pmsg1(0, _("Could not create Client record. ERR=%s\n"), db_strerror(db));
852 Pmsg1(000, _("Created Client record for Client: %s\n"), cr->Name);
857 static int create_fileset_record(B_DB *db, FILESET_DBR *fsr)
863 if (fsr->MD5[0] == 0) {
864 fsr->MD5[0] = ' '; /* Equivalent to nothing */
867 if (db_get_fileset_record(bjcr, db, fsr)) {
869 Pmsg1(000, _("Fileset \"%s\" already exists.\n"), fsr->FileSet);
872 if (!db_create_fileset_record(bjcr, db, fsr)) {
873 Pmsg2(0, _("Could not create FileSet record \"%s\". ERR=%s\n"),
874 fsr->FileSet, db_strerror(db));
878 Pmsg1(000, _("Created FileSet record \"%s\"\n"), fsr->FileSet);
885 * Simulate the two calls on the database to create
886 * the Job record and to update it when the Job actually
889 static JCR *create_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *label,
896 jr->JobId = label->JobId;
897 jr->Type = label->JobType;
898 jr->Level = label->JobLevel;
899 jr->JobStatus = JS_Created;
900 strcpy(jr->Name, label->JobName);
901 strcpy(jr->Job, label->Job);
902 if (label->VerNum >= 11) {
903 jr->SchedTime = btime_to_unix(label->write_btime);
905 dt.julian_day_number = label->write_date;
906 dt.julian_day_fraction = label->write_time;
908 jr->SchedTime = mktime(&tm);
911 jr->StartTime = jr->SchedTime;
912 jr->JobTDate = (utime_t)jr->SchedTime;
913 jr->VolSessionId = rec->VolSessionId;
914 jr->VolSessionTime = rec->VolSessionTime;
916 /* Now create a JCR as if starting the Job */
917 mjcr = create_jcr(jr, rec, label->JobId);
923 /* This creates the bare essentials */
924 if (!db_create_job_record(bjcr, db, jr)) {
925 Pmsg1(0, _("Could not create JobId record. ERR=%s\n"), db_strerror(db));
929 /* This adds the client, StartTime, JobTDate, ... */
930 if (!db_update_job_start_record(bjcr, db, jr)) {
931 Pmsg1(0, _("Could not update job start record. ERR=%s\n"), db_strerror(db));
934 Pmsg2(000, _("Created new JobId=%u record for original JobId=%u\n"), jr->JobId,
936 mjcr->JobId = jr->JobId; /* set new JobId */
941 * Simulate the database call that updates the Job
942 * at Job termination time.
944 static int update_job_record(B_DB *db, JOB_DBR *jr, SESSION_LABEL *elabel,
951 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
953 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for EOS record.\n"),
954 rec->VolSessionId, rec->VolSessionTime);
957 if (elabel->VerNum >= 11) {
958 jr->EndTime = btime_to_unix(elabel->write_btime);
960 dt.julian_day_number = elabel->write_date;
961 dt.julian_day_fraction = elabel->write_time;
963 jr->EndTime = mktime(&tm);
965 lasttime = jr->EndTime;
966 mjcr->end_time = jr->EndTime;
968 jr->JobId = mjcr->JobId;
969 jr->JobStatus = elabel->JobStatus;
970 mjcr->JobStatus = elabel->JobStatus;
971 jr->JobFiles = elabel->JobFiles;
972 jr->JobBytes = elabel->JobBytes;
973 jr->VolSessionId = rec->VolSessionId;
974 jr->VolSessionTime = rec->VolSessionTime;
975 jr->JobTDate = (utime_t)mjcr->start_time;
976 jr->ClientId = mjcr->ClientId;
983 if (!db_update_job_end_record(bjcr, db, jr)) {
984 Pmsg2(0, _("Could not update JobId=%u record. ERR=%s\n"), jr->JobId, db_strerror(db));
989 Pmsg1(000, _("Updated Job termination record for new JobId=%u\n"), jr->JobId);
993 static char term_code[70];
994 char sdt[50], edt[50];
995 char ec1[30], ec2[30], ec3[30];
997 switch (mjcr->JobStatus) {
999 term_msg = _("Backup OK");
1002 case JS_ErrorTerminated:
1003 term_msg = _("*** Backup Error ***");
1006 term_msg = _("Backup Canceled");
1009 term_msg = term_code;
1010 sprintf(term_code, _("Job Termination code: %d"), mjcr->JobStatus);
1013 bstrftime(sdt, sizeof(sdt), mjcr->start_time);
1014 bstrftime(edt, sizeof(edt), mjcr->end_time);
1015 Pmsg14(000, _("%s\n\
1023 Files Written: %s\n\
1024 Bytes Written: %s\n\
1025 Volume Session Id: %d\n\
1026 Volume Session Time: %d\n\
1027 Last Volume Bytes: %s\n\
1028 Termination: %s\n\n"),
1033 job_level_to_str(mjcr->JobLevel),
1037 edit_uint64_with_commas(mjcr->JobFiles, ec1),
1038 edit_uint64_with_commas(mjcr->JobBytes, ec2),
1040 mjcr->VolSessionTime,
1041 edit_uint64_with_commas(mr.VolBytes, ec3),
1048 static int create_jobmedia_record(B_DB *db, JCR *mjcr)
1052 if (dev->state & ST_TAPE) {
1053 mjcr->EndBlock = dev->EndBlock;
1054 mjcr->EndFile = dev->EndFile;
1056 mjcr->EndBlock = (uint32_t)dev->file_addr;
1057 mjcr->EndFile = (uint32_t)(dev->file_addr >> 32);
1060 memset(&jmr, 0, sizeof(jmr));
1061 jmr.JobId = mjcr->JobId;
1062 jmr.MediaId = mr.MediaId;
1063 jmr.FirstIndex = mjcr->VolFirstIndex;
1064 jmr.LastIndex = mjcr->FileIndex;
1065 jmr.StartFile = mjcr->StartFile;
1066 jmr.EndFile = mjcr->EndFile;
1067 jmr.StartBlock = mjcr->StartBlock;
1068 jmr.EndBlock = mjcr->EndBlock;
1075 if (!db_create_jobmedia_record(bjcr, db, &jmr)) {
1076 Pmsg1(0, _("Could not create JobMedia record. ERR=%s\n"), db_strerror(db));
1080 Pmsg2(000, _("Created JobMedia record JobId %d, MediaId %d\n"),
1081 jmr.JobId, jmr.MediaId);
1087 * Simulate the database call that updates the MD5/SHA1 record
1089 static int update_SIG_record(B_DB *db, char *SIGbuf, DEV_RECORD *rec, int type)
1093 mjcr = get_jcr_by_session(rec->VolSessionId, rec->VolSessionTime);
1095 if (mr.VolJobs > 0) {
1096 Pmsg2(000, _("Could not find SessId=%d SessTime=%d for MD5/SHA1 record.\n"),
1097 rec->VolSessionId, rec->VolSessionTime);
1109 if (!db_add_SIG_to_file_record(bjcr, db, mjcr->FileId, SIGbuf, type)) {
1110 Pmsg1(0, _("Could not add MD5/SHA1 to File record. ERR=%s\n"), db_strerror(db));
1115 Pmsg0(000, _("Updated MD5/SHA1 record\n"));
1123 * Create a JCR as if we are really starting the job
1125 static JCR *create_jcr(JOB_DBR *jr, DEV_RECORD *rec, uint32_t JobId)
1129 * Transfer as much as possible to the Job JCR. Most important is
1130 * the JobId and the ClientId.
1132 jobjcr = new_jcr(sizeof(JCR), dird_free_jcr);
1133 jobjcr->JobType = jr->Type;
1134 jobjcr->JobLevel = jr->Level;
1135 jobjcr->JobStatus = jr->JobStatus;
1136 strcpy(jobjcr->Job, jr->Job);
1137 jobjcr->JobId = JobId; /* this is JobId on tape */
1138 jobjcr->sched_time = jr->SchedTime;
1139 jobjcr->start_time = jr->StartTime;
1140 jobjcr->VolSessionId = rec->VolSessionId;
1141 jobjcr->VolSessionTime = rec->VolSessionTime;
1142 jobjcr->ClientId = jr->ClientId;
1143 attach_jcr_to_device(dev, jobjcr);
1147 /* Dummies to replace askdir.c */
1148 int dir_get_volume_info(JCR *jcr, int writing) { return 1;}
1149 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
1150 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
1151 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
1152 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
1153 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
1154 int dir_send_job_status(JCR *jcr) {return 1;}
1157 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
1160 * We are at the end of reading a tape. Now, we simulate handling
1161 * the end of writing a tape by wiffling through the attached
1162 * jcrs creating jobmedia records.
1164 Dmsg1(100, "Walk attached jcrs. Volume=%s\n", dev->VolCatInfo.VolCatName);
1165 for (JCR *mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
1167 Pmsg1(000, _("Create JobMedia for Job %s\n"), mjcr->Job);
1169 if (dev->state & ST_TAPE) {
1170 mjcr->EndBlock = dev->EndBlock;
1171 mjcr->EndFile = dev->EndFile;
1173 mjcr->EndBlock = (uint32_t)dev->file_addr;
1174 mjcr->StartBlock = (uint32_t)(dev->file_addr >> 32);
1176 if (!create_jobmedia_record(db, mjcr)) {
1177 Pmsg2(000, _("Could not create JobMedia record for Volume=%s Job=%s\n"),
1178 dev->VolCatInfo.VolCatName, mjcr->Job);
1182 fprintf(stderr, _("Mount Volume %s on device %s and press return when ready: "),
1183 jcr->VolumeName, dev_name(dev));