3 * Bacula Director -- User Agent Commands
5 * Kern Sibbald, September MM
11 Copyright (C) 2000-2003 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,
33 /* Imported subroutines */
34 extern void run_job(JCR *jcr);
36 /* Imported variables */
39 extern struct s_res resources[];
40 extern char my_name[];
42 extern workq_t job_wq; /* work queue */
45 extern char *list_pool;
47 /* Imported functions */
48 extern int statuscmd(UAContext *ua, char *cmd);
49 extern int listcmd(UAContext *ua, char *cmd);
50 extern int llistcmd(UAContext *ua, char *cmd);
51 extern int showcmd(UAContext *ua, char *cmd);
52 extern int messagescmd(UAContext *ua, char *cmd);
53 extern int autodisplaycmd(UAContext *ua, char *cmd);
54 extern int sqlquerycmd(UAContext *ua, char *cmd);
55 extern int querycmd(UAContext *ua, char *cmd);
56 extern int runcmd(UAContext *ua, char *cmd);
57 extern int retentioncmd(UAContext *ua, char *cmd);
58 extern int prunecmd(UAContext *ua, char *cmd);
59 extern int purgecmd(UAContext *ua, char *cmd);
60 extern int restorecmd(UAContext *ua, char *cmd);
61 extern int labelcmd(UAContext *ua, char *cmd);
62 extern int relabelcmd(UAContext *ua, char *cmd);
63 extern int update_slots(UAContext *ua); /* ua_label.c */
65 /* Forward referenced functions */
66 static int addcmd(UAContext *ua, char *cmd), createcmd(UAContext *ua, char *cmd), cancelcmd(UAContext *ua, char *cmd);
67 static int setdebugcmd(UAContext *ua, char *cmd);
68 static int helpcmd(UAContext *ua, char *cmd);
69 static int deletecmd(UAContext *ua, char *cmd);
70 static int usecmd(UAContext *ua, char *cmd), unmountcmd(UAContext *ua, char *cmd);
71 static int versioncmd(UAContext *ua, char *cmd), automountcmd(UAContext *ua, char *cmd);
72 static int timecmd(UAContext *ua, char *cmd);
73 static int update_volume(UAContext *ua);
74 static int update_pool(UAContext *ua);
75 static int delete_volume(UAContext *ua);
76 static int delete_pool(UAContext *ua);
77 static int mountcmd(UAContext *ua, char *cmd);
78 static int updatecmd(UAContext *ua, char *cmd);
80 int quitcmd(UAContext *ua, char *cmd);
83 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
84 static struct cmdstruct commands[] = {
85 { N_("add"), addcmd, _("add media to a pool")},
86 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
87 { N_("automount"), automountcmd, _("automount [on/off] -- after label")},
88 { N_("cancel"), cancelcmd, _("cancel job=nnn -- cancel a job")},
89 { N_("create"), createcmd, _("create DB Pool from resource")},
90 { N_("delete"), deletecmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
91 { N_("help"), helpcmd, _("print this command")},
92 { N_("label"), labelcmd, _("label a tape")},
93 { N_("relabel"), relabelcmd, _("relabel a tape")},
94 { N_("list"), listcmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
95 { N_("llist"), llistcmd, _("full or long list like list command")},
96 { N_("messages"), messagescmd, _("messages")},
97 { N_("mount"), mountcmd, _("mount <storage-name>")},
98 { N_("restore"), restorecmd, _("restore files")},
99 { N_("prune"), prunecmd, _("prune expired records from catalog")},
100 { N_("purge"), purgecmd, _("purge records from catalog")},
101 { N_("run"), runcmd, _("run <job-name>")},
102 { N_("setdebug"), setdebugcmd, _("sets debug level")},
103 { N_("show"), showcmd, _("show (resource records) [jobs | pools | ... | all]")},
104 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
105 { N_("status"), statuscmd, _("status [storage | client]=<name>")},
106 { N_("unmount"), unmountcmd, _("unmount <storage-name>")},
107 { N_("update"), updatecmd, _("update Volume or Pool")},
108 { N_("use"), usecmd, _("use catalog xxx")},
109 { N_("version"), versioncmd, _("print Director version")},
110 { N_("quit"), quitcmd, _("quit")},
111 { N_("query"), querycmd, _("query catalog")},
112 { N_("time"), timecmd, _("print current time")},
113 { N_("exit"), quitcmd, _("exit = quit")},
115 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
118 * Execute a command from the UA
120 int do_a_command(UAContext *ua, char *cmd)
129 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
134 len = strlen(ua->argk[0]);
135 for (i=0; i<comsize; i++) /* search for command */
136 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
137 stat = (*commands[i].func)(ua, cmd); /* go execute command */
142 strcat(ua->UA_sock->msg, _(": is an illegal command\n"));
143 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
144 bnet_send(ua->UA_sock);
150 * This is a common routine used to stuff the Pool DB record defaults
151 * into the Media DB record just before creating a media (Volume)
154 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
156 mr->PoolId = pr->PoolId;
157 strcpy(mr->VolStatus, "Append");
158 mr->Recycle = pr->Recycle;
159 mr->VolRetention = pr->VolRetention;
160 mr->VolUseDuration = pr->VolUseDuration;
161 mr->MaxVolJobs = pr->MaxVolJobs;
162 mr->MaxVolFiles = pr->MaxVolFiles;
163 mr->MaxVolBytes = pr->MaxVolBytes;
168 * Add Volumes to an existing Pool
170 static int addcmd(UAContext *ua, char *cmd)
174 int num, i, max, startnum;
176 char name[MAX_NAME_LENGTH];
181 "You probably don't want to be using this command since it\n"
182 "creates database records without labeling the Volumes.\n"
183 "You probably want to use the \"label\" command.\n\n"));
189 memset(&pr, 0, sizeof(pr));
190 memset(&mr, 0, sizeof(mr));
192 if (!get_pool_dbr(ua, &pr)) {
196 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
197 pr.MaxVols, pr.PoolType);
199 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
200 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
202 if (!get_cmd(ua, _("Enter new maximum (zero for unlimited): "))) {
205 pr.MaxVols = atoi(ua->cmd);
206 if (pr.MaxVols < 0) {
207 bsendmsg(ua, _("Max vols must be zero or greater.\n"));
215 if ((store = get_storage_resource(ua, 0)) != NULL) {
216 strcpy(mr.MediaType, store->media_type);
217 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
221 if (pr.MaxVols == 0) {
224 max = pr.MaxVols - pr.NumVols;
228 sprintf(buf, _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
229 if (!get_cmd(ua, buf)) {
233 if (num < 0 || num > max) {
234 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
241 if (!get_cmd(ua, _("Enter Volume name: "))) {
245 if (!get_cmd(ua, _("Enter base volume name: "))) {
249 /* Don't allow | in Volume name because it is the volume separator character */
250 if (strchr(ua->cmd, '|')) {
251 bsendmsg(ua, _("Illegal character | in a volume name.\n"));
254 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
255 bsendmsg(ua, _("Volume name too long.\n"));
258 if (strlen(ua->cmd) == 0) {
259 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
263 strcpy(name, ua->cmd);
265 strcat(name, "%04d");
268 if (!get_cmd(ua, _("Enter the starting number: "))) {
271 startnum = atoi(ua->cmd);
273 bsendmsg(ua, _("Start number must be greater than zero.\n"));
283 if (store && store->autochanger) {
284 if (!get_cmd(ua, _("Enter slot (0 for none): "))) {
287 slot = atoi(ua->cmd);
290 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
291 for (i=startnum; i < num+startnum; i++) {
292 sprintf(mr.VolumeName, name, i);
294 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
295 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
296 bsendmsg(ua, db_strerror(ua->db));
300 first_id = mr.PoolId;
304 Dmsg0(200, "Update pool record.\n");
305 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
306 bsendmsg(ua, db_strerror(ua->db));
309 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
315 * Turn auto mount on/off
320 int automountcmd(UAContext *ua, char *cmd)
325 if (!get_cmd(ua, _("Turn on or off? "))) {
333 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
341 static int cancelcmd(UAContext *ua, char *cmd)
347 char JobName[MAX_NAME_LENGTH];
353 for (i=1; i<ua->argc; i++) {
354 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
358 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
359 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
363 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
367 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
368 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
374 /* If we still do not have a jcr,
375 * throw up a list and ask the user to select one.
378 /* Count Jobs running */
380 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
381 if (jcr->JobId == 0) { /* this is us */
382 free_locked_jcr(jcr);
386 free_locked_jcr(jcr);
391 bsendmsg(ua, _("No Jobs running.\n"));
394 start_prompt(ua, _("Select Job:\n"));
396 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
397 if (jcr->JobId == 0) { /* this is us */
398 free_locked_jcr(jcr);
401 add_prompt(ua, jcr->Job);
402 free_locked_jcr(jcr);
406 if (do_prompt(ua, _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
410 if (!get_cmd(ua, _("Confirm cancel (yes/no): "))) {
413 if (strcasecmp(ua->cmd, _("yes")) != 0) {
417 jcr = get_jcr_by_full_name(JobName);
419 bsendmsg(ua, _("Job %s not found.\n"), JobName);
424 switch (jcr->JobStatus) {
426 set_jcr_job_status(jcr, JS_Canceled);
427 bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
428 jcr->JobId, jcr->Job);
429 #ifndef USE_SEMAPHORE
430 workq_remove(&job_wq, jcr->work_item); /* attempt to remove it from queue */
436 set_jcr_job_status(jcr, JS_Canceled);
437 /* Cancel File daemon */
438 ua->jcr->client = jcr->client;
439 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
440 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
444 Dmsg0(200, "Connected to file daemon\n");
445 fd = ua->jcr->file_bsock;
446 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
447 while (bnet_recv(fd) >= 0) {
448 bsendmsg(ua, "%s", fd->msg);
450 bnet_sig(fd, BNET_TERMINATE);
452 ua->jcr->file_bsock = NULL;
454 /* Cancel Storage daemon */
455 ua->jcr->store = jcr->store;
456 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
457 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
461 Dmsg0(200, "Connected to storage daemon\n");
462 sd = ua->jcr->store_bsock;
463 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
464 while (bnet_recv(sd) >= 0) {
465 bsendmsg(ua, "%s", sd->msg);
467 bnet_sig(sd, BNET_TERMINATE);
469 ua->jcr->store_bsock = NULL;
478 * This is a common routine to create or update a
479 * Pool DB base record from a Pool Resource. We handle
480 * the setting of MaxVols and NumVols slightly differently
481 * depending on if we are creating the Pool or we are
482 * simply bringing it into agreement with the resource (updage).
484 void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, int create)
486 strcpy(pr->PoolType, pool->pool_type);
488 pr->MaxVols = pool->max_volumes;
490 } else { /* update pool */
491 if (pr->MaxVols != pool->max_volumes) {
492 pr->MaxVols = pool->max_volumes;
494 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
495 pr->MaxVols = pr->NumVols;
498 pr->UseOnce = pool->use_volume_once;
499 pr->UseCatalog = pool->use_catalog;
500 pr->AcceptAnyVolume = pool->accept_any_volume;
501 pr->Recycle = pool->Recycle;
502 pr->VolRetention = pool->VolRetention;
503 pr->VolUseDuration = pool->VolUseDuration;
504 pr->MaxVolJobs = pool->MaxVolJobs;
505 pr->MaxVolFiles = pool->MaxVolFiles;
506 pr->MaxVolBytes = pool->MaxVolBytes;
507 if (pool->label_format) {
508 strcpy(pr->LabelFormat, pool->label_format);
510 strcpy(pr->LabelFormat, "*"); /* none */
516 * Create a pool record from a given Pool resource
517 * Also called from backup.c
518 * Returns: -1 on error
519 * 0 record already exists
523 int create_pool(JCR *jcr, B_DB *db, POOL *pool, int create)
527 memset(&pr, 0, sizeof(POOL_DBR));
529 strcpy(pr.Name, pool->hdr.name);
531 if (db_get_pool_record(jcr, db, &pr)) {
533 if (!create) { /* update request */
534 set_pooldbr_from_poolres(&pr, pool, 0);
535 db_update_pool_record(jcr, db, &pr);
537 return 0; /* exists */
540 set_pooldbr_from_poolres(&pr, pool, 1);
542 if (!db_create_pool_record(jcr, db, &pr)) {
543 return -1; /* error */
551 * Create a Pool Record in the database.
552 * It is always created from the Resource record.
554 static int createcmd(UAContext *ua, char *cmd)
562 pool = get_pool_resource(ua);
567 switch (create_pool(ua->jcr, ua->db, pool, 1)) {
569 bsendmsg(ua, _("Error: Pool %s already exists.\n\
570 Use update to change it.\n"), pool->hdr.name);
574 bsendmsg(ua, db_strerror(ua->db));
580 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
588 * Update a Pool Record in the database.
589 * It is always updated from the Resource record.
591 * update pool=<pool-name>
592 * updates pool from Pool resource
593 * update media pool=<pool-name> volume=<volume-name>
594 * changes pool info for volume
596 static int updatecmd(UAContext *ua, char *cmd)
598 static char *kw[] = {
600 N_("volume"), /* 1 */
609 switch (find_arg_keyword(ua, kw)) {
624 start_prompt(ua, _("Update choice:\n"));
625 add_prompt(ua, _("Volume parameters"));
626 add_prompt(ua, _("Pool from resource"));
627 add_prompt(ua, _("Slots from autochanger"));
628 switch (do_prompt(ua, _("Choose catalog item to update"), NULL, 0)) {
645 * Update a media record -- allows you to change the
646 * Volume status. E.g. if you want Bacula to stop
647 * writing on the volume, set it to anything other
650 static int update_volume(UAContext *ua)
657 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
661 for (int done=0; !done; ) {
662 if (!db_get_media_record(ua->jcr, ua->db, &mr)) {
663 if (mr.MediaId != 0) {
664 bsendmsg(ua, _("Volume record for MediaId %d not found.\n"), mr.MediaId);
666 bsendmsg(ua, _("Volume record for %s not found.\n"), mr.VolumeName);
670 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
671 start_prompt(ua, _("Parameters to modify:\n"));
672 add_prompt(ua, _("Volume Status"));
673 add_prompt(ua, _("Volume Retention Period"));
674 add_prompt(ua, _("Volume Use Duration"));
675 add_prompt(ua, _("Maximum Volume Jobs"));
676 add_prompt(ua, _("Maximum Volume Files"));
677 add_prompt(ua, _("Maximum Volume Bytes"));
678 add_prompt(ua, _("Recycle Flag"));
679 add_prompt(ua, _("Slot"));
680 add_prompt(ua, _("Volume Files"));
681 add_prompt(ua, _("Done"));
682 switch (do_prompt(ua, _("Select parameter to modify"), NULL, 0)) {
683 case 0: /* Volume Status */
684 /* Modify Volume Status */
685 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
686 start_prompt(ua, _("Possible Values are:\n"));
687 add_prompt(ua, "Append"); /* Better not translate these as */
688 add_prompt(ua, "Archive"); /* They are known in the database code */
689 add_prompt(ua, "Disabled");
690 add_prompt(ua, "Full");
691 add_prompt(ua, "Used");
692 if (strcmp(mr.VolStatus, "Purged") == 0) {
693 add_prompt(ua, "Recycle");
695 add_prompt(ua, "Read-Only");
696 if (do_prompt(ua, _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
699 bstrncpy(mr.VolStatus, ua->cmd, sizeof(mr.VolStatus));
700 query = get_pool_memory(PM_MESSAGE);
701 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
702 mr.VolStatus, mr.MediaId);
703 if (!db_sql_query(ua->db, query, NULL, NULL)) {
704 bsendmsg(ua, "%s", db_strerror(ua->db));
706 bsendmsg(ua, _("New Volume status is: %s\n"), mr.VolStatus);
708 free_pool_memory(query);
710 case 1: /* Retention */
711 bsendmsg(ua, _("Current retention seconds is: %s\n"),
712 edit_utime(mr.VolRetention, ed1));
713 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
716 if (!duration_to_utime(ua->cmd, &mr.VolRetention)) {
717 bsendmsg(ua, _("Invalid retention period specified.\n"));
720 query = get_pool_memory(PM_MESSAGE);
721 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
722 edit_uint64(mr.VolRetention, ed1), mr.MediaId);
723 if (!db_sql_query(ua->db, query, NULL, NULL)) {
724 bsendmsg(ua, "%s", db_strerror(ua->db));
726 bsendmsg(ua, _("New retention seconds is: %s\n"),
727 edit_utime(mr.VolRetention, ed1));
729 free_pool_memory(query);
732 case 2: /* Use Duration */
733 bsendmsg(ua, _("Current use duration is: %s\n"),
734 edit_utime(mr.VolUseDuration, ed1));
735 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
738 if (!duration_to_utime(ua->cmd, &mr.VolUseDuration)) {
739 bsendmsg(ua, _("Invalid use duration specified.\n"));
742 query = get_pool_memory(PM_MESSAGE);
743 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
744 edit_uint64(mr.VolUseDuration, ed1), mr.MediaId);
745 if (!db_sql_query(ua->db, query, NULL, NULL)) {
746 bsendmsg(ua, "%s", db_strerror(ua->db));
748 bsendmsg(ua, _("New use duration is: %s\n"),
749 edit_utime(mr.VolUseDuration, ed1));
751 free_pool_memory(query);
754 case 3: /* Max Jobs */
756 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
757 if (!get_cmd(ua, _("Enter new Maximum Jobs: "))) {
760 maxjobs = atoi(ua->cmd);
762 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
765 query = get_pool_memory(PM_MESSAGE);
766 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%u WHERE MediaId=%u",
767 maxjobs, mr.MediaId);
768 if (!db_sql_query(ua->db, query, NULL, NULL)) {
769 bsendmsg(ua, "%s", db_strerror(ua->db));
771 bsendmsg(ua, _("New max jobs is: %u\n"), maxjobs);
773 free_pool_memory(query);
776 case 4: /* Max Files */
778 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
779 if (!get_cmd(ua, _("Enter new Maximum Files: "))) {
782 maxfiles = atoi(ua->cmd);
784 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
787 query = get_pool_memory(PM_MESSAGE);
788 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%u WHERE MediaId=%u",
789 maxfiles, mr.MediaId);
790 if (!db_sql_query(ua->db, query, NULL, NULL)) {
791 bsendmsg(ua, "%s", db_strerror(ua->db));
793 bsendmsg(ua, _("New max files is: %u\n"), maxfiles);
795 free_pool_memory(query);
798 case 5: /* Max Bytes */
800 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
801 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
804 if (!size_to_uint64(ua->cmd, strlen(ua->cmd), &maxbytes)) {
805 bsendmsg(ua, _("Invalid byte size specification.\n"));
808 query = get_pool_memory(PM_MESSAGE);
809 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
810 edit_uint64(maxbytes, ed1), mr.MediaId);
811 if (!db_sql_query(ua->db, query, NULL, NULL)) {
812 bsendmsg(ua, "%s", db_strerror(ua->db));
814 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
816 free_pool_memory(query);
820 case 6: /* Recycle */
822 bsendmsg(ua, _("Current recycle flag is: %s\n"),
823 mr.Recycle==1?_("yes"):_("no"));
824 if (!get_cmd(ua, _("Enter new Recycle status: "))) {
827 if (strcasecmp(ua->cmd, _("yes")) == 0) {
829 } else if (strcasecmp(ua->cmd, _("no")) == 0) {
832 bsendmsg(ua, _("Invalid recycle status specified.\n"));
835 query = get_pool_memory(PM_MESSAGE);
836 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
837 recycle, mr.MediaId);
838 if (!db_sql_query(ua->db, query, NULL, NULL)) {
839 bsendmsg(ua, "%s", db_strerror(ua->db));
841 bsendmsg(ua, _("New recycle flag is: %s\n"),
842 mr.Recycle==1?_("yes"):_("no"));
844 free_pool_memory(query);
849 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
850 if (!get_cmd(ua, _("Enter new Slot: "))) {
853 slot = atoi(ua->cmd);
855 bsendmsg(ua, _("Invalid slot, it must be 0 or greater\n"));
857 } else if (pr.MaxVols > 0 && slot >(int)pr.MaxVols) {
858 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
862 query = get_pool_memory(PM_MESSAGE);
863 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
865 if (!db_sql_query(ua->db, query, NULL, NULL)) {
866 bsendmsg(ua, "%s", db_strerror(ua->db));
868 bsendmsg(ua, "New Slot is: %d\n", slot);
870 free_pool_memory(query);
873 case 8: /* Volume Files */
875 bsendmsg(ua, _("Warning changing Volume Files can result\n"
876 "in loss of data on your Volume\n\n"));
877 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
878 if (!get_cmd(ua, _("Enter new number of Files for Volume: "))) {
881 VolFiles = atoi(ua->cmd);
883 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
886 if (VolFiles != (int)(mr.VolFiles + 1)) {
887 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
888 if (!get_cmd(ua, _("Continue? (yes/no): ")) ||
889 strcasecmp(ua->cmd, "yes") != 0) {
893 query = get_pool_memory(PM_MESSAGE);
894 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
895 VolFiles, mr.MediaId);
896 if (!db_sql_query(ua->db, query, NULL, NULL)) {
897 bsendmsg(ua, "%s", db_strerror(ua->db));
899 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
901 free_pool_memory(query);
904 default: /* Done or error */
905 bsendmsg(ua, "Selection done.\n");
913 * Update pool record -- pull info from current POOL resource
915 static int update_pool(UAContext *ua)
923 pool = get_pool_resource(ua);
928 memset(&pr, 0, sizeof(pr));
929 strcpy(pr.Name, pool->hdr.name);
930 if (!get_pool_dbr(ua, &pr)) {
934 set_pooldbr_from_poolres(&pr, pool, 0); /* update */
936 id = db_update_pool_record(ua->jcr, ua->db, &pr);
938 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
939 id, db_strerror(ua->db));
941 query = get_pool_memory(PM_MESSAGE);
942 Mmsg(&query, list_pool, pr.PoolId);
943 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, 0);
944 free_pool_memory(query);
945 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
950 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
954 ua->jcr->store = store;
955 /* Try connecting for up to 15 seconds */
956 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
957 store->hdr.name, store->address, store->SDport);
958 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
959 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
962 Dmsg0(120, _("Connected to storage daemon\n"));
963 sd = ua->jcr->store_bsock;
964 bnet_fsend(sd, "setdebug=%d\n", level);
965 if (bnet_recv(sd) >= 0) {
966 bsendmsg(ua, "%s", sd->msg);
968 bnet_sig(sd, BNET_TERMINATE);
970 ua->jcr->store_bsock = NULL;
974 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
978 /* Connect to File daemon */
980 ua->jcr->client = client;
981 /* Try to connect for 15 seconds */
982 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
983 client->hdr.name, client->address, client->FDport);
984 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
985 bsendmsg(ua, _("Failed to connect to Client.\n"));
988 Dmsg0(120, "Connected to file daemon\n");
989 fd = ua->jcr->file_bsock;
990 bnet_fsend(fd, "setdebug=%d\n", level);
991 if (bnet_recv(fd) >= 0) {
992 bsendmsg(ua, "%s", fd->msg);
994 bnet_sig(fd, BNET_TERMINATE);
996 ua->jcr->file_bsock = NULL;
1002 static void do_all_setdebug(UAContext *ua, int level)
1004 STORE *store, **unique_store;
1005 CLIENT *client, **unique_client;
1009 debug_level = level;
1011 /* Count Storage items */
1014 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1016 unique_store = (STORE **) malloc(i * sizeof(STORE));
1017 /* Find Unique Storage address/port */
1018 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1020 unique_store[i++] = store;
1021 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1023 for (j=0; j<i; j++) {
1024 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1025 unique_store[j]->SDport == store->SDport) {
1031 unique_store[i++] = store;
1032 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1037 /* Call each unique Storage daemon */
1038 for (j=0; j<i; j++) {
1039 do_storage_setdebug(ua, unique_store[j], level);
1043 /* Count Client items */
1046 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1048 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1049 /* Find Unique Client address/port */
1050 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1052 unique_client[i++] = client;
1053 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1055 for (j=0; j<i; j++) {
1056 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1057 unique_client[j]->FDport == client->FDport) {
1063 unique_client[i++] = client;
1064 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1069 /* Call each unique File daemon */
1070 for (j=0; j<i; j++) {
1071 do_client_setdebug(ua, unique_client[j], level);
1073 free(unique_client);
1077 * setdebug level=nn all
1079 static int setdebugcmd(UAContext *ua, char *cmd)
1089 Dmsg1(120, "setdebug:%s:\n", cmd);
1092 for (i=1; i<ua->argc; i++) {
1093 if (strcasecmp(ua->argk[i], _("level")) == 0 && ua->argv[i]) {
1094 level = atoi(ua->argv[i]);
1099 if (!get_cmd(ua, _("Enter new debug level: "))) {
1102 level = atoi(ua->cmd);
1105 bsendmsg(ua, _("level cannot be negative.\n"));
1109 /* General debug? */
1110 for (i=1; i<ua->argc; i++) {
1111 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1112 do_all_setdebug(ua, level);
1115 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1116 strcasecmp(ua->argk[i], _("director")) == 0) {
1117 debug_level = level;
1120 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1123 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1125 do_client_setdebug(ua, client, level);
1129 client = select_client_resource(ua);
1131 do_client_setdebug(ua, client, level);
1136 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1137 strcasecmp(ua->argk[i], _("storage")) == 0) {
1140 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1142 do_storage_setdebug(ua, store, level);
1146 store = get_storage_resource(ua, 0);
1148 do_storage_setdebug(ua, store, level);
1154 * We didn't find an appropriate keyword above, so
1157 start_prompt(ua, _("Available daemons are: \n"));
1158 add_prompt(ua, _("Director"));
1159 add_prompt(ua, _("Storage"));
1160 add_prompt(ua, _("Client"));
1161 add_prompt(ua, _("All"));
1162 switch(do_prompt(ua, _("Select daemon type to set debug level"), NULL, 0)) {
1163 case 0: /* Director */
1164 debug_level = level;
1167 store = get_storage_resource(ua, 0);
1169 do_storage_setdebug(ua, store, level);
1173 client = select_client_resource(ua);
1175 do_client_setdebug(ua, client, level);
1179 do_all_setdebug(ua, level);
1190 static int timecmd(UAContext *ua, char *cmd)
1193 time_t ttime = time(NULL);
1195 localtime_r(&ttime, &tm);
1196 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1197 bsendmsg(ua, "%s\n", sdt);
1204 * Delete Pool records (should purge Media with it).
1206 * delete pool=<pool-name>
1207 * delete media pool=<pool-name> volume=<name>
1209 static int deletecmd(UAContext *ua, char *cmd)
1211 static char *keywords[] = {
1221 "In general it is not a good idea to delete either a\n"
1222 "Pool or a Volume since they may contain data.\n\n"));
1224 switch (find_arg_keyword(ua, keywords)) {
1234 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1242 bsendmsg(ua, _("Nothing done.\n"));
1249 * Delete media records from database -- dangerous
1251 static int delete_volume(UAContext *ua)
1256 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
1259 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1260 "and all Jobs saved on that volume from the Catalog\n"),
1263 if (!get_cmd(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1266 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1267 db_delete_media_record(ua->jcr, ua->db, &mr);
1273 * Delete a pool record from the database -- dangerous
1275 static int delete_pool(UAContext *ua)
1279 memset(&pr, 0, sizeof(pr));
1281 if (!get_pool_dbr(ua, &pr)) {
1284 if (!get_cmd(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1287 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1288 db_delete_pool_record(ua->jcr, ua->db, &pr);
1294 static void do_mount_cmd(int mount, UAContext *ua, char *cmd)
1298 char dev_name[MAX_NAME_LENGTH];
1304 Dmsg1(120, "mount: %s\n", ua->UA_sock->msg);
1306 store = get_storage_resource(ua, 1);
1311 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1312 store->media_type, store->dev_name);
1314 ua->jcr->store = store;
1315 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1316 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1319 sd = ua->jcr->store_bsock;
1320 strcpy(dev_name, store->dev_name);
1321 bash_spaces(dev_name);
1323 bnet_fsend(sd, "mount %s", dev_name);
1325 bnet_fsend(sd, "unmount %s", dev_name);
1327 while (bnet_recv(sd) >= 0) {
1328 bsendmsg(ua, "%s", sd->msg);
1329 if (strncmp(sd->msg, "3001 OK mount.", 14) == 0) {
1330 /***** ****FIXME**** fix JobStatus */
1333 bnet_sig(sd, BNET_TERMINATE);
1335 ua->jcr->store_bsock = NULL;
1339 * mount [storage | device] <name>
1341 static int mountcmd(UAContext *ua, char *cmd)
1343 do_mount_cmd(1, ua, cmd); /* mount */
1349 * unmount [storage | device] <name>
1351 static int unmountcmd(UAContext *ua, char *cmd)
1353 do_mount_cmd(0, ua, cmd); /* unmount */
1360 * use catalog=<name>
1362 static int usecmd(UAContext *ua, char *cmd)
1364 CAT *oldcatalog, *catalog;
1367 close_db(ua); /* close any previously open db */
1368 oldcatalog = ua->catalog;
1370 if (!(catalog = get_catalog_resource(ua))) {
1371 ua->catalog = oldcatalog;
1373 ua->catalog = catalog;
1376 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1377 ua->catalog->hdr.name, ua->catalog->db_name);
1382 int quitcmd(UAContext *ua, char *cmd)
1388 static int helpcmd(UAContext *ua, char *cmd)
1393 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1394 for (i=0; i<comsize; i++) {
1395 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1401 static int versioncmd(UAContext *ua, char *cmd)
1403 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1408 /* A bit brain damaged in that if the user has not done
1409 * a "use catalog xxx" command, we simply find the first
1410 * catalog resource and open it.
1412 int open_db(UAContext *ua)
1419 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1422 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1425 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1426 ua->catalog->hdr.name, ua->catalog->db_name);
1430 Dmsg0(150, "Open database\n");
1431 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1432 ua->catalog->db_password, ua->catalog->db_address,
1433 ua->catalog->db_port, ua->catalog->db_socket);
1434 if (!db_open_database(ua->jcr, ua->db)) {
1435 bsendmsg(ua, _("Could not open DB %s: ERR=%s"),
1436 ua->catalog->db_name, db_strerror(ua->db));
1440 ua->jcr->db = ua->db;
1441 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1445 void close_db(UAContext *ua)
1448 db_close_database(ua->jcr, ua->db);