]> 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 4657662a301ac0b18120e6e9cdc211922f0a542f..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);
@@ -131,8 +132,7 @@ int restore_cmd(UAContext *ua, char *cmd)
    }
 
    if (!open_db(ua)) {
-      free_rx(&rx);
-      return 0;
+      goto bail_out;
    }
 
    /* Ensure there is at least one Restore Job */
@@ -150,8 +150,7 @@ int restore_cmd(UAContext *ua, char *cmd)
       bsendmsg(ua, _(
          "No Restore Job Resource found. You must create at least\n"
          "one before running this command.\n"));
-      free_rx(&rx);
-      return 0;
+      goto bail_out;
    }
 
    /* 
@@ -162,10 +161,12 @@ int restore_cmd(UAContext *ua, char *cmd)
     */
    switch (user_select_jobids_or_files(ua, &rx)) {
    case 0:
-      free_rx(&rx);
-      return 0;                      /* error */
+      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;
@@ -174,16 +175,14 @@ int restore_cmd(UAContext *ua, char *cmd)
    if (rx.bsr->JobId) {
       if (!complete_bsr(ua, rx.bsr)) {  /* find Vol, SessId, SessTime from JobIds */
          bsendmsg(ua, _("Unable to construct a valid BSR. Cannot continue.\n"));
-        free_rx(&rx);
-        return 0;
+        goto bail_out;
       }
       write_bsr_file(ua, rx.bsr);
       bsendmsg(ua, _("\n%u file%s selected to be restored.\n\n"), rx.selected_files,
          rx.selected_files==1?"":"s");
    } else {
       bsendmsg(ua, _("No files selected to be restored.\n"));
-      free_rx(&rx);
-      return 0;
+      goto bail_out;
    }
 
    if (rx.restore_jobs == 1) {
@@ -192,28 +191,30 @@ int restore_cmd(UAContext *ua, char *cmd)
       job = select_restore_job_resource(ua);
    }
    if (!job) {
-      bsendmsg(ua, _("No Restore Job resource found!\n"));
-      free_rx(&rx);
-      return 0;
+      goto bail_out;
    }
 
    get_client_name(ua, &rx);
+   if (!rx.ClientName) {
+      bsendmsg(ua, _("No Restore Job resource found!\n"));
+      goto bail_out;
+   }
 
    /* Build run command */
    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);
@@ -222,6 +223,11 @@ int restore_cmd(UAContext *ua, char *cmd)
    bsendmsg(ua, _("Restore command done.\n"));
    free_rx(&rx);
    return 1;
+
+bail_out:
+   free_rx(&rx);
+   return 0;
+
 }
 
 static void free_rx(RESTORE_CTX *rx) 
@@ -260,7 +266,6 @@ static int get_client_name(UAContext *ua, RESTORE_CTX *rx)
       }
       memset(&cr, 0, sizeof(cr));
       if (!get_client_dbr(ua, &cr)) {
-        free_rx(rx);
         return 0;
       }
       bstrncpy(rx->ClientName, cr.Name, sizeof(rx->ClientName));
@@ -300,6 +305,7 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
       "before",    /* 2 */
       "file",      /* 3 */
       "select",    /* 4 */
+      "pool",      /* 5 */
       NULL
    };
 
@@ -360,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;
    }
@@ -502,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;
@@ -510,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"
@@ -527,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;
@@ -579,7 +600,7 @@ static int insert_file_into_findex_list(UAContext *ua, RESTORE_CTX *rx, char *fi
    }
    rx->selected_files++;
    /*
-    * Find the FileSets for this JobId and add to the name_list
+    * Find the MediaTypes for this JobId and add to the name_list
     */
    Mmsg(&rx->query, uar_mediatype, rx->JobId);
    if (!db_sql_query(ua->db, rx->query, unique_name_list_handler, (void *)&rx->name_list)) {
@@ -640,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));
    /* 
@@ -676,7 +698,7 @@ static void build_directory_tree(UAContext *ua, RESTORE_CTX *rx)
          bsendmsg(ua, "%s", db_strerror(ua->db));
       }
       /*
-       * Find the FileSets for this JobId and add to the name_list
+       * Find the MediaTypes for this JobId and add to the name_list
        */
       Mmsg(&rx->query, uar_mediatype, JobId);
       if (!db_sql_query(ua->db, rx->query, unique_name_list_handler, (void *)&rx->name_list)) {
@@ -690,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;
 }
 
 
@@ -722,6 +747,8 @@ 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;
 
 
    /* Create temp tables */
@@ -743,27 +770,53 @@ static int select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *date
    bstrncpy(rx->ClientName, cr.Name, sizeof(rx->ClientName));
 
    /*
-    * Select FileSet 
+    * Get FileSet 
     */
-   Mmsg(&rx->query, uar_sel_fileset, cr.ClientId, cr.ClientId);
-   start_prompt(ua, _("The defined FileSet resources are:\n"));
-   if (!db_sql_query(ua->db, rx->query, fileset_handler, (void *)ua)) {
-      bsendmsg(ua, "%s\n", db_strerror(ua->db));
+   memset(&fsr, 0, sizeof(fsr));
+   i = find_arg_with_value(ua, "FileSet"); 
+   if (i >= 0) {
+      bstrncpy(fsr.FileSet, ua->argv[i], sizeof(fsr.FileSet));
+      if (!db_get_fileset_record(ua->jcr, ua->db, &fsr)) {
+         bsendmsg(ua, _("Error getting FileSet \"%s\": ERR=%s\n"), fsr.FileSet, 
+           db_strerror(ua->db));
+        i = -1;
+      }
    }
-   if (do_prompt(ua, _("FileSet"), _("Select FileSet resource"), 
+   if (i < 0) {                      /* fileset not found */
+      Mmsg(&rx->query, uar_sel_fileset, cr.ClientId, cr.ClientId);
+      start_prompt(ua, _("The defined FileSet resources are:\n"));
+      if (!db_sql_query(ua->db, rx->query, fileset_handler, (void *)ua)) {
+         bsendmsg(ua, "%s\n", db_strerror(ua->db));
+      }
+      if (do_prompt(ua, _("FileSet"), _("Select FileSet resource"), 
                 fileset_name, sizeof(fileset_name)) < 0) {
-      goto bail_out;
-   }
-   fsr.FileSetId = atoi(fileset_name); /* Id is first part of name */
-   if (!db_get_fileset_record(ua->jcr, ua->db, &fsr)) {
-      bsendmsg(ua, _("Error getting FileSet record: %s\n"), db_strerror(ua->db));
-      bsendmsg(ua, _("This probably means you modified the FileSet.\n"
+        goto bail_out;
+      }
+
+      bstrncpy(fsr.FileSet, fileset_name, sizeof(fsr.FileSet));
+      if (!db_get_fileset_record(ua->jcr, ua->db, &fsr)) {
+         bsendmsg(ua, _("Error getting FileSet record: %s\n"), db_strerror(ua->db));
+         bsendmsg(ua, _("This probably means you modified the FileSet.\n"
                      "Continuing anyway.\n"));
+      }
    }
 
+   /* 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.FileSetId);
+   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;
@@ -788,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.FileSetId);
+       cr.ClientId, fsr.FileSet, pool_select);
    if (!db_sql_query(ua->db, rx->query, NULL, NULL)) {
       bsendmsg(ua, "%s\n", db_strerror(ua->db));
    }
@@ -815,6 +868,7 @@ bail_out:
    return stat;
 }
 
+
 /* Return next JobId from comma separated list */
 static int next_jobid_from_list(char **p, uint32_t *JobId)
 {
@@ -876,20 +930,19 @@ 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;
 }
 
 /*
- * Callback handler build fileset prompt list
+ * Callback handler build FileSet name prompt list
  */
 static int fileset_handler(void *ctx, int num_fields, char **row)
 {
-   char prompt[MAX_NAME_LENGTH+200];
-
-   snprintf(prompt, sizeof(prompt), "%s  %s  %s", row[0], row[1], row[2]);
-   add_prompt((UAContext *)ctx, prompt);
+   /* row[0] = FileSet (name) */
+   if (row[0]) {
+      add_prompt((UAContext *)ctx, row[0]);
+   }
    return 0;
 }
 
@@ -955,9 +1008,6 @@ static void free_name_list(NAME_LIST *name_list)
 
 static void get_storage_from_mediatype(UAContext *ua, NAME_LIST *name_list, RESTORE_CTX *rx)
 {
-   char name[MAX_NAME_LENGTH];
-   STORE *store = NULL;
-
    if (name_list->num_ids > 1) {
       bsendmsg(ua, _("Warning, the JobIds that you selected refer to more than one MediaType.\n"
          "Restore is not possible. The MediaTypes used are:\n"));
@@ -972,16 +1022,8 @@ static void get_storage_from_mediatype(UAContext *ua, NAME_LIST *name_list, REST
       return;
    }
 
-   start_prompt(ua, _("The defined Storage resources are:\n"));
-   LockRes();
-   while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
-      if (strcmp(store->media_type, name_list->name[0]) == 0) {
-        add_prompt(ua, store->hdr.name);
-      }
-   }
-   UnlockRes();
-   do_prompt(ua, _("Storage"),  _("Select Storage resource"), name, sizeof(name));
-   rx->store = (STORE *)GetResWithName(R_STORAGE, name);
+   rx->store = get_storage_resource(ua, false /* don't use default */);
+
    if (!rx->store) {
       bsendmsg(ua, _("\nWarning. Unable to find Storage resource for\n"
          "MediaType %s, needed by the Jobs you selected.\n"