]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/job.c
Make migration work with new subroutine
[bacula/bacula] / bacula / src / dird / job.c
index dfd3b1e1f3632ddc73145eecdff05da14675d1f3..339fb408440e625d7e7a5707915fecbdcad288a7 100644 (file)
@@ -73,10 +73,24 @@ void term_job_server()
  */
 JobId_t run_job(JCR *jcr)
 {
-   int stat, errstat;
-   JobId_t JobId = 0;
+   int stat;
+   if (setup_job(jcr)) {
+      /* Queue the job to be run */
+      if ((stat = jobq_add(&job_queue, jcr)) != 0) {
+         berrno be;
+         Jmsg(jcr, M_FATAL, 0, _("Could not add job queue: ERR=%s\n"), be.strerror(stat));
+         return 0;
+      }
+      return jcr->JobId;
+   }
+   return 0;
+}            
+
+bool setup_job(JCR *jcr) 
+{
+   int errstat;
 
-   P(jcr->mutex);
+   jcr->lock();
    sm_check(__FILE__, __LINE__, true);
    init_msg(jcr, jcr->messages);
 
@@ -88,6 +102,9 @@ JobId_t run_job(JCR *jcr)
    }
    jcr->term_wait_inited = true;
 
+   create_unique_job_name(jcr, jcr->job->hdr.name);
+   set_jcr_job_status(jcr, JS_Created);
+   jcr->unlock();
 
    /*
     * Open database
@@ -110,14 +127,12 @@ JobId_t run_job(JCR *jcr)
    /*
     * Create Job record
     */
-   create_unique_job_name(jcr, jcr->job->hdr.name);
-   set_jcr_job_status(jcr, JS_Created);
    init_jcr_job_record(jcr);
    if (!db_create_job_record(jcr, jcr->db, &jcr->jr)) {
       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
       goto bail_out;
    }
-   JobId = jcr->JobId = jcr->jr.JobId;
+   jcr->JobId = jcr->jr.JobId;
    Dmsg4(100, "Created job record JobId=%d Name=%s Type=%c Level=%c\n",
        jcr->JobId, jcr->Job, jcr->jr.JobType, jcr->jr.JobLevel);
 
@@ -133,25 +148,15 @@ JobId_t run_job(JCR *jcr)
 
    Dmsg0(200, "Add jrc to work queue\n");
 
-   /* Queue the job to be run */
-   if ((stat = jobq_add(&job_queue, jcr)) != 0) {
-      berrno be;
-      Jmsg(jcr, M_FATAL, 0, _("Could not add job queue: ERR=%s\n"), be.strerror(stat));
-      JobId = 0;
-      goto bail_out;
-   }
-   Dmsg0(100, "Done run_job()\n");
 
-   V(jcr->mutex);
-   return JobId;
+   return true;
 
 bail_out:
    if (jcr->fname) {
       free_memory(jcr->fname);
       jcr->fname = NULL;
    }
-   V(jcr->mutex);
-   return JobId;
+   return false;
 }
 
 
@@ -219,10 +224,8 @@ static void *job_thread(void *arg)
       }
       break;
    case JT_MIGRATE:
-   case JT_COPY:
-   case JT_ARCHIVE:
-      if (!do_mac_init(jcr)) {             /* migration, archive, copy */
-         mac_cleanup(jcr, JS_ErrorTerminated);
+      if (!do_migration_init(jcr)) { 
+         migration_cleanup(jcr, JS_ErrorTerminated);
       }
       break;
    default:
@@ -262,7 +265,19 @@ static void *job_thread(void *arg)
             goto bail_out;
          }
       }
-
+      /*
+       * We re-update the job start record so that the start
+       *  time is set after the run before job.  This avoids
+       *  that any files created by the run before job will
+       *  be saved twice.  They will be backed up in the current
+       *  job, but not in the next one unless they are changed.
+       *  Without this, they will be backed up in this job and
+       *  in the next job run because in that case, their date
+       *   is after the start of this run.
+       */
+      if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
+         Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
+      }
       generate_job_event(jcr, "JobRun");
 
       switch (jcr->JobType) {
@@ -297,10 +312,10 @@ static void *job_thread(void *arg)
       case JT_MIGRATE:
       case JT_COPY:
       case JT_ARCHIVE:
-         if (do_mac(jcr)) {              /* migration, archive, copy */
+         if (do_migration(jcr)) {
             do_autoprune(jcr);
          } else {
-            mac_cleanup(jcr, JS_ErrorTerminated);
+            migration_cleanup(jcr, JS_ErrorTerminated);
          }
          break;
       default:
@@ -400,7 +415,7 @@ bool cancel_job(UAContext *ua, JCR *jcr)
       /* Cancel Storage daemon */
       if (jcr->store_bsock) {
          if (!ua->jcr->storage) {
-            copy_storage(ua->jcr, jcr);
+            copy_storage(ua->jcr, jcr->storage);
          } else {
             set_storage(ua->jcr, jcr->store);
          }
@@ -780,10 +795,6 @@ void dird_free_jcr_pointers(JCR *jcr)
       free_pool_memory(jcr->client_uname);
       jcr->client_uname = NULL;
    }
-   if (jcr->term_wait_inited) {
-      pthread_cond_destroy(&jcr->term_wait);
-      jcr->term_wait_inited = false;
-   }
    if (jcr->attr) {
       free_pool_memory(jcr->attr);
       jcr->attr = NULL;
@@ -804,6 +815,10 @@ void dird_free_jcr(JCR *jcr)
    Dmsg0(200, "Start dird free_jcr\n");
 
    dird_free_jcr_pointers(jcr);
+   if (jcr->term_wait_inited) {
+      pthread_cond_destroy(&jcr->term_wait);
+      jcr->term_wait_inited = false;
+   }
 
    /* Delete lists setup to hold storage pointers */
    if (jcr->storage) {
@@ -822,7 +837,6 @@ void dird_free_jcr(JCR *jcr)
  */
 void set_jcr_defaults(JCR *jcr, JOB *job)
 {
-   STORE *st;
    jcr->job = job;
    jcr->JobType = job->JobType;
    switch (jcr->JobType) {
@@ -836,18 +850,7 @@ void set_jcr_defaults(JCR *jcr, JOB *job)
    }
    jcr->JobPriority = job->Priority;
    /* Copy storage definitions -- deleted in dir_free_jcr above */
-   if (job->storage) {
-      if (jcr->storage) {
-         delete jcr->storage;
-      }
-      jcr->storage = New(alist(10, not_owned_by_alist));
-      foreach_alist(st, job->storage) {
-         jcr->storage->append(st);
-      }
-   }
-   if (jcr->storage) {
-      jcr->store = (STORE *)jcr->storage->first();
-   }
+   copy_storage(jcr, job->storage);
    jcr->client = job->client;
    if (!jcr->client_name) {
       jcr->client_name = get_pool_memory(PM_NAME);
@@ -891,28 +894,28 @@ void set_jcr_defaults(JCR *jcr, JOB *job)
    }
 }
 
-/*
- * copy the storage definitions from an old JCR to a new one
+
+/* 
+ * Copy the storage definitions from an alist to the JCR
  */
-void copy_storage(JCR *new_jcr, JCR *old_jcr)
+void copy_storage(JCR *jcr, alist *storage)
 {
-   if (old_jcr->storage) {
+   if (storage) {
       STORE *st;
-      if (old_jcr->storage) {
-         delete old_jcr->storage;
+      if (jcr->storage) {
+         delete jcr->storage;
       }
-      new_jcr->storage = New(alist(10, not_owned_by_alist));
-      foreach_alist(st, old_jcr->storage) {
-         new_jcr->storage->append(st);
+      jcr->storage = New(alist(10, not_owned_by_alist));
+      foreach_alist(st, storage) {
+         jcr->storage->append(st);
       }
-   }
-   if (old_jcr->store) {
-      new_jcr->store = old_jcr->store;
-   } else if (new_jcr->storage) {
-      new_jcr->store = (STORE *)new_jcr->storage->first();
+   }               
+   if (jcr->storage) {
+      jcr->store = (STORE *)jcr->storage->first();
    }
 }
 
+
 /* Set storage override */
 void set_storage(JCR *jcr, STORE *store)
 {
@@ -927,3 +930,57 @@ void set_storage(JCR *jcr, STORE *store)
    /* Store not in list, so add it */
    jcr->storage->prepend(store);
 }
+
+void create_clones(JCR *jcr)
+{
+   /*
+    * Fire off any clone jobs (run directives)
+    */
+   Dmsg2(900, "cloned=%d run_cmds=%p\n", jcr->cloned, jcr->job->run_cmds);
+   if (!jcr->cloned && jcr->job->run_cmds) {
+      char *runcmd;
+      JOB *job = jcr->job;
+      POOLMEM *cmd = get_pool_memory(PM_FNAME);
+      UAContext *ua = new_ua_context(jcr);
+      ua->batch = true;
+      foreach_alist(runcmd, job->run_cmds) {
+         cmd = edit_job_codes(jcr, cmd, runcmd, "");              
+         Mmsg(ua->cmd, "run %s cloned=yes", cmd);
+         Dmsg1(900, "=============== Clone cmd=%s\n", ua->cmd);
+         parse_ua_args(ua);                 /* parse command */
+         int stat = run_cmd(ua, ua->cmd);
+         if (stat == 0) {
+            Jmsg(jcr, M_ERROR, 0, _("Could not start clone job.\n"));
+         } else {
+            Jmsg(jcr, M_INFO, 0, _("Clone JobId %d started.\n"), stat);
+         }
+      }
+      free_ua_context(ua);
+      free_pool_memory(cmd);
+   }
+}
+
+bool create_restore_bootstrap_file(JCR *jcr)
+{
+   RESTORE_CTX rx;
+   UAContext *ua;
+   memset(&rx, 0, sizeof(rx));
+   rx.bsr = new_bsr();
+   rx.JobIds = "";                       
+   rx.bsr->JobId = jcr->previous_jr.JobId;
+   ua = new_ua_context(jcr);
+   complete_bsr(ua, rx.bsr);
+   rx.bsr->fi = new_findex();
+   rx.bsr->fi->findex = 1;
+   rx.bsr->fi->findex2 = jcr->previous_jr.JobFiles;
+   jcr->ExpectedFiles = write_bsr_file(ua, rx);
+   if (jcr->ExpectedFiles == 0) {
+      free_ua_context(ua);
+      free_bsr(rx.bsr);
+      return false;
+   }
+   free_ua_context(ua);
+   free_bsr(rx.bsr);
+   jcr->needs_sd = true;
+   return true;
+}