/
ua_cmds.c
2205 lines (1984 loc) · 62.1 KB
/
ua_cmds.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) 2000-2012 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 Commands
*
* Kern Sibbald, September MM
*/
#include "bareos.h"
#include "dird.h"
/* Imported subroutines */
/* Imported variables */
extern jobq_t job_queue; /* job queue */
/* Imported functions */
extern int autodisplay_cmd(UAContext *ua, const char *cmd);
extern int gui_cmd(UAContext *ua, const char *cmd);
extern int label_cmd(UAContext *ua, const char *cmd);
extern int list_cmd(UAContext *ua, const char *cmd);
extern int llist_cmd(UAContext *ua, const char *cmd);
extern int messagescmd(UAContext *ua, const char *cmd);
extern int prunecmd(UAContext *ua, const char *cmd);
extern int purgecmd(UAContext *ua, const char *cmd);
extern int querycmd(UAContext *ua, const char *cmd);
extern int relabel_cmd(UAContext *ua, const char *cmd);
extern int restore_cmd(UAContext *ua, const char *cmd);
extern int retentioncmd(UAContext *ua, const char *cmd);
extern int show_cmd(UAContext *ua, const char *cmd);
extern int sqlquerycmd(UAContext *ua, const char *cmd);
extern int status_cmd(UAContext *ua, const char *cmd);
extern int update_cmd(UAContext *ua, const char *cmd);
/* Forward referenced functions */
static int add_cmd(UAContext *ua, const char *cmd);
static int automount_cmd(UAContext *ua, const char *cmd);
static int cancel_cmd(UAContext *ua, const char *cmd);
static int create_cmd(UAContext *ua, const char *cmd);
static int delete_cmd(UAContext *ua, const char *cmd);
static int disable_cmd(UAContext *ua, const char *cmd);
static int enable_cmd(UAContext *ua, const char *cmd);
static int estimate_cmd(UAContext *ua, const char *cmd);
static int help_cmd(UAContext *ua, const char *cmd);
static int memory_cmd(UAContext *ua, const char *cmd);
static int mount_cmd(UAContext *ua, const char *cmd);
static int release_cmd(UAContext *ua, const char *cmd);
static int reload_cmd(UAContext *ua, const char *cmd);
static int setdebug_cmd(UAContext *ua, const char *cmd);
static int setbwlimit_cmd(UAContext *ua, const char *cmd);
static int setip_cmd(UAContext *ua, const char *cmd);
static int time_cmd(UAContext *ua, const char *cmd);
static int trace_cmd(UAContext *ua, const char *cmd);
static int unmount_cmd(UAContext *ua, const char *cmd);
static int use_cmd(UAContext *ua, const char *cmd);
static int var_cmd(UAContext *ua, const char *cmd);
static int version_cmd(UAContext *ua, const char *cmd);
static int wait_cmd(UAContext *ua, const char *cmd);
static void do_job_delete(UAContext *ua, JobId_t JobId);
static bool delete_job_id_range(UAContext *ua, char *tok);
static int delete_volume(UAContext *ua);
static int delete_pool(UAContext *ua);
static void delete_job(UAContext *ua);
int qhelp_cmd(UAContext *ua, const char *cmd);
int quit_cmd(UAContext *ua, const char *cmd);
/*
* Not all in alphabetical order.
* New commands are added after existing commands with similar letters
* to prevent breakage of existing user scripts.
*/
struct cmdstruct {
const char *key; /* command */
int (*func)(UAContext *ua, const char *cmd); /* handler */
const char *help; /* main purpose */
const char *usage; /* all arguments to build usage */
const bool use_in_rs; /* Can use it in Console RunScript */
};
static struct cmdstruct commands[] = {
{ NT_("add"), add_cmd, _("Add media to a pool"),
NT_("pool=<pool-name> storage=<storage-name> jobid=<jobid>"), false },
{ NT_("autodisplay"), autodisplay_cmd,_("Autodisplay console messages"),
NT_("on | off"), false },
{ NT_("automount"), automount_cmd, _("Automount after label"),
NT_("on | off"), false },
{ NT_("cancel"), cancel_cmd, _("Cancel a job"),
NT_("jobid=<jobid> job=<job-name> ujobid=<unique-jobid>"), false },
{ NT_("create"), create_cmd, _("Create DB Pool from resource"),
NT_("pool=<pool-name>"), false },
{ NT_("delete"), delete_cmd, _("Delete volume, pool or job"),
NT_("volume=<vol-name> pool=<pool-name> jobid=<jobid>"), true },
{ NT_("disable"), disable_cmd, _("Disable a job"),
NT_("job=<job-name>"), true },
{ NT_("enable"), enable_cmd, _("Enable a job"),
NT_("job=<job-name>"), true },
{ NT_("estimate"), estimate_cmd, _("Performs FileSet estimate, listing gives full listing"),
NT_("fileset=<fileset-name> client=<client-name> level=<level> accurate=<yes/no> job=<job-name> listing"), true },
{ NT_("exit"), quit_cmd, _("Terminate Bconsole session"),
NT_(""), false },
{ NT_("export"), export_cmd, _("Export volumes from normal slots to import/export slots"),
NT_("storage=<storage-name> srcslots=<slot-selection> [ dstslots=<slot-selection> volume=<volume-name> scan ]"), true },
{ NT_("gui"), gui_cmd, _("Non-interactive gui mode"),
NT_("on | off"), false },
{ NT_("help"), help_cmd, _("Print help on specific command"),
NT_("add autodisplay automount cancel create delete disable\n"
"\tenable estimate exit gui label list llist\n"
"\tmessages memory mount prune purge quit query\n"
"\trestore relabel release reload run status\n"
"\tsetbandwidth setdebug setip show sqlquery time trace unmount\n"
"\tumount update use var version wait"), false },
{ NT_("import"), import_cmd, _("Import volumes from import/export slots to normal slots"),
NT_("storage=<storage-name> [ srcslots=<slot-selection> dstslots=<slot-selection> volume=<volume-name> scan ]"), true },
{ NT_("label"), label_cmd, _("Label a tape"),
NT_("storage=<storage-name> volume=<volume-name> pool=<pool-name> slot=<slot> [ barcodes ] [ encrypt ]"), false },
{ NT_("list"), list_cmd, _("List objects from catalog"),
NT_("jobs | jobid=<jobid> | ujobid=<complete_name> | job=<job-name> | jobmedia jobid=<jobid> |\n"
"\tjobmedia ujobid=<complete_name> | joblog jobid=<jobid> | joblog ujobid=<complete_name> |\n"
"\tbasefiles jobid=<jobid> | basefiles ujobid=<complete_name> |\n"
"\tfiles jobid=<jobid> | files ujobid=<complete_name> | pools | jobtotals |\n"
"\tvolumes [ jobid=<jobid> ujobid=<complete_name> pool=<pool-name> ] |\n"
"\tmedia [ jobid=<jobid> ujobid=<complete_name> pool=<pool-name> ] | clients |\n"
"\tnextvol job=<job-name> | nextvolume ujobid=<complete_name> | copies jobid=<jobid>"), true },
{ NT_("llist"), llist_cmd, _("Full or long list like list command"),
NT_("jobs | jobid=<jobid> | ujobid=<complete_name> | job=<job-name> | jobmedia jobid=<jobid> |\n"
"\tjobmedia ujobid=<complete_name> | joblog jobid=<jobid> | joblog ujobid=<complete_name> |\n"
"\tbasefiles jobid=<jobid> | basefiles ujobid=<complete_name> |\n"
"\tfiles jobid=<jobid> | files ujobid=<complete_name> | pools | jobtotals |\n"
"\tvolumes [ jobid=<jobid> ujobid=<complete_name> pool=<pool-name> ] |\n"
"\tmedia [ jobid=<jobid> ujobid=<complete_name> pool=<pool-name> ] | clients |\n"
"\tnextvol job=<job-name> | nextvolume ujobid=<complete_name> | copies jobid=<jobid>"), true },
{ NT_("messages"), messagescmd, _("Display pending messages"),
NT_(""), false },
{ NT_("memory"), memory_cmd, _("Print current memory usage"),
NT_(""), true },
{ NT_("mount"), mount_cmd, _("Mount storage"),
NT_("storage=<storage-name> slot=<num> drive=<num>\n"
"\tjobid=<jobid> | job=<job-name> | ujobid=<complete_name>"), false },
{ NT_("move"), move_cmd, _("Move slots in an autochanger"),
NT_("storage=<storage-name> srcslots=<slot-selection> dstslots=<slot-selection>"), true },
{ NT_("prune"), prunecmd, _("Prune expired records from catalog"),
NT_("files | jobs | pool=<pool-name> | client=<client-name> | volume=<volume-name> "), true },
{ NT_("purge"), purgecmd, _("Purge records from catalog"),
NT_("files jobs volume=<volume-name> [ action=<action> devicetype=<type> pool=<pool-name>\n"
"\tallpools storage=<storage-name> drive=<num> ]"), true },
{ NT_("quit"), quit_cmd, _("Terminate Bconsole session"),
NT_(""), false },
{ NT_("query"), querycmd, _("Query catalog"),
NT_(""), false },
{ NT_("restore"), restore_cmd, _("Restore files"),
NT_("where=</path> client=<client-name> storage=<storage-name> bootstrap=<file>\n"
"\trestorejob=<job-name> comment=<text> jobid=<jobid> copies done select all"), false },
{ NT_("relabel"), relabel_cmd, _("Relabel a tape"),
NT_("storage=<storage-name> oldvolume=<old-volume-name>\n"
"\tvolume=<new-volume-name> pool=<pool-name> [ encrypt ]"), false },
{ NT_("release"), release_cmd, _("Release storage"),
NT_("storage=<storage-name> [ drive=<num> ] [ alldrives ]"), false },
{ NT_("reload"), reload_cmd, _("Reload conf file"),
NT_(""), true },
{ NT_("rerun"), rerun_cmd, _("Rerun a job"),
NT_("jobid=<jobid> yes"), false },
{ NT_("run"), run_cmd, _("Run a job"),
NT_("job=<job-name> client=<client-name>\n"
"\tfileset=<fileset-name> level=<level>\n"
"\tstorage=<storage-name> where=<directory-prefix>\n"
"\twhen=<universal-time-specification>\n"
"\tcomment=<text> yes"), false },
{ NT_("status"), status_cmd, _("Report status"),
NT_("all | dir=<dir-name> | director | scheduler | schedule=<schedule-name> | client=<client-name> |\n"
"\tstorage=<storage-name> slots | days=nnn | job=<job-name> | schedule=<schedule-name>"), true },
{ NT_("setbandwidth"), setbwlimit_cmd, _("Sets bandwidth"),
NT_("limit=<nn-kbs> client=<client-name> jobid=<number> job=<job-name> ujobid=<unique-jobid>"), true },
{ NT_("setdebug"), setdebug_cmd, _("Sets debug level"),
NT_("level=<nn> trace=0/1 client=<client-name> | dir | storage=<storage-name> | all"), true },
{ NT_("setip"), setip_cmd, _("Sets new client address -- if authorized"),
NT_(""), false },
{ NT_("show"), show_cmd, _("Show resource records"),
NT_("job=<job-name> | pool=<pool-name> | fileset=<fileset-name> | schedule=<schedule-name> |\n"
"\tclient=<client-name> | jobs | pools | filesets | schedules | clients | disabled | all"), true },
{ NT_("sqlquery"), sqlquerycmd, _("Use SQL to query catalog"),
NT_(""), false },
{ NT_("time"), time_cmd, _("Print current time"),
NT_(""), true },
{ NT_("trace"), trace_cmd, _("Turn on/off trace to file"),
NT_("on | off"), true },
{ NT_("unmount"), unmount_cmd, _("Unmount storage"),
NT_("storage=<storage-name> [ drive=<num> ]\n"
"\tjobid=<jobid> | job=<job-name> | ujobid=<complete_name>"), false },
{ NT_("umount"), unmount_cmd, _("Umount - for old-time Unix guys, see unmount"),
NT_("storage=<storage-name> [ drive=<num> ]\n"
"\tjobid=<jobid> | job=<job-name> | ujobid=<complete_name>"), false },
{ NT_("update"), update_cmd, _("Update volume, pool or stats"),
NT_("stats\n"
"\tpool=<pool-name>\n"
"\tslots storage=<storage-name> scan\n"
"\tvolume=<volume-name> volstatus=<status> volretention=<time-def>\n"
"\tpool=<pool-name> recycle=<yes/no> slot=<number>\n"
"\tinchanger=<yes/no>\n"
"\tmaxvolbytes=<size> maxvolfiles=<nb> maxvoljobs=<nb>\n"
"\tenabled=<yes/no> recyclepool=<pool-name> actiononpurge=<action>"), true },
{ NT_("use"), use_cmd, _("Use specific catalog"),
NT_("catalog=<catalog>"), false },
{ NT_("var"), var_cmd, _("Does variable expansion"),
NT_(""), false },
{ NT_("version"), version_cmd, _("Print Director version"),
NT_(""), true },
{ NT_("wait"), wait_cmd, _("Wait until no jobs are running"),
NT_("jobname=<name> | jobid=<jobid> | ujobid=<complete_name>"), false }
};
#define comsize ((int)(sizeof(commands)/sizeof(struct cmdstruct)))
/*
* Execute a command from the UA
*/
bool do_a_command(UAContext *ua)
{
int i;
int len;
bool ok = false;
bool found = false;
BSOCK *user = ua->UA_sock;
Dmsg1(900, "Command: %s\n", ua->argk[0]);
if (ua->argc == 0) {
return false;
}
while (ua->jcr->wstorage->size()) {
ua->jcr->wstorage->remove(0);
}
len = strlen(ua->argk[0]);
for (i=0; i<comsize; i++) { /* search for command */
if (bstrncasecmp(ua->argk[0], commands[i].key, len)) {
/* Check if command permitted, but "quit" is always OK */
if (!bstrcmp(ua->argk[0], NT_("quit")) &&
!acl_access_ok(ua, Command_ACL, ua->argk[0], len)) {
break;
}
/* Check if this command is authorized in RunScript */
if (ua->runscript && !commands[i].use_in_rs) {
ua->error_msg(_("Can't use %s command in a runscript"), ua->argk[0]);
break;
}
if (ua->api) user->signal(BNET_CMD_BEGIN);
ok = (*commands[i].func)(ua, ua->cmd); /* go execute command */
if (ua->api) user->signal(ok?BNET_CMD_OK:BNET_CMD_FAILED);
found = true;
break;
}
}
if (!found) {
ua->error_msg(_("%s: is an invalid command.\n"), ua->argk[0]);
ok = false;
}
return ok;
}
/*
* This is a common routine used to stuff the Pool DB record defaults
* into the Media DB record just before creating a media (Volume)
* record.
*/
void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
{
mr->PoolId = pr->PoolId;
bstrncpy(mr->VolStatus, NT_("Append"), sizeof(mr->VolStatus));
mr->Recycle = pr->Recycle;
mr->VolRetention = pr->VolRetention;
mr->VolUseDuration = pr->VolUseDuration;
mr->ActionOnPurge = pr->ActionOnPurge;
mr->RecyclePoolId = pr->RecyclePoolId;
mr->MaxVolJobs = pr->MaxVolJobs;
mr->MaxVolFiles = pr->MaxVolFiles;
mr->MaxVolBytes = pr->MaxVolBytes;
mr->LabelType = pr->LabelType;
mr->Enabled = 1;
}
/*
* Add Volumes to an existing Pool
*/
static int add_cmd(UAContext *ua, const char *cmd)
{
POOL_DBR pr;
MEDIA_DBR mr;
int num, i, max, startnum;
char name[MAX_NAME_LENGTH];
STORERES *store;
int Slot = 0, InChanger = 0;
ua->send_msg(_(
"You probably don't want to be using this command since it\n"
"creates database records without labeling the Volumes.\n"
"You probably want to use the \"label\" command.\n\n"));
if (!open_client_db(ua)) {
return 1;
}
memset(&pr, 0, sizeof(pr));
if (!get_pool_dbr(ua, &pr)) {
return 1;
}
Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
pr.MaxVols, pr.PoolType);
while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
ua->warning_msg(_("Pool already has maximum volumes=%d\n"), pr.MaxVols);
if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
return 1;
}
pr.MaxVols = ua->pint32_val;
}
/* Get media type */
if ((store = get_storage_resource(ua, false/*no default*/)) != NULL) {
bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
} else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
return 1;
}
if (pr.MaxVols == 0) {
max = 1000;
} else {
max = pr.MaxVols - pr.NumVols;
}
for (;;) {
char buf[100];
bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
if (!get_pint(ua, buf)) {
return 1;
}
num = ua->pint32_val;
if (num < 0 || num > max) {
ua->warning_msg(_("The number must be between 0 and %d\n"), max);
continue;
}
break;
}
for (;;) {
if (num == 0) {
if (!get_cmd(ua, _("Enter Volume name: "))) {
return 1;
}
} else {
if (!get_cmd(ua, _("Enter base volume name: "))) {
return 1;
}
}
/* Don't allow | in Volume name because it is the volume separator character */
if (!is_volume_name_legal(ua, ua->cmd)) {
continue;
}
if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
ua->warning_msg(_("Volume name too long.\n"));
continue;
}
if (strlen(ua->cmd) == 0) {
ua->warning_msg(_("Volume name must be at least one character long.\n"));
continue;
}
break;
}
bstrncpy(name, ua->cmd, sizeof(name));
if (num > 0) {
bstrncat(name, "%04d", sizeof(name));
for (;;) {
if (!get_pint(ua, _("Enter the starting number: "))) {
return 1;
}
startnum = ua->pint32_val;
if (startnum < 1) {
ua->warning_msg(_("Start number must be greater than zero.\n"));
continue;
}
break;
}
} else {
startnum = 1;
num = 1;
}
if (store && store->autochanger) {
if (!get_pint(ua, _("Enter slot (0 for none): "))) {
return 1;
}
Slot = ua->pint32_val;
if (!get_yesno(ua, _("InChanger? yes/no: "))) {
return 1;
}
InChanger = ua->pint32_val;
}
set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
for (i=startnum; i < num+startnum; i++) {
bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
mr.Slot = Slot++;
mr.InChanger = InChanger;
mr.Enabled = 1;
set_storageid_in_mr(store, &mr);
Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
ua->error_msg("%s", db_strerror(ua->db));
return 1;
}
}
pr.NumVols += num;
Dmsg0(200, "Update pool record.\n");
if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
ua->warning_msg("%s", db_strerror(ua->db));
return 1;
}
ua->send_msg(_("%d Volumes created in pool %s\n"), num, pr.Name);
return 1;
}
/*
* Turn auto mount on/off
*
* automount on
* automount off
*/
int automount_cmd(UAContext *ua, const char *cmd)
{
char *onoff;
if (ua->argc != 2) {
if (!get_cmd(ua, _("Turn on or off? "))) {
return 1;
}
onoff = ua->cmd;
} else {
onoff = ua->argk[1];
}
ua->automount = (bstrcasecmp(onoff, NT_("off"))) ? 0 : 1;
return 1;
}
/*
* Cancel a job
*/
static int cancel_cmd(UAContext *ua, const char *cmd)
{
JCR *jcr = select_running_job(ua, "cancel");
if (!jcr) {
return 1;
}
int ret = cancel_job(ua, jcr);
free_jcr(jcr);
return ret;
}
/*
* This is a common routine to create or update a
* Pool DB base record from a Pool Resource. We handle
* the setting of MaxVols and NumVols slightly differently
* depending on if we are creating the Pool or we are
* simply bringing it into agreement with the resource (updage).
*
* Caution : RecyclePoolId isn't setup in this function.
* You can use set_pooldbr_recyclepoolid();
*
*/
void set_pooldbr_from_poolres(POOL_DBR *pr, POOLRES *pool, e_pool_op op)
{
bstrncpy(pr->PoolType, pool->pool_type, sizeof(pr->PoolType));
if (op == POOL_OP_CREATE) {
pr->MaxVols = pool->max_volumes;
pr->NumVols = 0;
} else { /* update pool */
if (pr->MaxVols != pool->max_volumes) {
pr->MaxVols = pool->max_volumes;
}
if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
pr->MaxVols = pr->NumVols;
}
}
pr->LabelType = pool->LabelType;
pr->UseOnce = pool->use_volume_once;
pr->UseCatalog = pool->use_catalog;
pr->Recycle = pool->Recycle;
pr->VolRetention = pool->VolRetention;
pr->VolUseDuration = pool->VolUseDuration;
pr->MaxVolJobs = pool->MaxVolJobs;
pr->MaxVolFiles = pool->MaxVolFiles;
pr->MaxVolBytes = pool->MaxVolBytes;
pr->AutoPrune = pool->AutoPrune;
pr->ActionOnPurge = pool->action_on_purge;
pr->Recycle = pool->Recycle;
if (pool->label_format) {
bstrncpy(pr->LabelFormat, pool->label_format, sizeof(pr->LabelFormat));
} else {
bstrncpy(pr->LabelFormat, "*", sizeof(pr->LabelFormat)); /* none */
}
}
/* set/update Pool.RecyclePoolId and Pool.ScratchPoolId in Catalog */
int update_pool_references(JCR *jcr, B_DB *db, POOLRES *pool)
{
POOL_DBR pr;
if (!pool->RecyclePool && !pool->ScratchPool) {
return 1;
}
memset(&pr, 0, sizeof(POOL_DBR));
bstrncpy(pr.Name, pool->name(), sizeof(pr.Name));
if (!db_get_pool_record(jcr, db, &pr)) {
return -1; /* not exists in database */
}
set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE);
if (!set_pooldbr_references(jcr, db, &pr, pool)) {
return -1; /* error */
}
if (!db_update_pool_record(jcr, db, &pr)) {
return -1; /* error */
}
return 1;
}
/* set POOL_DBR.RecyclePoolId and POOL_DBR.ScratchPoolId from Pool resource
* works with set_pooldbr_from_poolres
*/
bool set_pooldbr_references(JCR *jcr, B_DB *db, POOL_DBR *pr, POOLRES *pool)
{
POOL_DBR rpool;
bool ret = true;
if (pool->RecyclePool) {
memset(&rpool, 0, sizeof(POOL_DBR));
bstrncpy(rpool.Name, pool->RecyclePool->name(), sizeof(rpool.Name));
if (db_get_pool_record(jcr, db, &rpool)) {
pr->RecyclePoolId = rpool.PoolId;
} else {
Jmsg(jcr, M_WARNING, 0,
_("Can't set %s RecyclePool to %s, %s is not in database.\n" \
"Try to update it with 'update pool=%s'\n"),
pool->name(), rpool.Name, rpool.Name,pool->name());
ret = false;
}
} else { /* no RecyclePool used, set it to 0 */
pr->RecyclePoolId = 0;
}
if (pool->ScratchPool) {
memset(&rpool, 0, sizeof(POOL_DBR));
bstrncpy(rpool.Name, pool->ScratchPool->name(), sizeof(rpool.Name));
if (db_get_pool_record(jcr, db, &rpool)) {
pr->ScratchPoolId = rpool.PoolId;
} else {
Jmsg(jcr, M_WARNING, 0,
_("Can't set %s ScratchPool to %s, %s is not in database.\n" \
"Try to update it with 'update pool=%s'\n"),
pool->name(), rpool.Name, rpool.Name,pool->name());
ret = false;
}
} else { /* no ScratchPool used, set it to 0 */
pr->ScratchPoolId = 0;
}
return ret;
}
/*
* Create a pool record from a given Pool resource
* Also called from backup.c
* Returns: -1 on error
* 0 record already exists
* 1 record created
*/
int create_pool(JCR *jcr, B_DB *db, POOLRES *pool, e_pool_op op)
{
POOL_DBR pr;
memset(&pr, 0, sizeof(POOL_DBR));
bstrncpy(pr.Name, pool->name(), sizeof(pr.Name));
if (db_get_pool_record(jcr, db, &pr)) {
/* Pool Exists */
if (op == POOL_OP_UPDATE) { /* update request */
set_pooldbr_from_poolres(&pr, pool, op);
set_pooldbr_references(jcr, db, &pr, pool);
db_update_pool_record(jcr, db, &pr);
}
return 0; /* exists */
}
set_pooldbr_from_poolres(&pr, pool, op);
set_pooldbr_references(jcr, db, &pr, pool);
if (!db_create_pool_record(jcr, db, &pr)) {
return -1; /* error */
}
return 1;
}
/*
* Create a Pool Record in the database.
* It is always created from the Resource record.
*/
static int create_cmd(UAContext *ua, const char *cmd)
{
POOLRES *pool;
if (!open_client_db(ua)) {
return 1;
}
pool = get_pool_resource(ua);
if (!pool) {
return 1;
}
switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
case 0:
ua->error_msg(_("Error: Pool %s already exists.\n"
"Use update to change it.\n"), pool->name());
break;
case -1:
ua->error_msg("%s", db_strerror(ua->db));
break;
default:
break;
}
ua->send_msg(_("Pool %s created.\n"), pool->name());
return 1;
}
static int setbwlimit_cmd(UAContext *ua, const char *cmd)
{
int i;
JCR *jcr;
int64_t limit = -1;
CLIENTRES *client = NULL;
char Job[MAX_NAME_LENGTH];
const char *lst[] = {
"job",
"jobid",
"jobname",
NULL
};
memset(Job, 0, sizeof(Job));
i = find_arg_with_value(ua, "limit");
if (i >= 0) {
limit = atoi(ua->argv[i]) * 1024;
}
if (limit < 0) {
if (!get_pint(ua, _("Enter new bandwidth limit kb/s: "))) {
return 1;
}
limit = ua->pint32_val * 1024; /* kb/s */
}
if (find_arg_keyword(ua, lst) > 0) {
jcr = select_running_job(ua, "limit");
if (jcr) {
jcr->max_bandwidth = limit; /* TODO: see for locking (Should be safe)*/
bstrncpy(Job, jcr->Job, sizeof(Job));
client = jcr->res.client;
free_jcr(jcr);
} else {
return 1;
}
} else {
client = get_client_resource(ua);
}
if (!client) {
return 1;
}
/*
* Connect to File daemon
*/
ua->jcr->res.client = client;
ua->jcr->max_bandwidth = limit;
/*
* Try to connect for 15 seconds
*/
ua->send_msg(_("Connecting to Client %s at %s:%d\n"),
client->name(), client->address, client->FDport);
if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
ua->error_msg(_("Failed to connect to Client.\n"));
return 1;
}
Dmsg0(120, "Connected to file daemon\n");
if (!send_bwlimit(ua->jcr, Job)) {
ua->error_msg(_("Failed to set bandwidth limit to Client.\n"));
} else {
ua->info_msg(_("OK Limiting bandwidth to %lldkb/s %s\n"), limit/1024, Job);
}
ua->jcr->file_bsock->signal(BNET_TERMINATE);
ua->jcr->file_bsock->close();
ua->jcr->file_bsock = NULL;
ua->jcr->res.client = NULL;
ua->jcr->max_bandwidth = 0;
return 1;
}
/*
* Set a new address in a Client resource. We do this only
* if the Console name is the same as the Client name
* and the Console can access the client.
*/
static int setip_cmd(UAContext *ua, const char *cmd)
{
CLIENTRES *client;
char buf[1024];
if (!ua->cons || !acl_access_ok(ua, Client_ACL, ua->cons->name())) {
ua->error_msg(_("Unauthorized command from this console.\n"));
return 1;
}
LockRes();
client = GetClientResWithName(ua->cons->name());
if (!client) {
ua->error_msg(_("Client \"%s\" not found.\n"), ua->cons->name());
goto get_out;
}
if (client->address) {
free(client->address);
}
/* MA Bug 6 remove ifdef */
sockaddr_to_ascii(&(ua->UA_sock->client_addr), buf, sizeof(buf));
client->address = bstrdup(buf);
ua->send_msg(_("Client \"%s\" address set to %s\n"),
client->name(), client->address);
get_out:
UnlockRes();
return 1;
}
static void do_en_disable_cmd(UAContext *ua, bool setting)
{
JOBRES *job;
int i;
i = find_arg_with_value(ua, NT_("job"));
if (i < 0) {
job = select_enable_disable_job_resource(ua, setting);
if (!job) {
return;
}
} else {
LockRes();
job = GetJobResWithName(ua->argv[i]);
UnlockRes();
}
if (!job) {
ua->error_msg(_("Job \"%s\" not found.\n"), ua->argv[i]);
return;
}
if (!acl_access_ok(ua, Job_ACL, job->name())) {
ua->error_msg(_("Unauthorized command from this console.\n"));
return;
}
job->enabled = setting;
ua->send_msg(_("Job \"%s\" %sabled\n"), job->name(), setting?"en":"dis");
return;
}
static int enable_cmd(UAContext *ua, const char *cmd)
{
do_en_disable_cmd(ua, true);
return 1;
}
static int disable_cmd(UAContext *ua, const char *cmd)
{
do_en_disable_cmd(ua, false);
return 1;
}
static void do_storage_setdebug(UAContext *ua, STORERES *store, int level, int trace_flag)
{
BSOCK *sd;
JCR *jcr = ua->jcr;
USTORERES lstore;
lstore.store = store;
pm_strcpy(lstore.store_source, _("unknown source"));
set_wstorage(jcr, &lstore);
/* Try connecting for up to 15 seconds */
ua->send_msg(_("Connecting to Storage daemon %s at %s:%d\n"),
store->name(), store->address, store->SDport);
if (!connect_to_storage_daemon(jcr, 1, 15, 0)) {
ua->error_msg(_("Failed to connect to Storage daemon.\n"));
return;
}
Dmsg0(120, _("Connected to storage daemon\n"));
sd = jcr->store_bsock;
sd->fsend("setdebug=%d trace=%d\n", level, trace_flag);
if (sd->recv() >= 0) {
ua->send_msg("%s", sd->msg);
}
sd->signal(BNET_TERMINATE);
sd->close();
jcr->store_bsock = NULL;
return;
}
/*
* For the client, we have the following values that can be set
* level = debug level
* trace = send debug output to a file
* hangup = how many records to send to SD before hanging up
* obviously this is most useful for testing restarting
* failed jobs.
*/
static void do_client_setdebug(UAContext *ua, CLIENTRES *client,
int level, int trace, int hangup)
{
BSOCK *fd;
/* Connect to File daemon */
ua->jcr->res.client = client;
/* Try to connect for 15 seconds */
ua->send_msg(_("Connecting to Client %s at %s:%d\n"),
client->name(), client->address, client->FDport);
if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
ua->error_msg(_("Failed to connect to Client.\n"));
return;
}
Dmsg0(120, "Connected to file daemon\n");
fd = ua->jcr->file_bsock;
fd->fsend("setdebug=%d trace=%d hangup=%d\n", level, trace, hangup);
if (fd->recv() >= 0) {
ua->send_msg("%s", fd->msg);
}
fd->signal(BNET_TERMINATE);
fd->close();
ua->jcr->file_bsock = NULL;
return;
}
static void do_all_setdebug(UAContext *ua, int level, int trace_flag, int hangup)
{
STORERES *store, **unique_store;
CLIENTRES *client, **unique_client;
int i, j, found;
/* Director */
debug_level = level;
/* Count Storage items */
LockRes();
store = NULL;
i = 0;
foreach_res(store, R_STORAGE) {
i++;
}
unique_store = (STORERES **) malloc(i * sizeof(STORERES));
/* Find Unique Storage address/port */
store = (STORERES *)GetNextRes(R_STORAGE, NULL);
i = 0;
unique_store[i++] = store;
while ((store = (STORERES *)GetNextRes(R_STORAGE, (RES *)store))) {
found = 0;
for (j=0; j<i; j++) {
if (bstrcmp(unique_store[j]->address, store->address) &&
unique_store[j]->SDport == store->SDport) {
found = 1;
break;
}
}
if (!found) {
unique_store[i++] = store;
Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
}
}
UnlockRes();
/* Call each unique Storage daemon */
for (j=0; j<i; j++) {
do_storage_setdebug(ua, unique_store[j], level, trace_flag);
}
free(unique_store);
/* Count Client items */
LockRes();
client = NULL;
i = 0;
foreach_res(client, R_CLIENT) {
i++;
}
unique_client = (CLIENTRES **) malloc(i * sizeof(CLIENTRES));
/* Find Unique Client address/port */
client = (CLIENTRES *)GetNextRes(R_CLIENT, NULL);
i = 0;
unique_client[i++] = client;
while ((client = (CLIENTRES *)GetNextRes(R_CLIENT, (RES *)client))) {
found = 0;
for (j=0; j<i; j++) {
if (bstrcmp(unique_client[j]->address, client->address) &&
unique_client[j]->FDport == client->FDport) {
found = 1;
break;
}
}
if (!found) {
unique_client[i++] = client;
Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
}
}
UnlockRes();
/* Call each unique File daemon */
for (j=0; j<i; j++) {
do_client_setdebug(ua, unique_client[j], level, trace_flag, hangup);
}
free(unique_client);
}
/*
* setdebug level=nn all trace=1/0
*/
static int setdebug_cmd(UAContext *ua, const char *cmd)
{
STORERES *store;
CLIENTRES *client;
int level;
int trace_flag = -1;
int hangup = -1;
int i;
Dmsg1(120, "setdebug:%s:\n", cmd);
level = -1;
i = find_arg_with_value(ua, "level");
if (i >= 0) {
level = atoi(ua->argv[i]);
}
if (level < 0) {
if (!get_pint(ua, _("Enter new debug level: "))) {
return 1;
}
level = ua->pint32_val;
}
/* Look for trace flag. -1 => not change */
i = find_arg_with_value(ua, "trace");
if (i >= 0) {
trace_flag = atoi(ua->argv[i]);
if (trace_flag > 0) {
trace_flag = 1;
}
}