/
ua_restore.c
1647 lines (1503 loc) · 49.6 KB
/
ua_restore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2002-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2013 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* BAREOS Director -- User Agent Database restore Command
* Creates a bootstrap file for restoring files and
* starts the restore job.
*
* Tree handling routines split into ua_tree.c July MMIII.
* BSR (bootstrap record) handling routines split into
* bsr.c July MMIII
*
* Kern Sibbald, July MMII
*/
#include "bareos.h"
#include "dird.h"
/* Imported functions */
extern void print_bsr(UAContext *ua, RBSR *bsr);
/* 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 user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx);
static int fileset_handler(void *ctx, int num_fields, char **row);
static void free_name_list(NAME_LIST *name_list);
static bool select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *date);
static bool build_directory_tree(UAContext *ua, RESTORE_CTX *rx);
static void free_rx(RESTORE_CTX *rx);
static void split_path_and_filename(UAContext *ua, RESTORE_CTX *rx, char *fname);
static int jobid_fileindex_handler(void *ctx, int num_fields, char **row);
static bool insert_file_into_findex_list(UAContext *ua, RESTORE_CTX *rx, char *file,
char *date);
static bool insert_dir_into_findex_list(UAContext *ua, RESTORE_CTX *rx, char *dir,
char *date);
static void insert_one_file_or_dir(UAContext *ua, RESTORE_CTX *rx, char *date, bool dir);
static bool get_client_name(UAContext *ua, RESTORE_CTX *rx);
static bool get_restore_client_name(UAContext *ua, RESTORE_CTX &rx);
static bool get_date(UAContext *ua, char *date, int date_len);
static int restore_count_handler(void *ctx, int num_fields, char **row);
static bool insert_table_into_findex_list(UAContext *ua, RESTORE_CTX *rx, char *table);
static void get_and_display_basejobs(UAContext *ua, RESTORE_CTX *rx);
/*
* Restore files
*/
bool restore_cmd(UAContext *ua, const char *cmd)
{
RESTORE_CTX rx; /* restore context */
POOL_MEM buf;
JOBRES *job;
int i;
JCR *jcr = ua->jcr;
char *escaped_bsr_name = NULL;
char *escaped_where_name = NULL;
char *strip_prefix, *add_prefix, *add_suffix, *regexp;
strip_prefix = add_prefix = add_suffix = regexp = NULL;
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);
rx.JobIds[0] = 0;
rx.BaseJobIds = get_pool_memory(PM_FNAME);
rx.query = get_pool_memory(PM_FNAME);
rx.bsr = new_bsr();
i = find_arg_with_value(ua, "comment");
if (i >= 0) {
rx.comment = ua->argv[i];
if (!is_comment_legal(ua, rx.comment)) {
goto bail_out;
}
}
i = find_arg_with_value(ua, "backupformat");
if (i >= 0) {
rx.backup_format = ua->argv[i];
}
i = find_arg_with_value(ua, "where");
if (i >= 0) {
rx.where = ua->argv[i];
}
i = find_arg_with_value(ua, "replace");
if (i >= 0) {
rx.replace = ua->argv[i];
}
i = find_arg_with_value(ua, "pluginoptions");
if (i >= 0) {
rx.plugin_options = ua->argv[i];
}
i = find_arg_with_value(ua, "strip_prefix");
if (i >= 0) {
strip_prefix = ua->argv[i];
}
i = find_arg_with_value(ua, "add_prefix");
if (i >= 0) {
add_prefix = ua->argv[i];
}
i = find_arg_with_value(ua, "add_suffix");
if (i >= 0) {
add_suffix = ua->argv[i];
}
i = find_arg_with_value(ua, "regexwhere");
if (i >= 0) {
rx.RegexWhere = ua->argv[i];
}
if (strip_prefix || add_suffix || add_prefix) {
int len = bregexp_get_build_where_size(strip_prefix, add_prefix, add_suffix);
regexp = (char *)bmalloc(len * sizeof(char));
bregexp_build_where(regexp, len, strip_prefix, add_prefix, add_suffix);
rx.RegexWhere = regexp;
}
/* TODO: add acl for regexwhere ? */
if (rx.RegexWhere) {
if (!acl_access_ok(ua, Where_ACL, rx.RegexWhere, true)) {
ua->error_msg(_("\"RegexWhere\" specification not authorized.\n"));
goto bail_out;
}
}
if (rx.where) {
if (!acl_access_ok(ua, Where_ACL, rx.where, true)) {
ua->error_msg(_("\"where\" specification not authorized.\n"));
goto bail_out;
}
}
if (!open_client_db(ua, true)) {
goto bail_out;
}
/* Ensure there is at least one Restore Job */
LockRes();
foreach_res(job, R_JOB) {
if (job->JobType == JT_RESTORE) {
if (!rx.restore_job) {
rx.restore_job = job;
}
rx.restore_jobs++;
}
}
UnlockRes();
if (!rx.restore_jobs) {
ua->error_msg(_(
"No Restore Job Resource found in bareos-dir.conf.\n"
"You must create at least one before running this command.\n"));
goto bail_out;
}
/*
* Request user to select JobIds or files by various different methods
* last 20 jobs, where File saved, most recent backup, ...
* In the end, a list of files are pumped into
* add_findex()
*/
switch (user_select_jobids_or_files(ua, &rx)) {
case 0: /* error */
goto bail_out;
case 1: /* selected by jobid */
get_and_display_basejobs(ua, &rx);
if (!build_directory_tree(ua, &rx)) {
ua->send_msg(_("Restore not done.\n"));
goto bail_out;
}
break;
case 2: /* selected by filename, no tree needed */
break;
}
if (rx.bsr->JobId) {
char ed1[50];
if (!complete_bsr(ua, rx.bsr)) { /* find Vol, SessId, SessTime from JobIds */
ua->error_msg(_("Unable to construct a valid BSR. Cannot continue.\n"));
goto bail_out;
}
if (!(rx.selected_files = write_bsr_file(ua, rx))) {
ua->warning_msg(_("No files selected to be restored.\n"));
goto bail_out;
}
display_bsr_info(ua, rx); /* display vols needed, etc */
if (rx.selected_files==1) {
ua->info_msg(_("\n1 file selected to be restored.\n\n"));
} else {
ua->info_msg(_("\n%s files selected to be restored.\n\n"),
edit_uint64_with_commas(rx.selected_files, ed1));
}
} else {
ua->warning_msg(_("No files selected to be restored.\n"));
goto bail_out;
}
if (rx.restore_jobs == 1) {
job = rx.restore_job;
} else {
job = get_restore_job(ua);
}
if (!job) {
goto bail_out;
}
if (!get_client_name(ua, &rx)) {
goto bail_out;
}
if (!rx.ClientName) {
ua->error_msg(_("No Client resource found!\n"));
goto bail_out;
}
if (!get_restore_client_name(ua, rx)) {
goto bail_out;
}
escaped_bsr_name = escape_filename(jcr->RestoreBootstrap);
Mmsg(ua->cmd,
"run job=\"%s\" client=\"%s\" restoreclient=\"%s\" storage=\"%s\""
" bootstrap=\"%s\" files=%u catalog=\"%s\"",
job->name(), rx.ClientName, rx.RestoreClientName,
rx.store?rx.store->name():"",
escaped_bsr_name ? escaped_bsr_name : jcr->RestoreBootstrap,
rx.selected_files, ua->catalog->name());
/*
* Build run command
*/
if (rx.backup_format) {
Mmsg(buf, " backupformat=%s", rx.backup_format);
pm_strcat(ua->cmd, buf);
}
pm_strcpy(buf, "");
if (rx.RegexWhere) {
escaped_where_name = escape_filename(rx.RegexWhere);
Mmsg(buf, " regexwhere=\"%s\"",
escaped_where_name ? escaped_where_name : rx.RegexWhere);
} else if (rx.where) {
escaped_where_name = escape_filename(rx.where);
Mmsg(buf," where=\"%s\"",
escaped_where_name ? escaped_where_name : rx.where);
}
pm_strcat(ua->cmd, buf);
if (rx.replace) {
Mmsg(buf, " replace=%s", rx.replace);
pm_strcat(ua->cmd, buf);
}
if (rx.plugin_options) {
Mmsg(buf, " pluginoptions=%s", rx.plugin_options);
pm_strcat(ua->cmd, buf);
}
if (rx.comment) {
Mmsg(buf, " comment=\"%s\"", rx.comment);
pm_strcat(ua->cmd, buf);
}
if (escaped_bsr_name != NULL) {
bfree(escaped_bsr_name);
}
if (escaped_where_name != NULL) {
bfree(escaped_where_name);
}
if (regexp) {
bfree(regexp);
}
if (find_arg(ua, NT_("yes")) > 0) {
pm_strcat(ua->cmd, " yes"); /* pass it on to the run command */
}
Dmsg1(200, "Submitting: %s\n", ua->cmd);
/*
* Transfer jobids to jcr to for picking up restore objects
*/
jcr->JobIds = rx.JobIds;
rx.JobIds = NULL;
parse_ua_args(ua);
run_cmd(ua, ua->cmd);
free_rx(&rx);
garbage_collect_memory(); /* release unused memory */
return true;
bail_out:
if (escaped_bsr_name != NULL) {
bfree(escaped_bsr_name);
}
if (escaped_where_name != NULL) {
bfree(escaped_where_name);
}
if (regexp) {
bfree(regexp);
}
free_rx(&rx);
garbage_collect_memory(); /* release unused memory */
return false;
}
/*
* Fill the rx->BaseJobIds and display the list
*/
static void get_and_display_basejobs(UAContext *ua, RESTORE_CTX *rx)
{
db_list_ctx jobids;
if (!db_get_used_base_jobids(ua->jcr, ua->db, rx->JobIds, &jobids)) {
ua->warning_msg("%s", db_strerror(ua->db));
}
if (jobids.count) {
POOL_MEM q;
Mmsg(q, uar_print_jobs, jobids.list);
ua->send_msg(_("The restore will use the following job(s) as Base\n"));
db_list_sql_query(ua->jcr, ua->db, q.c_str(), ua->send, HORZ_LIST, true);
}
pm_strcpy(rx->BaseJobIds, jobids.list);
}
static void free_rx(RESTORE_CTX *rx)
{
free_bsr(rx->bsr);
rx->bsr = NULL;
if (rx->ClientName) {
free(rx->ClientName);
rx->ClientName = NULL;
}
if (rx->RestoreClientName) {
free(rx->RestoreClientName);
rx->RestoreClientName = NULL;
}
free_and_null_pool_memory(rx->JobIds);
free_and_null_pool_memory(rx->BaseJobIds);
free_and_null_pool_memory(rx->fname);
free_and_null_pool_memory(rx->path);
free_and_null_pool_memory(rx->query);
free_name_list(&rx->name_list);
}
static bool has_value(UAContext *ua, int i)
{
if (!ua->argv[i]) {
ua->error_msg(_("Missing value for keyword: %s\n"), ua->argk[i]);
return false;
}
return true;
}
/*
* This gets the client name from which the backup was made
*/
static bool get_client_name(UAContext *ua, RESTORE_CTX *rx)
{
int i;
CLIENT_DBR cr;
memset(&cr, 0, sizeof(cr));
/*
* If no client name specified yet, get it now
*/
if (!rx->ClientName) {
/*
* Try command line argument
*/
i = find_arg_with_value(ua, NT_("client"));
if (i < 0) {
i = find_arg_with_value(ua, NT_("backupclient"));
}
if (i >= 0) {
if (!is_name_valid(ua->argv[i], &ua->errmsg)) {
ua->error_msg("%s argument: %s", ua->argk[i], ua->errmsg);
return false;
}
bstrncpy(cr.Name, ua->argv[i], sizeof(cr.Name));
if (!db_get_client_record(ua->jcr, ua->db, &cr)) {
ua->error_msg("invalid %s argument: %s\n", ua->argk[i], ua->argv[i]);
return false;
}
rx->ClientName = bstrdup(ua->argv[i]);
return true;
}
if (!get_client_dbr(ua, &cr)) {
return false;
}
rx->ClientName = bstrdup(cr.Name);
}
return true;
}
/*
* This is where we pick up a client name to restore to.
*/
static bool get_restore_client_name(UAContext *ua, RESTORE_CTX &rx)
{
int i;
/*
* Try command line argument
*/
i = find_arg_with_value(ua, NT_("restoreclient"));
if (i >= 0) {
if (!is_name_valid(ua->argv[i], &ua->errmsg)) {
ua->error_msg("%s argument: %s", ua->argk[i], ua->errmsg);
return false;
}
if (!GetClientResWithName(ua->argv[i])) {
ua->error_msg("invalid %s argument: %s\n", ua->argk[i], ua->argv[i]);
return false;
}
rx.RestoreClientName = bstrdup(ua->argv[i]);
return true;
}
rx.RestoreClientName = bstrdup(rx.ClientName);
return true;
}
/*
* The first step in the restore process is for the user to
* select a list of JobIds from which he will subsequently
* select which files are to be restored.
*
* Returns: 2 if filename list made
* 1 if jobid list made
* 0 on error
*/
static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
{
char *p;
char date[MAX_TIME_LENGTH];
bool have_date = false;
/* Include current second if using current time */
utime_t now = time(NULL) + 1;
JobId_t JobId;
JOB_DBR jr = { (JobId_t)-1 };
bool done = false;
int i, j;
const char *list[] = {
_("List last 20 Jobs run"),
_("List Jobs where a given File is saved"),
_("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"),
_("Enter a list of files to restore"),
_("Enter a list of files to restore before a specified time"),
_("Find the JobIds of the most recent backup for a client"),
_("Find the JobIds for a backup for a client before a specified time"),
_("Enter a list of directories to restore for found JobIds"),
_("Select full restore to a specified Job date"),
_("Cancel"),
NULL
};
const char *kw[] = {
/*
* These keywords are handled in a for loop
*/
"jobid", /* 0 */
"current", /* 1 */
"before", /* 2 */
"file", /* 3 */
"directory", /* 4 */
"select", /* 5 */
"pool", /* 6 */
"all", /* 7 */
/*
* The keyword below are handled by individual arg lookups
*/
"client", /* 8 */
"storage", /* 9 */
"fileset", /* 10 */
"where", /* 11 */
"yes", /* 12 */
"bootstrap", /* 13 */
"done", /* 14 */
"strip_prefix", /* 15 */
"add_prefix", /* 16 */
"add_suffix", /* 17 */
"regexwhere", /* 18 */
"restoreclient", /* 19 */
"copies", /* 20 */
"comment", /* 21 */
"restorejob", /* 22 */
"replace", /* 23 */
"pluginoptions", /* 24 */
NULL
};
rx->JobIds[0] = 0;
for (i = 1; i<ua->argc; i++) { /* loop through arguments */
bool found_kw = false;
for (j = 0; kw[j]; j++) { /* loop through keywords */
if (bstrcasecmp(kw[j], ua->argk[i])) {
found_kw = true;
break;
}
}
if (!found_kw) {
ua->error_msg(_("Unknown keyword: %s\n"), ua->argk[i]);
return 0;
}
/* Found keyword in kw[] list, process it */
switch (j) {
case 0: /* jobid */
if (!has_value(ua, i)) {
return 0;
}
if (*rx->JobIds != 0) {
pm_strcat(rx->JobIds, ",");
}
pm_strcat(rx->JobIds, ua->argv[i]);
done = true;
break;
case 1: /* current */
/*
* Note, we add one second here just to include any job
* that may have finished within the current second,
* which happens a lot in scripting small jobs.
*/
bstrutime(date, sizeof(date), now);
have_date = true;
break;
case 2: /* before */
if (have_date || !has_value(ua, i)) {
return 0;
}
if (str_to_utime(ua->argv[i]) == 0) {
ua->error_msg(_("Improper date format: %s\n"), ua->argv[i]);
return 0;
}
bstrncpy(date, ua->argv[i], sizeof(date));
have_date = true;
break;
case 3: /* file */
case 4: /* dir */
if (!has_value(ua, i)) {
return 0;
}
if (!have_date) {
bstrutime(date, sizeof(date), now);
}
if (!get_client_name(ua, rx)) {
return 0;
}
pm_strcpy(ua->cmd, ua->argv[i]);
insert_one_file_or_dir(ua, rx, date, j==4);
return 2;
case 5: /* select */
if (!have_date) {
bstrutime(date, sizeof(date), now);
}
if (!select_backups_before_date(ua, rx, date)) {
return 0;
}
done = true;
break;
case 6: /* pool specified */
if (!has_value(ua, i)) {
return 0;
}
rx->pool = (POOLRES *)GetResWithName(R_POOL, ua->argv[i]);
if (!rx->pool) {
ua->error_msg(_("Error: Pool resource \"%s\" does not exist.\n"), ua->argv[i]);
return 0;
}
if (!acl_access_ok(ua, Pool_ACL, ua->argv[i], true)) {
rx->pool = NULL;
ua->error_msg(_("Error: Pool resource \"%s\" access not allowed.\n"), ua->argv[i]);
return 0;
}
break;
case 7: /* all specified */
rx->all = true;
break;
default:
/*
* All keywords 7 or greater are ignored or handled by a select prompt
*/
break;
}
}
if (!done) {
ua->send_msg(_("\nFirst you select one or more JobIds that contain files\n"
"to be restored. You will be presented several methods\n"
"of specifying the JobIds. Then you will be allowed to\n"
"select which files from those JobIds are to be restored.\n\n"));
}
/* If choice not already made above, prompt */
for ( ; !done; ) {
char *fname;
int len;
bool gui_save;
db_list_ctx jobids;
start_prompt(ua, _("To select the JobIds, you have the following choices:\n"));
for (int i=0; list[i]; i++) {
add_prompt(ua, list[i]);
}
done = true;
switch (do_prompt(ua, "", _("Select item: "), NULL, 0)) {
case -1: /* error or cancel */
return 0;
case 0: /* list last 20 Jobs run */
if (!acl_access_ok(ua, Command_ACL, NT_("sqlquery"), true)) {
ua->error_msg(_("SQL query not authorized.\n"));
return 0;
}
gui_save = ua->jcr->gui;
ua->jcr->gui = true;
db_list_sql_query(ua->jcr, ua->db, uar_list_jobs, ua->send, HORZ_LIST, true);
ua->jcr->gui = gui_save;
done = false;
break;
case 1: /* list where a file is saved */
if (!get_client_name(ua, rx)) {
return 0;
}
if (!get_cmd(ua, _("Enter Filename (no path):"))) {
return 0;
}
len = strlen(ua->cmd);
fname = (char *)malloc(len * 2 + 1);
db_escape_string(ua->jcr, ua->db, fname, ua->cmd, len);
Mmsg(rx->query, uar_file[db_get_type_index(ua->db)], rx->ClientName, fname);
free(fname);
gui_save = ua->jcr->gui;
ua->jcr->gui = true;
db_list_sql_query(ua->jcr, ua->db, rx->query, ua->send, HORZ_LIST, true);
ua->jcr->gui = gui_save;
done = false;
break;
case 2: /* enter a list of JobIds */
if (!get_cmd(ua, _("Enter JobId(s), comma separated, to restore: "))) {
return 0;
}
pm_strcpy(rx->JobIds, ua->cmd);
break;
case 3: /* Enter an SQL list command */
if (!acl_access_ok(ua, Command_ACL, NT_("sqlquery"), true)) {
ua->error_msg(_("SQL query not authorized.\n"));
return 0;
}
if (!get_cmd(ua, _("Enter SQL list command: "))) {
return 0;
}
gui_save = ua->jcr->gui;
ua->jcr->gui = true;
db_list_sql_query(ua->jcr, ua->db, ua->cmd, ua->send, HORZ_LIST, true);
ua->jcr->gui = gui_save;
done = false;
break;
case 4: /* Select the most recent backups */
if (!have_date) {
bstrutime(date, sizeof(date), now);
}
if (!select_backups_before_date(ua, rx, date)) {
return 0;
}
break;
case 5: /* select backup at specified time */
if (!have_date) {
if (!get_date(ua, date, sizeof(date))) {
return 0;
}
}
if (!select_backups_before_date(ua, rx, date)) {
return 0;
}
break;
case 6: /* Enter files */
if (!have_date) {
bstrutime(date, sizeof(date), now);
}
if (!get_client_name(ua, rx)) {
return 0;
}
ua->send_msg(_("Enter file names with paths, or < to enter a filename\n"
"containing a list of file names with paths, and terminate\n"
"them with a blank line.\n"));
for ( ;; ) {
if (!get_cmd(ua, _("Enter full filename: "))) {
return 0;
}
len = strlen(ua->cmd);
if (len == 0) {
break;
}
insert_one_file_or_dir(ua, rx, date, false);
}
return 2;
case 7: /* enter files backed up before specified time */
if (!have_date) {
if (!get_date(ua, date, sizeof(date))) {
return 0;
}
}
if (!get_client_name(ua, rx)) {
return 0;
}
ua->send_msg(_("Enter file names with paths, or < to enter a filename\n"
"containing a list of file names with paths, and terminate\n"
"them with a blank line.\n"));
for ( ;; ) {
if (!get_cmd(ua, _("Enter full filename: "))) {
return 0;
}
len = strlen(ua->cmd);
if (len == 0) {
break;
}
insert_one_file_or_dir(ua, rx, date, false);
}
return 2;
case 8: /* Find JobIds for current backup */
if (!have_date) {
bstrutime(date, sizeof(date), now);
}
if (!select_backups_before_date(ua, rx, date)) {
return 0;
}
done = false;
break;
case 9: /* Find JobIds for give date */
if (!have_date) {
if (!get_date(ua, date, sizeof(date))) {
return 0;
}
}
if (!select_backups_before_date(ua, rx, date)) {
return 0;
}
done = false;
break;
case 10: /* Enter directories */
if (*rx->JobIds != 0) {
ua->send_msg(_("You have already selected the following JobIds: %s\n"),
rx->JobIds);
} else if (get_cmd(ua, _("Enter JobId(s), comma separated, to restore: "))) {
if (*rx->JobIds != 0 && *ua->cmd) {
pm_strcat(rx->JobIds, ",");
}
pm_strcat(rx->JobIds, ua->cmd);
}
if (*rx->JobIds == 0 || *rx->JobIds == '.') {
*rx->JobIds = 0;
return 0; /* nothing entered, return */
}
if (!have_date) {
bstrutime(date, sizeof(date), now);
}
if (!get_client_name(ua, rx)) {
return 0;
}
ua->send_msg(_("Enter full directory names or start the name\n"
"with a < to indicate it is a filename containing a list\n"
"of directories and terminate them with a blank line.\n"));
for ( ;; ) {
if (!get_cmd(ua, _("Enter directory name: "))) {
return 0;
}
len = strlen(ua->cmd);
if (len == 0) {
break;
}
/* Add trailing slash to end of directory names */
if (ua->cmd[0] != '<' && !IsPathSeparator(ua->cmd[len-1])) {
strcat(ua->cmd, "/");
}
insert_one_file_or_dir(ua, rx, date, true);
}
return 2;
case 11: /* Choose a jobid and select jobs */
if (!get_cmd(ua, _("Enter JobId to get the state to restore: ")) ||
!is_an_integer(ua->cmd))
{
return 0;
}
memset(&jr, 0, sizeof(jr));
jr.JobId = str_to_int64(ua->cmd);
if (!db_get_job_record(ua->jcr, ua->db, &jr)) {
ua->error_msg(_("Unable to get Job record for JobId=%s: ERR=%s\n"),
ua->cmd, db_strerror(ua->db));
return 0;
}
ua->send_msg(_("Selecting jobs to build the Full state at %s\n"),
jr.cStartTime);
jr.JobLevel = L_INCREMENTAL; /* Take Full+Diff+Incr */
if (!db_accurate_get_jobids(ua->jcr, ua->db, &jr, &jobids)) {
return 0;
}
pm_strcpy(rx->JobIds, jobids.list);
Dmsg1(30, "Item 12: jobids = %s\n", rx->JobIds);
break;
case 12: /* Cancel or quit */
return 0;
}
}
memset(&jr, 0, sizeof(jr));
POOLMEM *JobIds = get_pool_memory(PM_FNAME);
*JobIds = 0;
rx->TotalFiles = 0;
/*
* Find total number of files to be restored, and filter the JobId
* list to contain only ones permitted by the ACL conditions.
*/
for (p=rx->JobIds; ; ) {
char ed1[50];
int status = get_next_jobid_from_list(&p, &JobId);
if (status < 0) {
ua->error_msg(_("Invalid JobId in list.\n"));
free_pool_memory(JobIds);
return 0;
}
if (status == 0) {
break;
}
if (jr.JobId == JobId) {
continue; /* duplicate of last JobId */
}
memset(&jr, 0, sizeof(jr));
jr.JobId = JobId;
if (!db_get_job_record(ua->jcr, ua->db, &jr)) {
ua->error_msg(_("Unable to get Job record for JobId=%s: ERR=%s\n"),
edit_int64(JobId, ed1), db_strerror(ua->db));
free_pool_memory(JobIds);
return 0;
}
if (!acl_access_ok(ua, Job_ACL, jr.Name, true)) {
ua->error_msg(_("Access to JobId=%s (Job \"%s\") not authorized. Not selected.\n"),
edit_int64(JobId, ed1), jr.Name);
continue;
}
if (*JobIds != 0) {
pm_strcat(JobIds, ",");
}
pm_strcat(JobIds, edit_int64(JobId, ed1));
rx->TotalFiles += jr.JobFiles;
}
free_pool_memory(rx->JobIds);
rx->JobIds = JobIds; /* Set ACL filtered list */
if (*rx->JobIds == 0) {
ua->warning_msg(_("No Jobs selected.\n"));
return 0;
}
if (strchr(rx->JobIds,',')) {
ua->info_msg(_("You have selected the following JobIds: %s\n"), rx->JobIds);
} else {
ua->info_msg(_("You have selected the following JobId: %s\n"), rx->JobIds);
}
return true;
}
/*
* Get date from user
*/
static bool get_date(UAContext *ua, char *date, int date_len)
{
ua->send_msg(_("The restored files will the most current backup\n"
"BEFORE the date you specify below.\n\n"));
for ( ;; ) {
if (!get_cmd(ua, _("Enter date as YYYY-MM-DD HH:MM:SS :"))) {
return false;
}
if (str_to_utime(ua->cmd) != 0) {
break;
}
ua->error_msg(_("Improper date format.\n"));
}
bstrncpy(date, ua->cmd, date_len);
return true;
}
/*
* Insert a single file, or read a list of files from a file
*/
static void insert_one_file_or_dir(UAContext *ua, RESTORE_CTX *rx, char *date, bool dir)
{
FILE *ffd;
char file[5000];
char *p = ua->cmd;
int line = 0;
switch (*p) {
case '<':
p++;
if ((ffd = fopen(p, "rb")) == NULL) {
berrno be;
ua->error_msg(_("Cannot open file %s: ERR=%s\n"),
p, be.bstrerror());
break;
}
while (fgets(file, sizeof(file), ffd)) {
line++;
if (dir) {
if (!insert_dir_into_findex_list(ua, rx, file, date)) {
ua->error_msg(_("Error occurred on line %d of file \"%s\"\n"), line, p);
}
} else {
if (!insert_file_into_findex_list(ua, rx, file, date)) {
ua->error_msg(_("Error occurred on line %d of file \"%s\"\n"), line, p);
}
}
}
fclose(ffd);
break;
case '?':
p++;
insert_table_into_findex_list(ua, rx, p);
break;
default:
if (dir) {
insert_dir_into_findex_list(ua, rx, ua->cmd, date);
} else {
insert_file_into_findex_list(ua, rx, ua->cmd, date);
}
break;
}
}
/*
* For a given file (path+filename), split into path and file, then
* lookup the most recent backup in the catalog to get the JobId
* and FileIndex, then insert them into the findex list.
*/
static bool insert_file_into_findex_list(UAContext *ua, RESTORE_CTX *rx, char *file,
char *date)
{
strip_trailing_newline(file);
split_path_and_filename(ua, rx, file);
if (*rx->JobIds == 0) {
Mmsg(rx->query, uar_jobid_fileindex, date, rx->path, rx->fname,
rx->ClientName);
} else {
Mmsg(rx->query, uar_jobids_fileindex, rx->JobIds, date,
rx->path, rx->fname, rx->ClientName);
}
rx->found = false;
/* Find and insert jobid and File Index */
if (!db_sql_query(ua->db, rx->query, jobid_fileindex_handler, (void *)rx)) {
ua->error_msg(_("Query failed: %s. ERR=%s\n"),
rx->query, db_strerror(ua->db));
}
if (!rx->found) {