From 31289ca573be789d2291e694af3fc0d70f337066 Mon Sep 17 00:00:00 2001 From: Craig Miskell Date: Mon, 7 Mar 2011 17:28:48 +0100 Subject: [PATCH] Fix for duplicate jobmedia records bug #1666 --- bacula/src/jcr.h | 1 + bacula/src/stored/bscan.c | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/bacula/src/jcr.h b/bacula/src/jcr.h index e4fecfacab..0bdf0a389b 100644 --- a/bacula/src/jcr.h +++ b/bacula/src/jcr.h @@ -436,6 +436,7 @@ public: bool write_part_after_job; /* Set to write part after job */ bool PreferMountedVols; /* Prefer mounted vols rather than new */ bool Resched; /* Job may be rescheduled */ + bool bscan_insert_jobmedia_records; /*Bscan: needs to insert job media records */ /* Parmaters for Open Read Session */ BSR *bsr; /* Bootstrap record -- has everything */ diff --git a/bacula/src/stored/bscan.c b/bacula/src/stored/bscan.c index 0e11790807..dd373b9084 100644 --- a/bacula/src/stored/bscan.c +++ b/bacula/src/stored/bscan.c @@ -348,9 +348,11 @@ static bool bscan_mount_next_read_volume(DCR *dcr) mdcr->EndFile = dcr->EndFile; mdcr->VolMediaId = dcr->VolMediaId; mjcr->read_dcr->VolLastIndex = dcr->VolLastIndex; - if (!create_jobmedia_record(db, mjcr)) { - Pmsg2(000, _("Could not create JobMedia record for Volume=%s Job=%s\n"), - dev->getVolCatName(), mjcr->Job); + if( mjcr->bscan_insert_jobmedia_records ) { + if (!create_jobmedia_record(db, mjcr)) { + Pmsg2(000, _("Could not create JobMedia record for Volume=%s Job=%s\n"), + dev->getVolCatName(), mjcr->Job); + } } } @@ -405,6 +407,9 @@ static bool record_cb(DCR *dcr, DEV_RECORD *rec) DEVICE *dev = dcr->dev; JCR *bjcr = dcr->jcr; DEV_BLOCK *block = dcr->block; + POOL_MEM sql_buffer; + db_int64_ctx jmr_count; + char digest[BASE64_SIZE(CRYPTO_DIGEST_MAX_SIZE)]; if (rec->data_len > 0) { @@ -527,6 +532,7 @@ static bool record_cb(DCR *dcr, DEV_RECORD *rec) jr.JobId); } } + /* Create Client record if not already there */ bstrncpy(cr.Name, label.ClientName, sizeof(cr.Name)); create_client_record(db, &cr); @@ -548,6 +554,19 @@ static bool record_cb(DCR *dcr, DEV_RECORD *rec) bstrncpy(dcr->pool_type, label.PoolType, sizeof(dcr->pool_type)); bstrncpy(dcr->pool_name, label.PoolName, sizeof(dcr->pool_name)); + /* Look for existing Job Media records for this job. If there are + any, no new ones need be created. This may occur if File + Retention has expired before Job Retention, or if the volume + has already been bscan'd */ + Mmsg(sql_buffer, "SELECT count(*) from JobMedia where JobId=%d", jr.JobId); + db_sql_query(db, sql_buffer.c_str(), db_int64_handler, &jmr_count); + if( jmr_count.value > 0 ) { + //FIELD NAME TO BE DEFINED/CONFIRMED (maybe a struct?) + mjcr->bscan_insert_jobmedia_records = false; + } else { + mjcr->bscan_insert_jobmedia_records = true; + } + if (rec->VolSessionId != jr.VolSessionId) { Pmsg3(000, _("SOS_LABEL: VolSessId mismatch for JobId=%u. DB=%d Vol=%d\n"), jr.JobId, @@ -592,7 +611,9 @@ static bool record_cb(DCR *dcr, DEV_RECORD *rec) /* Create JobMedia record */ mjcr->read_dcr->VolLastIndex = dcr->VolLastIndex; - create_jobmedia_record(db, mjcr); + if( mjcr->bscan_insert_jobmedia_records ) { + create_jobmedia_record(db, mjcr); + } free_dcr(mjcr->read_dcr); free_jcr(mjcr); -- 2.39.5