3 * Bacula Director -- User Agent Commands
5 * Kern Sibbald, September MM
11 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 /* Imported subroutines */
35 extern void run_job(JCR *jcr);
37 /* Imported variables */
40 extern struct s_res resources[];
41 extern char my_name[];
43 /* Imported functions */
44 extern int statuscmd(UAContext *ua, char *cmd);
45 extern int listcmd(UAContext *ua, char *cmd);
46 extern int showcmd(UAContext *ua, char *cmd);
47 extern int messagescmd(UAContext *ua, char *cmd);
48 extern int autodisplaycmd(UAContext *ua, char *cmd);
49 extern int sqlquerycmd(UAContext *ua, char *cmd);
50 extern int querycmd(UAContext *ua, char *cmd);
51 extern int runcmd(UAContext *ua, char *cmd);
52 extern int retentioncmd(UAContext *ua, char *cmd);
53 extern int prunecmd(UAContext *ua, char *cmd);
54 extern int purgecmd(UAContext *ua, char *cmd);
55 extern int restorecmd(UAContext *ua, char *cmd);
57 /* Forward referenced functions */
58 static int addcmd(UAContext *ua, char *cmd), createcmd(UAContext *ua, char *cmd), cancelcmd(UAContext *ua, char *cmd);
59 static int setdebugcmd(UAContext *ua, char *cmd);
60 static int helpcmd(UAContext *ua, char *cmd);
61 static int deletecmd(UAContext *ua, char *cmd);
62 static int usecmd(UAContext *ua, char *cmd), unmountcmd(UAContext *ua, char *cmd);
63 static int labelcmd(UAContext *ua, char *cmd), mountcmd(UAContext *ua, char *cmd), updatecmd(UAContext *ua, char *cmd);
64 static int versioncmd(UAContext *ua, char *cmd), automountcmd(UAContext *ua, char *cmd);
65 static int update_volume(UAContext *ua);
66 static int update_pool(UAContext *ua);
67 static int delete_volume(UAContext *ua);
68 static int delete_pool(UAContext *ua);
70 int quitcmd(UAContext *ua, char *cmd);
73 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
74 static struct cmdstruct commands[] = {
75 { N_("add"), addcmd, _("add media to a pool")},
76 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
77 { N_("automount"), automountcmd, _("automount [on/off] -- after label")},
78 { N_("cancel"), cancelcmd, _("cancel job=nnn -- cancel a job")},
79 { N_("create"), createcmd, _("create DB Pool from resource")},
80 { N_("delete"), deletecmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
81 { N_("help"), helpcmd, _("print this command")},
82 { N_("label"), labelcmd, _("label a tape")},
83 { N_("list"), listcmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
84 { N_("messages"), messagescmd, _("messages")},
85 { N_("mount"), mountcmd, _("mount <storage-name>")},
86 { N_("restore"), restorecmd, _("restore files")},
87 { N_("prune"), prunecmd, _("prune expired records from catalog")},
88 { N_("purge"), purgecmd, _("purge records from catalog")},
89 { N_("run"), runcmd, _("run <job-name>")},
90 { N_("setdebug"), setdebugcmd, _("sets debug level")},
91 { N_("show"), showcmd, _("show (resource records) [jobs | pools | ... | all]")},
92 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
93 { N_("status"), statuscmd, _("status [storage | client]=<name>")},
94 { N_("unmount"), unmountcmd, _("unmount <storage-name>")},
95 { N_("update"), updatecmd, _("update Volume or Pool")},
96 { N_("use"), usecmd, _("use catalog xxx")},
97 { N_("version"), versioncmd, _("print Director version")},
98 { N_("quit"), quitcmd, _("quit")},
99 { N_("query"), querycmd, _("query catalog")},
100 { N_("exit"), quitcmd, _("exit = quit")},
102 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
105 * Execute a command from the UA
107 int do_a_command(UAContext *ua, char *cmd)
116 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
121 len = strlen(ua->argk[0]);
122 for (i=0; i<comsize; i++) /* search for command */
123 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
124 stat = (*commands[i].func)(ua, cmd); /* go execute command */
129 strcat(ua->UA_sock->msg, _(": is an illegal command\n"));
130 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
131 bnet_send(ua->UA_sock);
138 * Add Volumes to an existing Pool
141 static int addcmd(UAContext *ua, char *cmd)
145 int num, i, max, startnum;
147 char name[MAX_NAME_LENGTH];
152 "You probably don't want to be using this command since it\n"
153 "creates database records without labeling the Volumes.\n"
154 "You probably want to use the \"label\" command.\n\n"));
160 memset(&pr, 0, sizeof(pr));
161 memset(&mr, 0, sizeof(mr));
163 if (!get_pool_dbr(ua, &pr)) {
167 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
168 pr.MaxVols, pr.PoolType);
170 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
171 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
173 if (!get_cmd(ua, _("Enter new maximum (zero for unlimited): "))) {
176 pr.MaxVols = atoi(ua->cmd);
177 if (pr.MaxVols < 0) {
178 bsendmsg(ua, _("Max vols must be zero or greater.\n"));
186 if ((store = get_storage_resource(ua, cmd)) != NULL) {
187 strcpy(mr.MediaType, store->media_type);
188 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
192 if (pr.MaxVols == 0) {
195 max = pr.MaxVols - pr.NumVols;
199 sprintf(buf, _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
200 if (!get_cmd(ua, buf)) {
204 if (num < 0 || num > max) {
205 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
212 if (!get_cmd(ua, _("Enter Volume name: "))) {
216 if (!get_cmd(ua, _("Enter base volume name: "))) {
220 /* Don't allow | in Volume name because it is the volume separator character */
221 if (strchr(ua->cmd, '|')) {
222 bsendmsg(ua, _("Illegal character | in a volume name.\n"));
225 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
226 bsendmsg(ua, _("Volume name too long.\n"));
229 if (strlen(ua->cmd) == 0) {
230 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
234 strcpy(name, ua->cmd);
236 strcat(name, "%04d");
239 if (!get_cmd(ua, _("Enter the starting number: "))) {
242 startnum = atoi(ua->cmd);
244 bsendmsg(ua, _("Start number must be greater than zero.\n"));
254 if (store && store->autochanger) {
255 if (!get_cmd(ua, _("Enter slot (0 for none): "))) {
258 slot = atoi(ua->cmd);
261 mr.PoolId = pr.PoolId;
262 strcpy(mr.VolStatus, "Append");
263 mr.Recycle = pr.Recycle;
264 mr.VolRetention = pr.VolRetention;
265 mr.VolUseDuration = pr.VolUseDuration;
266 mr.MaxVolJobs = pr.MaxVolJobs;
267 mr.MaxVolFiles = pr.MaxVolFiles;
268 mr.MaxVolBytes = pr.MaxVolBytes;
269 for (i=startnum; i < num+startnum; i++) {
270 sprintf(mr.VolumeName, name, i);
272 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
273 if (!db_create_media_record(ua->db, &mr)) {
274 bsendmsg(ua, db_strerror(ua->db));
278 first_id = mr.PoolId;
282 Dmsg0(200, "Update pool record.\n");
283 if (db_update_pool_record(ua->db, &pr) != 1) {
284 bsendmsg(ua, db_strerror(ua->db));
287 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
293 * Turn auto mount on/off
298 int automountcmd(UAContext *ua, char *cmd)
303 if (!get_cmd(ua, _("Turn on or off? "))) {
311 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
319 static int cancelcmd(UAContext *ua, char *cmd)
325 char JobName[MAX_NAME_LENGTH];
331 for (i=1; i<ua->argc; i++) {
332 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
336 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
337 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
341 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
345 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
346 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
352 /* If we still do not have a jcr,
353 * throw up a list and ask the user to select one.
356 /* Count Jobs running */
358 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
359 if (jcr->JobId == 0) { /* this is us */
360 free_locked_jcr(jcr);
364 free_locked_jcr(jcr);
369 bsendmsg(ua, _("No Jobs running.\n"));
372 start_prompt(ua, _("Select Job:\n"));
374 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
375 if (jcr->JobId == 0) { /* this is us */
376 free_locked_jcr(jcr);
379 add_prompt(ua, jcr->Job);
380 free_locked_jcr(jcr);
384 if (do_prompt(ua, _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
388 if (!get_cmd(ua, _("Confirm cancel (yes/no): "))) {
391 if (strcasecmp(ua->cmd, _("yes")) != 0) {
395 jcr = get_jcr_by_full_name(JobName);
397 bsendmsg(ua, _("Job %s not found.\n"), JobName);
402 switch (jcr->JobStatus) {
404 jcr->JobStatus = JS_Cancelled;
405 bsendmsg(ua, _("JobId %d, Job %s marked to be cancelled.\n"),
406 jcr->JobId, jcr->Job);
412 jcr->JobStatus = JS_Cancelled;
413 /* Cancel File daemon */
414 ua->jcr->client = jcr->client;
415 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
416 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
420 Dmsg0(200, "Connected to file daemon\n");
421 fd = ua->jcr->file_bsock;
422 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
423 while (bnet_recv(fd) >= 0) {
424 bsendmsg(ua, "%s", fd->msg);
426 bnet_sig(fd, BNET_TERMINATE);
428 ua->jcr->file_bsock = NULL;
430 /* Cancel Storage daemon */
431 ua->jcr->store = jcr->store;
432 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
433 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
437 Dmsg0(200, "Connected to storage daemon\n");
438 sd = ua->jcr->store_bsock;
439 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
440 while (bnet_recv(sd) >= 0) {
441 bsendmsg(ua, "%s", sd->msg);
443 bnet_sig(sd, BNET_TERMINATE);
445 ua->jcr->store_bsock = NULL;
454 * Create a pool record from a given Pool resource
455 * Also called from backup.c
456 * Returns: -1 on error
457 * 0 record already exists
461 int create_pool(B_DB *db, POOL *pool)
465 memset(&pr, 0, sizeof(POOL_DBR));
467 strcpy(pr.Name, pool->hdr.name);
469 if (db_get_pool_record(db, &pr)) {
470 return 0; /* exists */
473 strcpy(pr.PoolType, pool->pool_type);
474 pr.MaxVols = pool->max_volumes;
476 pr.UseOnce = pool->use_volume_once;
477 pr.UseCatalog = pool->use_catalog;
478 pr.AcceptAnyVolume = pool->accept_any_volume;
479 pr.Recycle = pool->Recycle;
480 pr.VolRetention = pool->VolRetention;
481 pr.VolUseDuration = pool->VolUseDuration;
482 pr.MaxVolJobs = pool->MaxVolJobs;
483 pr.MaxVolFiles = pool->MaxVolFiles;
484 pr.MaxVolBytes = pool->MaxVolBytes;
485 pr.AutoPrune = pool->AutoPrune;
486 if (pool->label_format) {
487 strcpy(pr.LabelFormat, pool->label_format);
489 strcpy(pr.LabelFormat, "*"); /* none */
492 if (!db_create_pool_record(db, &pr)) {
493 return -1; /* error */
501 * Create a Pool Record in the database.
502 * It is always created from the Resource record.
504 static int createcmd(UAContext *ua, char *cmd)
512 pool = get_pool_resource(ua);
517 switch (create_pool(ua->db, pool)) {
519 bsendmsg(ua, _("Error: Pool %s already exists.\n\
520 Use update to change it.\n"), pool->hdr.name);
524 bsendmsg(ua, db_strerror(ua->db));
530 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
538 * Update a Pool Record in the database.
539 * It is always updated from the Resource record.
541 * update pool=<pool-name>
542 * updates pool from Pool resource
543 * update media pool=<pool-name> volume=<volume-name>
544 * changes pool info for volume
546 static int updatecmd(UAContext *ua, char *cmd)
548 static char *kw[] = {
558 switch (find_arg_keyword(ua, kw)) {
570 start_prompt(ua, _("Update choice:\n"));
571 add_prompt(ua, _("Volume parameters"));
572 add_prompt(ua, _("Pool from resource"));
573 switch (do_prompt(ua, _("Choose catalog item to update"), NULL, 0)) {
587 * Update a media record -- allows you to change the
588 * Volume status. E.g. if you want Bacula to stop
589 * writing on the volume, set it to anything other
592 static int update_volume(UAContext *ua)
599 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
603 for (int done=0; !done; ) {
604 if (!db_get_media_record(ua->db, &mr)) {
605 if (mr.MediaId != 0) {
606 bsendmsg(ua, _("Volume record for MediaId %d not found.\n"), mr.MediaId);
608 bsendmsg(ua, _("Volume record for %s not found.\n"), mr.VolumeName);
612 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
613 start_prompt(ua, _("Parameters to modify:\n"));
614 add_prompt(ua, _("Volume Status"));
615 add_prompt(ua, _("Volume Retention Period"));
616 add_prompt(ua, _("Volume Use Duration"));
617 add_prompt(ua, _("Maximum Volume Jobs"));
618 add_prompt(ua, _("Maximum Volume Files"));
619 add_prompt(ua, _("Maximum Volume Bytes"));
620 add_prompt(ua, _("Recycle Flag"));
621 add_prompt(ua, _("Slot"));
622 add_prompt(ua, _("Done"));
623 switch (do_prompt(ua, _("Select parameter to modify"), NULL, 0)) {
624 case 0: /* Volume Status */
625 /* Modify Volume Status */
626 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
627 start_prompt(ua, _("Possible Values are:\n"));
628 add_prompt(ua, "Append"); /* Better not translate these as */
629 add_prompt(ua, "Archive"); /* They are known in the database code */
630 add_prompt(ua, "Disabled");
631 add_prompt(ua, "Full");
632 add_prompt(ua, "Used");
633 if (strcmp(mr.VolStatus, "Purged") == 0) {
634 add_prompt(ua, "Recycle");
636 add_prompt(ua, "Read-Only");
637 if (do_prompt(ua, _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
640 bstrncpy(mr.VolStatus, ua->cmd, sizeof(mr.VolStatus));
641 query = get_pool_memory(PM_MESSAGE);
642 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
643 mr.VolStatus, mr.MediaId);
644 if (!db_sql_query(ua->db, query, NULL, NULL)) {
645 bsendmsg(ua, "%s", db_strerror(ua->db));
647 bsendmsg(ua, _("New Volume status is: %s\n"), mr.VolStatus);
649 free_pool_memory(query);
651 case 1: /* Retention */
652 bsendmsg(ua, _("Current retention seconds is: %s\n"),
653 edit_utime(mr.VolRetention, ed1));
654 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
657 if (!duration_to_utime(ua->cmd, &mr.VolRetention)) {
658 bsendmsg(ua, _("Invalid retention period specified.\n"));
661 query = get_pool_memory(PM_MESSAGE);
662 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
663 edit_uint64(mr.VolRetention, ed1), mr.MediaId);
664 if (!db_sql_query(ua->db, query, NULL, NULL)) {
665 bsendmsg(ua, "%s", db_strerror(ua->db));
667 bsendmsg(ua, _("New retention seconds is: %s\n"),
668 edit_utime(mr.VolRetention, ed1));
670 free_pool_memory(query);
673 case 2: /* Use Duration */
674 bsendmsg(ua, _("Current use duration is: %s\n"),
675 edit_utime(mr.VolUseDuration, ed1));
676 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
679 if (!duration_to_utime(ua->cmd, &mr.VolUseDuration)) {
680 bsendmsg(ua, _("Invalid use duration specified.\n"));
683 query = get_pool_memory(PM_MESSAGE);
684 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
685 edit_uint64(mr.VolUseDuration, ed1), mr.MediaId);
686 if (!db_sql_query(ua->db, query, NULL, NULL)) {
687 bsendmsg(ua, "%s", db_strerror(ua->db));
689 bsendmsg(ua, _("New use duration is: %s\n"),
690 edit_utime(mr.VolUseDuration, ed1));
692 free_pool_memory(query);
695 case 3: /* Max Jobs */
697 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
698 if (!get_cmd(ua, _("Enter new Maximum Jobs: "))) {
701 maxjobs = atoi(ua->cmd);
703 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
706 query = get_pool_memory(PM_MESSAGE);
707 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%u WHERE MediaId=%u",
708 maxjobs, mr.MediaId);
709 if (!db_sql_query(ua->db, query, NULL, NULL)) {
710 bsendmsg(ua, "%s", db_strerror(ua->db));
712 bsendmsg(ua, _("New max jobs is: %u\n"), maxjobs);
714 free_pool_memory(query);
717 case 4: /* Max Files */
719 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
720 if (!get_cmd(ua, _("Enter new Maximum Files: "))) {
723 maxfiles = atoi(ua->cmd);
725 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
728 query = get_pool_memory(PM_MESSAGE);
729 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%u WHERE MediaId=%u",
730 maxfiles, mr.MediaId);
731 if (!db_sql_query(ua->db, query, NULL, NULL)) {
732 bsendmsg(ua, "%s", db_strerror(ua->db));
734 bsendmsg(ua, _("New max files is: %u\n"), maxfiles);
736 free_pool_memory(query);
739 case 5: /* Max Bytes */
741 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
742 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
745 if (!size_to_uint64(ua->cmd, strlen(ua->cmd), &maxbytes)) {
746 bsendmsg(ua, _("Invalid byte size specification.\n"));
749 query = get_pool_memory(PM_MESSAGE);
750 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
751 edit_uint64(maxbytes, ed1), mr.MediaId);
752 if (!db_sql_query(ua->db, query, NULL, NULL)) {
753 bsendmsg(ua, "%s", db_strerror(ua->db));
755 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
757 free_pool_memory(query);
761 case 6: /* Recycle */
763 bsendmsg(ua, _("Current recycle flag is: %s\n"),
764 mr.Recycle==1?_("yes"):_("no"));
765 if (!get_cmd(ua, _("Enter new Recycle status: "))) {
768 if (strcasecmp(ua->cmd, _("yes")) == 0) {
770 } else if (strcasecmp(ua->cmd, _("no")) == 0) {
773 bsendmsg(ua, _("Invalid recycle status specified.\n"));
776 query = get_pool_memory(PM_MESSAGE);
777 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
778 recycle, mr.MediaId);
779 if (!db_sql_query(ua->db, query, NULL, NULL)) {
780 bsendmsg(ua, "%s", db_strerror(ua->db));
782 bsendmsg(ua, _("New recycle flag is: %s\n"),
783 mr.Recycle==1?_("yes"):_("no"));
785 free_pool_memory(query);
790 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
791 if (!get_cmd(ua, _("Enter new Slot: "))) {
794 slot = atoi(ua->cmd);
796 bsendmsg(ua, _("Invalid slot, it must be 0 or greater\n"));
798 } else if (pr.MaxVols > 0 && slot >(int)pr.MaxVols) {
799 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
803 query = get_pool_memory(PM_MESSAGE);
804 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
806 if (!db_sql_query(ua->db, query, NULL, NULL)) {
807 bsendmsg(ua, "%s", db_strerror(ua->db));
809 bsendmsg(ua, "New Slot is: %d\n", slot);
811 free_pool_memory(query);
814 default: /* Done or error */
815 bsendmsg(ua, "Selection done.\n");
823 * Update pool record -- pull info from current POOL resource
825 static int update_pool(UAContext *ua)
832 pool = get_pool_resource(ua);
837 memset(&pr, 0, sizeof(pr));
838 strcpy(pr.Name, pool->hdr.name);
839 if (!get_pool_dbr(ua, &pr)) {
842 strcpy(pr.PoolType, pool->pool_type);
843 if (pr.MaxVols != pool->max_volumes) {
844 pr.MaxVols = pool->max_volumes;
846 if (pr.MaxVols != 0 && pr.MaxVols < pr.NumVols) {
847 pr.MaxVols = pr.NumVols;
849 pr.UseOnce = pool->use_volume_once;
850 pr.UseCatalog = pool->use_catalog;
851 pr.AcceptAnyVolume = pool->accept_any_volume;
852 pr.Recycle = pool->Recycle;
853 pr.VolRetention = pool->VolRetention;
854 pr.VolUseDuration = pool->VolUseDuration;
855 pr.MaxVolJobs = pool->MaxVolJobs;
856 pr.MaxVolFiles = pool->MaxVolFiles;
857 pr.MaxVolBytes = pool->MaxVolBytes;
859 if (pool->label_format) {
860 strcpy(pr.LabelFormat, pool->label_format);
862 strcpy(pr.LabelFormat, "*"); /* none */
864 id = db_update_pool_record(ua->db, &pr);
866 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
867 id, db_strerror(ua->db));
869 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
874 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
878 ua->jcr->store = store;
879 /* Try connecting for up to 15 seconds */
880 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
881 store->hdr.name, store->address, store->SDport);
882 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
883 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
886 Dmsg0(120, _("Connected to storage daemon\n"));
887 sd = ua->jcr->store_bsock;
888 bnet_fsend(sd, "setdebug=%d\n", level);
889 if (bnet_recv(sd) >= 0) {
890 bsendmsg(ua, "%s", sd->msg);
892 bnet_sig(sd, BNET_TERMINATE);
894 ua->jcr->store_bsock = NULL;
898 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
902 /* Connect to File daemon */
904 ua->jcr->client = client;
905 /* Try to connect for 15 seconds */
906 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
907 client->hdr.name, client->address, client->FDport);
908 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
909 bsendmsg(ua, _("Failed to connect to Client.\n"));
912 Dmsg0(120, "Connected to file daemon\n");
913 fd = ua->jcr->file_bsock;
914 bnet_fsend(fd, "setdebug=%d\n", level);
915 if (bnet_recv(fd) >= 0) {
916 bsendmsg(ua, "%s", fd->msg);
918 bnet_sig(fd, BNET_TERMINATE);
920 ua->jcr->file_bsock = NULL;
926 static void do_all_setdebug(UAContext *ua, int level)
928 STORE *store, **unique_store;
929 CLIENT *client, **unique_client;
935 /* Count Storage items */
938 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
940 unique_store = (STORE **) malloc(i * sizeof(STORE));
941 /* Find Unique Storage address/port */
942 store = (STORE *)GetNextRes(R_STORAGE, NULL);
944 unique_store[i++] = store;
945 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
947 for (j=0; j<i; j++) {
948 if (strcmp(unique_store[j]->address, store->address) == 0 &&
949 unique_store[j]->SDport == store->SDport) {
955 unique_store[i++] = store;
956 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
961 /* Call each unique Storage daemon */
962 for (j=0; j<i; j++) {
963 do_storage_setdebug(ua, unique_store[j], level);
967 /* Count Client items */
970 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
972 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
973 /* Find Unique Client address/port */
974 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
976 unique_client[i++] = client;
977 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
979 for (j=0; j<i; j++) {
980 if (strcmp(unique_client[j]->address, client->address) == 0 &&
981 unique_client[j]->FDport == client->FDport) {
987 unique_client[i++] = client;
988 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
993 /* Call each unique File daemon */
994 for (j=0; j<i; j++) {
995 do_client_setdebug(ua, unique_client[j], level);
1001 * setdebug level=nn all
1003 static int setdebugcmd(UAContext *ua, char *cmd)
1013 Dmsg1(120, "setdebug:%s:\n", cmd);
1016 for (i=1; i<ua->argc; i++) {
1017 if (strcasecmp(ua->argk[i], _("level")) == 0 && ua->argv[i]) {
1018 level = atoi(ua->argv[i]);
1023 if (!get_cmd(ua, _("Enter new debug level: "))) {
1026 level = atoi(ua->cmd);
1029 bsendmsg(ua, _("level cannot be negative.\n"));
1033 /* General debug? */
1034 for (i=1; i<ua->argc; i++) {
1035 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1036 do_all_setdebug(ua, level);
1039 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1040 strcasecmp(ua->argk[i], _("director")) == 0) {
1041 debug_level = level;
1044 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1047 client = (CLIENT *) GetResWithName(R_CLIENT, ua->argv[i]);
1049 do_client_setdebug(ua, client, level);
1053 client = select_client_resource(ua);
1055 do_client_setdebug(ua, client, level);
1059 store = get_storage_resource(ua, cmd);
1061 do_storage_setdebug(ua, store, level);
1067 * We didn't find an appropriate keyword above, so
1070 start_prompt(ua, _("Available daemons are: \n"));
1071 add_prompt(ua, _("Director"));
1072 add_prompt(ua, _("Storage"));
1073 add_prompt(ua, _("Client"));
1074 add_prompt(ua, _("All"));
1075 switch(do_prompt(ua, _("Select daemon type to set debug level"), NULL, 0)) {
1076 case 0: /* Director */
1077 debug_level = level;
1080 store = get_storage_resource(ua, cmd);
1082 do_storage_setdebug(ua, store, level);
1086 client = select_client_resource(ua);
1088 do_client_setdebug(ua, client, level);
1092 do_all_setdebug(ua, level);
1103 * Delete Pool records (should purge Media with it).
1105 * delete pool=<pool-name>
1106 * delete media pool=<pool-name> volume=<name>
1108 static int deletecmd(UAContext *ua, char *cmd)
1110 static char *keywords[] = {
1120 "In general it is not a good idea to delete either a\n"
1121 "Pool or a Volume since they may contain data.\n\n"));
1123 switch (find_arg_keyword(ua, keywords)) {
1133 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1141 bsendmsg(ua, _("Nothing done.\n"));
1148 * Delete media records from database -- dangerous
1150 static int delete_volume(UAContext *ua)
1155 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
1158 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1159 "and all Jobs saved on that volume from the Catalog\n"),
1162 if (!get_cmd(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1165 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1166 db_delete_media_record(ua->db, &mr);
1172 * Delete a pool record from the database -- dangerous
1174 static int delete_pool(UAContext *ua)
1178 memset(&pr, 0, sizeof(pr));
1180 if (!get_pool_dbr(ua, &pr)) {
1183 if (!get_cmd(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1186 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1187 db_delete_pool_record(ua->db, &pr);
1196 * label storage=xxx volume=vvv
1198 static int labelcmd(UAContext *ua, char *cmd)
1202 char dev_name[MAX_NAME_LENGTH];
1206 int mounted = FALSE;
1209 static char *keyword[] = {
1216 store = get_storage_resource(ua, cmd);
1221 i = find_arg_keyword(ua, keyword);
1222 if (i >=0 && ua->argv[i]) {
1223 strcpy(ua->cmd, ua->argv[i]);
1228 if (!get_cmd(ua, _("Enter new Volume name: "))) {
1232 if (strchr(ua->cmd, '|')) {
1233 bsendmsg(ua, _("Illegal character | in a volume name.\n"));
1236 if (strlen(ua->cmd) >= MAX_NAME_LENGTH) {
1237 bsendmsg(ua, _("Volume name too long.\n"));
1240 if (strlen(ua->cmd) == 0) {
1241 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
1246 memset(&mr, 0, sizeof(mr));
1247 strcpy(mr.VolumeName, ua->cmd);
1248 if (db_get_media_record(ua->db, &mr)) {
1249 bsendmsg(ua, _("Media record for Volume %s already exists.\n"),
1254 /* Do some more checking on slot ****FIXME**** */
1255 if (store->autochanger) {
1256 if (!get_cmd(ua, _("Enter slot (0 for none): "))) {
1259 slot = atoi(ua->cmd);
1261 strcpy(mr.MediaType, store->media_type);
1264 memset(&pr, 0, sizeof(pr));
1265 if (!select_pool_dbr(ua, &pr)) {
1268 mr.PoolId = pr.PoolId;
1269 strcpy(mr.VolStatus, "Append");
1270 mr.Recycle = pr.Recycle;
1271 mr.VolRetention = pr.VolRetention;
1273 ua->jcr->store = store;
1274 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
1275 store->hdr.name, store->address, store->SDport);
1276 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1277 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1280 sd = ua->jcr->store_bsock;
1281 strcpy(dev_name, store->dev_name);
1282 bash_spaces(dev_name);
1283 bash_spaces(mr.VolumeName);
1284 bash_spaces(mr.MediaType);
1285 bash_spaces(pr.Name);
1286 bnet_fsend(sd, _("label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d"),
1287 dev_name, mr.VolumeName, pr.Name, mr.MediaType, mr.Slot);
1288 bsendmsg(ua, "Sending label command ...\n");
1289 while (bget_msg(sd, 0) >= 0) {
1290 bsendmsg(ua, "%s", sd->msg);
1291 if (strncmp(sd->msg, "3000 OK label.", 14) == 0) {
1295 ua->jcr->store_bsock = NULL;
1296 unbash_spaces(dev_name);
1297 unbash_spaces(mr.VolumeName);
1298 unbash_spaces(mr.MediaType);
1299 unbash_spaces(pr.Name);
1301 mr.Recycle = pr.Recycle;
1302 mr.VolRetention = pr.VolRetention;
1303 mr.VolUseDuration = pr.VolUseDuration;
1304 mr.MaxVolJobs = pr.MaxVolJobs;
1305 mr.MaxVolFiles = pr.MaxVolFiles;
1306 mr.MaxVolBytes = pr.MaxVolBytes;
1307 if (db_create_media_record(ua->db, &mr)) {
1308 bsendmsg(ua, _("Media record for Volume=%s successfully created.\n"),
1310 if (ua->automount) {
1311 bsendmsg(ua, _("Requesting mount %s ...\n"), dev_name);
1312 bash_spaces(dev_name);
1313 bnet_fsend(sd, "mount %s", dev_name);
1314 unbash_spaces(dev_name);
1315 while (bnet_recv(sd) >= 0) {
1316 bsendmsg(ua, "%s", sd->msg);
1318 * 3001 OK mount. Device=xxx or
1319 * 3001 Mounted Volume vvvv
1321 if (strncmp(sd->msg, "3001 ", 5) == 0) {
1323 /***** ****FIXME***** find job waiting for
1324 ***** mount, and change to waiting for SD
1330 bsendmsg(ua, "%s", db_strerror(ua->db));
1334 bsendmsg(ua, _("Do not forget to mount the drive!!!\n"));
1336 bnet_sig(sd, BNET_TERMINATE);
1341 static void do_mount_cmd(int mount, UAContext *ua, char *cmd)
1345 char dev_name[MAX_NAME_LENGTH];
1351 Dmsg1(120, "mount: %s\n", ua->UA_sock->msg);
1353 store = get_storage_resource(ua, cmd);
1358 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1359 store->media_type, store->dev_name);
1361 ua->jcr->store = store;
1362 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1363 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1366 sd = ua->jcr->store_bsock;
1367 strcpy(dev_name, store->dev_name);
1368 bash_spaces(dev_name);
1370 bnet_fsend(sd, "mount %s", dev_name);
1372 bnet_fsend(sd, "unmount %s", dev_name);
1374 while (bnet_recv(sd) >= 0) {
1375 bsendmsg(ua, "%s", sd->msg);
1376 if (strncmp(sd->msg, "3001 OK mount.", 14) == 0) {
1377 /***** ****FIXME**** fix JobStatus */
1380 bnet_sig(sd, BNET_TERMINATE);
1382 ua->jcr->store_bsock = NULL;
1386 * mount [storage | device] <name>
1388 static int mountcmd(UAContext *ua, char *cmd)
1390 do_mount_cmd(1, ua, cmd); /* mount */
1396 * unmount [storage | device] <name>
1398 static int unmountcmd(UAContext *ua, char *cmd)
1400 do_mount_cmd(0, ua, cmd); /* unmount */
1407 * use catalog=<name>
1409 static int usecmd(UAContext *ua, char *cmd)
1411 CAT *oldcatalog, *catalog;
1414 close_db(ua); /* close any previously open db */
1415 oldcatalog = ua->catalog;
1417 if (!(catalog = get_catalog_resource(ua))) {
1418 ua->catalog = oldcatalog;
1420 ua->catalog = catalog;
1423 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1424 ua->catalog->hdr.name, ua->catalog->db_name);
1429 int quitcmd(UAContext *ua, char *cmd)
1435 static int helpcmd(UAContext *ua, char *cmd)
1440 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1441 for (i=0; i<comsize; i++) {
1442 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1448 static int versioncmd(UAContext *ua, char *cmd)
1450 bsendmsg(ua, "%s Version: " VERSION " (" DATE ")\n", my_name);
1455 /* A bit brain damaged in that if the user has not done
1456 * a "use catalog xxx" command, we simply find the first
1457 * catalog resource and open it.
1459 int open_db(UAContext *ua)
1466 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1469 bnet_fsend(ua->UA_sock, _("Could not find a Catalog resource\n"));
1472 bnet_fsend(ua->UA_sock, _("Using default Catalog name=%s DB=%s\n"),
1473 ua->catalog->hdr.name, ua->catalog->db_name);
1477 Dmsg0(150, "Open database\n");
1478 ua->db = db_init_database(NULL, ua->catalog->db_name, ua->catalog->db_user,
1479 ua->catalog->db_password);
1480 if (!db_open_database(ua->db)) {
1481 bnet_fsend(ua->UA_sock, _("Could not open DB %s: ERR=%s"),
1482 ua->catalog->db_name, db_strerror(ua->db));
1486 ua->jcr->db = ua->db;
1487 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1491 void close_db(UAContext *ua)
1494 db_close_database(ua->db);