]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/migrate.c
Update technotes and version
[bacula/bacula] / bacula / src / dird / migrate.c
index bb6e46d4dfe05e50d3b7105af1b5e6cb9e1c1961..b45c9876b3d9d9089468fb9563758c2863048dfa 100644 (file)
@@ -1,23 +1,7 @@
-/*
- *
- *   Bacula Director -- migrate.c -- responsible for doing
- *     migration jobs.
- *
- *     Kern Sibbald, September MMIV
- *
- *  Basic tasks done here:
- *     Open DB and create records for this job.
- *     Open Message Channel with Storage daemon to tell him a job will be starting.
- *     Open connection with Storage daemon and pass him commands
- *       to do the backup.
- *     When the Storage daemon finishes the job, update the DB.
- *
- *   Version $Id$
- */
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2004-2006 Free Software Foundation Europe e.V.
+   Copyright (C) 2004-2007 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.
    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
    Switzerland, email:ftf@fsfeurope.org.
 */
+/*
+ *
+ *   Bacula Director -- migrate.c -- responsible for doing
+ *     migration jobs.
+ *
+ *     Kern Sibbald, September MMIV
+ *
+ *  Basic tasks done here:
+ *     Open DB and create records for this job.
+ *     Open Message Channel with Storage daemon to tell him a job will be starting.
+ *     Open connection with Storage daemon and pass him commands
+ *       to do the backup.
+ *     When the Storage daemon finishes the job, update the DB.
+ *
+ *   Version $Id$
+ */
 
 #include "bacula.h"
 #include "dird.h"
@@ -277,25 +277,6 @@ bool do_migration(JCR *jcr)
    Jmsg(jcr, M_INFO, 0, _("Start Migration JobId %s, Job=%s\n"),
         edit_uint64(jcr->JobId, ed1), jcr->Job);
 
-   set_jcr_job_status(jcr, JS_Running);
-   set_jcr_job_status(mig_jcr, JS_Running);
-   Dmsg2(dbglevel, "JobId=%d JobLevel=%c\n", (int)jcr->jr.JobId, jcr->jr.JobLevel);
-
-   /* Update job start record for this migration control job */
-   if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
-      Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
-      return false;
-   }
-
-   Dmsg4(dbglevel, "mig_jcr: Name=%s JobId=%d Type=%c Level=%c\n",
-      mig_jcr->jr.Name, (int)mig_jcr->jr.JobId, 
-      mig_jcr->jr.JobType, mig_jcr->jr.JobLevel);
-
-   /* Update job start record for the real migration backup job */
-   if (!db_update_job_start_record(mig_jcr, mig_jcr->db, &mig_jcr->jr)) {
-      Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(mig_jcr->db));
-      return false;
-   }
 
 
    /*
@@ -335,6 +316,49 @@ bool do_migration(JCR *jcr)
       return false;
    }
 
+   /*    
+    * 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.
+    */
+   jcr->start_time = time(NULL);
+   jcr->jr.StartTime = jcr->start_time;
+   jcr->jr.JobTDate = jcr->start_time;
+   set_jcr_job_status(jcr, JS_Running);
+
+   /* Update job start record for this migration control job */
+   if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
+      Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
+      return false;
+   }
+
+
+   mig_jcr->start_time = time(NULL);
+   mig_jcr->jr.StartTime = mig_jcr->start_time;
+   mig_jcr->jr.JobTDate = mig_jcr->start_time;
+   set_jcr_job_status(mig_jcr, JS_Running);
+
+   /* Update job start record for the real migration backup job */
+   if (!db_update_job_start_record(mig_jcr, mig_jcr->db, &mig_jcr->jr)) {
+      Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(mig_jcr->db));
+      return false;
+   }
+
+   Dmsg4(dbglevel, "mig_jcr: Name=%s JobId=%d Type=%c Level=%c\n",
+      mig_jcr->jr.Name, (int)mig_jcr->jr.JobId, 
+      mig_jcr->jr.JobType, mig_jcr->jr.JobLevel);
+
+
+   /*
+    * Start the job prior to starting the message thread below
+    * to avoid two threads from using the BSOCK structure at
+    * the same time.
+    */
    if (!bnet_fsend(sd, "run")) {
       return false;
    }
@@ -353,16 +377,19 @@ bool do_migration(JCR *jcr)
    /* Pickup Job termination data */
    /* Note, the SD stores in jcr->JobFiles/ReadBytes/JobBytes/Errors */
    wait_for_storage_daemon_termination(jcr);
-
    set_jcr_job_status(jcr, jcr->SDJobStatus);
+   db_write_batch_file_records(jcr);    /* used by bulk batch file insert */
    if (jcr->JobStatus != JS_Terminated) {
       return false;
    }
 
    migration_cleanup(jcr, jcr->JobStatus);
    if (mig_jcr) {
+      char jobid[50];
       UAContext *ua = new_ua_context(jcr);
-      purge_job_records_from_catalog(ua, jcr->previous_jr.JobId);
+      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;
@@ -473,7 +500,7 @@ const char *sql_client =
 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.ClientId=Client.ClientId AND Job.Type='B'"
    " ORDER by Job.StartTime";
 
 /* Get Volume names in Pool */
@@ -486,7 +513,7 @@ const char *sql_vol =
 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 JobMedia.JobId=Job.JobId AND Job.Type='B'"
    " ORDER by Job.StartTime";
 
 
@@ -506,6 +533,7 @@ const char *sql_oldest_vol =
 const char *sql_jobids_from_mediaid =
    "SELECT DISTINCT Job.JobId,Job.StartTime FROM JobMedia,Job"
    " WHERE JobMedia.JobId=Job.JobId AND JobMedia.MediaId=%s"
+   " AND Job.Type='B'"
    " ORDER by Job.StartTime";
 
 /* Get tne number of bytes in the pool */
@@ -530,6 +558,7 @@ const char *sql_pool_time =
    "SELECT DISTINCT Job.JobId from Pool,Job,Media,JobMedia WHERE"
    " Pool.Name='%s' AND Media.PoolId=Pool.PoolId AND"
    " VolStatus in ('Full','Used','Error') AND Media.Enabled=1 AND"
+   " Job.Type='B' AND"
    " JobMedia.JobId=Job.JobId AND Job.PoolId=Media.PoolId"
    " AND Job.RealEndTime<='%s'";
 
@@ -561,7 +590,7 @@ const char *sql_pool_time =
  */
 static int get_job_to_migrate(JCR *jcr)
 {
-   char ed1[30];
+   char ed1[30], ed2[30];
    POOL_MEM query(PM_MESSAGE);
    JobId_t JobId;
    DBId_t  MediaId = 0;
@@ -700,22 +729,26 @@ static int get_job_to_migrate(JCR *jcr)
             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());
             if (!db_sql_query(jcr->db, query.c_str(), db_int64_handler, (void *)&ctx)) {
                Jmsg(jcr, M_FATAL, 0, _("SQL failed. ERR=%s\n"), db_strerror(jcr->db));
                goto bail_out;
             }
             pool_bytes -= ctx.value;
-            Dmsg1(dbglevel, "Job bytes=%d\n", (int)ctx.value);
-            Dmsg2(dbglevel, "lowbytes=%d pool=%d\n", (int)jcr->rpool->MigrationLowBytes,
-                  (int)pool_bytes);
+            Dmsg1(dbglevel, "Total migrate Job bytes=%s\n", edit_int64(ctx.value, ed1));
+            Dmsg2(dbglevel, "lowbytes=%s poolafter=%s\n", 
+                  edit_int64(jcr->rpool->MigrationLowBytes, ed1),
+                  edit_int64(pool_bytes, ed2));
             if (pool_bytes <= (int64_t)jcr->rpool->MigrationLowBytes) {
                Dmsg0(dbglevel, "We should be done.\n");
                break;
             }
 
          }
-         Dmsg2(dbglevel, "Pool Occupancy ids=%d JobIds=%s\n", jids.count, jids.list);
-
+         /* Transfer jids to ids, where the jobs list is expected */
+         ids.count = jids.count;
+         pm_strcpy(ids.list, jids.list);
+         Dmsg2(dbglevel, "Pool Occupancy ids=%d JobIds=%s\n", ids.count, ids.list);
          break;
 
       case MT_POOL_TIME:
@@ -753,8 +786,10 @@ static int get_job_to_migrate(JCR *jcr)
       Jmsg(jcr, M_INFO, 0, _("No JobIds found to migrate.\n"));
       goto ok_out;
    }
-   Jmsg(jcr, M_INFO, 0, _("The following %u JobId%s will be migrated: %s\n"),
+
+   Jmsg(jcr, M_INFO, 0, _("The following %u JobId%s were chosen to be migrated: %s\n"),
       ids.count, ids.count==0?"":"s", ids.list);
+
    Dmsg2(dbglevel, "Before loop count=%d ids=%s\n", ids.count, ids.list);
    for (int i=1; i < (int)ids.count; i++) {
       JobId = 0;
@@ -846,9 +881,10 @@ static bool find_mediaid_then_jobids(JCR *jcr, idpkt *ids, const char *query1,
    }
    if (ids->count == 0) {
       Jmsg(jcr, M_INFO, 0, _("No %ss found to migrate.\n"), type);
-   }
-   if (ids->count != 1) {
-      Jmsg(jcr, M_FATAL, 0, _("SQL logic error. Count should be 1 but is %d\n"), 
+      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);
       goto bail_out;
    }
@@ -975,6 +1011,9 @@ static bool regex_find_jobids(JCR *jcr, idpkt *ids, const char *query1,
 
 bail_out:
    Dmsg2(dbglevel, "Count=%d Jobids=%s\n", ids->count, ids->list);
+   foreach_dlist(item, item_chain) {
+      free(item->item);
+   }
    delete item_chain;
    return ok;
 }
@@ -998,10 +1037,8 @@ void migration_cleanup(JCR *jcr, int TermCode)
    POOL_MEM query(PM_MESSAGE);
 
    Dmsg2(100, "Enter migrate_cleanup %d %c\n", TermCode, TermCode);
-   dequeue_messages(jcr);             /* display any queued messages */
+   update_job_end(jcr, TermCode);
    memset(&mr, 0, sizeof(mr));
-   set_jcr_job_status(jcr, TermCode);
-   update_job_end_record(jcr);        /* update database */
 
    /* 
     * Check if we actually did something.  
@@ -1015,8 +1052,7 @@ void migration_cleanup(JCR *jcr, int TermCode)
       mig_jcr->jr.RealEndTime = 0; 
       mig_jcr->jr.PriorJobId = jcr->previous_jr.JobId;
 
-      set_jcr_job_status(mig_jcr, TermCode);
-      update_job_end_record(mig_jcr);
+      update_job_end(mig_jcr, TermCode);
      
       /* Update final items to set them to the previous job's values */
       Mmsg(query, "UPDATE Job SET StartTime='%s',EndTime='%s',"
@@ -1093,6 +1129,13 @@ void migration_cleanup(JCR *jcr, int TermCode)
          break;
       }
   } else {
+     if (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");
   }
 
@@ -1109,7 +1152,8 @@ 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\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"
@@ -1135,9 +1179,8 @@ void migration_cleanup(JCR *jcr, int TermCode)
 "  SD Errors:              %d\n"
 "  SD termination status:  %s\n"
 "  Termination:            %s\n\n"),
-   VERSION,
-   LSMDATE,
-        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),