]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/migrate.c
23Aug07
[bacula/bacula] / bacula / src / dird / migrate.c
index f7aec4eca3c3c405c97da279dc60126b3a61c30d..223328c1f561b55f12f69988ffb65f4b5f7e9f91 100644 (file)
@@ -7,8 +7,8 @@
    many others, a complete list can be found in the file AUTHORS.
    This program is Free Software; you can redistribute it and/or
    modify it under the terms of version two of the GNU General Public
-   License as published by the Free Software Foundation plus additions
-   that are listed in the file LICENSE.
+   License as published by the Free Software Foundation and included
+   in the file LICENSE.
 
    This program is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -596,7 +596,7 @@ static int get_job_to_migrate(JCR *jcr)
    char ed1[30], ed2[30];
    POOL_MEM query(PM_MESSAGE);
    JobId_t JobId;
-   DBId_t  MediaId = 0;
+   DBId_t DBId = 0;
    int stat;
    char *p;
    idpkt ids, mid, jids;
@@ -703,33 +703,29 @@ static int get_job_to_migrate(JCR *jcr)
          }
          Dmsg2(dbglevel, "Pool Occupancy ids=%d MediaIds=%s\n", ids.count, ids.list);
 
-         /*
-          * Now loop over MediaIds getting more JobIds to migrate until
-          *  we reduce the pool occupancy below the low water mark.
-          */
+         if (!find_jobids_from_mediaid_list(jcr, &ids, "Volumes")) {
+            goto bail_out;
+         }
+         /* ids == list of jobs  */
          p = ids.list;
          for (int i=0; i < (int)ids.count; i++) {
-            stat = get_next_dbid_from_list(&p, &MediaId);
-            Dmsg2(dbglevel, "get_next_dbid stat=%d MediaId=%u\n", stat, MediaId);
+            stat = get_next_dbid_from_list(&p, &DBId);
+            Dmsg2(dbglevel, "get_next_dbid stat=%d JobId=%u\n", stat, (uint32_t)DBId);
             if (stat < 0) {
-               Jmsg(jcr, M_FATAL, 0, _("Invalid MediaId found.\n"));
+               Jmsg(jcr, M_FATAL, 0, _("Invalid JobId found.\n"));
                goto bail_out;
             } else if (stat == 0) {
                break;
             }
+
             mid.count = 1;
-            Mmsg(mid.list, "%s", edit_int64(MediaId, ed1));
-            if (!find_jobids_from_mediaid_list(jcr, &mid, "Volumes")) {
-               continue;
-            }
-            if (i != 0) {
+            Mmsg(mid.list, "%s", edit_int64(DBId, ed1));
+            if (jids.count > 0) {
                pm_strcat(jids.list, ",");
             }
             pm_strcat(jids.list, mid.list);
             jids.count += mid.count;
 
-            /* Now get the count of bytes added */
-            ctx.count = 0;
             /* Find count of bytes from Jobs */
             Mmsg(query, sql_job_bytes, mid.list);
             Dmsg1(dbglevel, "Jobbytes query: %s\n", query.c_str());
@@ -746,7 +742,6 @@ static int get_job_to_migrate(JCR *jcr)
                Dmsg0(dbglevel, "We should be done.\n");
                break;
             }
-
          }
          /* Transfer jids to ids, where the jobs list is expected */
          ids.count = jids.count;
@@ -887,8 +882,7 @@ static bool find_mediaid_then_jobids(JCR *jcr, idpkt *ids, const char *query1,
       ok = true;         /* Not an error */
       goto bail_out;
    } else if (ids->count != 1) {
-      Jmsg(jcr, M_FATAL, 0, _("SQL error. Expected 1 MediaId got %d\n"), 
-         ids->count);
+      Jmsg(jcr, M_FATAL, 0, _("SQL error. Expected 1 MediaId got %d\n"), ids->count);
       goto bail_out;
    }
    Dmsg2(dbglevel, "%s MediaIds=%s\n", type, ids->list);
@@ -899,6 +893,12 @@ bail_out:
    return ok;
 }
 
+/* 
+ * This routine returns:
+ *    false       if an error occurred
+ *    true        otherwise
+ *    ids.count   number of jobids found (may be zero)
+ */       
 static bool find_jobids_from_mediaid_list(JCR *jcr, idpkt *ids, const char *type) 
 {
    bool ok = false;
@@ -914,6 +914,7 @@ static bool find_jobids_from_mediaid_list(JCR *jcr, idpkt *ids, const char *type
       Jmsg(jcr, M_INFO, 0, _("No %ss found to migrate.\n"), type);
    }
    ok = true;
+
 bail_out:
    return ok;
 }
@@ -1078,12 +1079,6 @@ void migration_cleanup(JCR *jcr, int TermCode)
          set_jcr_job_status(jcr, JS_ErrorTerminated);
       }
 
-      bstrncpy(mr.VolumeName, jcr->VolumeName, sizeof(mr.VolumeName));
-      if (!db_get_media_record(jcr, jcr->db, &mr)) {
-         Jmsg(jcr, M_WARNING, 0, _("Error getting Media record for Volume \"%s\": ERR=%s"),
-            mr.VolumeName, db_strerror(jcr->db));
-         set_jcr_job_status(jcr, JS_ErrorTerminated);
-      }
 
       update_bootstrap_file(mig_jcr);
 
@@ -1099,6 +1094,20 @@ void migration_cleanup(JCR *jcr, int TermCode)
          }
          mig_jcr->VolumeName[0] = 0;         /* none */
       }
+
+      if (mig_jcr->VolumeName[0]) {
+         /* Find last volume name. Multiple vols are separated by | */
+         char *p = strrchr(mig_jcr->VolumeName, '|');
+         if (!p) {
+            p = mig_jcr->VolumeName;
+         }
+         bstrncpy(mr.VolumeName, p, sizeof(mr.VolumeName));
+         if (!db_get_media_record(jcr, jcr->db, &mr)) {
+            Jmsg(jcr, M_WARNING, 0, _("Error getting Media record for Volume \"%s\": ERR=%s"),
+               mr.VolumeName, db_strerror(jcr->db));
+         }
+      }
+
       switch (jcr->JobStatus) {
       case JS_Terminated:
          if (jcr->Errors || jcr->SDErrors) {