3 * Bacula Director -- User Agent Database restore Command
4 * Creates a bootstrap file for restoring files and
5 * starts the restore job.
7 * Tree handling routines split into ua_tree.c July MMIII.
8 * BSR (bootstrap record) handling routines split into
11 * Kern Sibbald, July MMII
17 Copyright (C) 2002-2005 Kern Sibbald
19 This program is free software; you can redistribute it and/or
20 modify it under the terms of the GNU General Public License as
21 published by the Free Software Foundation; either version 2 of
22 the License, or (at your option) any later version.
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received a copy of the GNU General Public
30 License along with this program; if not, write to the Free
31 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
40 /* Imported functions */
41 extern void print_bsr(UAContext *ua, RBSR *bsr);
43 /* Imported variables */
44 extern char *uar_list_jobs, *uar_file, *uar_sel_files;
45 extern char *uar_del_temp, *uar_del_temp1, *uar_create_temp;
46 extern char *uar_create_temp1, *uar_last_full, *uar_full;
47 extern char *uar_inc, *uar_list_temp, *uar_sel_jobid_temp;
48 extern char *uar_sel_all_temp1, *uar_sel_fileset, *uar_mediatype;
49 extern char *uar_jobid_fileindex, *uar_dif, *uar_sel_all_temp;
50 extern char *uar_count_files, *uar_jobids_fileindex;
54 char **name; /* list of names */
55 int num_ids; /* ids stored */
56 int max_ids; /* size of array */
57 int num_del; /* number deleted */
58 int tot_ids; /* total to process */
62 /* Main structure for obtaining JobIds or Files to be restored */
67 char ClientName[MAX_NAME_LENGTH];
69 POOLMEM *JobIds; /* User entered string of JobIds */
74 uint32_t selected_files;
77 POOLMEM *fname; /* filename only */
78 POOLMEM *path; /* path only */
80 int fnl; /* filename length */
81 int pnl; /* path length */
83 bool all; /* mark all as default */
88 #define MAX_ID_LIST_LEN 1000000
91 /* Forward referenced functions */
92 static int last_full_handler(void *ctx, int num_fields, char **row);
93 static int jobid_handler(void *ctx, int num_fields, char **row);
94 static int get_next_jobid_from_list(char **p, uint32_t *JobId);
95 static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx);
96 static int fileset_handler(void *ctx, int num_fields, char **row);
97 static void print_name_list(UAContext *ua, NAME_LIST *name_list);
98 static int unique_name_list_handler(void *ctx, int num_fields, char **row);
99 static void free_name_list(NAME_LIST *name_list);
100 static void get_storage_from_mediatype(UAContext *ua, NAME_LIST *name_list, RESTORE_CTX *rx);
101 static int select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *date);
102 static bool build_directory_tree(UAContext *ua, RESTORE_CTX *rx);
103 static void free_rx(RESTORE_CTX *rx);
104 static void split_path_and_filename(RESTORE_CTX *rx, char *fname);
105 static int jobid_fileindex_handler(void *ctx, int num_fields, char **row);
106 static int insert_file_into_findex_list(UAContext *ua, RESTORE_CTX *rx, char *file,
108 static void insert_one_file(UAContext *ua, RESTORE_CTX *rx, char *date);
109 static int get_client_name(UAContext *ua, RESTORE_CTX *rx);
110 static int get_date(UAContext *ua, char *date, int date_len);
111 static int count_handler(void *ctx, int num_fields, char **row);
117 int restore_cmd(UAContext *ua, const char *cmd)
119 RESTORE_CTX rx; /* restore context */
123 memset(&rx, 0, sizeof(rx));
124 rx.path = get_pool_memory(PM_FNAME);
125 rx.fname = get_pool_memory(PM_FNAME);
126 rx.JobIds = get_pool_memory(PM_FNAME);
127 rx.query = get_pool_memory(PM_FNAME);
130 i = find_arg_with_value(ua, "where");
132 rx.where = ua->argv[i];
139 /* Ensure there is at least one Restore Job */
141 foreach_res(job, R_JOB) {
142 if (job->JobType == JT_RESTORE) {
143 if (!rx.restore_job) {
144 rx.restore_job = job;
150 if (!rx.restore_jobs) {
152 "No Restore Job Resource found. You must create at least\n"
153 "one before running this command.\n"));
158 * Request user to select JobIds or files by various different methods
159 * last 20 jobs, where File saved, most recent backup, ...
160 * In the end, a list of files are pumped into
163 switch (user_select_jobids_or_files(ua, &rx)) {
166 case 1: /* select by jobid */
167 if (!build_directory_tree(ua, &rx)) {
168 bsendmsg(ua, _("Restore not done.\n"));
172 case 2: /* select by filename, no tree needed */
177 if (!complete_bsr(ua, rx.bsr)) { /* find Vol, SessId, SessTime from JobIds */
178 bsendmsg(ua, _("Unable to construct a valid BSR. Cannot continue.\n"));
181 if (!write_bsr_file(ua, rx.bsr)) {
184 bsendmsg(ua, _("\n%u file%s selected to be restored.\n\n"), rx.selected_files,
185 rx.selected_files==1?"":"s");
187 bsendmsg(ua, _("No files selected to be restored.\n"));
191 if (rx.restore_jobs == 1) {
192 job = rx.restore_job;
194 job = select_restore_job_resource(ua);
200 get_client_name(ua, &rx);
201 if (!rx.ClientName) {
202 bsendmsg(ua, _("No Restore Job resource found!\n"));
206 /* Build run command */
209 "run job=\"%s\" client=\"%s\" storage=\"%s\" bootstrap=\"%s/restore.bsr\""
210 " where=\"%s\" files=%d catalog=\"%s\"",
211 job->hdr.name, rx.ClientName, rx.store?rx.store->hdr.name:"",
212 working_directory, rx.where, rx.selected_files, ua->catalog->hdr.name);
215 "run job=\"%s\" client=\"%s\" storage=\"%s\" bootstrap=\"%s/restore.bsr\""
216 " files=%d catalog=\"%s\"",
217 job->hdr.name, rx.ClientName, rx.store?rx.store->hdr.name:"",
218 working_directory, rx.selected_files, ua->catalog->hdr.name);
220 if (find_arg(ua, _("yes")) > 0) {
221 pm_strcat(ua->cmd, " yes"); /* pass it on to the run command */
223 Dmsg1(400, "Submitting: %s\n", ua->cmd);
225 run_cmd(ua, ua->cmd);
235 static void free_rx(RESTORE_CTX *rx)
240 free_pool_memory(rx->JobIds);
244 free_pool_memory(rx->fname);
248 free_pool_memory(rx->path);
252 free_pool_memory(rx->query);
255 free_name_list(&rx->name_list);
258 static int get_client_name(UAContext *ua, RESTORE_CTX *rx)
260 /* If no client name specified yet, get it now */
261 if (!rx->ClientName[0]) {
263 /* try command line argument */
264 int i = find_arg_with_value(ua, _("client"));
266 bstrncpy(rx->ClientName, ua->argv[i], sizeof(rx->ClientName));
269 memset(&cr, 0, sizeof(cr));
270 if (!get_client_dbr(ua, &cr)) {
273 bstrncpy(rx->ClientName, cr.Name, sizeof(rx->ClientName));
279 * The first step in the restore process is for the user to
280 * select a list of JobIds from which he will subsequently
281 * select which files are to be restored.
283 static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
286 char date[MAX_TIME_LENGTH];
287 bool have_date = false;
292 const char *list[] = {
293 "List last 20 Jobs run",
294 "List Jobs where a given File is saved",
295 "Enter list of comma separated JobIds to select",
296 "Enter SQL list command",
297 "Select the most recent backup for a client",
298 "Select backup for a client before a specified time",
299 "Enter a list of files to restore",
300 "Enter a list of files to restore before a specified time",
323 for (i=1; i<ua->argc; i++) { /* loop through arguments */
324 bool found_kw = false;
325 for (j=0; kw[j]; j++) { /* loop through keywords */
326 if (strcasecmp(kw[j], ua->argk[i]) == 0) {
332 bsendmsg(ua, _("Unknown keyword: %s\n"), ua->argk[i]);
335 /* Found keyword in kw[] list, process it */
338 if (*rx->JobIds != 0) {
339 pm_strcat(rx->JobIds, ",");
341 pm_strcat(rx->JobIds, ua->argv[i]);
344 case 1: /* current */
345 bstrutime(date, sizeof(date), time(NULL));
349 if (str_to_utime(ua->argv[i]) == 0) {
350 bsendmsg(ua, _("Improper date format: %s\n"), ua->argv[i]);
353 bstrncpy(date, ua->argv[i], sizeof(date));
358 bstrutime(date, sizeof(date), time(NULL));
360 if (!get_client_name(ua, rx)) {
363 pm_strcpy(ua->cmd, ua->argv[i]);
364 insert_one_file(ua, rx, date);
365 if (rx->name_list.num_ids) {
366 /* Check MediaType and select storage that corresponds */
367 get_storage_from_mediatype(ua, &rx->name_list, rx);
373 bstrutime(date, sizeof(date), time(NULL));
375 if (!select_backups_before_date(ua, rx, date)) {
380 case 5: /* pool specified */
381 rx->pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]);
383 bsendmsg(ua, _("Error: Pool resource \"%s\" does not exist.\n"), ua->argv[i]);
386 if (!acl_access_ok(ua, Pool_ACL, ua->argv[i])) {
388 bsendmsg(ua, _("Error: Pool resource \"%s\" access not allowed.\n"), ua->argv[i]);
392 case 6: /* all specified */
396 * All keywords 7 or greater are ignored or handled by a select prompt
402 if (rx->name_list.num_ids) {
403 return 2; /* filename list made */
407 bsendmsg(ua, _("\nFirst you select one or more JobIds that contain files\n"
408 "to be restored. You will be presented several methods\n"
409 "of specifying the JobIds. Then you will be allowed to\n"
410 "select which files from those JobIds are to be restored.\n\n"));
413 /* If choice not already made above, prompt */
419 start_prompt(ua, _("To select the JobIds, you have the following choices:\n"));
420 for (int i=0; list[i]; i++) {
421 add_prompt(ua, list[i]);
424 switch (do_prompt(ua, "", _("Select item: "), NULL, 0)) {
427 case 0: /* list last 20 Jobs run */
428 gui_save = ua->jcr->gui;
430 db_list_sql_query(ua->jcr, ua->db, uar_list_jobs, prtit, ua, 1, HORZ_LIST);
431 ua->jcr->gui = gui_save;
434 case 1: /* list where a file is saved */
435 if (!get_cmd(ua, _("Enter Filename (no path):"))) {
438 len = strlen(ua->cmd);
439 fname = (char *)malloc(len * 2 + 1);
440 db_escape_string(fname, ua->cmd, len);
441 Mmsg(rx->query, uar_file, fname);
443 gui_save = ua->jcr->gui;
445 db_list_sql_query(ua->jcr, ua->db, rx->query, prtit, ua, 1, HORZ_LIST);
446 ua->jcr->gui = gui_save;
449 case 2: /* enter a list of JobIds */
450 if (!get_cmd(ua, _("Enter JobId(s), comma separated, to restore: "))) {
453 pm_strcpy(rx->JobIds, ua->cmd);
455 case 3: /* Enter an SQL list command */
456 if (!get_cmd(ua, _("Enter SQL list command: "))) {
459 gui_save = ua->jcr->gui;
461 db_list_sql_query(ua->jcr, ua->db, ua->cmd, prtit, ua, 1, HORZ_LIST);
462 ua->jcr->gui = gui_save;
465 case 4: /* Select the most recent backups */
466 bstrutime(date, sizeof(date), time(NULL));
467 if (!select_backups_before_date(ua, rx, date)) {
471 case 5: /* select backup at specified time */
472 if (!get_date(ua, date, sizeof(date))) {
475 if (!select_backups_before_date(ua, rx, date)) {
479 case 6: /* Enter files */
480 bstrutime(date, sizeof(date), time(NULL));
481 if (!get_client_name(ua, rx)) {
484 bsendmsg(ua, _("Enter file names with paths, or < to enter a filename\n"
485 "containg a list of file names with paths, and terminate\n"
486 "them with a blank line.\n"));
488 if (!get_cmd(ua, _("Enter full filename: "))) {
491 len = strlen(ua->cmd);
495 insert_one_file(ua, rx, date);
497 /* Check MediaType and select storage that corresponds */
498 if (rx->name_list.num_ids) {
499 get_storage_from_mediatype(ua, &rx->name_list, rx);
502 case 7: /* enter files backed up before specified time */
503 if (!get_date(ua, date, sizeof(date))) {
506 if (!get_client_name(ua, rx)) {
509 bsendmsg(ua, _("Enter file names with paths, or < to enter a filename\n"
510 "containg a list of file names with paths, and terminate\n"
511 "them with a blank line.\n"));
513 if (!get_cmd(ua, _("Enter full filename: "))) {
516 len = strlen(ua->cmd);
520 insert_one_file(ua, rx, date);
522 /* Check MediaType and select storage that corresponds */
523 if (rx->name_list.num_ids) {
524 get_storage_from_mediatype(ua, &rx->name_list, rx);
529 case 8: /* Cancel or quit */
534 if (*rx->JobIds == 0) {
535 bsendmsg(ua, _("No Jobs selected.\n"));
538 bsendmsg(ua, _("You have selected the following JobId%s: %s\n"),
539 strchr(rx->JobIds,',')?"s":"",rx->JobIds);
541 memset(&jr, 0, sizeof(JOB_DBR));
544 for (p=rx->JobIds; ; ) {
545 int stat = get_next_jobid_from_list(&p, &JobId);
547 bsendmsg(ua, _("Invalid JobId in list.\n"));
553 if (jr.JobId == JobId) {
554 continue; /* duplicate of last JobId */
557 if (!db_get_job_record(ua->jcr, ua->db, &jr)) {
558 bsendmsg(ua, _("Unable to get Job record for JobId=%u: ERR=%s\n"),
559 JobId, db_strerror(ua->db));
562 if (!acl_access_ok(ua, Job_ACL, jr.Name)) {
563 bsendmsg(ua, _("No authorization. Job \"%s\" not selected.\n"),
567 rx->TotalFiles += jr.JobFiles;
575 static int get_date(UAContext *ua, char *date, int date_len)
577 bsendmsg(ua, _("The restored files will the most current backup\n"
578 "BEFORE the date you specify below.\n\n"));
580 if (!get_cmd(ua, _("Enter date as YYYY-MM-DD HH:MM:SS :"))) {
583 if (str_to_utime(ua->cmd) != 0) {
586 bsendmsg(ua, _("Improper date format.\n"));
588 bstrncpy(date, ua->cmd, date_len);
593 * Insert a single file, or read a list of files from a file
595 static void insert_one_file(UAContext *ua, RESTORE_CTX *rx, char *date)
605 if ((ffd = fopen(p, "r")) == NULL) {
607 bsendmsg(ua, _("Cannot open file %s: ERR=%s\n"),
611 while (fgets(file, sizeof(file), ffd)) {
613 if (!insert_file_into_findex_list(ua, rx, file, date)) {
614 bsendmsg(ua, _("Error occurred on line %d of %s\n"), line, p);
620 insert_file_into_findex_list(ua, rx, ua->cmd, date);
626 * For a given file (path+filename), split into path and file, then
627 * lookup the most recent backup in the catalog to get the JobId
628 * and FileIndex, then insert them into the findex list.
630 static int insert_file_into_findex_list(UAContext *ua, RESTORE_CTX *rx, char *file,
633 strip_trailing_junk(file);
634 split_path_and_filename(rx, file);
635 if (*rx->JobIds == 0) {
636 Mmsg(rx->query, uar_jobid_fileindex, date, rx->path, rx->fname, rx->ClientName);
638 Mmsg(rx->query, uar_jobids_fileindex, rx->JobIds, date,
639 rx->path, rx->fname, rx->ClientName);
642 /* Find and insert jobid and File Index */
643 if (!db_sql_query(ua->db, rx->query, jobid_fileindex_handler, (void *)rx)) {
644 bsendmsg(ua, _("Query failed: %s. ERR=%s\n"),
645 rx->query, db_strerror(ua->db));
648 bsendmsg(ua, _("No database record found for: %s\n"), file);
651 rx->selected_files++;
653 * Find the MediaTypes for this JobId and add to the name_list
655 Mmsg(rx->query, uar_mediatype, rx->JobId);
656 if (!db_sql_query(ua->db, rx->query, unique_name_list_handler, (void *)&rx->name_list)) {
657 bsendmsg(ua, "%s", db_strerror(ua->db));
663 static void split_path_and_filename(RESTORE_CTX *rx, char *name)
667 /* Find path without the filename.
668 * I.e. everything after the last / is a "filename".
669 * OK, maybe it is a directory name, but we treat it like
670 * a filename. If we don't find a / then the whole name
671 * must be a path name (e.g. c:).
673 for (p=f=name; *p; p++) {
675 f = p; /* set pos of last slash */
678 if (*f == '/') { /* did we find a slash? */
679 f++; /* yes, point to filename */
680 } else { /* no, whole thing must be path name */
684 /* If filename doesn't exist (i.e. root directory), we
685 * simply create a blank name consisting of a single
686 * space. This makes handling zero length filenames
691 rx->fname = check_pool_memory_size(rx->fname, rx->fnl+1);
692 memcpy(rx->fname, f, rx->fnl); /* copy filename */
693 rx->fname[rx->fnl] = 0;
701 rx->path = check_pool_memory_size(rx->path, rx->pnl+1);
702 memcpy(rx->path, name, rx->pnl);
703 rx->path[rx->pnl] = 0;
709 Dmsg2(100, "sllit path=%s file=%s\n", rx->path, rx->fname);
712 static bool build_directory_tree(UAContext *ua, RESTORE_CTX *rx)
715 JobId_t JobId, last_JobId;
719 memset(&tree, 0, sizeof(TREE_CTX));
721 * Build the directory tree containing JobIds user selected
723 tree.root = new_tree(rx->TotalFiles);
728 * For display purposes, the same JobId, with different volumes may
729 * appear more than once, however, we only insert it once.
733 tree.FileEstimate = 0;
734 if (get_next_jobid_from_list(&p, &JobId) > 0) {
735 /* Use first JobId as estimate of the number of files to restore */
736 Mmsg(rx->query, uar_count_files, JobId);
737 if (!db_sql_query(ua->db, rx->query, count_handler, (void *)rx)) {
738 bsendmsg(ua, "%s\n", db_strerror(ua->db));
741 /* Add about 25% more than this job for over estimate */
742 tree.FileEstimate = rx->JobId + (rx->JobId >> 2);
743 tree.DeltaCount = rx->JobId/50; /* print 50 ticks */
746 for (p=rx->JobIds; get_next_jobid_from_list(&p, &JobId) > 0; ) {
748 if (JobId == last_JobId) {
749 continue; /* eliminate duplicate JobIds */
752 bsendmsg(ua, _("\nBuilding directory tree for JobId %u ... "), JobId);
755 * Find files for this JobId and insert them in the tree
757 Mmsg(rx->query, uar_sel_files, JobId);
758 if (!db_sql_query(ua->db, rx->query, insert_tree_handler, (void *)&tree)) {
759 bsendmsg(ua, "%s", db_strerror(ua->db));
762 * Find the MediaTypes for this JobId and add to the name_list
764 Mmsg(rx->query, uar_mediatype, JobId);
765 if (!db_sql_query(ua->db, rx->query, unique_name_list_handler, (void *)&rx->name_list)) {
766 bsendmsg(ua, "%s", db_strerror(ua->db));
770 bsendmsg(ua, "\n%d Job%s, %s files inserted into the tree%s.\n",
771 items, items==1?"":"s", edit_uint64_with_commas(tree.FileCount, ec1),
772 tree.all?" and marked for extraction":"");
774 /* Check MediaType and select storage that corresponds */
775 get_storage_from_mediatype(ua, &rx->name_list, rx);
777 if (find_arg(ua, _("done")) < 0) {
778 /* Let the user interact in selecting which files to restore */
779 OK = user_select_files_from_tree(&tree);
783 * Walk down through the tree finding all files marked to be
784 * extracted making a bootstrap file.
787 for (TREE_NODE *node=first_tree_node(tree.root); node; node=next_tree_node(node)) {
788 Dmsg2(400, "FI=%d node=0x%x\n", node->FileIndex, node);
789 if (node->extract || node->extract_dir) {
790 Dmsg2(400, "type=%d FI=%d\n", node->type, node->FileIndex);
791 add_findex(rx->bsr, node->JobId, node->FileIndex);
792 if (node->extract && node->type != TN_NEWDIR) {
793 rx->selected_files++; /* count only saved files */
799 free_tree(tree.root); /* free the directory tree */
805 * This routine is used to get the current backup or a backup
806 * before the specified date.
808 static int select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *date)
813 char fileset_name[MAX_NAME_LENGTH];
815 char pool_select[MAX_NAME_LENGTH];
819 /* Create temp tables */
820 db_sql_query(ua->db, uar_del_temp, NULL, NULL);
821 db_sql_query(ua->db, uar_del_temp1, NULL, NULL);
822 if (!db_sql_query(ua->db, uar_create_temp, NULL, NULL)) {
823 bsendmsg(ua, "%s\n", db_strerror(ua->db));
825 if (!db_sql_query(ua->db, uar_create_temp1, NULL, NULL)) {
826 bsendmsg(ua, "%s\n", db_strerror(ua->db));
829 * Select Client from the Catalog
831 memset(&cr, 0, sizeof(cr));
832 if (!get_client_dbr(ua, &cr)) {
835 bstrncpy(rx->ClientName, cr.Name, sizeof(rx->ClientName));
840 memset(&fsr, 0, sizeof(fsr));
841 i = find_arg_with_value(ua, "FileSet");
843 bstrncpy(fsr.FileSet, ua->argv[i], sizeof(fsr.FileSet));
844 if (!db_get_fileset_record(ua->jcr, ua->db, &fsr)) {
845 bsendmsg(ua, _("Error getting FileSet \"%s\": ERR=%s\n"), fsr.FileSet,
846 db_strerror(ua->db));
850 if (i < 0) { /* fileset not found */
851 Mmsg(rx->query, uar_sel_fileset, cr.ClientId, cr.ClientId);
852 start_prompt(ua, _("The defined FileSet resources are:\n"));
853 if (!db_sql_query(ua->db, rx->query, fileset_handler, (void *)ua)) {
854 bsendmsg(ua, "%s\n", db_strerror(ua->db));
856 if (do_prompt(ua, _("FileSet"), _("Select FileSet resource"),
857 fileset_name, sizeof(fileset_name)) < 0) {
861 bstrncpy(fsr.FileSet, fileset_name, sizeof(fsr.FileSet));
862 if (!db_get_fileset_record(ua->jcr, ua->db, &fsr)) {
863 bsendmsg(ua, _("Error getting FileSet record: %s\n"), db_strerror(ua->db));
864 bsendmsg(ua, _("This probably means you modified the FileSet.\n"
865 "Continuing anyway.\n"));
869 /* If Pool specified, add PoolId specification */
873 memset(&pr, 0, sizeof(pr));
874 bstrncpy(pr.Name, rx->pool->hdr.name, sizeof(pr.Name));
875 if (db_get_pool_record(ua->jcr, ua->db, &pr)) {
876 bsnprintf(pool_select, sizeof(pool_select), "AND Media.PoolId=%u ", pr.PoolId);
878 bsendmsg(ua, _("Pool \"%s\" not found, using any pool.\n"), pr.Name);
882 /* Find JobId of last Full backup for this client, fileset */
883 Mmsg(rx->query, uar_last_full, cr.ClientId, cr.ClientId, date, fsr.FileSet,
885 if (!db_sql_query(ua->db, rx->query, NULL, NULL)) {
886 bsendmsg(ua, "%s\n", db_strerror(ua->db));
890 /* Find all Volumes used by that JobId */
891 if (!db_sql_query(ua->db, uar_full, NULL, NULL)) {
892 bsendmsg(ua, "%s\n", db_strerror(ua->db));
895 /* Note, this is needed because I don't seem to get the callback
896 * from the call just above.
899 if (!db_sql_query(ua->db, uar_sel_all_temp1, last_full_handler, (void *)rx)) {
900 bsendmsg(ua, "%s\n", db_strerror(ua->db));
902 if (rx->JobTDate == 0) {
903 bsendmsg(ua, _("No Full backup before %s found.\n"), date);
907 /* Now find most recent Differental Job after Full save, if any */
908 Mmsg(rx->query, uar_dif, edit_uint64(rx->JobTDate, ed1), date,
909 cr.ClientId, fsr.FileSet, pool_select);
910 if (!db_sql_query(ua->db, rx->query, NULL, NULL)) {
911 bsendmsg(ua, "%s\n", db_strerror(ua->db));
913 /* Now update JobTDate to lock onto Differental, if any */
915 if (!db_sql_query(ua->db, uar_sel_all_temp, last_full_handler, (void *)rx)) {
916 bsendmsg(ua, "%s\n", db_strerror(ua->db));
918 if (rx->JobTDate == 0) {
919 bsendmsg(ua, _("No Full backup before %s found.\n"), date);
923 /* Now find all Incremental Jobs after Full/dif save */
924 Mmsg(rx->query, uar_inc, edit_uint64(rx->JobTDate, ed1), date,
925 cr.ClientId, fsr.FileSet, pool_select);
926 if (!db_sql_query(ua->db, rx->query, NULL, NULL)) {
927 bsendmsg(ua, "%s\n", db_strerror(ua->db));
930 /* Get the JobIds from that list */
932 rx->last_jobid[0] = 0;
933 if (!db_sql_query(ua->db, uar_sel_jobid_temp, jobid_handler, (void *)rx)) {
934 bsendmsg(ua, "%s\n", db_strerror(ua->db));
937 if (rx->JobIds[0] != 0) {
938 /* Display a list of Jobs selected for this restore */
939 db_list_sql_query(ua->jcr, ua->db, uar_list_temp, prtit, ua, 1, HORZ_LIST);
941 bsendmsg(ua, _("No jobs found.\n"));
947 db_sql_query(ua->db, uar_del_temp, NULL, NULL);
948 db_sql_query(ua->db, uar_del_temp1, NULL, NULL);
953 /* Return next JobId from comma separated list */
954 static int get_next_jobid_from_list(char **p, uint32_t *JobId)
960 for (int i=0; i<(int)sizeof(jobid); i++) {
961 if (*q == ',' || *q == 0) {
968 if (jobid[0] == 0 || !is_a_number(jobid)) {
972 *JobId = str_to_int64(jobid);
976 static int count_handler(void *ctx, int num_fields, char **row)
978 RESTORE_CTX *rx = (RESTORE_CTX *)ctx;
979 rx->JobId = str_to_int64(row[0]);
985 * Callback handler to get JobId and FileIndex for files
987 static int jobid_fileindex_handler(void *ctx, int num_fields, char **row)
989 RESTORE_CTX *rx = (RESTORE_CTX *)ctx;
990 rx->JobId = str_to_int64(row[0]);
991 add_findex(rx->bsr, rx->JobId, str_to_int64(row[1]));
997 * Callback handler make list of JobIds
999 static int jobid_handler(void *ctx, int num_fields, char **row)
1001 RESTORE_CTX *rx = (RESTORE_CTX *)ctx;
1003 if (strcmp(rx->last_jobid, row[0]) == 0) {
1004 return 0; /* duplicate id */
1006 bstrncpy(rx->last_jobid, row[0], sizeof(rx->last_jobid));
1007 if (rx->JobIds[0] != 0) {
1008 pm_strcat(rx->JobIds, ",");
1010 pm_strcat(rx->JobIds, row[0]);
1016 * Callback handler to pickup last Full backup JobTDate
1018 static int last_full_handler(void *ctx, int num_fields, char **row)
1020 RESTORE_CTX *rx = (RESTORE_CTX *)ctx;
1022 rx->JobTDate = str_to_int64(row[1]);
1027 * Callback handler build FileSet name prompt list
1029 static int fileset_handler(void *ctx, int num_fields, char **row)
1031 /* row[0] = FileSet (name) */
1033 add_prompt((UAContext *)ctx, row[0]);
1039 * Called here with each name to be added to the list. The name is
1040 * added to the list if it is not already in the list.
1042 * Used to make unique list of FileSets and MediaTypes
1044 static int unique_name_list_handler(void *ctx, int num_fields, char **row)
1046 NAME_LIST *name = (NAME_LIST *)ctx;
1048 if (name->num_ids == MAX_ID_LIST_LEN) {
1051 if (name->num_ids == name->max_ids) {
1052 if (name->max_ids == 0) {
1053 name->max_ids = 1000;
1054 name->name = (char **)bmalloc(sizeof(char *) * name->max_ids);
1056 name->max_ids = (name->max_ids * 3) / 2;
1057 name->name = (char **)brealloc(name->name, sizeof(char *) * name->max_ids);
1060 for (int i=0; i<name->num_ids; i++) {
1061 if (strcmp(name->name[i], row[0]) == 0) {
1062 return 0; /* already in list, return */
1065 /* Add new name to list */
1066 name->name[name->num_ids++] = bstrdup(row[0]);
1072 * Print names in the list
1074 static void print_name_list(UAContext *ua, NAME_LIST *name_list)
1076 for (int i=0; i < name_list->num_ids; i++) {
1077 bsendmsg(ua, "%s\n", name_list->name[i]);
1083 * Free names in the list
1085 static void free_name_list(NAME_LIST *name_list)
1087 for (int i=0; i < name_list->num_ids; i++) {
1088 free(name_list->name[i]);
1090 if (name_list->name) {
1091 free(name_list->name);
1092 name_list->name = NULL;
1094 name_list->max_ids = 0;
1095 name_list->num_ids = 0;
1098 static void get_storage_from_mediatype(UAContext *ua, NAME_LIST *name_list, RESTORE_CTX *rx)
1102 if (name_list->num_ids > 1) {
1103 bsendmsg(ua, _("Warning, the JobIds that you selected refer to more than one MediaType.\n"
1104 "Restore is not possible. The MediaTypes used are:\n"));
1105 print_name_list(ua, name_list);
1106 rx->store = select_storage_resource(ua);
1110 if (name_list->num_ids == 0) {
1111 bsendmsg(ua, _("No MediaType found for your JobIds.\n"));
1112 rx->store = select_storage_resource(ua);
1119 * We have a single MediaType, look it up in our Storage resource
1122 foreach_res(store, R_STORAGE) {
1123 if (strcmp(name_list->name[0], store->media_type) == 0) {
1124 if (acl_access_ok(ua, Storage_ACL, store->hdr.name)) {
1133 /* Check if an explicit storage resource is given */
1135 int i = find_arg_with_value(ua, "storage");
1137 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1138 if (store && !acl_access_ok(ua, Storage_ACL, store->hdr.name)) {
1142 if (store && (store != rx->store)) {
1143 bsendmsg(ua, _("Warning default storage overridden by %s on command line.\n"),
1150 /* Take command line arg, or ask user if none */
1151 rx->store = get_storage_resource(ua, false /* don't use default */);
1154 bsendmsg(ua, _("\nWarning. Unable to find Storage resource for\n"
1155 "MediaType \"%s\", needed by the Jobs you selected.\n"
1156 "You will be allowed to select a Storage device later.\n"),
1157 name_list->name[0]);