]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/migrate.c
ebl fix segfault with debug mode
[bacula/bacula] / bacula / src / dird / migrate.c
index e78a8a9665cc818f172a9bab36951689b12677e0..bf20d4ba03655f0c8f25a7df2ffa7bb4b0278923 100644 (file)
@@ -187,7 +187,7 @@ bool do_migration_init(JCR *jcr)
 
    jcr->spool_data = job->spool_data;     /* turn on spooling if requested in job */ 
 
-   /* Create a migation jcr */
+   /* Create a migration jcr */
    mig_jcr = jcr->mig_jcr = new_jcr(sizeof(JCR), dird_free_jcr);
    memcpy(&mig_jcr->previous_jr, &jcr->previous_jr, sizeof(mig_jcr->previous_jr));
 
@@ -406,14 +406,7 @@ bool do_migration(JCR *jcr)
    }
 
    migration_cleanup(jcr, jcr->JobStatus);
-   if (jcr->get_JobType() == JT_MIGRATE && mig_jcr) {
-      char jobid[50];
-      UAContext *ua = new_ua_context(jcr);
-      edit_uint64(jcr->previous_jr.JobId, jobid);
-      /* Purge all old file records, but leave Job record */
-      purge_files_from_jobs(ua, jobid);
-      free_ua_context(ua);
-   }
+
    return true;
 }
 
@@ -524,6 +517,7 @@ const char *sql_jobids_from_client =
    "SELECT DISTINCT Job.JobId,Job.StartTime FROM Job,Pool,Client"
    " WHERE Client.Name='%s' AND Pool.Name='%s' AND Job.PoolId=Pool.PoolId"
    " AND Job.ClientId=Client.ClientId AND Job.Type='B'"
+   " AND Job.JobStatus = 'T'"
    " ORDER by Job.StartTime";
 
 /* Get Volume names in Pool */
@@ -537,6 +531,7 @@ const char *sql_jobids_from_vol =
    "SELECT DISTINCT Job.JobId,Job.StartTime FROM Media,JobMedia,Job"
    " WHERE Media.VolumeName='%s' AND Media.MediaId=JobMedia.MediaId"
    " AND JobMedia.JobId=Job.JobId AND Job.Type='B'"
+   " AND Job.JobStatus = 'T' AND Media.Enabled=1"
    " ORDER by Job.StartTime";
 
 const char *sql_smallest_vol = 
@@ -569,13 +564,6 @@ const char *sql_pool_bytes =
    " Job.Type='B' AND Job.JobStatus = 'T' AND"
    " JobMedia.JobId=Job.JobId AND Job.PoolId=Media.PoolId)";
 
-#ifdef xxx
-/* Broken */
-   "SELECT SUM(VolBytes) FROM Media,Pool WHERE"
-   " VolStatus in ('Full','Used','Error','Append') AND Media.Enabled=1 AND"
-   " Media.PoolId=Pool.PoolId AND Pool.Name='%s'";
-#endif
-
 /* Get the number of bytes in the Jobs */
 const char *sql_job_bytes =
    "SELECT SUM(JobBytes) FROM Job WHERE JobId IN (%s)";
@@ -1142,11 +1130,26 @@ void migration_cleanup(JCR *jcr, int TermCode)
          edit_uint64(mig_jcr->jr.JobId, ec2));
       db_sql_query(mig_jcr->db, query.c_str(), NULL, NULL);
 
-      /* Now mark the previous job as migrated if it terminated normally */
+      /*
+       * If we terminated a migration normally:
+       *   - mark the previous job as migrated
+       *   - move any Log records to the new JobId
+       *   - Purge the File records from the previous job
+       */
       if (jcr->get_JobType() == JT_MIGRATE && jcr->JobStatus == JS_Terminated) {
+         char old_jobid[50], new_jobid[50];
          Mmsg(query, "UPDATE Job SET Type='%c' WHERE JobId=%s",
-              (char)JT_MIGRATED_JOB, edit_uint64(jcr->previous_jr.JobId, ec1));
+              (char)JT_MIGRATED_JOB, edit_uint64(jcr->previous_jr.JobId, new_jobid));
+         db_sql_query(mig_jcr->db, query.c_str(), NULL, NULL);
+         UAContext *ua = new_ua_context(jcr);
+         /* Move JobLog to new JobId */
+         Mmsg(query, "UPDATE Log SET JobId=%s WHERE JobId=%s",
+           new_jobid,
+           edit_uint64(jcr->previous_jr.JobId, old_jobid));
          db_sql_query(mig_jcr->db, query.c_str(), NULL, NULL);
+         /* Purge all old file records, but leave Job record */
+         purge_files_from_jobs(ua, old_jobid);
+         free_ua_context(ua);
       } 
 
       if (!db_get_job_record(jcr, jcr->db, &jcr->jr)) {