X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=bacula%2Fsrc%2Fdird%2Fua_restore.c;h=0f20e3a51264fdbab9ba6e01f3b18d3acb5bdf76;hb=7ebf8f564b27ca8448a9a7365ba73b130ae69c21;hp=75d8277cb7e7bbcbd7b557ff8788b578c034fbde;hpb=490140e6fcffe6d4aa5bdbba769e2a98890c8595;p=bacula%2Fbacula diff --git a/bacula/src/dird/ua_restore.c b/bacula/src/dird/ua_restore.c index 75d8277cb7..0f20e3a512 100644 --- a/bacula/src/dird/ua_restore.c +++ b/bacula/src/dird/ua_restore.c @@ -45,9 +45,9 @@ extern void print_bsr(UAContext *ua, RBSR *bsr); 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_inc, *uar_list_temp, *uar_sel_jobid_temp; extern char *uar_sel_all_temp1, *uar_sel_fileset, *uar_mediatype; -extern char *uar_jobid_fileindex; +extern char *uar_jobid_fileindex, *uar_dif, *uar_sel_all_temp; struct NAME_LIST { @@ -74,12 +74,13 @@ struct RESTORE_CTX { uint32_t selected_files; char *where; RBSR *bsr; - POOLMEM *fname; - POOLMEM *path; + POOLMEM *fname; /* filename only */ + POOLMEM *path; /* path only */ POOLMEM *query; - int fnl; - int pnl; + int fnl; /* filename length */ + int pnl; /* path length */ bool found; + bool all; /* mark all as default */ NAME_LIST name_list; }; @@ -90,7 +91,7 @@ struct RESTORE_CTX { /* Forward referenced functions */ static int last_full_handler(void *ctx, int num_fields, char **row); static int jobid_handler(void *ctx, int num_fields, char **row); -static int next_jobid_from_list(char **p, uint32_t *JobId); +static int get_next_jobid_from_list(char **p, uint32_t *JobId); static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx); static int fileset_handler(void *ctx, int num_fields, char **row); static void print_name_list(UAContext *ua, NAME_LIST *name_list); @@ -115,11 +116,10 @@ static int get_date(UAContext *ua, char *date, int date_len); int restore_cmd(UAContext *ua, char *cmd) { RESTORE_CTX rx; /* restore context */ - JOB *job = NULL; + JOB *job; int i; memset(&rx, 0, sizeof(rx)); - rx.path = get_pool_memory(PM_FNAME); rx.fname = get_pool_memory(PM_FNAME); rx.JobIds = get_pool_memory(PM_FNAME); @@ -137,7 +137,7 @@ int restore_cmd(UAContext *ua, char *cmd) /* Ensure there is at least one Restore Job */ LockRes(); - while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) { + foreach_res(job, R_JOB) { if (job->JobType == JT_RESTORE) { if (!rx.restore_job) { rx.restore_job = job; @@ -177,7 +177,9 @@ int restore_cmd(UAContext *ua, char *cmd) bsendmsg(ua, _("Unable to construct a valid BSR. Cannot continue.\n")); goto bail_out; } - write_bsr_file(ua, rx.bsr); + if (!write_bsr_file(ua, rx.bsr)) { + goto bail_out; + } bsendmsg(ua, _("\n%u file%s selected to be restored.\n\n"), rx.selected_files, rx.selected_files==1?"":"s"); } else { @@ -213,7 +215,7 @@ int restore_cmd(UAContext *ua, char *cmd) job->hdr.name, rx.ClientName, rx.store?rx.store->hdr.name:"", working_directory); } - if (find_arg(ua, _("run")) >= 0 || find_arg(ua, _("yes"))) { + 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); @@ -286,11 +288,11 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx) JobId_t JobId; JOB_DBR jr; bool done = false; - int i; + int i, j; char *list[] = { "List last 20 Jobs run", "List Jobs where a given File is saved", - "Enter list of JobIds to select", + "Enter list of comma separated JobIds to select", "Enter SQL list command", "Select the most recent backup for a client", "Select backup for a client before a specified time", @@ -306,76 +308,99 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx) "file", /* 3 */ "select", /* 4 */ "pool", /* 5 */ + "all", /* 6 */ + "client", /* 7 */ + "storage", /* 8 */ + "fileset", /* 9 */ + "where", /* 10 */ + "yes", /* 11 */ + "done", /* 12 */ NULL }; *rx->JobIds = 0; - switch (find_arg_keyword(ua, kw)) { - case 0: /* jobid */ - for ( ;; ) { - i = find_arg_with_value(ua, _("jobid")); - if (i < 0) { + + for (i=1; iargc; i++) { /* loop through arguments */ + bool found_kw = false; + for (j=0; kw[j]; j++) { /* loop through keywords */ + if (strcasecmp(kw[j], ua->argk[i]) == 0) { + found_kw = true; break; } - pm_strcpy(&rx->JobIds, ua->argv[i]); - ua->argk[i][0] = 0; /* "consume" jobid= */ - } - done = true; - break; - case 1: /* current */ - bstrutime(date, sizeof(date), time(NULL)); - have_date = true; - break; - case 2: /* before */ - i = find_arg_with_value(ua, _("before")); - if (i < 0) { - return 0; } - if (str_to_utime(ua->argv[i]) == 0) { - bsendmsg(ua, _("Improper date format: %s\n"), ua->argv[i]); + if (!found_kw) { + bsendmsg(ua, _("Unknown keyword: %s\n"), ua->argk[i]); return 0; } - bstrncpy(date, ua->argv[i], sizeof(date)); - have_date = true; - break; - case 3: /* file */ - if (!have_date) { + /* Found keyword in kw[] list, process it */ + switch (j) { + case 0: /* jobid */ + if (*rx->JobIds != 0) { + pm_strcat(&rx->JobIds, ","); + } + pm_strcat(&rx->JobIds, ua->argv[i]); + done = true; + break; + case 1: /* current */ bstrutime(date, sizeof(date), time(NULL)); - } - if (!get_client_name(ua, rx)) { - return 0; - } - for ( ;; ) { - i = find_arg_with_value(ua, _("file")); - if (i < 0) { - break; + have_date = true; + break; + case 2: /* before */ + if (str_to_utime(ua->argv[i]) == 0) { + bsendmsg(ua, _("Improper date format: %s\n"), ua->argv[i]); + return 0; + } + bstrncpy(date, ua->argv[i], sizeof(date)); + have_date = true; + break; + case 3: /* file */ + if (!have_date) { + bstrutime(date, sizeof(date), time(NULL)); + } + if (!get_client_name(ua, rx)) { + return 0; } pm_strcpy(&ua->cmd, ua->argv[i]); insert_one_file(ua, rx, date); - ua->argk[i][0] = 0; /* "consume" the file= */ - } - /* Check MediaType and select storage that corresponds */ - get_storage_from_mediatype(ua, &rx->name_list, rx); - return 2; - case 4: /* select */ - if (!have_date) { - bstrutime(date, sizeof(date), time(NULL)); - } - if (!select_backups_before_date(ua, rx, date)) { - return 0; - } - done = true; - break; - case 5: /* pool specified */ - i = find_arg_with_value(ua, "pool"); - if (i >= 0) { + if (rx->name_list.num_ids) { + /* Check MediaType and select storage that corresponds */ + get_storage_from_mediatype(ua, &rx->name_list, rx); + done = true; + } + break; + case 4: /* select */ + if (!have_date) { + bstrutime(date, sizeof(date), time(NULL)); + } + if (!select_backups_before_date(ua, rx, date)) { + return 0; + } + done = true; + break; + case 5: /* pool specified */ rx->pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]); - } else { - bsendmsg(ua, _("Error: Pool resource \"%s\" does not exist.\n"), ua->argv[i]); + if (!rx->pool) { + bsendmsg(ua, _("Error: Pool resource \"%s\" does not exist.\n"), ua->argv[i]); + return 0; + } + if (!acl_access_ok(ua, Pool_ACL, ua->argv[i])) { + rx->pool = NULL; + bsendmsg(ua, _("Error: Pool resource \"%s\" access not allowed.\n"), ua->argv[i]); + return 0; + } + break; + case 6: /* all specified */ + rx->all = true; + break; + /* + * All keywords 7 or greater are ignored or handled by a select prompt + */ + default: + break; } - break; - default: - break; + } + if (rx->name_list.num_ids) { + return 2; /* filename list made */ } if (!done) { @@ -460,7 +485,9 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx) insert_one_file(ua, rx, date); } /* Check MediaType and select storage that corresponds */ - get_storage_from_mediatype(ua, &rx->name_list, rx); + if (rx->name_list.num_ids) { + get_storage_from_mediatype(ua, &rx->name_list, rx); + } return 2; case 7: /* enter files backed up before specified time */ if (!get_date(ua, date, sizeof(date))) { @@ -483,7 +510,9 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx) insert_one_file(ua, rx, date); } /* Check MediaType and select storage that corresponds */ - get_storage_from_mediatype(ua, &rx->name_list, rx); + if (rx->name_list.num_ids) { + get_storage_from_mediatype(ua, &rx->name_list, rx); + } return 2; @@ -503,7 +532,7 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx) rx->TotalFiles = 0; for (p=rx->JobIds; ; ) { - int stat = next_jobid_from_list(&p, &JobId); + int stat = get_next_jobid_from_list(&p, &JobId); if (stat < 0) { bsendmsg(ua, _("Invalid JobId in list.\n")); return 0; @@ -520,6 +549,11 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx) JobId, db_strerror(ua->db)); return 0; } + if (!acl_access_ok(ua, Job_ACL, jr.Name)) { + bsendmsg(ua, _("No authorization. Job \"%s\" not selected.\n"), + jr.Name); + continue; + } rx->TotalFiles += jr.JobFiles; } return 1; @@ -676,13 +710,14 @@ static bool build_directory_tree(UAContext *ua, RESTORE_CTX *rx) tree.root = new_tree(rx->TotalFiles); tree.root->fname = nofname; tree.ua = ua; + tree.all = rx->all; last_JobId = 0; /* * For display purposes, the same JobId, with different volumes may * appear more than once, however, we only insert it once. */ int items = 0; - for (p=rx->JobIds; next_jobid_from_list(&p, &JobId) > 0; ) { + for (p=rx->JobIds; get_next_jobid_from_list(&p, &JobId) > 0; ) { if (JobId == last_JobId) { continue; /* eliminate duplicate JobIds */ @@ -705,13 +740,13 @@ static bool build_directory_tree(UAContext *ua, RESTORE_CTX *rx) bsendmsg(ua, "%s", db_strerror(ua->db)); } } - bsendmsg(ua, "%d Job%s inserted into the tree and marked for extraction.\n", - items, items==1?"":"s"); + bsendmsg(ua, "%d Job%s inserted into the tree%s.\n", + items, items==1?"":"s", tree.all?" and marked for extraction":""); /* Check MediaType and select storage that corresponds */ get_storage_from_mediatype(ua, &rx->name_list, rx); - if (find_arg(ua, _("all")) < 0) { + if (find_arg(ua, _("done")) < 0) { /* Let the user interact in selecting which files to restore */ OK = user_select_files_from_tree(&tree); } @@ -839,8 +874,24 @@ static int select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *date goto bail_out; } - /* Now find all Incremental/Decremental Jobs after Full save */ - Mmsg(&rx->query, uar_inc_dec, edit_uint64(rx->JobTDate, ed1), date, + /* Now find most recent Differental Job after Full save, if any */ + Mmsg(&rx->query, uar_dif, edit_uint64(rx->JobTDate, ed1), date, + cr.ClientId, fsr.FileSet, pool_select); + if (!db_sql_query(ua->db, rx->query, NULL, NULL)) { + bsendmsg(ua, "%s\n", db_strerror(ua->db)); + } + /* Now update JobTDate to lock onto Differental, if any */ + rx->JobTDate = 0; + if (!db_sql_query(ua->db, uar_sel_all_temp, last_full_handler, (void *)rx)) { + bsendmsg(ua, "%s\n", db_strerror(ua->db)); + } + if (rx->JobTDate == 0) { + bsendmsg(ua, _("No Full backup before %s found.\n"), date); + goto bail_out; + } + + /* Now find all Incremental Jobs after Full/dif save */ + Mmsg(&rx->query, uar_inc, edit_uint64(rx->JobTDate, ed1), date, cr.ClientId, fsr.FileSet, pool_select); if (!db_sql_query(ua->db, rx->query, NULL, NULL)) { bsendmsg(ua, "%s\n", db_strerror(ua->db)); @@ -870,7 +921,7 @@ bail_out: /* Return next JobId from comma separated list */ -static int next_jobid_from_list(char **p, uint32_t *JobId) +static int get_next_jobid_from_list(char **p, uint32_t *JobId) { char jobid[30]; char *q = *p; @@ -1008,6 +1059,8 @@ static void free_name_list(NAME_LIST *name_list) static void get_storage_from_mediatype(UAContext *ua, NAME_LIST *name_list, RESTORE_CTX *rx) { + STORE *store; + 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")); @@ -1021,12 +1074,28 @@ static void get_storage_from_mediatype(UAContext *ua, NAME_LIST *name_list, REST rx->store = select_storage_resource(ua); return; } + if (rx->store) { + return; + } + /* + * We have a single MediaType, look it up in our Storage resource + */ + LockRes(); + foreach_res(store, R_STORAGE) { + if (strcmp(name_list->name[0], store->media_type) == 0) { + rx->store = store; + UnlockRes(); + return; + } + } + UnlockRes(); + /* Try asking user */ 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" + "MediaType \"%s\", needed by the Jobs you selected.\n" "You will be allowed to select a Storage device later.\n"), name_list->name[0]); }