]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/ua_restore.c
Add IP address to BSOCK + authentication sets console + tweak run argument handling...
[bacula/bacula] / bacula / src / dird / ua_restore.c
index 36733d0ab2bf6968794de1f106cd19be4bd7f6f8..6b99e2a7cdb910e39643c9f8f9b35e2d78c7995c 100644 (file)
@@ -14,7 +14,7 @@
  */
 
 /*
-   Copyright (C) 2002-2003 Kern Sibbald and John Walker
+   Copyright (C) 2002-2004 Kern Sibbald and John Walker
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -42,11 +42,11 @@ extern int run_cmd(UAContext *ua, char *cmd);
 extern void print_bsr(UAContext *ua, RBSR *bsr);
 
 /* Imported variables */
-extern char *uar_list_jobs,    *uar_file,        *uar_sel_files;
-extern char *uar_del_temp,     *uar_del_temp1,   *uar_create_temp;
-extern char *uar_create_temp1, *uar_last_full,   *uar_full;
-extern char *uar_inc_dec,      *uar_list_temp,   *uar_sel_jobid_temp;
-extern char *uar_sel_all_temp1, *uar_sel_fileset, *uar_mediatype;
+extern char *uar_list_jobs,     *uar_file,        *uar_sel_files;
+extern char *uar_del_temp,      *uar_del_temp1,   *uar_create_temp;
+extern char *uar_create_temp1,  *uar_last_full,   *uar_full;
+extern char *uar_inc_dec,       *uar_list_temp,   *uar_sel_jobid_temp;
+extern char *uar_sel_all_temp1,  *uar_sel_fileset, *uar_mediatype;
 extern char *uar_jobid_fileindex;
 
 
@@ -69,6 +69,7 @@ struct RESTORE_CTX {
    POOLMEM *JobIds;                  /* User entered string of JobIds */
    STORE  *store;
    JOB *restore_job;
+   POOL *pool;
    int restore_jobs;
    uint32_t selected_files;
    char *where;
@@ -97,7 +98,7 @@ static int unique_name_list_handler(void *ctx, int num_fields, char **row);
 static void free_name_list(NAME_LIST *name_list);
 static void get_storage_from_mediatype(UAContext *ua, NAME_LIST *name_list, RESTORE_CTX *rx);
 static int select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *date);
-static void build_directory_tree(UAContext *ua, RESTORE_CTX *rx);
+static bool build_directory_tree(UAContext *ua, RESTORE_CTX *rx);
 static void free_rx(RESTORE_CTX *rx);
 static void split_path_and_filename(RESTORE_CTX *rx, char *fname);
 static int jobid_fileindex_handler(void *ctx, int num_fields, char **row);
@@ -162,7 +163,10 @@ int restore_cmd(UAContext *ua, char *cmd)
    case 0:
       goto bail_out;
    case 1:                           /* select by jobid */
-      build_directory_tree(ua, &rx);
+      if (!build_directory_tree(ua, &rx)) {
+         bsendmsg(ua, _("Restore not done.\n"));
+        goto bail_out;
+      }
       break;
    case 2:                           /* select by filename, no tree needed */
       break;
@@ -200,17 +204,17 @@ int restore_cmd(UAContext *ua, char *cmd)
    if (rx.where) {
       Mmsg(&ua->cmd, 
           "run job=\"%s\" client=\"%s\" storage=\"%s\" bootstrap=\"%s/restore.bsr\""
-          " where=\"%s\"",
+          " where=\"%s\" files=%d",
           job->hdr.name, rx.ClientName, rx.store?rx.store->hdr.name:"",
-         working_directory, rx.where);
+         working_directory, rx.where, rx.selected_files);
    } else {
       Mmsg(&ua->cmd, 
           "run job=\"%s\" client=\"%s\" storage=\"%s\" bootstrap=\"%s/restore.bsr\"",
           job->hdr.name, rx.ClientName, rx.store?rx.store->hdr.name:"",
          working_directory);
    }
-   if (find_arg(ua, _("run")) >= 0) {
-      pm_strcat(&ua->cmd, " run");    /* pass it on to the run command */
+   if (find_arg(ua, _("yes")) > 0) {
+      pm_strcat(&ua->cmd, " yes");    /* pass it on to the run command */
    }
    Dmsg1(400, "Submitting: %s\n", ua->cmd);
    parse_ua_args(ua);
@@ -301,6 +305,7 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
       "before",    /* 2 */
       "file",      /* 3 */
       "select",    /* 4 */
+      "pool",      /* 5 */
       NULL
    };
 
@@ -361,6 +366,14 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
       }
       done = true;
       break;
+   case 5:                           /* pool specified */
+      i = find_arg_with_value(ua, "pool");
+      if (i >= 0) {
+        rx->pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]);
+      } else {
+         bsendmsg(ua, _("Error: Pool resource \"%s\" does not exist.\n"), ua->argv[i]);
+      }
+      break;
    default:
       break;
    }
@@ -503,7 +516,8 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
       }
       jr.JobId = JobId;
       if (!db_get_job_record(ua->jcr, ua->db, &jr)) {
-         bsendmsg(ua, _("Unable to get Job record. ERR=%s\n"), db_strerror(ua->db));
+         bsendmsg(ua, _("Unable to get Job record for JobId=%u: ERR=%s\n"), 
+           JobId, db_strerror(ua->db));
         return 0;
       }
       rx->TotalFiles += jr.JobFiles;
@@ -511,6 +525,9 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
    return 1;
 }
 
+/* 
+ * Get date from user
+ */
 static int get_date(UAContext *ua, char *date, int date_len)
 {
    bsendmsg(ua, _("The restored files will the most current backup\n"
@@ -528,6 +545,9 @@ static int get_date(UAContext *ua, char *date, int date_len)
    return 1;
 }
 
+/*
+ * Insert a single file, or read a list of files from a file 
+ */
 static void insert_one_file(UAContext *ua, RESTORE_CTX *rx, char *date)
 {
    FILE *ffd;
@@ -641,12 +661,13 @@ static void split_path_and_filename(RESTORE_CTX *rx, char *name)
    Dmsg2(100, "sllit path=%s file=%s\n", rx->path, rx->fname);
 }
 
-static void build_directory_tree(UAContext *ua, RESTORE_CTX *rx)
+static bool build_directory_tree(UAContext *ua, RESTORE_CTX *rx)
 {
    TREE_CTX tree;
    JobId_t JobId, last_JobId;
    char *p;
    char *nofname = "";
+   bool OK = true;
 
    memset(&tree, 0, sizeof(TREE_CTX));
    /* 
@@ -691,24 +712,27 @@ static void build_directory_tree(UAContext *ua, RESTORE_CTX *rx)
    get_storage_from_mediatype(ua, &rx->name_list, rx);
 
    if (find_arg(ua, _("all")) < 0) {
-      /* Let the user select which files to restore */
-      user_select_files_from_tree(&tree);
+      /* Let the user interact in selecting which files to restore */
+      OK = user_select_files_from_tree(&tree);
    }
 
    /*
     * Walk down through the tree finding all files marked to be 
     *  extracted making a bootstrap file.
     */
-   for (TREE_NODE *node=first_tree_node(tree.root); node; node=next_tree_node(node)) {
-      Dmsg2(400, "FI=%d node=0x%x\n", node->FileIndex, node);
-      if (node->extract) {
-         Dmsg2(400, "type=%d FI=%d\n", node->type, node->FileIndex);
-        add_findex(rx->bsr, node->JobId, node->FileIndex);
-        rx->selected_files++;
+   if (OK) {
+      for (TREE_NODE *node=first_tree_node(tree.root); node; node=next_tree_node(node)) {
+         Dmsg2(400, "FI=%d node=0x%x\n", node->FileIndex, node);
+        if (node->extract || node->extract_dir) {
+            Dmsg2(400, "type=%d FI=%d\n", node->type, node->FileIndex);
+           add_findex(rx->bsr, node->JobId, node->FileIndex);
+           rx->selected_files++;
+        }
       }
    }
 
    free_tree(tree.root);             /* free the directory tree */
+   return OK;
 }
 
 
@@ -723,6 +747,7 @@ static int select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *date
    CLIENT_DBR cr;
    char fileset_name[MAX_NAME_LENGTH];
    char ed1[50];
+   char pool_select[MAX_NAME_LENGTH];
    int i;
 
 
@@ -776,9 +801,22 @@ static int select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *date
       }
    }
 
+   /* 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->hdr.name, sizeof(pr.Name));
+      if (db_get_pool_record(ua->jcr, ua->db, &pr)) {
+         bsnprintf(pool_select, sizeof(pool_select), "AND Media.PoolId=%u ", pr.PoolId);
+      } else {
+         bsendmsg(ua, _("Pool \"%s\" not found, using any pool.\n"), pr.Name);
+      }
+   }
 
    /* Find JobId of last Full backup for this client, fileset */
-   Mmsg(&rx->query, uar_last_full, cr.ClientId, cr.ClientId, date, fsr.FileSet);
+   Mmsg(&rx->query, uar_last_full, cr.ClientId, cr.ClientId, date, fsr.FileSet,
+        pool_select);
    if (!db_sql_query(ua->db, rx->query, NULL, NULL)) {
       bsendmsg(ua, "%s\n", db_strerror(ua->db));
       goto bail_out;
@@ -803,7 +841,7 @@ static int select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *date
 
    /* Now find all Incremental/Decremental Jobs after Full save */
    Mmsg(&rx->query, uar_inc_dec, edit_uint64(rx->JobTDate, ed1), date,
-       cr.ClientId, fsr.FileSet);
+       cr.ClientId, fsr.FileSet, pool_select);
    if (!db_sql_query(ua->db, rx->query, NULL, NULL)) {
       bsendmsg(ua, "%s\n", db_strerror(ua->db));
    }
@@ -892,8 +930,7 @@ static int last_full_handler(void *ctx, int num_fields, char **row)
 {
    RESTORE_CTX *rx = (RESTORE_CTX *)ctx;
 
-   rx->JobTDate = strtoll(row[1], NULL, 10);
-
+   rx->JobTDate = str_to_int64(row[1]); 
    return 0;
 }
 
@@ -902,7 +939,6 @@ static int last_full_handler(void *ctx, int num_fields, char **row)
  */
 static int fileset_handler(void *ctx, int num_fields, char **row)
 {
-
    /* row[0] = FileSet (name) */
    if (row[0]) {
       add_prompt((UAContext *)ctx, row[0]);