]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/ua_prune.c
Keep the same keywords as in previous version
[bacula/bacula] / bacula / src / dird / ua_prune.c
index 6c7ecd999f142e2d446e496c471a01d38286044a..af64c389cff11486f36a96c558e6f12eba0f96af 100644 (file)
@@ -6,7 +6,7 @@
    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
+   modify it under the terms of version three of the GNU Affero General Public
    License as published by the Free Software Foundation and included
    in the file LICENSE.
 
@@ -15,7 +15,7 @@
    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
+   You should have received a copy of the GNU Affero 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.
@@ -74,7 +74,7 @@ int job_delete_handler(void *ctx, int num_fields, char **row)
       return 1;
    }
    del->JobId[del->num_ids] = (JobId_t)str_to_int64(row[0]);
-   Dmsg2(0, "job_delete_handler row=%d val=%d\n", del->num_ids, del->JobId[del->num_ids]);
+   Dmsg2(60, "job_delete_handler row=%d val=%d\n", del->num_ids, del->JobId[del->num_ids]);
    del->PurgedFiles[del->num_ids++] = (char)str_to_int64(row[1]);
    return 0;
 }
@@ -215,6 +215,46 @@ int prune_stats(UAContext *ua, utime_t retention)
    return true;
 }
 
+/* 
+ * Use pool and client specified by user to select jobs to prune 
+ * returns add_from string to add in FROM clause
+ *         add_where string to add in WHERE clause
+ */
+bool prune_set_filter(UAContext *ua, CLIENT *client, POOL *pool, utime_t period,
+                      POOL_MEM *add_from, POOL_MEM *add_where)
+{
+   utime_t now;
+   char ed1[50], ed2[MAX_ESCAPE_NAME_LENGTH]; 
+   POOL_MEM tmp(PM_MESSAGE);
+
+   now = (utime_t)time(NULL);
+   edit_int64(now - period, ed1);
+   Dmsg3(150, "now=%lld period=%lld JobTDate=%s\n", now, period, ed1);
+   Mmsg(tmp, " AND JobTDate < %s ", ed1);
+   pm_strcat(*add_where, tmp.c_str());
+
+   db_lock(ua->db);
+   if (client) { 
+      db_escape_string(ua->jcr, ua->db, ed2, 
+                       client->name(), strlen(client->name()));
+      Mmsg(tmp, " AND Client.Name = '%s' ", ed2);
+      pm_strcat(*add_where, tmp.c_str());
+      pm_strcat(*add_from, " JOIN Client USING (ClientId) ");
+   }
+
+   if (pool) { 
+      db_escape_string(ua->jcr, ua->db, ed2, 
+                       pool->name(), strlen(pool->name()));
+      Mmsg(tmp, " AND Pool.Name = '%s' ", ed2);
+      pm_strcat(*add_where, tmp.c_str());
+      /* Use ON() instead of USING for some old SQLite */
+      pm_strcat(*add_from, " JOIN Pool ON (Job.PoolId = Pool.PoolId) ");
+   }
+   Dmsg2(150, "f=%s w=%s\n", add_from->c_str(), add_where->c_str());
+   db_unlock(ua->db);
+   return true;
+}
+
 /*
  * Prune File records from the database. For any Job which
  * is older than the retention period, we unconditionally delete
@@ -226,41 +266,44 @@ int prune_stats(UAContext *ua, utime_t retention)
  * This routine assumes you want the pruning to be done. All checking
  *  must be done before calling this routine.
  *
- * Note: pool can possibly be NULL.
+ * Note: client or pool can possibly be NULL (not both).
  */
 int prune_files(UAContext *ua, CLIENT *client, POOL *pool)
 {
    struct del_ctx del;
    struct s_count_ctx cnt;
    POOL_MEM query(PM_MESSAGE);
-   utime_t now, period;
-   CLIENT_DBR cr;
-   char ed1[50], ed2[50];
-
-   db_lock(ua->db);
-   memset(&cr, 0, sizeof(cr));
+   POOL_MEM sql_where(PM_MESSAGE);
+   POOL_MEM sql_from(PM_MESSAGE);
+   utime_t period;
+   char ed1[50];
+   
    memset(&del, 0, sizeof(del));
-   bstrncpy(cr.Name, client->name(), sizeof(cr.Name));
-   if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
-      db_unlock(ua->db);
-      return 0;
-   }
 
    if (pool && pool->FileRetention > 0) {
       period = pool->FileRetention;
-   } else {
+
+   } else if (client) {
       period = client->FileRetention;
+
+   } else {                     /* should specify at least pool or client */
+      return false;
+   }
+
+   db_lock(ua->db);
+   /* Specify JobTDate and Pool.Name= and/or Client.Name= in the query */
+   if (!prune_set_filter(ua, client, pool, period, &sql_from, &sql_where)) {
+      goto bail_out;
    }
-   now = (utime_t)time(NULL);
 
 //   edit_utime(now-period, ed1, sizeof(ed1));
 //   Jmsg(ua->jcr, M_INFO, 0, _("Begin pruning Jobs older than %s secs.\n"), ed1);
-   Jmsg(ua->jcr, M_INFO, 0, _("Begin pruning Jobs.\n"));
+   Jmsg(ua->jcr, M_INFO, 0, _("Begin pruning Files.\n"));
    /* Select Jobs -- for counting */ 
-   edit_int64(now - period, ed1);
-   Mmsg(query, count_select_job, ed1, edit_int64(cr.ClientId, ed2));
-   Dmsg3(050, "select now=%u period=%u sql=%s\n", (uint32_t)now, 
-               (uint32_t)period, query.c_str());
+   Mmsg(query, 
+        "SELECT COUNT(1) FROM Job %s WHERE PurgedFiles=0 %s", 
+        sql_from.c_str(), sql_where.c_str());
+   Dmsg1(050, "select sql=%s\n", query.c_str());
    cnt.count = 0;
    if (!db_sql_query(ua->db, query.c_str(), del_count_handler, (void *)&cnt)) {
       ua->error_msg("%s", db_strerror(ua->db));
@@ -285,8 +328,9 @@ int prune_files(UAContext *ua, CLIENT *client, POOL *pool)
    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
 
    /* Now process same set but making a delete list */
-   Mmsg(query, select_job, edit_int64(now - period, ed1), 
-        edit_int64(cr.ClientId, ed2));
+   Mmsg(query, "SELECT JobId FROM Job %s WHERE PurgedFiles=0 %s", 
+        sql_from.c_str(), sql_where.c_str());
+   Dmsg1(050, "select sql=%s\n", query.c_str());
    db_sql_query(ua->db, query.c_str(), file_delete_handler, (void *)&del);
 
    purge_files_from_job_list(ua, del);
@@ -343,37 +387,39 @@ static bool grow_del_list(struct del_ctx *del)
    return true;
 }
 
-struct verify_ctx {
+struct accurate_check_ctx {
    DBId_t ClientId;                   /* Id of client */
    DBId_t FileSetId;                  /* Id of FileSet */ 
 };
 
-/* row: Job.Name, FileSet, Client.Name, FileSetId, ClientId */
+/* row: Job.Name, FileSet, Client.Name, FileSetId, ClientId, Type */
 static int job_select_handler(void *ctx, int num_fields, char **row)
 {
    alist *lst = (alist *)ctx;
-   struct verify_ctx *res;
-
-   if (num_fields != 5) {
-      return 1;
-   }
+   struct accurate_check_ctx *res;
+   ASSERT(num_fields == 6);
 
    /* If this job doesn't exist anymore in the configuration, delete it */
    if (GetResWithName(R_JOB, row[0]) == NULL) {
-      return 1;
+      return 0;
    }
 
    /* If this fileset doesn't exist anymore in the configuration, delete it */
    if (GetResWithName(R_FILESET, row[1]) == NULL) {
-      return 1;
+      return 0;
    }
 
    /* If this client doesn't exist anymore in the configuration, delete it */
    if (GetResWithName(R_CLIENT, row[2]) == NULL) {
-      return 1;
+      return 0;
+   }
+
+   /* Don't compute accurate things for Verify jobs */
+   if (*row[5] == 'V') {
+      return 0;
    }
 
-   res = (struct verify_ctx*) malloc(sizeof(struct verify_ctx));
+   res = (struct accurate_check_ctx*) malloc(sizeof(struct accurate_check_ctx));
    res->FileSetId = str_to_int64(row[3]);
    res->ClientId = str_to_int64(row[4]);
    lst->append(res);
@@ -382,29 +428,6 @@ static int job_select_handler(void *ctx, int num_fields, char **row)
    return 0;
 }
 
-static void dump_del(struct del_ctx *del)
-{
-   POOL_MEM query;
-   for (int i=0; del->num_ids; ) {
-      pm_strcpy(query, "");
-      for (int j=0; j<1000 && del->num_ids>0; j++) {
-         del->num_ids--;
-         if (del->JobId[i] == 0 || ua->jcr->JobId == del->JobId[i]) {
-            Dmsg2(0, "skip JobId[%d]=%d\n", i, (int)del->JobId[i]);
-            i++;
-            continue;
-         }
-         if (*query.c_str() != 0) {
-            pm_strcat(query, ",");
-         }
-         pm_strcat(query, edit_int64(del->JobId[i++], ed1));
-         del->num_del++;
-      }
-      Dmsg1(0, "num_ids=%d\n", del->num_ids);
-      Dmsg1(0, "purge_jobs_from_catalog %s\n", query.c_str());
-   }
-}
-
 /*
  * Pruning Jobs is a bit more complicated than purging Files
  * because we delete Job records only if there is a more current
@@ -419,31 +442,32 @@ static void dump_del(struct del_ctx *del)
  */
 int prune_jobs(UAContext *ua, CLIENT *client, POOL *pool, int JobType)
 {
-   struct del_ctx del;
    POOL_MEM query(PM_MESSAGE);
-   utime_t now, period;
-   CLIENT_DBR cr;
-   char ed1[50], ed2[50];
+   POOL_MEM sql_where(PM_MESSAGE);
+   POOL_MEM sql_from(PM_MESSAGE);
+   utime_t period;
+   char ed1[50];
    alist *jobids_check=NULL;
-   struct verify_ctx *elt;
-   db_list_ctx jobids;
-   JCR *jcr;
-
-   db_lock(ua->db);
-   memset(&cr, 0, sizeof(cr));
-
-   bstrncpy(cr.Name, client->name(), sizeof(cr.Name));
-   if (!db_create_client_record(ua->jcr, ua->db, &cr)) {
-      db_unlock(ua->db);
-      return 0;
-   }
+   struct accurate_check_ctx *elt;
+   db_list_ctx jobids, tempids;
+   JOB_DBR jr;
+   struct del_ctx del;
+   memset(&del, 0, sizeof(del));
 
    if (pool && pool->JobRetention > 0) {
       period = pool->JobRetention;
-   } else {
+
+   } else if (client) {
       period = client->JobRetention;
+
+   } else {                     /* should specify at least pool or client */
+      return false;
+   }
+
+   db_lock(ua->db);
+   if (!prune_set_filter(ua, client, pool, period, &sql_from, &sql_where)) {
+      goto bail_out;
    }
-   now = (utime_t)time(NULL);
 
    /* Drop any previous temporary tables still there */
    drop_temp_tables(ua);
@@ -456,105 +480,113 @@ int prune_jobs(UAContext *ua, CLIENT *client, POOL *pool, int JobType)
    edit_utime(period, ed1, sizeof(ed1));
    Jmsg(ua->jcr, M_INFO, 0, _("Begin pruning Jobs older than %s.\n"), ed1);
 
-   edit_int64(now - period, ed1); /* Jobs older than ed1 are good candidates */
-   edit_int64(cr.ClientId, ed2);
-
-   memset(&del, 0, sizeof(del));
    del.max_ids = 100;
    del.JobId = (JobId_t *)malloc(sizeof(JobId_t) * del.max_ids);
    del.PurgedFiles = (char *)malloc(del.max_ids);
 
-   /* Prune garbage jobs (JobStatus not successful) */
-   Mmsg(query, 
-   "SELECT JobId, PurgedFiles FROM Job "
-    "WHERE ( JobFiles=0 "
-         "OR JobStatus NOT IN ('T', 'W') "
-          ") "
-      "AND JobTDate < %s "
-      "AND ClientId = %s ",
-        ed1, ed2);
-   
-   Dmsg1(150, "Query=%s\n", query.c_str());
-   if (!db_sql_query(ua->db, query.c_str(), job_delete_handler, (void *)&del)) {
-      ua->error_msg("%s", db_strerror(ua->db));
-   }
-
-   /* Prune Admin, Restore, Copy and Migration jobs */
-   Mmsg(query, 
-   "SELECT JobId, PurgedFiles FROM Job "
-    "WHERE Type IN ('D', 'R', 'c', 'm') "
-      "AND JobTDate < %s "
-      "AND ClientId = %s ",
-        ed1, ed2);
-   
-   Dmsg1(150, "Query=%s\n", query.c_str());
-   if (!db_sql_query(ua->db, query.c_str(), job_delete_handler, (void *)&del)) {
-      ua->error_msg("%s", db_strerror(ua->db));
-   }
-
    /*
     * Select all files that are older than the JobRetention period
-    *  and stuff them into the "DeletionCandidates" table.
+    *  and add them into the "DeletionCandidates" table.
     */
-   Mmsg(query, insert_delcand, (char)JobType, ed1, 
-        edit_int64(cr.ClientId, ed2));
+   Mmsg(query, 
+        "INSERT INTO DelCandidates "
+          "SELECT JobId,PurgedFiles,FileSetId,JobFiles,JobStatus "
+            "FROM Job %s "      /* JOIN Pool/Client */
+           "WHERE Type IN ('B', 'C', 'M', 'V',  'D', 'R', 'c', 'm', 'g') "
+             " %s ",            /* Pool/Client + JobTDate */
+        sql_from.c_str(), sql_where.c_str());
+
+   Dmsg1(050, "select sql=%s\n", query.c_str());
    if (!db_sql_query(ua->db, query.c_str(), NULL, (void *)NULL)) {
       if (ua->verbose) {
          ua->error_msg("%s", db_strerror(ua->db));
       }
-      Dmsg0(050, "insert delcand failed\n");
       goto bail_out;
    }
 
    /* Now, for the selection, we discard some of them in order to be always
-    * able to restore files. (ie, last full, last diff, last incr)
+    * able to restore files. (ie, last full, last diff, last incrs)
+    * Note: The DISTINCT could be more useful if we don't get FileSetId
     */
    jobids_check = New(alist(10, owned_by_alist));
    Mmsg(query, 
-        "SELECT DISTINCT Job.Name, FileSet, Client.Name, Job.FileSetId, Job.ClientId "
-          "FROM DelCandidates "
-               "JOIN Job USING (JobId) "
-               "JOIN Client USING (ClientId) "
-               "JOIN FileSet ON (Job.FileSetId = FileSet.FileSetId) "
+"SELECT DISTINCT Job.Name, FileSet, Client.Name, Job.FileSetId, "
+                "Job.ClientId, Job.Type "
+  "FROM DelCandidates "
+       "JOIN Job USING (JobId) "
+       "JOIN Client USING (ClientId) "
+       "JOIN FileSet ON (Job.FileSetId = FileSet.FileSetId) "
+ "WHERE Job.Type IN ('B') "               /* Look only Backup jobs */
+   "AND Job.JobStatus IN ('T', 'W') "     /* Look only useful jobs */
       );
 
+   /* The job_select_handler will skip jobs or filesets that are no longer
+    * in the configuration file. Interesting ClientId/FileSetId will be
+    * added to jobids_check
+    */
    if (!db_sql_query(ua->db, query.c_str(), job_select_handler, jobids_check)) {
       ua->error_msg("%s", db_strerror(ua->db));
    }
 
-   /* For all jobs of this client, we exclude current jobs used for restore or
+   /* For this selection, we exclude current jobs used for restore or
     * accurate. This will prevent to prune the last full backup used for
     * current backup & restore
     */
-   jcr = ua->jcr;
-   jcr->jr.JobLevel = L_INCREMENTAL; /* To find useful jobs, we do like an incremental */
-
+   memset(&jr, 0, sizeof(jr));
+   /* To find useful jobs, we do like an incremental */
+   jr.JobLevel = L_INCREMENTAL; 
    foreach_alist(elt, jobids_check) {
-      jcr->jr.ClientId = elt->ClientId;
-      jcr->jr.FileSetId = elt->FileSetId;
-      db_accurate_get_jobids(jcr, ua->db, &jcr->jr, &jobids);
+      jr.ClientId = elt->ClientId;   /* should be always the same */
+      jr.FileSetId = elt->FileSetId;
+      db_accurate_get_jobids(ua->jcr, ua->db, &jr, &tempids);
+      jobids.cat(tempids);
    }
 
+   /* Discard latest Verify level=InitCatalog job 
+    * TODO: can have multiple fileset
+    */
+   Mmsg(query, 
+        "SELECT JobId, JobTDate "
+          "FROM Job %s "                         /* JOIN Client/Pool */
+         "WHERE Type='V'    AND Level='V' "
+              " %s "                             /* Pool, JobTDate, Client */
+         "ORDER BY JobTDate DESC LIMIT 1", 
+        sql_from.c_str(), sql_where.c_str());
+
+   if (!db_sql_query(ua->db, query.c_str(), db_list_handler, &jobids)) {
+      ua->error_msg("%s", db_strerror(ua->db));
+   }
+
+   /* If we found jobs to exclude from the DelCandidates list, we should
+    * also remove BaseJobs that can be linked with them
+    */
    if (jobids.count > 0) {
-      Dmsg1(0, "jobids to exclude before basejobs = %s\n", jobids.list);
+      Dmsg1(60, "jobids to exclude before basejobs = %s\n", jobids.list);
       /* We also need to exclude all basejobs used */
-      db_get_used_base_jobids(jcr, ua->db, jobids.list, &jobids);
+      db_get_used_base_jobids(ua->jcr, ua->db, jobids.list, &jobids);
+
+      /* Removing useful jobs from the DelCandidates list */
+      Mmsg(query, "DELETE FROM DelCandidates "
+                   "WHERE JobId IN (%s) "        /* JobId used in accurate */
+                     "AND JobFiles!=0",          /* Discard when JobFiles=0 */
+           jobids.list);
 
-      Mmsg(query, "DELETE FROM DelCandidates WHERE JobId IN (%s)", jobids.list);
       if (!db_sql_query(ua->db, query.c_str(), NULL, NULL)) {
          ua->error_msg("%s", db_strerror(ua->db));
+         goto bail_out;         /* Don't continue if the list isn't clean */
       }
-      Dmsg1(0, "jobids to exclude = %s\n", jobids.list);
+      Dmsg1(60, "jobids to exclude = %s\n", jobids.list);
    }
 
-   Mmsg(query, "SELECT DelCandidates.JobId,DelCandidates.PurgedFiles FROM DelCandidates");
-   Dmsg1(150, "Query=%s\n", query.c_str());
+   /* We use DISTINCT because we can have two times the same job */
+   Mmsg(query, 
+        "SELECT DISTINCT DelCandidates.JobId,DelCandidates.PurgedFiles "
+          "FROM DelCandidates");
    if (!db_sql_query(ua->db, query.c_str(), job_delete_handler, (void *)&del)) {
       ua->error_msg("%s", db_strerror(ua->db));
    }
 
-   dump_del(&del);
-//   purge_job_list_from_catalog(ua, del);
+   purge_job_list_from_catalog(ua, del);
 
    if (del.num_del > 0) {
       ua->info_msg(_("Pruned %d %s for client %s from catalog.\n"), del.num_del,