]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/migrate.c
ebl Add new ScratchPool directive to Pool. Thanks to Graham
[bacula/bacula] / bacula / src / dird / migrate.c
index e78a8a9665cc818f172a9bab36951689b12677e0..9bdfab58f96e46f47186643d1581db80d4ce898d 100644 (file)
@@ -129,6 +129,12 @@ bool do_migration_init(JCR *jcr)
 
    Dmsg2(dbglevel, "Read pool=%s (From %s)\n", jcr->rpool->name(), jcr->rpool_source);
 
+   if (!get_or_create_fileset_record(jcr)) {
+      Dmsg1(dbglevel, "JobId=%d no FileSet\n", (int)jcr->JobId);
+      Jmsg(jcr, M_FATAL, 0, _("Could not get or create the FileSet record.\n"));
+      return false;
+   }
+
    /* If we find a job or jobs to migrate it is previous_jr.JobId */
    count = get_job_to_migrate(jcr);
    if (count < 0) {
@@ -146,12 +152,6 @@ bool do_migration_init(JCR *jcr)
       return true;                    /* no work */
    }
 
-   if (!get_or_create_fileset_record(jcr)) {
-      Dmsg1(dbglevel, "JobId=%d no FileSet\n", (int)jcr->JobId);
-      Jmsg(jcr, M_FATAL, 0, _("Could not get or create the FileSet record.\n"));
-      return false;
-   }
-
    create_restore_bootstrap_file(jcr);
 
    if (jcr->previous_jr.JobId == 0 || jcr->ExpectedFiles == 0) {
@@ -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)";
@@ -602,7 +590,7 @@ const char *sql_jobids_of_pool_uncopied_jobs =
    " AND Job.Type = 'B' AND Job.JobStatus = 'T'"
    " AND Job.JobId NOT IN"
    " (SELECT PriorJobId FROM Job WHERE"
-   " Type = 'B' AND Job.JobStatus = 'T'"
+   " Type IN ('B','C') AND Job.JobStatus = 'T'"
    " AND PriorJobId != 0)"
    " ORDER by Job.StartTime";
 
@@ -1125,6 +1113,11 @@ void migration_cleanup(JCR *jcr, int TermCode)
     *  mig_jcr is jcr of the newly migrated job.
     */
    if (mig_jcr) {
+      char old_jobid[50], new_jobid[50];
+
+      edit_uint64(jcr->previous_jr.JobId, old_jobid);
+      edit_uint64(mig_jcr->jr.JobId, new_jobid);
+
       mig_jcr->JobFiles = jcr->JobFiles = jcr->SDJobFiles;
       mig_jcr->JobBytes = jcr->JobBytes = jcr->SDJobBytes;
       mig_jcr->VolSessionId = jcr->VolSessionId;
@@ -1139,13 +1132,42 @@ void migration_cleanup(JCR *jcr, int TermCode)
                   "JobTDate=%s WHERE JobId=%s", 
          jcr->previous_jr.cStartTime, jcr->previous_jr.cEndTime, 
          edit_uint64(jcr->previous_jr.JobTDate, ec1),
-         edit_uint64(mig_jcr->jr.JobId, ec2));
+         new_jobid);
       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) {
          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, old_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, 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 we terminated a copy normally:
+       *   - copy any Log records to the new JobId
+       *   - set type="Job Copy" for the new job
+       */
+      if (jcr->get_JobType() == JT_COPY && jcr->JobStatus == JS_Terminated) {
+         /* Copy JobLog to new JobId */
+         Mmsg(query, "INSERT INTO Log (JobId, Time, LogText ) " 
+                      "SELECT %s, Time, LogText FROM Log WHERE JobId=%s",
+              new_jobid, old_jobid);
+         db_sql_query(mig_jcr->db, query.c_str(), NULL, NULL);
+         Mmsg(query, "UPDATE Job SET Type='%c' WHERE JobId=%s",
+              (char)JT_JOB_COPY, new_jobid);
          db_sql_query(mig_jcr->db, query.c_str(), NULL, NULL);
       }