X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;ds=sidebyside;f=bacula%2Fsrc%2Fdird%2Fua_restore.c;h=0f20e3a51264fdbab9ba6e01f3b18d3acb5bdf76;hb=7ebf8f564b27ca8448a9a7365ba73b130ae69c21;hp=36733d0ab2bf6968794de1f106cd19be4bd7f6f8;hpb=59ceb7bda26d458975db740607423080a4443b74;p=bacula%2Fbacula diff --git a/bacula/src/dird/ua_restore.c b/bacula/src/dird/ua_restore.c index 36733d0ab2..0f20e3a512 100644 --- a/bacula/src/dird/ua_restore.c +++ b/bacula/src/dird/ua_restore.c @@ -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,12 +42,12 @@ 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_jobid_fileindex; +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, *uar_list_temp, *uar_sel_jobid_temp; +extern char *uar_sel_all_temp1, *uar_sel_fileset, *uar_mediatype; +extern char *uar_jobid_fileindex, *uar_dif, *uar_sel_all_temp; struct NAME_LIST { @@ -69,16 +69,18 @@ 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; 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; }; @@ -89,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); @@ -97,7 +99,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); @@ -114,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); @@ -136,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; @@ -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; @@ -173,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 { @@ -200,17 +206,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); @@ -282,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", @@ -301,68 +307,100 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx) "before", /* 2 */ "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) { + if (!found_kw) { + bsendmsg(ua, _("Unknown keyword: %s\n"), ua->argk[i]); return 0; } - 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) { + /* 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; + 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]); + 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; } - done = true; - break; - default: - break; + } + if (rx->name_list.num_ids) { + return 2; /* filename list made */ } if (!done) { @@ -447,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))) { @@ -470,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; @@ -490,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; @@ -503,14 +545,23 @@ 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; } + 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; } +/* + * 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 +579,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 +695,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)); /* @@ -655,13 +710,14 @@ static void 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 */ @@ -684,31 +740,34 @@ static void 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) { - /* Let the user select which files to restore */ - user_select_files_from_tree(&tree); + if (find_arg(ua, _("done")) < 0) { + /* 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 +782,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 +836,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; @@ -801,9 +874,25 @@ 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, - cr.ClientId, fsr.FileSet); + /* 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)); } @@ -832,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; @@ -892,8 +981,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 +990,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]); @@ -972,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")); @@ -985,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]); }