]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/ua_purge.c
kes Move the checking of the database in initializion of the Director
[bacula/bacula] / bacula / src / dird / ua_purge.c
index 5a2d1189ff6b41b65e63e946d99ef4bac316d214..f41156558d9c5eb7918adf7128ef422a91450bd4 100644 (file)
  *   Version $Id$
  */
 /*
-   Copyright (C) 2002-2006 Kern Sibbald
+   Bacula® - The Network Backup Solution
 
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License
-   version 2 as amended with additional clauses defined in the
-   file LICENSE in the main source directory.
+   Copyright (C) 2002-2007 Free Software Foundation Europe e.V.
 
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
-   the file LICENSE for additional details.
+   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.
 
- */
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
+
+   Bacula® is a registered trademark of John Walker.
+   The licensor of Bacula is the Free Software Foundation Europe
+   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
+   Switzerland, email:ftf@fsfeurope.org.
+*/
 
 #include "bacula.h"
 #include "dird.h"
@@ -121,7 +134,7 @@ static int job_delete_handler(void *ctx, int num_fields, char **row)
       del->PurgedFiles = (char *)brealloc(del->PurgedFiles, del->max_ids);
    }
    del->JobId[del->num_ids] = (JobId_t)str_to_int64(row[0]);
-   del->PurgedFiles[del->num_ids++] = (char)str_to_int64(row[0]);
+   del->PurgedFiles[del->num_ids++] = (char)str_to_int64(row[1]);
    return 0;
 }
 
@@ -156,21 +169,21 @@ int purgecmd(UAContext *ua, const char *cmd)
    MEDIA_DBR mr;
    JOB_DBR  jr;
    static const char *keywords[] = {
-      N_("files"),
-      N_("jobs"),
-      N_("volume"),
+      NT_("files"),
+      NT_("jobs"),
+      NT_("volume"),
       NULL};
 
    static const char *files_keywords[] = {
-      N_("Job"),
-      N_("JobId"),
-      N_("Client"),
-      N_("Volume"),
+      NT_("Job"),
+      NT_("JobId"),
+      NT_("Client"),
+      NT_("Volume"),
       NULL};
 
    static const char *jobs_keywords[] = {
-      N_("Client"),
-      N_("Volume"),
+      NT_("Client"),
+      NT_("Volume"),
       NULL};
 
    bsendmsg(ua, _(
@@ -191,7 +204,7 @@ int purgecmd(UAContext *ua, const char *cmd)
       case 0:                         /* Job */
       case 1:                         /* JobId */
          if (get_job_dbr(ua, &jr)) {
-            purge_files_from_job(ua, &jr);
+            purge_files_from_job(ua, jr.JobId);
          }
          return 1;
       case 2:                         /* client */
@@ -223,7 +236,7 @@ int purgecmd(UAContext *ua, const char *cmd)
       }
    /* Volume */
    case 2:
-      while ((i=find_arg(ua, N_("volume"))) >= 0) {
+      while ((i=find_arg(ua, NT_("volume"))) >= 0) {
          if (select_media_dbr(ua, &mr)) {
             purge_jobs_from_volume(ua, &mr);
          }
@@ -275,7 +288,7 @@ static int purge_files_from_client(UAContext *ua, CLIENT *client)
    memset(&cr, 0, sizeof(cr));
    memset(&del, 0, sizeof(del));
 
-   bstrncpy(cr.Name, client->hdr.name, sizeof(cr.Name));
+   bstrncpy(cr.Name, client->name(), sizeof(cr.Name));
    if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
       return 0;
    }
@@ -292,7 +305,7 @@ static int purge_files_from_client(UAContext *ua, CLIENT *client)
 
    if (del.tot_ids == 0) {
       bsendmsg(ua, _("No Files found for client %s to purge from %s catalog.\n"),
-         client->hdr.name, client->catalog->hdr.name);
+         client->name(), client->catalog->name());
       goto bail_out;
    }
 
@@ -308,22 +321,10 @@ static int purge_files_from_client(UAContext *ua, CLIENT *client)
    db_sql_query(ua->db, query, file_delete_handler, (void *)&del);
 
    for (i=0; i < del.num_ids; i++) {
-      edit_int64(del.JobId[i], ed1);
-      Dmsg1(050, "Delete Files JobId=%s\n", ed1);
-      Mmsg(query, "DELETE FROM File WHERE JobId=%s", ed1);
-      db_sql_query(ua->db, query, NULL, (void *)NULL);
-      /*
-       * Now mark Job as having files purged. This is necessary to
-       * avoid having too many Jobs to process in future prunings. If
-       * we don't do this, the number of JobId's in our in memory list
-       * will grow very large.
-       */
-      Mmsg(query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%s", ed1);
-      db_sql_query(ua->db, query, NULL, (void *)NULL);
-      Dmsg1(050, "Update Purged sql=%s\n", query);
+      purge_files_from_job(ua, del.JobId[i]);
    }
    bsendmsg(ua, _("%d Files for client \"%s\" purged from %s catalog.\n"), del.num_ids,
-      client->hdr.name, client->catalog->hdr.name);
+      client->name(), client->catalog->name());
 
 bail_out:
    if (del.JobId) {
@@ -353,7 +354,7 @@ static int purge_jobs_from_client(UAContext *ua, CLIENT *client)
    memset(&cr, 0, sizeof(cr));
    memset(&del, 0, sizeof(del));
 
-   bstrncpy(cr.Name, client->hdr.name, sizeof(cr.Name));
+   bstrncpy(cr.Name, client->name(), sizeof(cr.Name));
    if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
       return 0;
    }
@@ -370,7 +371,7 @@ static int purge_jobs_from_client(UAContext *ua, CLIENT *client)
    }
    if (del.tot_ids == 0) {
       bsendmsg(ua, _("No Jobs found for client %s to purge from %s catalog.\n"),
-         client->hdr.name, client->catalog->hdr.name);
+         client->name(), client->catalog->name());
       goto bail_out;
    }
 
@@ -393,24 +394,14 @@ static int purge_jobs_from_client(UAContext *ua, CLIENT *client)
     * Then delete the Job entry, and finally and JobMedia records.
     */
    for (i=0; i < del.num_ids; i++) {
-      edit_int64(del.JobId[i], ed1);
       Dmsg1(050, "Delete Files JobId=%s\n", ed1); 
       if (!del.PurgedFiles[i]) {
-         Mmsg(query, "DELETE FROM File WHERE JobId=%s", ed1);
-         db_sql_query(ua->db, query, NULL, (void *)NULL);
-         Dmsg1(050, "Del sql=%s\n", query);
+         purge_files_from_job(ua, del.JobId[i]);
       }
-
-      Mmsg(query, "DELETE FROM Job WHERE JobId=%s", ed1);
-      db_sql_query(ua->db, query, NULL, (void *)NULL);
-      Dmsg1(050, "Delete Job sql=%s\n", query);
-
-      Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%s", ed1);
-      db_sql_query(ua->db, query, NULL, (void *)NULL);
-      Dmsg1(050, "Delete JobMedia sql=%s\n", query);
+      purge_job_from_catalog(ua, del.JobId[i]);
    }
    bsendmsg(ua, _("%d Jobs for client %s purged from %s catalog.\n"), del.num_ids,
-      client->hdr.name, client->catalog->hdr.name);
+      client->name(), client->catalog->name());
 
 bail_out:
    if (del.JobId) {
@@ -423,19 +414,65 @@ bail_out:
    return 1;
 }
 
-void purge_files_from_job(UAContext *ua, JOB_DBR *jr)
+void purge_job_from_catalog(UAContext *ua, JobId_t JobId)
 {
-   POOLMEM *query = get_pool_memory(PM_MESSAGE);
+   POOL_MEM query(PM_MESSAGE);
    char ed1[50];
 
-   edit_int64(jr->JobId, ed1);
-   Mmsg(query, "DELETE FROM File WHERE JobId=%s", ed1);
-   db_sql_query(ua->db, query, NULL, (void *)NULL);
+   /* Delete (or purge) records associated with the job */
+   purge_job_records_from_catalog(ua, JobId);
 
-   Mmsg(query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%s", ed1);
-   db_sql_query(ua->db, query, NULL, (void *)NULL);
+   /* Now remove the Job record itself */
+   edit_int64(JobId, ed1);
+   Mmsg(query, "DELETE FROM Job WHERE JobId=%s", ed1);
+   db_sql_query(ua->db, query.c_str(), NULL, (void *)NULL);
+   Dmsg1(050, "Delete Job sql=%s\n", query.c_str());
+}
+
+/*
+ * This removes all the records associated with a Job from
+ *  the catalog (i.e. prunes it) without removing the Job
+ *  record itself.
+ */
+void purge_job_records_from_catalog(UAContext *ua, JobId_t JobId)
+{
+   POOL_MEM query(PM_MESSAGE);
+   char ed1[50];
+
+   purge_files_from_job(ua, JobId);
+
+   edit_int64(JobId, ed1);
+   Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%s", ed1);
+   db_sql_query(ua->db, query.c_str(), NULL, (void *)NULL);
+   Dmsg1(050, "Delete JobMedia sql=%s\n", query.c_str());
+
+   Mmsg(query, "DELETE FROM Log WHERE JobId=%s", ed1);
+   db_sql_query(ua->db, query.c_str(), NULL, (void *)NULL);
+   Dmsg1(050, "Delete Log sql=%s\n", query.c_str());
 
-   free_pool_memory(query);
+}
+
+
+/*
+ * Remove File records for a particular Job.
+ */
+void purge_files_from_job(UAContext *ua, JobId_t JobId)
+{
+   POOL_MEM query(PM_MESSAGE);
+   char ed1[50];
+
+   edit_int64(JobId, ed1);
+   Mmsg(query, del_File, ed1);
+   db_sql_query(ua->db, query.c_str(), NULL, (void *)NULL);
+
+   /*
+    * Now mark Job as having files purged. This is necessary to
+    * avoid having too many Jobs to process in future prunings. If
+    * we don't do this, the number of JobId's in our in memory list
+    * could grow very large.
+    */
+   Mmsg(query, upd_Purged, ed1);
+   db_sql_query(ua->db, query.c_str(), NULL, (void *)NULL);
 }
 
 void purge_files_from_volume(UAContext *ua, MEDIA_DBR *mr )
@@ -517,15 +554,8 @@ int purge_jobs_from_volume(UAContext *ua, MEDIA_DBR *mr)
    }
 
    for (i=0; i < del.num_ids; i++) {
-      edit_int64(del.JobId[i], ed1);
-      Dmsg1(050, "Delete JobId=%s\n", ed1);
-      Mmsg(query, "DELETE FROM File WHERE JobId=%s", ed1);
-      db_sql_query(ua->db, query, NULL, (void *)NULL);
-      Mmsg(query, "DELETE FROM Job WHERE JobId=%s", ed1);
-      db_sql_query(ua->db, query, NULL, (void *)NULL);
-      Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%s", ed1);
-      db_sql_query(ua->db, query, NULL, (void *)NULL);
-      Dmsg1(050, "Del sql=%s\n", query);
+      purge_files_from_job(ua, del.JobId[i]);
+      purge_job_from_catalog(ua, del.JobId[i]);
       del.num_del++;
    }
    if (del.JobId) {
@@ -575,6 +605,36 @@ bool mark_media_purged(UAContext *ua, MEDIA_DBR *mr)
       }
       pm_strcpy(jcr->VolumeName, mr->VolumeName);
       generate_job_event(jcr, "VolumePurged");
+      /*
+       * If the RecyclePool is defined, move the volume there
+       */
+      if (mr->RecyclePoolId && mr->RecyclePoolId != mr->PoolId) {
+         POOL_DBR oldpr, newpr;
+         memset(&oldpr, 0, sizeof(POOL_DBR));
+         memset(&newpr, 0, sizeof(POOL_DBR));
+         newpr.PoolId = mr->RecyclePoolId;
+         oldpr.PoolId = mr->PoolId;
+         if (   db_get_pool_record(jcr, ua->db, &oldpr) 
+             && db_get_pool_record(jcr, ua->db, &newpr)) 
+         {
+            /* check if destination pool size is ok */
+            if (newpr.MaxVols > 0 && newpr.NumVols >= newpr.MaxVols) {
+               bsendmsg(ua, _("Unable move recycled Volume in full " 
+                              "Pool \"%s\" MaxVols=%d\n"),
+                        newpr.Name, newpr.MaxVols);
+
+            } else {            /* move media */
+               update_vol_pool(ua, newpr.Name, mr, &oldpr);
+            }
+         } else {
+            bsendmsg(ua, "%s", db_strerror(ua->db));
+         }
+      }
+      /* Send message to Job report, if it is a *real* job */           
+      if (jcr && jcr->JobId > 0) {
+         Jmsg1(jcr, M_INFO, 0, _("All records pruned from Volume \"%s\"; marking it \"Purged\"\n"),
+            mr->VolumeName); 
+      }
       return true;
    } else {
       bsendmsg(ua, _("Cannot purge Volume with VolStatus=%s\n"), mr->VolStatus);