]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Fix #7137 About checking for Storage being used for job restart/resume
authorMichal Rakowski <michal.rakowski@baculasystems.com>
Mon, 21 Dec 2020 16:35:16 +0000 (17:35 +0100)
committerEric Bollengier <eric@baculasystems.com>
Thu, 24 Mar 2022 08:02:59 +0000 (09:02 +0100)
bacula/src/dird/protos.h
bacula/src/dird/ua_run.c

index 33b61013a9208746fdc6e83bcfc6cf30e50c4559..35536dd455b284b414057112305297ff6969db9c 100644 (file)
@@ -356,6 +356,7 @@ void purge_files_from_job_list(UAContext *ua, del_ctx &del);
 
 
 /* ua_run.c */
+bool check_storage_presence(UAContext *ua, JobId_t JobId);
 extern int run_cmd(UAContext *ua, const char *cmd);
 extern int restart_cmd(UAContext *ua, const char *cmd);
 extern bool check_pool(int32_t JobType, int32_t JobLevel, POOL *pool, POOL *nextpool, const char **name);
index 33e1b737066a08430b28f44b7e01d7ee4ab9a4a1..27fad7d596a38799aca55665db3231476b4c9928 100644 (file)
@@ -318,6 +318,25 @@ static JobId_t start_job(UAContext *ua, JCR *jcr, run_ctx &rc)
    return JobId;
 }
 
+/* Check if there is entry in Storage table with StorageId related to the job */
+static bool check_storage_db_presence(UAContext *ua, JobId_t JobId, char *name, int len)
+{
+   POOL_MEM query;
+   if (len < MAX_NAME_LENGTH) {
+      return false;
+   }
+
+   Mmsg(query, "SELECT Storage.Name FROM Storage JOIN Media USING (StorageId) "
+         "JOIN JobMedia USING (MediaId) JOIN Job USING (JobId) WHERE JobId=%ld ORDER BY JobMediaId DESC LIMIT 1",
+        JobId);
+   if (!db_sql_query(ua->db, query.c_str(), db_name_handler, name)) {
+      return false;
+   }
+
+
+   return true;
+}
+
 /*
  * If no job_name defined in the run context, ask
  *  the user for it.
@@ -522,6 +541,32 @@ static bool get_storage(UAContext *ua, run_ctx &rc)
    return true;
 }
 
+static bool get_storage_from_job_record(UAContext *ua, run_ctx &rc)
+{
+   if (rc.restart) {
+      char name[MAX_NAME_LENGTH];
+      if (check_storage_db_presence(ua, rc.JobId, name, sizeof(name))) {
+         /* Check if there is storage of name returned */
+         STORE *storage = (STORE *)GetResWithName(R_STORAGE, name);
+         if (storage) {
+            rc.store->store = storage;
+            pm_strcpy(rc.store->store_source, _("Job record"));
+            Dmsg1(50, "Found Storage resource related to the JobId=%ld\n",
+                  rc.JobId);
+            return true;
+         }
+         Dmsg1(50, "Could not find any Storage resource related to the one refered by JobId=%ld\n",
+               rc.JobId);
+
+      } else {
+         Dmsg1(50, "Could not find any Storage record in catalog related to the one refered by JobId=%ld\n",
+               rc.JobId);
+      }
+   }
+   get_job_storage(rc.store, rc.job, NULL);
+   return true;
+}
+
 /*
  * Get and pass back a list of Jobids in rc.jid
  */
@@ -641,7 +686,6 @@ static bool get_jobid_from_list(UAContext *ua, sellist &sl, run_ctx &rc)
    if (!get_pool(ua, rc)) {
       return false;
    }
-   get_job_storage(rc.store, rc.job, NULL);
 
    bmemset(&cr, 0, sizeof(cr));
    cr.ClientId = rc.jr.ClientId;
@@ -655,6 +699,8 @@ static bool get_jobid_from_list(UAContext *ua, sellist &sl, run_ctx &rc)
       return false;
    }
 
+   get_storage_from_job_record(ua, rc);
+
    bmemset(&fr, 0, sizeof(fr));
    fr.FileSetId = rc.jr.FileSetId;
    if (!db_get_fileset_record(ua->jcr, ua->db, &fr)) {
@@ -702,6 +748,7 @@ int restart_cmd(UAContext *ua, const char *cmd)
       return 0;
    }
 
+   rc.restart = true;
    rc.jr.JobStatus = 0;
 
    /* Users can set the jobid list in command line */