]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/migrate.c
ebl Modify disk-changer to check if slot contains something before
[bacula/bacula] / bacula / src / dird / migrate.c
index cb37dad38a27d99b7de3aabad50f0dbf62c96b6b..42fd5cf10655555273f1cf08335afa8bd21cb0bf 100644 (file)
@@ -1,14 +1,14 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2004-2007 Free Software Foundation Europe e.V.
+   Copyright (C) 2004-2008 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    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
@@ -28,7 +28,9 @@
 /*
  *
  *   Bacula Director -- migrate.c -- responsible for doing
- *     migration jobs.
+ *     migration and copy jobs.
+ * 
+ *   Also handles Copy jobs (March MMVIII)
  *
  *     Kern Sibbald, September MMIV
  *
@@ -104,6 +106,10 @@ bool do_migration_init(JCR *jcr)
 
    apply_pool_overrides(jcr);
 
+   if (!allow_duplicate_job(jcr)) {
+      return false;
+   }
+
    jcr->jr.PoolId = get_or_create_pool_record(jcr, jcr->pool->name());
    if (jcr->jr.PoolId == 0) {
       Dmsg1(dbglevel, "JobId=%d no PoolId\n", (int)jcr->JobId);
@@ -182,6 +188,8 @@ bool do_migration_init(JCR *jcr)
       return false;
    }
 
+   jcr->spool_data = job->spool_data;     /* turn on spooling if requested in job */ 
+
    /* Create a migation 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));
@@ -198,9 +206,8 @@ bool do_migration_init(JCR *jcr)
 
    /* Now reset the job record from the previous job */
    memcpy(&mig_jcr->jr, &jcr->previous_jr, sizeof(mig_jcr->jr));
-   /* Update the jr to reflect the new values of PoolId, FileSetId, and JobId. */
+   /* Update the jr to reflect the new values of PoolId and JobId. */
    mig_jcr->jr.PoolId = jcr->jr.PoolId;
-   mig_jcr->jr.FileSetId = jcr->jr.FileSetId;
    mig_jcr->jr.JobId = mig_jcr->JobId;
 
    Dmsg4(dbglevel, "mig_jcr: Name=%s JobId=%d Type=%c Level=%c\n",
@@ -274,7 +281,8 @@ bool do_migration(JCR *jcr)
    }
 
    /* Print Job Start message */
-   Jmsg(jcr, M_INFO, 0, _("Start Migration JobId %s, Job=%s\n"),
+   Jmsg(jcr, M_INFO, 0, _("Start %s JobId %s, Job=%s\n"),
+        jcr->JobType == JT_MIGRATE ? "Migration" : "Copy",
         edit_uint64(jcr->JobId, ed1), jcr->Job);
 
 
@@ -359,7 +367,7 @@ bool do_migration(JCR *jcr)
     * to avoid two threads from using the BSOCK structure at
     * the same time.
     */
-   if (!bnet_fsend(sd, "run")) {
+   if (!sd->fsend("run")) {
       return false;
    }
 
@@ -384,7 +392,7 @@ bool do_migration(JCR *jcr)
    }
 
    migration_cleanup(jcr, jcr->JobStatus);
-   if (mig_jcr) {
+   if (jcr->JobType == JT_MIGRATE && mig_jcr) {
       char jobid[50];
       UAContext *ua = new_ua_context(jcr);
       edit_uint64(jcr->previous_jr.JobId, jobid);
@@ -403,13 +411,14 @@ struct idpkt {
 /* Add an item to the list if it is unique */
 static void add_unique_id(idpkt *ids, char *item) 
 {
-   char id[30];
+   const int maxlen = 30;
+   char id[maxlen+1];
    char *q = ids->list;
 
    /* Walk through current list to see if each item is the same as item */
    for ( ; *q; ) {
        id[0] = 0;
-       for (int i=0; i<(int)sizeof(id); i++) {
+       for (int i=0; i<maxlen; i++) {
           if (*q == 0) {
              break;
           } else if (*q == ',') {
@@ -518,21 +527,23 @@ const char *sql_jobids_from_vol =
 
 
 const char *sql_smallest_vol = 
-   "SELECT MediaId FROM Media,Pool WHERE"
-   " VolStatus in ('Full','Used','Error') AND Media.Enabled=1 AND"
+   "SELECT Media.MediaId FROM Media,Pool,JobMedia WHERE"
+   " Media.MediaId in (SELECT DISTINCT MediaId from JobMedia) AND"
+   " Media.VolStatus in ('Full','Used','Error') AND Media.Enabled=1 AND"
    " Media.PoolId=Pool.PoolId AND Pool.Name='%s'"
    " ORDER BY VolBytes ASC LIMIT 1";
 
 const char *sql_oldest_vol = 
-   "SELECT MediaId FROM Media,Pool WHERE"
-   " VolStatus in ('Full','Used','Error') AND Media.Enabled=1 AND"
+   "SELECT Media.MediaId FROM Media,Pool,JobMedia WHERE"
+   " Media.MediaId in (SELECT DISTINCT MediaId from JobMedia) AND"
+   " Media.VolStatus in ('Full','Used','Error') AND Media.Enabled=1 AND"
    " Media.PoolId=Pool.PoolId AND Pool.Name='%s'"
    " ORDER BY LastWritten ASC LIMIT 1";
 
 /* Get JobIds when we have selected MediaId */
 const char *sql_jobids_from_mediaid =
    "SELECT DISTINCT Job.JobId,Job.StartTime FROM JobMedia,Job"
-   " WHERE JobMedia.JobId=Job.JobId AND JobMedia.MediaId=%s"
+   " WHERE JobMedia.JobId=Job.JobId AND JobMedia.MediaId IN (%s)"
    " AND Job.Type='B'"
    " ORDER by Job.StartTime";
 
@@ -593,7 +604,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;
@@ -678,8 +689,8 @@ static int get_job_to_migrate(JCR *jcr)
             goto ok_out;
          }
          pool_bytes = ctx.value;
-         Dmsg2(dbglevel, "highbytes=%d pool=%d\n", (int)jcr->rpool->MigrationHighBytes,
-               (int)pool_bytes);
+         Dmsg2(dbglevel, "highbytes=%lld pool=%lld\n", jcr->rpool->MigrationHighBytes,
+               pool_bytes);
          if (pool_bytes < (int64_t)jcr->rpool->MigrationHighBytes) {
             Jmsg(jcr, M_INFO, 0, _("No Volumes found to migrate.\n"));
             goto ok_out;
@@ -700,33 +711,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());
@@ -743,7 +750,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;
@@ -857,11 +863,11 @@ static void start_migration_job(JCR *jcr)
         edit_uint64(jcr->MigrateJobId, ed1));
    Dmsg1(dbglevel, "=============== Migration cmd=%s\n", ua->cmd);
    parse_ua_args(ua);                 /* parse command */
-   int stat = run_cmd(ua, ua->cmd);
-   if (stat == 0) {
+   JobId_t jobid = run_cmd(ua, ua->cmd);
+   if (jobid == 0) {
       Jmsg(jcr, M_ERROR, 0, _("Could not start migration job.\n"));
    } else {
-      Jmsg(jcr, M_INFO, 0, _("Migration JobId %d started.\n"), stat);
+      Jmsg(jcr, M_INFO, 0, _("Migration JobId %d started.\n"), (int)jobid);
    }
    free_ua_context(ua);
 }
@@ -880,15 +886,14 @@ static bool find_mediaid_then_jobids(JCR *jcr, idpkt *ids, const char *query1,
       goto bail_out;
    }
    if (ids->count == 0) {
-      Jmsg(jcr, M_INFO, 0, _("No %ss found to migrate.\n"), type);
+      Jmsg(jcr, M_INFO, 0, _("No %s found to migrate.\n"), type);
       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;
    }
-   Dmsg1(dbglevel, "Smallest Vol Jobids=%s\n", ids->list);
+   Dmsg2(dbglevel, "%s MediaIds=%s\n", type, ids->list);
 
    ok = find_jobids_from_mediaid_list(jcr, ids, type);
 
@@ -896,6 +901,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;
@@ -911,6 +922,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;
 }
@@ -1063,25 +1075,18 @@ void migration_cleanup(JCR *jcr, int TermCode)
       db_sql_query(mig_jcr->db, query.c_str(), NULL, NULL);
 
       /* Now mark the previous job as migrated if it terminated normally */
-      if (jcr->JobStatus == JS_Terminated) {
+      if (jcr->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));
          db_sql_query(mig_jcr->db, query.c_str(), NULL, NULL);
       } 
 
       if (!db_get_job_record(jcr, jcr->db, &jcr->jr)) {
-         Jmsg(jcr, M_WARNING, 0, _("Error getting job record for stats: %s"),
+         Jmsg(jcr, M_WARNING, 0, _("Error getting Job record for Job report: ERR=%s"),
             db_strerror(jcr->db));
          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);
 
       if (!db_get_job_volume_names(mig_jcr, mig_jcr->db, mig_jcr->jr.JobId, &mig_jcr->VolumeName)) {
@@ -1096,6 +1101,22 @@ 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++;                         /* skip | */
+         } else {
+            p = mig_jcr->VolumeName;     /* no |, take full name */
+         }
+         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) {
@@ -1129,11 +1150,10 @@ void migration_cleanup(JCR *jcr, int TermCode)
          break;
       }
   } else {
-     if (jcr->previous_jr.JobId != 0) {
+     if (jcr->JobType == JT_MIGRATE && jcr->previous_jr.JobId != 0) {
         /* Mark previous job as migrated */
         Mmsg(query, "UPDATE Job SET Type='%c' WHERE JobId=%s",
              (char)JT_MIGRATED_JOB, edit_uint64(jcr->previous_jr.JobId, ec1));
-        Dmsg1(000, "Mark: %s\n", query.c_str());
         db_sql_query(jcr->db, query.c_str(), NULL, NULL);
      }
      term_msg = _("%s -- no files to migrate");
@@ -1152,11 +1172,12 @@ void migration_cleanup(JCR *jcr, int TermCode)
 
    jobstatus_to_ascii(jcr->SDJobStatus, sd_term_msg, sizeof(sd_term_msg));
 
-   Jmsg(jcr, msg_type, 0, _("Bacula %s %s (%s) %s %s %s at %s\n"
+   Jmsg(jcr, msg_type, 0, _("Bacula %s %s (%s): %s\n"
+"  Build OS:               %s %s %s\n"
 "  Prev Backup JobId:      %s\n"
 "  New Backup JobId:       %s\n"
-"  Migration JobId:        %s\n"
-"  Migration Job:          %s\n"
+"  Current JobId:          %s\n"
+"  Current Job:            %s\n"
 "  Backup Level:           %s%s\n"
 "  Client:                 %s\n"
 "  FileSet:                \"%s\" %s\n"
@@ -1164,6 +1185,7 @@ void migration_cleanup(JCR *jcr, int TermCode)
 "  Read Storage:           \"%s\" (From %s)\n"
 "  Write Pool:             \"%s\" (From %s)\n"
 "  Write Storage:          \"%s\" (From %s)\n"
+"  Catalog:                \"%s\" (From %s)\n"
 "  Start time:             %s\n"
 "  End time:               %s\n"
 "  Elapsed time:           %s\n"
@@ -1178,8 +1200,8 @@ void migration_cleanup(JCR *jcr, int TermCode)
 "  SD Errors:              %d\n"
 "  SD termination status:  %s\n"
 "  Termination:            %s\n\n"),
-        my_name, VERSION, BDATE, HOST_OS, DISTNAME, DISTVER,
-        edt, 
+        my_name, VERSION, LSMDATE, edt,
+        HOST_OS, DISTNAME, DISTVER,
         edit_uint64(jcr->previous_jr.JobId, ec6),
         mig_jcr ? edit_uint64(mig_jcr->jr.JobId, ec7) : "0",
         edit_uint64(jcr->jr.JobId, ec8),
@@ -1193,6 +1215,7 @@ void migration_cleanup(JCR *jcr, int TermCode)
         jcr->pool->name(), jcr->pool_source,
         jcr->wstore?jcr->wstore->name():"*None*", 
         NPRT(jcr->wstore_source),
+        jcr->catalog->name(), jcr->catalog_source,
         sdt,
         edt,
         edit_utime(RunTime, elapsed, sizeof(elapsed)),
@@ -1228,11 +1251,12 @@ void migration_cleanup(JCR *jcr, int TermCode)
  */
 static int get_next_dbid_from_list(char **p, DBId_t *DBId)
 {
-   char id[30];
+   const int maxlen = 30;
+   char id[maxlen+1];
    char *q = *p;
 
    id[0] = 0;
-   for (int i=0; i<(int)sizeof(id); i++) {
+   for (int i=0; i<maxlen; i++) {
       if (*q == 0) {
          break;
       } else if (*q == ',') {