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 for (int done=0; !done; ) {
658 if (!select_media_dbr(ua, &mr)) {
661 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
662 start_prompt(ua, _("Parameters to modify:\n"));
663 add_prompt(ua, _("Volume Status"));
664 add_prompt(ua, _("Volume Retention Period"));
665 add_prompt(ua, _("Volume Use Duration"));
666 add_prompt(ua, _("Maximum Volume Jobs"));
667 add_prompt(ua, _("Maximum Volume Files"));
668 add_prompt(ua, _("Maximum Volume Bytes"));
669 add_prompt(ua, _("Recycle Flag"));
670 add_prompt(ua, _("Slot"));
671 add_prompt(ua, _("Volume Files"));
672 add_prompt(ua, _("Done"));
673 switch (do_prompt(ua, _("Select parameter to modify"), NULL, 0)) {
674 case 0: /* Volume Status */
675 /* Modify Volume Status */
676 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
677 start_prompt(ua, _("Possible Values are:\n"));
678 add_prompt(ua, "Append"); /* Better not translate these as */
679 add_prompt(ua, "Archive"); /* They are known in the database code */
680 add_prompt(ua, "Disabled");
681 add_prompt(ua, "Full");
682 add_prompt(ua, "Used");
683 if (strcmp(mr.VolStatus, "Purged") == 0) {
684 add_prompt(ua, "Recycle");
686 add_prompt(ua, "Read-Only");
687 if (do_prompt(ua, _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
690 bstrncpy(mr.VolStatus, ua->cmd, sizeof(mr.VolStatus));
691 query = get_pool_memory(PM_MESSAGE);
692 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
693 mr.VolStatus, mr.MediaId);
694 if (!db_sql_query(ua->db, query, NULL, NULL)) {
695 bsendmsg(ua, "%s", db_strerror(ua->db));
697 bsendmsg(ua, _("New Volume status is: %s\n"), mr.VolStatus);
699 free_pool_memory(query);
701 case 1: /* Retention */
702 bsendmsg(ua, _("Current retention seconds is: %s\n"),
703 edit_utime(mr.VolRetention, ed1));
704 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
707 if (!duration_to_utime(ua->cmd, &mr.VolRetention)) {
708 bsendmsg(ua, _("Invalid retention period specified.\n"));
711 query = get_pool_memory(PM_MESSAGE);
712 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
713 edit_uint64(mr.VolRetention, ed1), mr.MediaId);
714 if (!db_sql_query(ua->db, query, NULL, NULL)) {
715 bsendmsg(ua, "%s", db_strerror(ua->db));
717 bsendmsg(ua, _("New retention seconds is: %s\n"),
718 edit_utime(mr.VolRetention, ed1));
720 free_pool_memory(query);
723 case 2: /* Use Duration */
724 bsendmsg(ua, _("Current use duration is: %s\n"),
725 edit_utime(mr.VolUseDuration, ed1));
726 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
729 if (!duration_to_utime(ua->cmd, &mr.VolUseDuration)) {
730 bsendmsg(ua, _("Invalid use duration specified.\n"));
733 query = get_pool_memory(PM_MESSAGE);
734 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
735 edit_uint64(mr.VolUseDuration, ed1), mr.MediaId);
736 if (!db_sql_query(ua->db, query, NULL, NULL)) {
737 bsendmsg(ua, "%s", db_strerror(ua->db));
739 bsendmsg(ua, _("New use duration is: %s\n"),
740 edit_utime(mr.VolUseDuration, ed1));
742 free_pool_memory(query);
745 case 3: /* Max Jobs */
747 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
748 if (!get_cmd(ua, _("Enter new Maximum Jobs: "))) {
751 maxjobs = atoi(ua->cmd);
753 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
756 query = get_pool_memory(PM_MESSAGE);
757 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%u WHERE MediaId=%u",
758 maxjobs, mr.MediaId);
759 if (!db_sql_query(ua->db, query, NULL, NULL)) {
760 bsendmsg(ua, "%s", db_strerror(ua->db));
762 bsendmsg(ua, _("New max jobs is: %u\n"), maxjobs);
764 free_pool_memory(query);
767 case 4: /* Max Files */
769 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
770 if (!get_cmd(ua, _("Enter new Maximum Files: "))) {
773 maxfiles = atoi(ua->cmd);
775 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
778 query = get_pool_memory(PM_MESSAGE);
779 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%u WHERE MediaId=%u",
780 maxfiles, mr.MediaId);
781 if (!db_sql_query(ua->db, query, NULL, NULL)) {
782 bsendmsg(ua, "%s", db_strerror(ua->db));
784 bsendmsg(ua, _("New max files is: %u\n"), maxfiles);
786 free_pool_memory(query);
789 case 5: /* Max Bytes */
791 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
792 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
795 if (!size_to_uint64(ua->cmd, strlen(ua->cmd), &maxbytes)) {
796 bsendmsg(ua, _("Invalid byte size specification.\n"));
799 query = get_pool_memory(PM_MESSAGE);
800 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
801 edit_uint64(maxbytes, ed1), mr.MediaId);
802 if (!db_sql_query(ua->db, query, NULL, NULL)) {
803 bsendmsg(ua, "%s", db_strerror(ua->db));
805 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
807 free_pool_memory(query);
811 case 6: /* Recycle */
813 bsendmsg(ua, _("Current recycle flag is: %s\n"),
814 mr.Recycle==1?_("yes"):_("no"));
815 if (!get_cmd(ua, _("Enter new Recycle status: "))) {
818 if (strcasecmp(ua->cmd, _("yes")) == 0) {
820 } else if (strcasecmp(ua->cmd, _("no")) == 0) {
823 bsendmsg(ua, _("Invalid recycle status specified.\n"));
826 query = get_pool_memory(PM_MESSAGE);
827 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
828 recycle, mr.MediaId);
829 if (!db_sql_query(ua->db, query, NULL, NULL)) {
830 bsendmsg(ua, "%s", db_strerror(ua->db));
832 bsendmsg(ua, _("New recycle flag is: %s\n"),
833 mr.Recycle==1?_("yes"):_("no"));
835 free_pool_memory(query);
842 memset(&pr, 0, sizeof(POOL_DBR));
843 pr.PoolId = mr.PoolId;
844 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
845 bsendmsg(ua, "%s", db_strerror(ua->db));
848 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
849 if (!get_cmd(ua, _("Enter new Slot: "))) {
852 slot = atoi(ua->cmd);
854 bsendmsg(ua, _("Invalid slot, it must be 0 or greater\n"));
856 } else if (pr.MaxVols > 0 && slot >(int)pr.MaxVols) {
857 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
861 query = get_pool_memory(PM_MESSAGE);
862 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
864 if (!db_sql_query(ua->db, query, NULL, NULL)) {
865 bsendmsg(ua, "%s", db_strerror(ua->db));
867 bsendmsg(ua, "New Slot is: %d\n", slot);
869 free_pool_memory(query);
872 case 8: /* Volume Files */
874 bsendmsg(ua, _("Warning changing Volume Files can result\n"
875 "in loss of data on your Volume\n\n"));
876 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
877 if (!get_cmd(ua, _("Enter new number of Files for Volume: "))) {
880 VolFiles = atoi(ua->cmd);
882 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
885 if (VolFiles != (int)(mr.VolFiles + 1)) {
886 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
887 if (!get_cmd(ua, _("Continue? (yes/no): ")) ||
888 strcasecmp(ua->cmd, "yes") != 0) {
892 query = get_pool_memory(PM_MESSAGE);
893 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
894 VolFiles, mr.MediaId);
895 if (!db_sql_query(ua->db, query, NULL, NULL)) {
896 bsendmsg(ua, "%s", db_strerror(ua->db));
898 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
900 free_pool_memory(query);
903 default: /* Done or error */
904 bsendmsg(ua, "Selection done.\n");
912 * Update pool record -- pull info from current POOL resource
914 static int update_pool(UAContext *ua)
922 pool = get_pool_resource(ua);
927 memset(&pr, 0, sizeof(pr));
928 strcpy(pr.Name, pool->hdr.name);
929 if (!get_pool_dbr(ua, &pr)) {
933 set_pooldbr_from_poolres(&pr, pool, 0); /* update */
935 id = db_update_pool_record(ua->jcr, ua->db, &pr);
937 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
938 id, db_strerror(ua->db));
940 query = get_pool_memory(PM_MESSAGE);
941 Mmsg(&query, list_pool, pr.PoolId);
942 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, 0);
943 free_pool_memory(query);
944 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
949 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
953 ua->jcr->store = store;
954 /* Try connecting for up to 15 seconds */
955 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
956 store->hdr.name, store->address, store->SDport);
957 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
958 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
961 Dmsg0(120, _("Connected to storage daemon\n"));
962 sd = ua->jcr->store_bsock;
963 bnet_fsend(sd, "setdebug=%d\n", level);
964 if (bnet_recv(sd) >= 0) {
965 bsendmsg(ua, "%s", sd->msg);
967 bnet_sig(sd, BNET_TERMINATE);
969 ua->jcr->store_bsock = NULL;
973 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
977 /* Connect to File daemon */
979 ua->jcr->client = client;
980 /* Try to connect for 15 seconds */
981 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
982 client->hdr.name, client->address, client->FDport);
983 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
984 bsendmsg(ua, _("Failed to connect to Client.\n"));
987 Dmsg0(120, "Connected to file daemon\n");
988 fd = ua->jcr->file_bsock;
989 bnet_fsend(fd, "setdebug=%d\n", level);
990 if (bnet_recv(fd) >= 0) {
991 bsendmsg(ua, "%s", fd->msg);
993 bnet_sig(fd, BNET_TERMINATE);
995 ua->jcr->file_bsock = NULL;
1001 static void do_all_setdebug(UAContext *ua, int level)
1003 STORE *store, **unique_store;
1004 CLIENT *client, **unique_client;
1008 debug_level = level;
1010 /* Count Storage items */
1013 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1015 unique_store = (STORE **) malloc(i * sizeof(STORE));
1016 /* Find Unique Storage address/port */
1017 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1019 unique_store[i++] = store;
1020 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1022 for (j=0; j<i; j++) {
1023 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1024 unique_store[j]->SDport == store->SDport) {
1030 unique_store[i++] = store;
1031 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1036 /* Call each unique Storage daemon */
1037 for (j=0; j<i; j++) {
1038 do_storage_setdebug(ua, unique_store[j], level);
1042 /* Count Client items */
1045 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1047 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1048 /* Find Unique Client address/port */
1049 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1051 unique_client[i++] = client;
1052 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1054 for (j=0; j<i; j++) {
1055 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1056 unique_client[j]->FDport == client->FDport) {
1062 unique_client[i++] = client;
1063 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1068 /* Call each unique File daemon */
1069 for (j=0; j<i; j++) {
1070 do_client_setdebug(ua, unique_client[j], level);
1072 free(unique_client);
1076 * setdebug level=nn all
1078 static int setdebugcmd(UAContext *ua, char *cmd)
1088 Dmsg1(120, "setdebug:%s:\n", cmd);
1091 for (i=1; i<ua->argc; i++) {
1092 if (strcasecmp(ua->argk[i], _("level")) == 0 && ua->argv[i]) {
1093 level = atoi(ua->argv[i]);
1098 if (!get_cmd(ua, _("Enter new debug level: "))) {
1101 level = atoi(ua->cmd);
1104 bsendmsg(ua, _("level cannot be negative.\n"));
1108 /* General debug? */
1109 for (i=1; i<ua->argc; i++) {
1110 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1111 do_all_setdebug(ua, level);
1114 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1115 strcasecmp(ua->argk[i], _("director")) == 0) {
1116 debug_level = level;
1119 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1122 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1124 do_client_setdebug(ua, client, level);
1128 client = select_client_resource(ua);
1130 do_client_setdebug(ua, client, level);
1135 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1136 strcasecmp(ua->argk[i], _("storage")) == 0) {
1139 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1141 do_storage_setdebug(ua, store, level);
1145 store = get_storage_resource(ua, 0);
1147 do_storage_setdebug(ua, store, level);
1153 * We didn't find an appropriate keyword above, so
1156 start_prompt(ua, _("Available daemons are: \n"));
1157 add_prompt(ua, _("Director"));
1158 add_prompt(ua, _("Storage"));
1159 add_prompt(ua, _("Client"));
1160 add_prompt(ua, _("All"));
1161 switch(do_prompt(ua, _("Select daemon type to set debug level"), NULL, 0)) {
1162 case 0: /* Director */
1163 debug_level = level;
1166 store = get_storage_resource(ua, 0);
1168 do_storage_setdebug(ua, store, level);
1172 client = select_client_resource(ua);
1174 do_client_setdebug(ua, client, level);
1178 do_all_setdebug(ua, level);
1189 static int timecmd(UAContext *ua, char *cmd)
1192 time_t ttime = time(NULL);
1194 localtime_r(&ttime, &tm);
1195 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1196 bsendmsg(ua, "%s\n", sdt);
1203 * Delete Pool records (should purge Media with it).
1205 * delete pool=<pool-name>
1206 * delete media pool=<pool-name> volume=<name>
1208 static int deletecmd(UAContext *ua, char *cmd)
1210 static char *keywords[] = {
1220 "In general it is not a good idea to delete either a\n"
1221 "Pool or a Volume since they may contain data.\n\n"));
1223 switch (find_arg_keyword(ua, keywords)) {
1233 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1241 bsendmsg(ua, _("Nothing done.\n"));
1248 * Delete media records from database -- dangerous
1250 static int delete_volume(UAContext *ua)
1254 if (!select_media_dbr(ua, &mr)) {
1257 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1258 "and all Jobs saved on that volume from the Catalog\n"),
1261 if (!get_cmd(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1264 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1265 db_delete_media_record(ua->jcr, ua->db, &mr);
1271 * Delete a pool record from the database -- dangerous
1273 static int delete_pool(UAContext *ua)
1277 memset(&pr, 0, sizeof(pr));
1279 if (!get_pool_dbr(ua, &pr)) {
1282 if (!get_cmd(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1285 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1286 db_delete_pool_record(ua->jcr, ua->db, &pr);
1292 static void do_mount_cmd(int mount, UAContext *ua, char *cmd)
1296 char dev_name[MAX_NAME_LENGTH];
1302 Dmsg1(120, "mount: %s\n", ua->UA_sock->msg);
1304 store = get_storage_resource(ua, 1);
1309 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1310 store->media_type, store->dev_name);
1312 ua->jcr->store = store;
1313 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1314 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1317 sd = ua->jcr->store_bsock;
1318 strcpy(dev_name, store->dev_name);
1319 bash_spaces(dev_name);
1321 bnet_fsend(sd, "mount %s", dev_name);
1323 bnet_fsend(sd, "unmount %s", dev_name);
1325 while (bnet_recv(sd) >= 0) {
1326 bsendmsg(ua, "%s", sd->msg);
1327 if (strncmp(sd->msg, "3001 OK mount.", 14) == 0) {
1328 /***** ****FIXME**** fix JobStatus */
1331 bnet_sig(sd, BNET_TERMINATE);
1333 ua->jcr->store_bsock = NULL;
1337 * mount [storage | device] <name>
1339 static int mountcmd(UAContext *ua, char *cmd)
1341 do_mount_cmd(1, ua, cmd); /* mount */
1347 * unmount [storage | device] <name>
1349 static int unmountcmd(UAContext *ua, char *cmd)
1351 do_mount_cmd(0, ua, cmd); /* unmount */
1358 * use catalog=<name>
1360 static int usecmd(UAContext *ua, char *cmd)
1362 CAT *oldcatalog, *catalog;
1365 close_db(ua); /* close any previously open db */
1366 oldcatalog = ua->catalog;
1368 if (!(catalog = get_catalog_resource(ua))) {
1369 ua->catalog = oldcatalog;
1371 ua->catalog = catalog;
1374 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1375 ua->catalog->hdr.name, ua->catalog->db_name);
1380 int quitcmd(UAContext *ua, char *cmd)
1386 static int helpcmd(UAContext *ua, char *cmd)
1391 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1392 for (i=0; i<comsize; i++) {
1393 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1399 static int versioncmd(UAContext *ua, char *cmd)
1401 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1406 /* A bit brain damaged in that if the user has not done
1407 * a "use catalog xxx" command, we simply find the first
1408 * catalog resource and open it.
1410 int open_db(UAContext *ua)
1417 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1420 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1423 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1424 ua->catalog->hdr.name, ua->catalog->db_name);
1428 Dmsg0(150, "Open database\n");
1429 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1430 ua->catalog->db_password, ua->catalog->db_address,
1431 ua->catalog->db_port, ua->catalog->db_socket);
1432 if (!db_open_database(ua->jcr, ua->db)) {
1433 bsendmsg(ua, _("Could not open DB %s: ERR=%s"),
1434 ua->catalog->db_name, db_strerror(ua->db));
1438 ua->jcr->db = ua->db;
1439 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1443 void close_db(UAContext *ua)
1446 db_close_database(ua->jcr, ua->db);