]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/ua_restore.c
Fix bug #1307 AllowHigherDuplicates=no prevents automatic job escalation
[bacula/bacula] / bacula / src / dird / ua_restore.c
index f9430b9dad099860eba2fbdb3dab2e8e72d165da..90a475c6b299f2e8d67796d756c4821705288075 100644 (file)
@@ -990,8 +990,12 @@ static void split_path_and_filename(UAContext *ua, RESTORE_CTX *rx, char *name)
 
 static bool ask_for_fileregex(UAContext *ua, RESTORE_CTX *rx)
 {
-   ua->send_msg(_("\nThere were no files inserted into the tree, so file selection\n"
-                  "is not possible.Most likely your retention policy pruned the files\n"));
+   if (find_arg(ua, NT_("all")) >= 0) {  /* if user enters all on command line */
+      return true;                       /* select everything */
+   }
+   ua->send_msg(_("\n\nFor one or more of the JobIds selected, no files were found,\n"
+                 "so file selection is not possible.\n"
+                 "Most likely your retention policy pruned the files.\n"));
    if (get_yesno(ua, _("\nDo you want to restore all the files? (yes|no): "))) {
       if (ua->pint32_val == 1)
          return true;
@@ -1005,8 +1009,9 @@ static bool ask_for_fileregex(UAContext *ua, RESTORE_CTX *rx)
 
             fileregex_re = (regex_t *)bmalloc(sizeof(regex_t));
             rc = regcomp(fileregex_re, ua->cmd, REG_EXTENDED|REG_NOSUB);
-            if (rc != 0)
+            if (rc != 0) {
                regerror(rc, fileregex_re, errmsg, sizeof(errmsg));
+            }
             regfree(fileregex_re);
             free(fileregex_re);
             if (*errmsg) {
@@ -1081,6 +1086,22 @@ static bool build_directory_tree(UAContext *ua, RESTORE_CTX *rx)
       }
    }
 #endif
+   /*
+    * Look at the first JobId on the list (presumably the oldest) and
+    *  if it is marked purged, don't do the manual selection because
+    *  the Job was pruned, so the tree is incomplete.
+    */
+   if (tree.FileCount != 0) {
+      /* Find out if any Job is purged */
+      Mmsg(rx->query, "SELECT SUM(PurgedFiles) FROM Job WHERE JobId IN (%s)", rx->JobIds);
+      if (!db_sql_query(ua->db, rx->query, restore_count_handler, (void *)rx)) {
+         ua->error_msg("%s\n", db_strerror(ua->db));
+      }
+      /* rx->JobId is the PurgedFiles flag */
+      if (rx->found && rx->JobId > 0) {
+         tree.FileCount = 0;           /* set count to zero, no tree selection */
+      }
+   }
    if (tree.FileCount == 0) {
       OK = ask_for_fileregex(ua, rx);
       if (OK) {
@@ -1139,10 +1160,9 @@ static bool select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *dat
    bool ok = false;
    FILESET_DBR fsr;
    CLIENT_DBR cr;
-   POOL_MEM other_filter(PM_MESSAGE);
-   POOL_MEM temp_filter(PM_MESSAGE);
    char fileset_name[MAX_NAME_LENGTH];
    char ed1[50], ed2[50];
+   char pool_select[MAX_NAME_LENGTH];
    int i;
 
    /* Create temp tables */
@@ -1198,32 +1218,23 @@ static bool select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *dat
    }
 
    /* If Pool specified, add PoolId specification */
+   pool_select[0] = 0;
    if (rx->pool) {
       POOL_DBR pr;
       memset(&pr, 0, sizeof(pr));
       bstrncpy(pr.Name, rx->pool->name(), sizeof(pr.Name));
       if (db_get_pool_record(ua->jcr, ua->db, &pr)) {
-         Mmsg(other_filter, " AND Media.PoolId=%s ", 
-              edit_int64(pr.PoolId, ed1));
+         bsnprintf(pool_select, sizeof(pool_select), "AND Media.PoolId=%s ", 
+            edit_int64(pr.PoolId, ed1));
       } else {
          ua->warning_msg(_("Pool \"%s\" not found, using any pool.\n"), pr.Name);
       }
    }
-   /* include copies or not in job selection */
-   if (find_arg(ua, NT_("copies")) > 0) {
-      Mmsg(temp_filter, "%s AND Job.Type IN ('%c', '%c') ", 
-           other_filter.c_str(), (char)JT_BACKUP, (char)JT_JOB_COPY);
-   } else {
-      Mmsg(temp_filter, "%s AND Job.Type = '%c' ", other_filter.c_str(),
-           (char)JT_BACKUP);
-   }
-   pm_strcpy(other_filter, temp_filter.c_str());
 
    /* Find JobId of last Full backup for this client, fileset */
    edit_int64(cr.ClientId, ed1);
    Mmsg(rx->query, uar_last_full, ed1, ed1, date, fsr.FileSet,
-        other_filter.c_str());
-   Dmsg1(0, "sql=%s\n", rx->query);
+         pool_select);
    if (!db_sql_query(ua->db, rx->query, NULL, NULL)) {
       ua->error_msg("%s\n", db_strerror(ua->db));
       goto bail_out;
@@ -1249,13 +1260,12 @@ static bool select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *dat
 
    /* Now find most recent Differental Job after Full save, if any */
    Mmsg(rx->query, uar_dif, edit_uint64(rx->JobTDate, ed1), date,
-        edit_int64(cr.ClientId, ed2), fsr.FileSet, other_filter.c_str());
+        edit_int64(cr.ClientId, ed2), fsr.FileSet, pool_select);
    if (!db_sql_query(ua->db, rx->query, NULL, NULL)) {
       ua->warning_msg("%s\n", db_strerror(ua->db));
    }
    /* Now update JobTDate to lock onto Differental, if any */
    rx->JobTDate = 0;
-   Dmsg1(0, "sql=%s\n", rx->query);
    if (!db_sql_query(ua->db, uar_sel_all_temp, last_full_handler, (void *)rx)) {
       ua->warning_msg("%s\n", db_strerror(ua->db));
    }
@@ -1266,8 +1276,7 @@ static bool select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *dat
 
    /* Now find all Incremental Jobs after Full/dif save */
    Mmsg(rx->query, uar_inc, edit_uint64(rx->JobTDate, ed1), date,
-        edit_int64(cr.ClientId, ed2), fsr.FileSet, other_filter.c_str());
-   Dmsg1(0, "sql=%s\n", rx->query);
+        edit_int64(cr.ClientId, ed2), fsr.FileSet, pool_select);
    if (!db_sql_query(ua->db, rx->query, NULL, NULL)) {
       ua->warning_msg("%s\n", db_strerror(ua->db));
    }
@@ -1280,8 +1289,11 @@ static bool select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *dat
    }
 
    if (rx->JobIds[0] != 0) {
-      /* Display a list of all copies */
-      db_list_copies_records(ua->jcr, ua->db, 0, rx->JobIds, prtit, ua, HORZ_LIST);
+      if (find_arg(ua, NT_("copies")) > 0) {
+         /* Display a list of all copies */
+         db_list_copies_records(ua->jcr, ua->db, 0, rx->JobIds, 
+                                prtit, ua, HORZ_LIST);
+      }
       /* Display a list of Jobs selected for this restore */
       db_list_sql_query(ua->jcr, ua->db, uar_list_temp, prtit, ua, 1, HORZ_LIST);
       ok = true;