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 int console_msg_pending;
43 extern char my_name[];
45 /* Imported functions */
46 extern int statuscmd(UAContext *ua, char *cmd);
47 extern int listcmd(UAContext *ua, char *cmd);
48 extern int showcmd(UAContext *ua, char *cmd);
49 extern int messagescmd(UAContext *ua, char *cmd);
50 extern int autodisplaycmd(UAContext *ua, char *cmd);
51 extern int sqlquerycmd(UAContext *ua, char *cmd);
52 extern int querycmd(UAContext *ua, char *cmd);
53 extern int runcmd(UAContext *ua, char *cmd);
54 extern int retentioncmd(UAContext *ua, char *cmd);
55 extern int prunecmd(UAContext *ua, char *cmd);
56 extern int purgecmd(UAContext *ua, char *cmd);
58 /* Forward referenced functions */
59 static int addcmd(UAContext *ua, char *cmd), createcmd(UAContext *ua, char *cmd), cancelcmd(UAContext *ua, char *cmd);
60 static int setdebugcmd(UAContext *ua, char *cmd);
61 static int helpcmd(UAContext *ua, char *cmd);
62 static int deletecmd(UAContext *ua, char *cmd);
63 static int usecmd(UAContext *ua, char *cmd), unmountcmd(UAContext *ua, char *cmd);
64 static int labelcmd(UAContext *ua, char *cmd), mountcmd(UAContext *ua, char *cmd), updatecmd(UAContext *ua, char *cmd);
65 static int versioncmd(UAContext *ua, char *cmd), automountcmd(UAContext *ua, char *cmd);
66 static int update_volume(UAContext *ua);
67 static int update_pool(UAContext *ua);
68 static int delete_volume(UAContext *ua);
69 static int delete_pool(UAContext *ua);
71 int quitcmd(UAContext *ua, char *cmd);
74 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
75 static struct cmdstruct commands[] = {
76 { N_("add"), addcmd, _("add media to a pool")},
77 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
78 { N_("automount"), automountcmd, _("automount [on/off] -- after label")},
79 { N_("cancel"), cancelcmd, _("cancel job=nnn -- cancel a job")},
80 { N_("create"), createcmd, _("create DB Pool from resource")},
81 { N_("delete"), deletecmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
82 { N_("help"), helpcmd, _("print this command")},
83 { N_("label"), labelcmd, _("label a tape")},
84 { N_("list"), listcmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
85 { N_("messages"), messagescmd, _("messages")},
86 { N_("mount"), mountcmd, _("mount <storage-name>")},
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 DB Pool from resource")},
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"));
224 strcpy(name, ua->cmd);
226 strcat(name, "%04d");
229 if (!get_cmd(ua, _("Enter the starting number: "))) {
232 startnum = atoi(ua->cmd);
234 bsendmsg(ua, _("Start number must be greater than zero.\n"));
244 mr.PoolId = pr.PoolId;
245 strcpy(mr.VolStatus, "Append");
246 mr.Recycle = pr.Recycle;
247 mr.VolRetention = pr.VolRetention;
248 for (i=startnum; i < num+startnum; i++) {
249 sprintf(mr.VolumeName, name, i);
250 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
251 if (!db_create_media_record(ua->db, &mr)) {
252 bsendmsg(ua, db_strerror(ua->db));
256 first_id = mr.PoolId;
260 Dmsg0(200, "Update pool record.\n");
261 if (db_update_pool_record(ua->db, &pr) != 1) {
262 bsendmsg(ua, db_strerror(ua->db));
265 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
271 * Turn auto mount on/off
276 int automountcmd(UAContext *ua, char *cmd)
281 if (!get_cmd(ua, _("Turn on or off? "))) {
289 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
297 static int cancelcmd(UAContext *ua, char *cmd)
303 char JobName[MAX_NAME_LENGTH];
309 for (i=1; i<ua->argc; i++) {
310 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
314 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
315 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
319 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
323 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
324 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
330 /* If we still do not have a jcr,
331 * throw up a list and ask the user to select one.
334 /* Count Jobs running */
336 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
337 if (jcr->JobId == 0) { /* this is us */
338 free_locked_jcr(jcr);
342 free_locked_jcr(jcr);
346 bsendmsg(ua, _("No Jobs running.\n"));
349 start_prompt(ua, _("Select Job:\n"));
351 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
352 if (jcr->JobId == 0) { /* this is us */
353 free_locked_jcr(jcr);
356 add_prompt(ua, jcr->Job);
357 free_locked_jcr(jcr);
360 if (do_prompt(ua, _("Choose Job to cancel"), JobName) < 0) {
364 if (!get_cmd(ua, _("Confirm cancel (yes/no): "))) {
367 if (strcasecmp(ua->cmd, _("yes")) != 0) {
371 jcr = get_jcr_by_full_name(JobName);
373 bsendmsg(ua, _("Job %s not found.\n"), JobName);
378 switch (jcr->JobStatus) {
380 jcr->JobStatus = JS_Cancelled;
381 bsendmsg(ua, _("JobId %d, Job %s marked to be cancelled.\n"),
382 jcr->JobId, jcr->Job);
388 jcr->JobStatus = JS_Cancelled;
389 /* Cancel File daemon */
390 ua->jcr->client = jcr->client;
391 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
392 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
396 Dmsg0(200, "Connected to file daemon\n");
397 fd = ua->jcr->file_bsock;
398 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
399 while (bnet_recv(fd) > 0) {
400 bsendmsg(ua, "%s", fd->msg);
402 bnet_sig(fd, BNET_TERMINATE);
404 ua->jcr->file_bsock = NULL;
406 /* Cancel Storage daemon */
407 ua->jcr->store = jcr->store;
408 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
409 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
413 Dmsg0(200, "Connected to storage daemon\n");
414 sd = ua->jcr->store_bsock;
415 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
416 while (bnet_recv(sd) > 0) {
417 bsendmsg(ua, "%s", sd->msg);
419 bnet_sig(sd, BNET_TERMINATE);
421 ua->jcr->store_bsock = NULL;
430 * Create a pool record from a given Pool resource
431 * Also called from backup.c
432 * Returns: -1 on error
433 * 0 record already exists
437 int create_pool(B_DB *db, POOL *pool)
441 memset(&pr, 0, sizeof(POOL_DBR));
443 strcpy(pr.Name, pool->hdr.name);
445 if (db_get_pool_record(db, &pr)) {
446 return 0; /* exists */
449 strcpy(pr.PoolType, pool->pool_type);
450 pr.MaxVols = pool->max_volumes;
452 pr.UseOnce = pool->use_volume_once;
453 pr.UseCatalog = pool->use_catalog;
454 pr.AcceptAnyVolume = pool->accept_any_volume;
455 pr.Recycle = pool->Recycle;
456 pr.VolRetention = pool->VolRetention;
457 pr.AutoPrune = pool->AutoPrune;
458 if (pool->label_format) {
459 strcpy(pr.LabelFormat, pool->label_format);
461 strcpy(pr.LabelFormat, "*"); /* none */
464 if (!db_create_pool_record(db, &pr)) {
465 return -1; /* error */
473 * Create a Pool Record in the database.
474 * It is always created from the Resource record.
476 static int createcmd(UAContext *ua, char *cmd)
484 pool = get_pool_resource(ua);
489 switch (create_pool(ua->db, pool)) {
491 bsendmsg(ua, _("Error: Pool %s already exists.\n\
492 Use update to change it.\n"), pool->hdr.name);
496 bsendmsg(ua, db_strerror(ua->db));
509 * Update a Pool Record in the database.
510 * It is always updated from the Resource record.
512 * update pool=<pool-name>
513 * updates pool from Pool resource
514 * update media pool=<pool-name> volume=<volume-name>
515 * changes pool info for volume
517 static int updatecmd(UAContext *ua, char *cmd)
519 static char *kw[] = {
529 switch (find_arg_keyword(ua, kw)) {
541 start_prompt(ua, _("Update choice:\n"));
542 add_prompt(ua, _("pool"));
543 add_prompt(ua, _("volume"));
544 switch (do_prompt(ua, _("Choose catalog item to update"), NULL)) {
558 * Update a media record -- allows you to change the
559 * Volume status. E.g. if you want Bacula to stop
560 * writing on the volume, set it to anything other
563 static int update_volume(UAContext *ua)
568 static char *kw[] = {
574 memset(&pr, 0, sizeof(pr));
575 memset(&mr, 0, sizeof(mr));
576 if (!get_pool_dbr(ua, &pr)) {
579 mr.PoolId = pr.PoolId;
580 mr.VolumeName[0] = 0;
582 i = find_arg_keyword(ua, kw);
583 if (i == 0 && ua->argv[i]) {
584 strcpy(mr.VolumeName, ua->argv[i]);
586 if (mr.VolumeName[0] == 0) {
587 db_list_media_records(ua->db, &mr, prtit, ua);
588 if (!get_cmd(ua, _("Enter Volume name to update: "))) {
591 strcpy(mr.VolumeName, ua->cmd);
594 if (!db_get_media_record(ua->db, &mr)) {
595 bsendmsg(ua, _("Volume record for %s not found.\n"), mr.VolumeName);
599 for (int done=0; !done; ) {
600 start_prompt(ua, _("Parameters to modify:\n"));
601 add_prompt(ua, _("Volume Status"));
602 add_prompt(ua, _("Volume Retention"));
603 add_prompt(ua, _("Done"));
604 switch (do_prompt(ua, _("Select paramter to modify"), NULL)) {
605 case 0: /* Volume Status */
607 bsendmsg(ua, _("Current value is: %s\n"), mr.VolStatus);
608 start_prompt(ua, _("Possible Values are:\n"));
609 add_prompt(ua, "Append");
610 add_prompt(ua, "Archive");
611 add_prompt(ua, "Disabled");
612 add_prompt(ua, "Full");
613 if (strcmp(mr.VolStatus, "Purged") == 0) {
614 add_prompt(ua, "Recycle");
616 add_prompt(ua, "Read-Only");
617 if (do_prompt(ua, _("Choose new Volume Status"), ua->cmd) < 0) {
620 strcpy(mr.VolStatus, ua->cmd);
621 query = (char *)get_pool_memory(PM_MESSAGE);
622 Mmsg(&query, "UPDATE Media SET VolStatus=\"%s\" WHERE MediaId=%d",
623 mr.VolStatus, mr.MediaId);
624 if (!db_sql_query(ua->db, query, NULL, NULL)) {
625 bsendmsg(ua, "%s", db_strerror(ua->db));
627 free_pool_memory(query);
629 case 1: /* Retention */
630 bsendmsg(ua, _("Current value is: %s\n"),
631 edit_btime(mr.VolRetention, ed1));
632 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
635 if (!string_to_btime(ua->cmd, &mr.VolRetention)) {
636 bsendmsg(ua, _("Invalid retention period specified.\n"));
639 query = (char *)get_pool_memory(PM_MESSAGE);
640 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%d",
641 edit_uint64(mr.VolRetention, ed1), mr.MediaId);
642 if (!db_sql_query(ua->db, query, NULL, NULL)) {
643 bsendmsg(ua, "%s", db_strerror(ua->db));
645 free_pool_memory(query);
647 default: /* Done or error */
655 * Update pool record -- pull info from current POOL resource
657 static int update_pool(UAContext *ua)
663 memset(&pr, 0, sizeof(pr));
664 if (!get_pool_dbr(ua, &pr)) {
668 strcpy(pr.PoolType, pool->pool_type);
669 if (pr.MaxVols != (uint32_t) (pool->max_volumes)) {
670 pr.MaxVols = pool->max_volumes;
672 if (pr.MaxVols != 0 && pr.MaxVols < pr.NumVols) {
673 pr.MaxVols = pr.NumVols;
675 pr.UseOnce = pool->use_volume_once;
676 pr.UseCatalog = pool->use_catalog;
677 pr.AcceptAnyVolume = pool->accept_any_volume;
678 if (pool->label_format) {
679 strcpy(pr.LabelFormat, pool->label_format);
681 strcpy(pr.LabelFormat, "*"); /* none */
683 id = db_update_pool_record(ua->db, &pr);
685 bsendmsg(ua, "Error: db_update_pool_record returned %d\n", id);
691 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
695 ua->jcr->store = store;
696 /* Try connecting for up to 15 seconds */
697 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
698 store->hdr.name, store->address, store->SDport);
699 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
700 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
703 Dmsg0(20, _("Connected to storage daemon\n"));
704 sd = ua->jcr->store_bsock;
705 bnet_fsend(sd, "setdebug=%d\n", level);
706 if (bnet_recv(sd) > 0) {
707 bsendmsg(ua, "%s", sd->msg);
709 bnet_sig(sd, BNET_TERMINATE);
711 ua->jcr->store_bsock = NULL;
715 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
719 /* Connect to File daemon */
721 ua->jcr->client = client;
722 /* Try to connect for 15 seconds */
723 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
724 client->hdr.name, client->address, client->FDport);
725 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
726 bsendmsg(ua, _("Failed to connect to Client.\n"));
729 Dmsg0(20, "Connected to file daemon\n");
730 fd = ua->jcr->file_bsock;
731 bnet_fsend(fd, "setdebug=%d\n", level);
732 if (bnet_recv(fd) > 0) {
733 bsendmsg(ua, "%s", fd->msg);
735 bnet_sig(fd, BNET_TERMINATE);
737 ua->jcr->file_bsock = NULL;
743 static void do_all_setdebug(UAContext *ua, int level)
745 STORE *store, **unique_store;
746 CLIENT *client, **unique_client;
752 /* Count Storage items */
755 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
757 unique_store = (STORE **) malloc(i * sizeof(STORE));
758 /* Find Unique Storage address/port */
759 store = (STORE *)GetNextRes(R_STORAGE, NULL);
761 unique_store[i++] = store;
762 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
764 for (j=0; j<i; j++) {
765 if (strcmp(unique_store[j]->address, store->address) == 0 &&
766 unique_store[j]->SDport == store->SDport) {
772 unique_store[i++] = store;
773 Dmsg2(40, "Stuffing: %s:%d\n", store->address, store->SDport);
778 /* Call each unique Storage daemon */
779 for (j=0; j<i; j++) {
780 do_storage_setdebug(ua, unique_store[j], level);
784 /* Count Client items */
787 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
789 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
790 /* Find Unique Client address/port */
791 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
793 unique_client[i++] = client;
794 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
796 for (j=0; j<i; j++) {
797 if (strcmp(unique_client[j]->address, client->address) == 0 &&
798 unique_client[j]->FDport == client->FDport) {
804 unique_client[i++] = client;
805 Dmsg2(40, "Stuffing: %s:%d\n", client->address, client->FDport);
810 /* Call each unique File daemon */
811 for (j=0; j<i; j++) {
812 do_client_setdebug(ua, unique_client[j], level);
818 * setdebug level=nn all
820 static int setdebugcmd(UAContext *ua, char *cmd)
830 Dmsg1(20, "setdebug:%s:\n", cmd);
833 for (i=1; i<ua->argc; i++) {
834 if (strcasecmp(ua->argk[i], _("level")) == 0 && ua->argv[i]) {
835 level = atoi(ua->argv[i]);
840 if (!get_cmd(ua, _("Enter new debug level: "))) {
843 level = atoi(ua->cmd);
846 bsendmsg(ua, _("level cannot be negative.\n"));
851 for (i=1; i<ua->argc; i++) {
852 if (strcasecmp(ua->argk[i], _("all")) == 0) {
853 do_all_setdebug(ua, level);
856 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
857 strcasecmp(ua->argk[i], _("director")) == 0) {
861 if (strcasecmp(ua->argk[i], _("client")) == 0) {
864 client = (CLIENT *) GetResWithName(R_CLIENT, ua->argv[i]);
866 do_client_setdebug(ua, client, level);
870 client = select_client_resource(ua);
872 do_client_setdebug(ua, client, level);
876 store = get_storage_resource(ua, cmd);
878 do_storage_setdebug(ua, store, level);
884 * We didn't find an appropriate keyword above, so
887 start_prompt(ua, _("Available daemons are: \n"));
888 add_prompt(ua, _("Director"));
889 add_prompt(ua, _("Storage"));
890 add_prompt(ua, _("Client"));
891 add_prompt(ua, _("All"));
892 switch(do_prompt(ua, _("Select daemon type to set debug level"), NULL)) {
893 case 0: /* Director */
897 store = get_storage_resource(ua, cmd);
899 do_storage_setdebug(ua, store, level);
903 client = select_client_resource(ua);
905 do_client_setdebug(ua, client, level);
909 do_all_setdebug(ua, level);
920 * Delete Pool records (should purge Media with it).
922 * delete pool=<pool-name>
923 * delete media pool=<pool-name> volume=<name>
925 static int deletecmd(UAContext *ua, char *cmd)
927 static char *keywords[] = {
937 "In general it is not a good idea to delete either a\n"
938 "Pool or Media since in both cases, you may delete Media\n"
939 "that contain data.\n\n"));
941 switch (find_arg_keyword(ua, keywords)) {
951 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
959 bsendmsg(ua, _("Nothing done.\n"));
966 * Delete media records from database -- dangerous
968 static int delete_volume(UAContext *ua)
973 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
976 bsendmsg(ua, _("\nThis command will delete volume %s\n"
977 "and all Jobs saved on that volume from the Catalog\n"),
980 if (!get_cmd(ua, _("If you want to continue enter pretty please: "))) {
983 if (strcmp(ua->cmd, _("pretty please")) == 0) {
984 db_delete_media_record(ua->db, &mr);
990 * Delete a pool record from the database -- dangerous
992 static int delete_pool(UAContext *ua)
996 memset(&pr, 0, sizeof(pr));
998 if (!get_pool_dbr(ua, &pr)) {
1001 if (!get_cmd(ua, _("If you want to continue enter pretty please: "))) {
1004 if (strcmp(ua->cmd, _("pretty please")) == 0) {
1005 db_delete_pool_record(ua->db, &pr);
1014 * label storage=xxx volume=vvv
1016 static int labelcmd(UAContext *ua, char *cmd)
1020 char dev_name[MAX_NAME_LENGTH];
1024 int mounted = FALSE;
1026 static char *keyword[] = {
1033 store = get_storage_resource(ua, cmd);
1038 i = find_arg_keyword(ua, keyword);
1039 if (i >=0 && ua->argv[i]) {
1040 strcpy(ua->cmd, ua->argv[i]);
1045 if (!get_cmd(ua, _("Enter new Volume name: "))) {
1049 if (strchr(ua->cmd, '|')) {
1050 bsendmsg(ua, _("Illegal character | in a volume name.\n"));
1053 if (strlen(ua->cmd) >= MAX_NAME_LENGTH) {
1054 bsendmsg(ua, _("Volume name too long.\n"));
1058 memset(&mr, 0, sizeof(mr));
1059 strcpy(mr.VolumeName, ua->cmd);
1060 if (db_get_media_record(ua->db, &mr)) {
1061 bsendmsg(ua, _("Media record for Volume %s already exists.\n"),
1065 strcpy(mr.MediaType, store->media_type);
1067 memset(&pr, 0, sizeof(pr));
1068 if (!select_pool_dbr(ua, &pr)) {
1071 mr.PoolId = pr.PoolId;
1072 strcpy(mr.VolStatus, "Append");
1073 mr.Recycle = pr.Recycle;
1074 mr.VolRetention = pr.VolRetention;
1076 ua->jcr->store = store;
1077 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
1078 store->hdr.name, store->address, store->SDport);
1079 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1080 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1083 sd = ua->jcr->store_bsock;
1084 strcpy(dev_name, store->dev_name);
1085 bash_spaces(dev_name);
1086 bash_spaces(mr.VolumeName);
1087 bash_spaces(mr.MediaType);
1088 bash_spaces(pr.Name);
1089 bnet_fsend(sd, _("label %s VolumeName=%s PoolName=%s MediaType=%s"),
1090 dev_name, mr.VolumeName, pr.Name, mr.MediaType);
1091 bsendmsg(ua, "Sending label command ...\n");
1092 while (bget_msg(sd, 0) > 0) {
1093 bsendmsg(ua, "%s", sd->msg);
1094 if (strncmp(sd->msg, "3000 OK label.", 14) == 0) {
1098 ua->jcr->store_bsock = NULL;
1099 unbash_spaces(dev_name);
1100 unbash_spaces(mr.VolumeName);
1101 unbash_spaces(mr.MediaType);
1102 unbash_spaces(pr.Name);
1104 if (db_create_media_record(ua->db, &mr)) {
1105 bsendmsg(ua, _("Media record for Volume=%s successfully created.\n"),
1107 if (ua->automount) {
1108 bsendmsg(ua, _("Requesting mount %s ...\n"), dev_name);
1109 bash_spaces(dev_name);
1110 bnet_fsend(sd, "mount %s", dev_name);
1111 unbash_spaces(dev_name);
1112 while (bnet_recv(sd) > 0) {
1113 bsendmsg(ua, "%s", sd->msg);
1115 * 3001 OK mount. Device=xxx or
1116 * 3001 Mounted Volume vvvv
1118 if (strncmp(sd->msg, "3001 ", 5) == 0) {
1120 /***** ****FIXME***** find job waiting for
1121 ***** mount, and change to waiting for SD
1127 bsendmsg(ua, "%s", db_strerror(ua->db));
1131 bsendmsg(ua, _("Do not forget to mount the drive!!!\n"));
1133 bnet_sig(sd, BNET_TERMINATE);
1138 static void do_mount_cmd(int mount, UAContext *ua, char *cmd)
1142 char dev_name[MAX_NAME_LENGTH];
1148 Dmsg1(20, "mount: %s\n", ua->UA_sock->msg);
1150 store = get_storage_resource(ua, cmd);
1155 Dmsg2(20, "Found storage, MediaType=%s DevName=%s\n",
1156 store->media_type, store->dev_name);
1158 ua->jcr->store = store;
1159 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1160 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1163 sd = ua->jcr->store_bsock;
1164 strcpy(dev_name, store->dev_name);
1165 bash_spaces(dev_name);
1167 bnet_fsend(sd, "mount %s", dev_name);
1169 bnet_fsend(sd, "unmount %s", dev_name);
1171 while (bnet_recv(sd) > 0) {
1172 bsendmsg(ua, "%s", sd->msg);
1173 if (strncmp(sd->msg, "3001 OK mount.", 14) == 0) {
1174 /***** ****FIXME**** fix JobStatus */
1177 bnet_sig(sd, BNET_TERMINATE);
1179 ua->jcr->store_bsock = NULL;
1183 * mount [storage | device] <name>
1185 static int mountcmd(UAContext *ua, char *cmd)
1187 do_mount_cmd(1, ua, cmd); /* mount */
1193 * unmount [storage | device] <name>
1195 static int unmountcmd(UAContext *ua, char *cmd)
1197 do_mount_cmd(0, ua, cmd); /* unmount */
1204 * use catalog=<name>
1206 static int usecmd(UAContext *ua, char *cmd)
1208 CAT *oldcatalog, *catalog;
1211 close_db(ua); /* close any previously open db */
1212 oldcatalog = ua->catalog;
1214 if (!(catalog = get_catalog_resource(ua))) {
1215 ua->catalog = oldcatalog;
1217 ua->catalog = catalog;
1220 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1221 ua->catalog->hdr.name, ua->catalog->db_name);
1226 int quitcmd(UAContext *ua, char *cmd)
1232 static int helpcmd(UAContext *ua, char *cmd)
1237 bnet_fsend(ua->UA_sock, _(" Command Description\n ======= ===========\n"));
1238 for (i=0; i<comsize; i++) {
1239 bnet_fsend(ua->UA_sock, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1241 bnet_fsend(ua->UA_sock, "\n");
1245 static int versioncmd(UAContext *ua, char *cmd)
1247 bsendmsg(ua, "%s Version: " VERSION " (" DATE ")\n", my_name);
1252 /* A bit brain damaged in that if the user has not done
1253 * a "use catalog xxx" command, we simply find the first
1254 * catalog resource and open it.
1256 int open_db(UAContext *ua)
1263 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1266 bnet_fsend(ua->UA_sock, _("Could not find a Catalog resource\n"));
1269 bnet_fsend(ua->UA_sock, _("Using default Catalog name=%s DB=%s\n"),
1270 ua->catalog->hdr.name, ua->catalog->db_name);
1274 Dmsg0(50, "Open database\n");
1275 ua->db = db_init_database(ua->catalog->db_name, ua->catalog->db_user,
1276 ua->catalog->db_password);
1277 if (!db_open_database(ua->db)) {
1278 bnet_fsend(ua->UA_sock, _("Could not open DB %s: ERR=%s"),
1279 ua->catalog->db_name, db_strerror(ua->db));
1283 ua->jcr->db = ua->db;
1284 Dmsg1(50, "DB %s opened\n", ua->catalog->db_name);
1288 void close_db(UAContext *ua)
1291 db_close_database(ua->db);