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(20, "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];
150 "You probably don't want to be using this command since it\n"
151 "creates database records without labeling the Volumes.\n"
152 "You probably want to use the label command.\n\n"));
158 memset(&pr, 0, sizeof(pr));
159 memset(&mr, 0, sizeof(mr));
161 if (!get_pool_dbr(ua, &pr)) {
165 Dmsg4(20, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
166 pr.MaxVols, pr.PoolType);
168 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
169 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
171 if (!get_cmd(ua, _("Enter new maximum (zero for unlimited): "))) {
174 pr.MaxVols = atoi(ua->cmd);
175 if (pr.MaxVols < 0) {
176 bsendmsg(ua, _("Max vols must be zero or greater.\n"));
183 if (!get_media_type(ua, mr.MediaType)) {
187 if (pr.MaxVols == 0) {
190 max = pr.MaxVols - pr.NumVols;
194 sprintf(buf, _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
195 if (!get_cmd(ua, buf)) {
199 if (num < 0 || num > max) {
200 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
207 if (!get_cmd(ua, _("Enter Volume name: "))) {
211 if (!get_cmd(ua, _("Enter base volume name: "))) {
215 if (strchr(ua->cmd, '|')) {
216 bsendmsg(ua, _("Illegal character | in a volume name.\n"));
219 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
220 bsendmsg(ua, _("Volume name too long.\n"));
223 if (strlen(ua->cmd) == 0) {
224 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
228 strcpy(name, ua->cmd);
230 strcat(name, "%04d");
233 if (!get_cmd(ua, _("Enter the starting number: "))) {
236 startnum = atoi(ua->cmd);
238 bsendmsg(ua, _("Start number must be greater than zero.\n"));
248 mr.PoolId = pr.PoolId;
249 strcpy(mr.VolStatus, "Append");
250 mr.Recycle = pr.Recycle;
251 mr.VolRetention = pr.VolRetention;
252 for (i=startnum; i < num+startnum; i++) {
253 sprintf(mr.VolumeName, name, i);
254 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
255 if (!db_create_media_record(ua->db, &mr)) {
256 bsendmsg(ua, db_strerror(ua->db));
260 first_id = mr.PoolId;
264 Dmsg0(200, "Update pool record.\n");
265 if (db_update_pool_record(ua->db, &pr) != 1) {
266 bsendmsg(ua, db_strerror(ua->db));
269 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
275 * Turn auto mount on/off
280 int automountcmd(UAContext *ua, char *cmd)
285 if (!get_cmd(ua, _("Turn on or off? "))) {
293 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
301 static int cancelcmd(UAContext *ua, char *cmd)
307 char JobName[MAX_NAME_LENGTH];
313 for (i=1; i<ua->argc; i++) {
314 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
318 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
319 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
323 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
327 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
328 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
334 /* If we still do not have a jcr,
335 * throw up a list and ask the user to select one.
338 /* Count Jobs running */
340 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
341 if (jcr->JobId == 0) { /* this is us */
342 free_locked_jcr(jcr);
346 free_locked_jcr(jcr);
351 bsendmsg(ua, _("No Jobs running.\n"));
354 start_prompt(ua, _("Select Job:\n"));
356 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
357 if (jcr->JobId == 0) { /* this is us */
358 free_locked_jcr(jcr);
361 add_prompt(ua, jcr->Job);
362 free_locked_jcr(jcr);
366 if (do_prompt(ua, _("Choose Job to cancel"), JobName) < 0) {
370 if (!get_cmd(ua, _("Confirm cancel (yes/no): "))) {
373 if (strcasecmp(ua->cmd, _("yes")) != 0) {
377 jcr = get_jcr_by_full_name(JobName);
379 bsendmsg(ua, _("Job %s not found.\n"), JobName);
384 switch (jcr->JobStatus) {
386 jcr->JobStatus = JS_Cancelled;
387 bsendmsg(ua, _("JobId %d, Job %s marked to be cancelled.\n"),
388 jcr->JobId, jcr->Job);
394 jcr->JobStatus = JS_Cancelled;
395 /* Cancel File daemon */
396 ua->jcr->client = jcr->client;
397 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
398 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
402 Dmsg0(200, "Connected to file daemon\n");
403 fd = ua->jcr->file_bsock;
404 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
405 while (bnet_recv(fd) > 0) {
406 bsendmsg(ua, "%s", fd->msg);
408 bnet_sig(fd, BNET_TERMINATE);
410 ua->jcr->file_bsock = NULL;
412 /* Cancel Storage daemon */
413 ua->jcr->store = jcr->store;
414 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
415 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
419 Dmsg0(200, "Connected to storage daemon\n");
420 sd = ua->jcr->store_bsock;
421 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
422 while (bnet_recv(sd) > 0) {
423 bsendmsg(ua, "%s", sd->msg);
425 bnet_sig(sd, BNET_TERMINATE);
427 ua->jcr->store_bsock = NULL;
436 * Create a pool record from a given Pool resource
437 * Also called from backup.c
438 * Returns: -1 on error
439 * 0 record already exists
443 int create_pool(B_DB *db, POOL *pool)
447 memset(&pr, 0, sizeof(POOL_DBR));
449 strcpy(pr.Name, pool->hdr.name);
451 if (db_get_pool_record(db, &pr)) {
452 return 0; /* exists */
455 strcpy(pr.PoolType, pool->pool_type);
456 pr.MaxVols = pool->max_volumes;
458 pr.UseOnce = pool->use_volume_once;
459 pr.UseCatalog = pool->use_catalog;
460 pr.AcceptAnyVolume = pool->accept_any_volume;
461 pr.Recycle = pool->Recycle;
462 pr.VolRetention = pool->VolRetention;
463 pr.AutoPrune = pool->AutoPrune;
464 if (pool->label_format) {
465 strcpy(pr.LabelFormat, pool->label_format);
467 strcpy(pr.LabelFormat, "*"); /* none */
470 if (!db_create_pool_record(db, &pr)) {
471 return -1; /* error */
479 * Create a Pool Record in the database.
480 * It is always created from the Resource record.
482 static int createcmd(UAContext *ua, char *cmd)
490 pool = get_pool_resource(ua);
495 switch (create_pool(ua->db, pool)) {
497 bsendmsg(ua, _("Error: Pool %s already exists.\n\
498 Use update to change it.\n"), pool->hdr.name);
502 bsendmsg(ua, db_strerror(ua->db));
508 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
516 * Update a Pool Record in the database.
517 * It is always updated from the Resource record.
519 * update pool=<pool-name>
520 * updates pool from Pool resource
521 * update media pool=<pool-name> volume=<volume-name>
522 * changes pool info for volume
524 static int updatecmd(UAContext *ua, char *cmd)
526 static char *kw[] = {
536 switch (find_arg_keyword(ua, kw)) {
548 start_prompt(ua, _("Update choice:\n"));
549 add_prompt(ua, _("Volume parameters"));
550 add_prompt(ua, _("Pool from resource"));
551 switch (do_prompt(ua, _("Choose catalog item to update"), NULL)) {
565 * Update a media record -- allows you to change the
566 * Volume status. E.g. if you want Bacula to stop
567 * writing on the volume, set it to anything other
570 static int update_volume(UAContext *ua)
577 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
581 for (int done=0; !done; ) {
582 if (!db_get_media_record(ua->db, &mr)) {
583 if (mr.MediaId != 0) {
584 bsendmsg(ua, _("Volume record for MediaId %d not found.\n"), mr.MediaId);
586 bsendmsg(ua, _("Volume record for %s not found.\n"), mr.VolumeName);
590 bsendmsg(ua, _("Updating Volume %s\n"), mr.VolumeName);
591 start_prompt(ua, _("Parameters to modify:\n"));
592 add_prompt(ua, _("Volume Status"));
593 add_prompt(ua, _("Volume Retention Period"));
594 add_prompt(ua, _("Recycle Flag"));
595 add_prompt(ua, _("Slot"));
596 add_prompt(ua, _("Done"));
597 switch (do_prompt(ua, _("Select parameter to modify"), NULL)) {
598 case 0: /* Volume Status */
599 /* Modify Volume Status */
600 bsendmsg(ua, _("Current value is: %s\n"), mr.VolStatus);
601 start_prompt(ua, _("Possible Values are:\n"));
602 add_prompt(ua, "Append");
603 add_prompt(ua, "Archive");
604 add_prompt(ua, "Disabled");
605 add_prompt(ua, "Full");
606 if (strcmp(mr.VolStatus, "Purged") == 0) {
607 add_prompt(ua, "Recycle");
609 add_prompt(ua, "Read-Only");
610 if (do_prompt(ua, _("Choose new Volume Status"), ua->cmd) < 0) {
613 strcpy(mr.VolStatus, ua->cmd);
614 query = get_pool_memory(PM_MESSAGE);
615 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%d",
616 mr.VolStatus, mr.MediaId);
617 if (!db_sql_query(ua->db, query, NULL, NULL)) {
618 bsendmsg(ua, "%s", db_strerror(ua->db));
620 free_pool_memory(query);
622 case 1: /* Retention */
623 bsendmsg(ua, _("Current value is: %s\n"),
624 edit_btime(mr.VolRetention, ed1));
625 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
628 if (!string_to_btime(ua->cmd, &mr.VolRetention)) {
629 bsendmsg(ua, _("Invalid retention period specified.\n"));
632 query = get_pool_memory(PM_MESSAGE);
633 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%d",
634 edit_uint64(mr.VolRetention, ed1), mr.MediaId);
635 if (!db_sql_query(ua->db, query, NULL, NULL)) {
636 bsendmsg(ua, "%s", db_strerror(ua->db));
638 free_pool_memory(query);
640 case 2: /* Recycle */
642 bsendmsg(ua, _("Current value is: %s\n"),
643 mr.Recycle==1?_("yes"):_("no"));
644 if (!get_cmd(ua, _("Enter new Recycle status: "))) {
647 if (strcasecmp(ua->cmd, _("yes")) == 0) {
649 } else if (strcasecmp(ua->cmd, _("no")) == 0) {
652 bsendmsg(ua, _("Invalid recycle status specified.\n"));
655 query = get_pool_memory(PM_MESSAGE);
656 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%d",
657 recycle, mr.MediaId);
658 if (!db_sql_query(ua->db, query, NULL, NULL)) {
659 bsendmsg(ua, "%s", db_strerror(ua->db));
661 free_pool_memory(query);
666 bsendmsg(ua, _("Current value is: %d\n"), mr.Slot);
667 if (!get_cmd(ua, _("Enter new Slot: "))) {
670 slot = atoi(ua->cmd);
672 bsendmsg(ua, _("Invalid slot, it must be 0 or greater\n"));
674 } else if (pr.MaxVols > 0 && slot >(int)pr.MaxVols) {
675 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
679 query = get_pool_memory(PM_MESSAGE);
680 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%d",
682 if (!db_sql_query(ua->db, query, NULL, NULL)) {
683 bsendmsg(ua, "%s", db_strerror(ua->db));
685 bsendmsg(ua, "New value is: %d\n", slot);
687 free_pool_memory(query);
691 default: /* Done or error */
692 bsendmsg(ua, "Selection done.\n");
700 * Update pool record -- pull info from current POOL resource
702 static int update_pool(UAContext *ua)
709 pool = get_pool_resource(ua);
714 memset(&pr, 0, sizeof(pr));
715 strcpy(pr.Name, pool->hdr.name);
716 if (!get_pool_dbr(ua, &pr)) {
719 strcpy(pr.PoolType, pool->pool_type);
720 if (pr.MaxVols != (uint32_t) (pool->max_volumes)) {
721 pr.MaxVols = pool->max_volumes;
723 if (pr.MaxVols != 0 && pr.MaxVols < pr.NumVols) {
724 pr.MaxVols = pr.NumVols;
726 pr.UseOnce = pool->use_volume_once;
727 pr.UseCatalog = pool->use_catalog;
728 pr.AcceptAnyVolume = pool->accept_any_volume;
729 if (pool->label_format) {
730 strcpy(pr.LabelFormat, pool->label_format);
732 strcpy(pr.LabelFormat, "*"); /* none */
734 id = db_update_pool_record(ua->db, &pr);
736 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
737 id, db_strerror(ua->db));
739 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
744 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
748 ua->jcr->store = store;
749 /* Try connecting for up to 15 seconds */
750 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
751 store->hdr.name, store->address, store->SDport);
752 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
753 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
756 Dmsg0(20, _("Connected to storage daemon\n"));
757 sd = ua->jcr->store_bsock;
758 bnet_fsend(sd, "setdebug=%d\n", level);
759 if (bnet_recv(sd) > 0) {
760 bsendmsg(ua, "%s", sd->msg);
762 bnet_sig(sd, BNET_TERMINATE);
764 ua->jcr->store_bsock = NULL;
768 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
772 /* Connect to File daemon */
774 ua->jcr->client = client;
775 /* Try to connect for 15 seconds */
776 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
777 client->hdr.name, client->address, client->FDport);
778 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
779 bsendmsg(ua, _("Failed to connect to Client.\n"));
782 Dmsg0(20, "Connected to file daemon\n");
783 fd = ua->jcr->file_bsock;
784 bnet_fsend(fd, "setdebug=%d\n", level);
785 if (bnet_recv(fd) > 0) {
786 bsendmsg(ua, "%s", fd->msg);
788 bnet_sig(fd, BNET_TERMINATE);
790 ua->jcr->file_bsock = NULL;
796 static void do_all_setdebug(UAContext *ua, int level)
798 STORE *store, **unique_store;
799 CLIENT *client, **unique_client;
805 /* Count Storage items */
808 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
810 unique_store = (STORE **) malloc(i * sizeof(STORE));
811 /* Find Unique Storage address/port */
812 store = (STORE *)GetNextRes(R_STORAGE, NULL);
814 unique_store[i++] = store;
815 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
817 for (j=0; j<i; j++) {
818 if (strcmp(unique_store[j]->address, store->address) == 0 &&
819 unique_store[j]->SDport == store->SDport) {
825 unique_store[i++] = store;
826 Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
831 /* Call each unique Storage daemon */
832 for (j=0; j<i; j++) {
833 do_storage_setdebug(ua, unique_store[j], level);
837 /* Count Client items */
840 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
842 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
843 /* Find Unique Client address/port */
844 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
846 unique_client[i++] = client;
847 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
849 for (j=0; j<i; j++) {
850 if (strcmp(unique_client[j]->address, client->address) == 0 &&
851 unique_client[j]->FDport == client->FDport) {
857 unique_client[i++] = client;
858 Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
863 /* Call each unique File daemon */
864 for (j=0; j<i; j++) {
865 do_client_setdebug(ua, unique_client[j], level);
871 * setdebug level=nn all
873 static int setdebugcmd(UAContext *ua, char *cmd)
883 Dmsg1(20, "setdebug:%s:\n", cmd);
886 for (i=1; i<ua->argc; i++) {
887 if (strcasecmp(ua->argk[i], _("level")) == 0 && ua->argv[i]) {
888 level = atoi(ua->argv[i]);
893 if (!get_cmd(ua, _("Enter new debug level: "))) {
896 level = atoi(ua->cmd);
899 bsendmsg(ua, _("level cannot be negative.\n"));
904 for (i=1; i<ua->argc; i++) {
905 if (strcasecmp(ua->argk[i], _("all")) == 0) {
906 do_all_setdebug(ua, level);
909 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
910 strcasecmp(ua->argk[i], _("director")) == 0) {
914 if (strcasecmp(ua->argk[i], _("client")) == 0) {
917 client = (CLIENT *) GetResWithName(R_CLIENT, ua->argv[i]);
919 do_client_setdebug(ua, client, level);
923 client = select_client_resource(ua);
925 do_client_setdebug(ua, client, level);
929 store = get_storage_resource(ua, cmd);
931 do_storage_setdebug(ua, store, level);
937 * We didn't find an appropriate keyword above, so
940 start_prompt(ua, _("Available daemons are: \n"));
941 add_prompt(ua, _("Director"));
942 add_prompt(ua, _("Storage"));
943 add_prompt(ua, _("Client"));
944 add_prompt(ua, _("All"));
945 switch(do_prompt(ua, _("Select daemon type to set debug level"), NULL)) {
946 case 0: /* Director */
950 store = get_storage_resource(ua, cmd);
952 do_storage_setdebug(ua, store, level);
956 client = select_client_resource(ua);
958 do_client_setdebug(ua, client, level);
962 do_all_setdebug(ua, level);
973 * Delete Pool records (should purge Media with it).
975 * delete pool=<pool-name>
976 * delete media pool=<pool-name> volume=<name>
978 static int deletecmd(UAContext *ua, char *cmd)
980 static char *keywords[] = {
990 "In general it is not a good idea to delete either a\n"
991 "Pool or Media since in both cases, you may delete Media\n"
992 "that contain data.\n\n"));
994 switch (find_arg_keyword(ua, keywords)) {
1004 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1012 bsendmsg(ua, _("Nothing done.\n"));
1019 * Delete media records from database -- dangerous
1021 static int delete_volume(UAContext *ua)
1026 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
1029 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1030 "and all Jobs saved on that volume from the Catalog\n"),
1033 if (!get_cmd(ua, _("If you want to continue enter pretty please: "))) {
1036 if (strcmp(ua->cmd, _("pretty please")) == 0) {
1037 db_delete_media_record(ua->db, &mr);
1043 * Delete a pool record from the database -- dangerous
1045 static int delete_pool(UAContext *ua)
1049 memset(&pr, 0, sizeof(pr));
1051 if (!get_pool_dbr(ua, &pr)) {
1054 if (!get_cmd(ua, _("If you want to continue enter pretty please: "))) {
1057 if (strcmp(ua->cmd, _("pretty please")) == 0) {
1058 db_delete_pool_record(ua->db, &pr);
1067 * label storage=xxx volume=vvv
1069 static int labelcmd(UAContext *ua, char *cmd)
1073 char dev_name[MAX_NAME_LENGTH];
1077 int mounted = FALSE;
1079 static char *keyword[] = {
1086 store = get_storage_resource(ua, cmd);
1091 i = find_arg_keyword(ua, keyword);
1092 if (i >=0 && ua->argv[i]) {
1093 strcpy(ua->cmd, ua->argv[i]);
1098 if (!get_cmd(ua, _("Enter new Volume name: "))) {
1102 if (strchr(ua->cmd, '|')) {
1103 bsendmsg(ua, _("Illegal character | in a volume name.\n"));
1106 if (strlen(ua->cmd) >= MAX_NAME_LENGTH) {
1107 bsendmsg(ua, _("Volume name too long.\n"));
1110 if (strlen(ua->cmd) == 0) {
1111 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
1115 memset(&mr, 0, sizeof(mr));
1116 strcpy(mr.VolumeName, ua->cmd);
1117 if (db_get_media_record(ua->db, &mr)) {
1118 bsendmsg(ua, _("Media record for Volume %s already exists.\n"),
1122 strcpy(mr.MediaType, store->media_type);
1124 memset(&pr, 0, sizeof(pr));
1125 if (!select_pool_dbr(ua, &pr)) {
1128 mr.PoolId = pr.PoolId;
1129 strcpy(mr.VolStatus, "Append");
1130 mr.Recycle = pr.Recycle;
1131 mr.VolRetention = pr.VolRetention;
1133 ua->jcr->store = store;
1134 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
1135 store->hdr.name, store->address, store->SDport);
1136 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1137 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1140 sd = ua->jcr->store_bsock;
1141 strcpy(dev_name, store->dev_name);
1142 bash_spaces(dev_name);
1143 bash_spaces(mr.VolumeName);
1144 bash_spaces(mr.MediaType);
1145 bash_spaces(pr.Name);
1146 bnet_fsend(sd, _("label %s VolumeName=%s PoolName=%s MediaType=%s"),
1147 dev_name, mr.VolumeName, pr.Name, mr.MediaType);
1148 bsendmsg(ua, "Sending label command ...\n");
1149 while (bget_msg(sd, 0) > 0) {
1150 bsendmsg(ua, "%s", sd->msg);
1151 if (strncmp(sd->msg, "3000 OK label.", 14) == 0) {
1155 ua->jcr->store_bsock = NULL;
1156 unbash_spaces(dev_name);
1157 unbash_spaces(mr.VolumeName);
1158 unbash_spaces(mr.MediaType);
1159 unbash_spaces(pr.Name);
1161 if (db_create_media_record(ua->db, &mr)) {
1162 bsendmsg(ua, _("Media record for Volume=%s successfully created.\n"),
1164 if (ua->automount) {
1165 bsendmsg(ua, _("Requesting mount %s ...\n"), dev_name);
1166 bash_spaces(dev_name);
1167 bnet_fsend(sd, "mount %s", dev_name);
1168 unbash_spaces(dev_name);
1169 while (bnet_recv(sd) > 0) {
1170 bsendmsg(ua, "%s", sd->msg);
1172 * 3001 OK mount. Device=xxx or
1173 * 3001 Mounted Volume vvvv
1175 if (strncmp(sd->msg, "3001 ", 5) == 0) {
1177 /***** ****FIXME***** find job waiting for
1178 ***** mount, and change to waiting for SD
1184 bsendmsg(ua, "%s", db_strerror(ua->db));
1188 bsendmsg(ua, _("Do not forget to mount the drive!!!\n"));
1190 bnet_sig(sd, BNET_TERMINATE);
1195 static void do_mount_cmd(int mount, UAContext *ua, char *cmd)
1199 char dev_name[MAX_NAME_LENGTH];
1205 Dmsg1(20, "mount: %s\n", ua->UA_sock->msg);
1207 store = get_storage_resource(ua, cmd);
1212 Dmsg2(20, "Found storage, MediaType=%s DevName=%s\n",
1213 store->media_type, store->dev_name);
1215 ua->jcr->store = store;
1216 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1217 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1220 sd = ua->jcr->store_bsock;
1221 strcpy(dev_name, store->dev_name);
1222 bash_spaces(dev_name);
1224 bnet_fsend(sd, "mount %s", dev_name);
1226 bnet_fsend(sd, "unmount %s", dev_name);
1228 while (bnet_recv(sd) > 0) {
1229 bsendmsg(ua, "%s", sd->msg);
1230 if (strncmp(sd->msg, "3001 OK mount.", 14) == 0) {
1231 /***** ****FIXME**** fix JobStatus */
1234 bnet_sig(sd, BNET_TERMINATE);
1236 ua->jcr->store_bsock = NULL;
1240 * mount [storage | device] <name>
1242 static int mountcmd(UAContext *ua, char *cmd)
1244 do_mount_cmd(1, ua, cmd); /* mount */
1250 * unmount [storage | device] <name>
1252 static int unmountcmd(UAContext *ua, char *cmd)
1254 do_mount_cmd(0, ua, cmd); /* unmount */
1261 * use catalog=<name>
1263 static int usecmd(UAContext *ua, char *cmd)
1265 CAT *oldcatalog, *catalog;
1268 close_db(ua); /* close any previously open db */
1269 oldcatalog = ua->catalog;
1271 if (!(catalog = get_catalog_resource(ua))) {
1272 ua->catalog = oldcatalog;
1274 ua->catalog = catalog;
1277 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1278 ua->catalog->hdr.name, ua->catalog->db_name);
1283 int quitcmd(UAContext *ua, char *cmd)
1289 static int helpcmd(UAContext *ua, char *cmd)
1294 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1295 for (i=0; i<comsize; i++) {
1296 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1302 static int versioncmd(UAContext *ua, char *cmd)
1304 bsendmsg(ua, "%s Version: " VERSION " (" DATE ")\n", my_name);
1309 /* A bit brain damaged in that if the user has not done
1310 * a "use catalog xxx" command, we simply find the first
1311 * catalog resource and open it.
1313 int open_db(UAContext *ua)
1320 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1323 bnet_fsend(ua->UA_sock, _("Could not find a Catalog resource\n"));
1326 bnet_fsend(ua->UA_sock, _("Using default Catalog name=%s DB=%s\n"),
1327 ua->catalog->hdr.name, ua->catalog->db_name);
1331 Dmsg0(50, "Open database\n");
1332 ua->db = db_init_database(ua->catalog->db_name, ua->catalog->db_user,
1333 ua->catalog->db_password);
1334 if (!db_open_database(ua->db)) {
1335 bnet_fsend(ua->UA_sock, _("Could not open DB %s: ERR=%s"),
1336 ua->catalog->db_name, db_strerror(ua->db));
1340 ua->jcr->db = ua->db;
1341 Dmsg1(50, "DB %s opened\n", ua->catalog->db_name);
1345 void close_db(UAContext *ua)
1348 db_close_database(ua->db);