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);
64 /* Forward referenced functions */
65 static int addcmd(UAContext *ua, char *cmd), createcmd(UAContext *ua, char *cmd), cancelcmd(UAContext *ua, char *cmd);
66 static int setdebugcmd(UAContext *ua, char *cmd);
67 static int helpcmd(UAContext *ua, char *cmd);
68 static int deletecmd(UAContext *ua, char *cmd);
69 static int usecmd(UAContext *ua, char *cmd), unmountcmd(UAContext *ua, char *cmd);
70 static int versioncmd(UAContext *ua, char *cmd), automountcmd(UAContext *ua, char *cmd);
71 static int timecmd(UAContext *ua, char *cmd);
72 static int update_volume(UAContext *ua);
73 static int update_pool(UAContext *ua);
74 static int delete_volume(UAContext *ua);
75 static int delete_pool(UAContext *ua);
76 static int mountcmd(UAContext *ua, char *cmd);
77 static int updatecmd(UAContext *ua, char *cmd);
79 int quitcmd(UAContext *ua, char *cmd);
82 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
83 static struct cmdstruct commands[] = {
84 { N_("add"), addcmd, _("add media to a pool")},
85 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
86 { N_("automount"), automountcmd, _("automount [on/off] -- after label")},
87 { N_("cancel"), cancelcmd, _("cancel job=nnn -- cancel a job")},
88 { N_("create"), createcmd, _("create DB Pool from resource")},
89 { N_("delete"), deletecmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
90 { N_("help"), helpcmd, _("print this command")},
91 { N_("label"), labelcmd, _("label a tape")},
92 { N_("relabel"), relabelcmd, _("relabel a tape")},
93 { N_("list"), listcmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
94 { N_("llist"), llistcmd, _("full or long list like list command")},
95 { N_("messages"), messagescmd, _("messages")},
96 { N_("mount"), mountcmd, _("mount <storage-name>")},
97 { N_("restore"), restorecmd, _("restore files")},
98 { N_("prune"), prunecmd, _("prune expired records from catalog")},
99 { N_("purge"), purgecmd, _("purge records from catalog")},
100 { N_("run"), runcmd, _("run <job-name>")},
101 { N_("setdebug"), setdebugcmd, _("sets debug level")},
102 { N_("show"), showcmd, _("show (resource records) [jobs | pools | ... | all]")},
103 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
104 { N_("status"), statuscmd, _("status [storage | client]=<name>")},
105 { N_("unmount"), unmountcmd, _("unmount <storage-name>")},
106 { N_("update"), updatecmd, _("update Volume or Pool")},
107 { N_("use"), usecmd, _("use catalog xxx")},
108 { N_("version"), versioncmd, _("print Director version")},
109 { N_("quit"), quitcmd, _("quit")},
110 { N_("query"), querycmd, _("query catalog")},
111 { N_("time"), timecmd, _("print current time")},
112 { N_("exit"), quitcmd, _("exit = quit")},
114 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
117 * Execute a command from the UA
119 int do_a_command(UAContext *ua, char *cmd)
128 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
133 len = strlen(ua->argk[0]);
134 for (i=0; i<comsize; i++) /* search for command */
135 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
136 stat = (*commands[i].func)(ua, cmd); /* go execute command */
141 strcat(ua->UA_sock->msg, _(": is an illegal command\n"));
142 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
143 bnet_send(ua->UA_sock);
149 * This is a common routine used to stuff the Pool DB record defaults
150 * into the Media DB record just before creating a media (Volume)
153 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
155 mr->PoolId = pr->PoolId;
156 strcpy(mr->VolStatus, "Append");
157 mr->Recycle = pr->Recycle;
158 mr->VolRetention = pr->VolRetention;
159 mr->VolUseDuration = pr->VolUseDuration;
160 mr->MaxVolJobs = pr->MaxVolJobs;
161 mr->MaxVolFiles = pr->MaxVolFiles;
162 mr->MaxVolBytes = pr->MaxVolBytes;
167 * Add Volumes to an existing Pool
169 static int addcmd(UAContext *ua, char *cmd)
173 int num, i, max, startnum;
175 char name[MAX_NAME_LENGTH];
180 "You probably don't want to be using this command since it\n"
181 "creates database records without labeling the Volumes.\n"
182 "You probably want to use the \"label\" command.\n\n"));
188 memset(&pr, 0, sizeof(pr));
189 memset(&mr, 0, sizeof(mr));
191 if (!get_pool_dbr(ua, &pr)) {
195 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
196 pr.MaxVols, pr.PoolType);
198 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
199 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
201 if (!get_cmd(ua, _("Enter new maximum (zero for unlimited): "))) {
204 pr.MaxVols = atoi(ua->cmd);
205 if (pr.MaxVols < 0) {
206 bsendmsg(ua, _("Max vols must be zero or greater.\n"));
214 if ((store = get_storage_resource(ua, cmd)) != NULL) {
215 strcpy(mr.MediaType, store->media_type);
216 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
220 if (pr.MaxVols == 0) {
223 max = pr.MaxVols - pr.NumVols;
227 sprintf(buf, _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
228 if (!get_cmd(ua, buf)) {
232 if (num < 0 || num > max) {
233 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
240 if (!get_cmd(ua, _("Enter Volume name: "))) {
244 if (!get_cmd(ua, _("Enter base volume name: "))) {
248 /* Don't allow | in Volume name because it is the volume separator character */
249 if (strchr(ua->cmd, '|')) {
250 bsendmsg(ua, _("Illegal character | in a volume name.\n"));
253 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
254 bsendmsg(ua, _("Volume name too long.\n"));
257 if (strlen(ua->cmd) == 0) {
258 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
262 strcpy(name, ua->cmd);
264 strcat(name, "%04d");
267 if (!get_cmd(ua, _("Enter the starting number: "))) {
270 startnum = atoi(ua->cmd);
272 bsendmsg(ua, _("Start number must be greater than zero.\n"));
282 if (store && store->autochanger) {
283 if (!get_cmd(ua, _("Enter slot (0 for none): "))) {
286 slot = atoi(ua->cmd);
289 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
290 for (i=startnum; i < num+startnum; i++) {
291 sprintf(mr.VolumeName, name, i);
293 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
294 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
295 bsendmsg(ua, db_strerror(ua->db));
299 first_id = mr.PoolId;
303 Dmsg0(200, "Update pool record.\n");
304 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
305 bsendmsg(ua, db_strerror(ua->db));
308 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
314 * Turn auto mount on/off
319 int automountcmd(UAContext *ua, char *cmd)
324 if (!get_cmd(ua, _("Turn on or off? "))) {
332 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
340 static int cancelcmd(UAContext *ua, char *cmd)
346 char JobName[MAX_NAME_LENGTH];
352 for (i=1; i<ua->argc; i++) {
353 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
357 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
358 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
362 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
366 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
367 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
373 /* If we still do not have a jcr,
374 * throw up a list and ask the user to select one.
377 /* Count Jobs running */
379 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
380 if (jcr->JobId == 0) { /* this is us */
381 free_locked_jcr(jcr);
385 free_locked_jcr(jcr);
390 bsendmsg(ua, _("No Jobs running.\n"));
393 start_prompt(ua, _("Select Job:\n"));
395 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
396 if (jcr->JobId == 0) { /* this is us */
397 free_locked_jcr(jcr);
400 add_prompt(ua, jcr->Job);
401 free_locked_jcr(jcr);
405 if (do_prompt(ua, _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
409 if (!get_cmd(ua, _("Confirm cancel (yes/no): "))) {
412 if (strcasecmp(ua->cmd, _("yes")) != 0) {
416 jcr = get_jcr_by_full_name(JobName);
418 bsendmsg(ua, _("Job %s not found.\n"), JobName);
423 switch (jcr->JobStatus) {
425 set_jcr_job_status(jcr, JS_Canceled);
426 bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
427 jcr->JobId, jcr->Job);
428 #ifndef USE_SEMAPHORE
429 workq_remove(&job_wq, jcr->work_item); /* attempt to remove it from queue */
435 set_jcr_job_status(jcr, JS_Canceled);
436 /* Cancel File daemon */
437 ua->jcr->client = jcr->client;
438 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
439 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
443 Dmsg0(200, "Connected to file daemon\n");
444 fd = ua->jcr->file_bsock;
445 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
446 while (bnet_recv(fd) >= 0) {
447 bsendmsg(ua, "%s", fd->msg);
449 bnet_sig(fd, BNET_TERMINATE);
451 ua->jcr->file_bsock = NULL;
453 /* Cancel Storage daemon */
454 ua->jcr->store = jcr->store;
455 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
456 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
460 Dmsg0(200, "Connected to storage daemon\n");
461 sd = ua->jcr->store_bsock;
462 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
463 while (bnet_recv(sd) >= 0) {
464 bsendmsg(ua, "%s", sd->msg);
466 bnet_sig(sd, BNET_TERMINATE);
468 ua->jcr->store_bsock = NULL;
477 * This is a common routine to create or update a
478 * Pool DB base record from a Pool Resource. We handle
479 * the setting of MaxVols and NumVols slightly differently
480 * depending on if we are creating the Pool or we are
481 * simply bringing it into agreement with the resource (updage).
483 void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, int create)
485 strcpy(pr->PoolType, pool->pool_type);
487 pr->MaxVols = pool->max_volumes;
489 } else { /* update pool */
490 if (pr->MaxVols != pool->max_volumes) {
491 pr->MaxVols = pool->max_volumes;
493 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
494 pr->MaxVols = pr->NumVols;
497 pr->UseOnce = pool->use_volume_once;
498 pr->UseCatalog = pool->use_catalog;
499 pr->AcceptAnyVolume = pool->accept_any_volume;
500 pr->Recycle = pool->Recycle;
501 pr->VolRetention = pool->VolRetention;
502 pr->VolUseDuration = pool->VolUseDuration;
503 pr->MaxVolJobs = pool->MaxVolJobs;
504 pr->MaxVolFiles = pool->MaxVolFiles;
505 pr->MaxVolBytes = pool->MaxVolBytes;
506 if (pool->label_format) {
507 strcpy(pr->LabelFormat, pool->label_format);
509 strcpy(pr->LabelFormat, "*"); /* none */
515 * Create a pool record from a given Pool resource
516 * Also called from backup.c
517 * Returns: -1 on error
518 * 0 record already exists
522 int create_pool(JCR *jcr, B_DB *db, POOL *pool, int create)
526 memset(&pr, 0, sizeof(POOL_DBR));
528 strcpy(pr.Name, pool->hdr.name);
530 if (db_get_pool_record(jcr, db, &pr)) {
532 if (!create) { /* update request */
533 set_pooldbr_from_poolres(&pr, pool, 0);
534 db_update_pool_record(jcr, db, &pr);
536 return 0; /* exists */
539 set_pooldbr_from_poolres(&pr, pool, 1);
541 if (!db_create_pool_record(jcr, db, &pr)) {
542 return -1; /* error */
550 * Create a Pool Record in the database.
551 * It is always created from the Resource record.
553 static int createcmd(UAContext *ua, char *cmd)
561 pool = get_pool_resource(ua);
566 switch (create_pool(ua->jcr, ua->db, pool, 1)) {
568 bsendmsg(ua, _("Error: Pool %s already exists.\n\
569 Use update to change it.\n"), pool->hdr.name);
573 bsendmsg(ua, db_strerror(ua->db));
579 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
587 * Update a Pool Record in the database.
588 * It is always updated from the Resource record.
590 * update pool=<pool-name>
591 * updates pool from Pool resource
592 * update media pool=<pool-name> volume=<volume-name>
593 * changes pool info for volume
595 static int updatecmd(UAContext *ua, char *cmd)
597 static char *kw[] = {
607 switch (find_arg_keyword(ua, kw)) {
619 start_prompt(ua, _("Update choice:\n"));
620 add_prompt(ua, _("Volume parameters"));
621 add_prompt(ua, _("Pool from resource"));
622 switch (do_prompt(ua, _("Choose catalog item to update"), NULL, 0)) {
636 * Update a media record -- allows you to change the
637 * Volume status. E.g. if you want Bacula to stop
638 * writing on the volume, set it to anything other
641 static int update_volume(UAContext *ua)
648 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
652 for (int done=0; !done; ) {
653 if (!db_get_media_record(ua->jcr, ua->db, &mr)) {
654 if (mr.MediaId != 0) {
655 bsendmsg(ua, _("Volume record for MediaId %d not found.\n"), mr.MediaId);
657 bsendmsg(ua, _("Volume record for %s not found.\n"), mr.VolumeName);
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);
840 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
841 if (!get_cmd(ua, _("Enter new Slot: "))) {
844 slot = atoi(ua->cmd);
846 bsendmsg(ua, _("Invalid slot, it must be 0 or greater\n"));
848 } else if (pr.MaxVols > 0 && slot >(int)pr.MaxVols) {
849 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
853 query = get_pool_memory(PM_MESSAGE);
854 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
856 if (!db_sql_query(ua->db, query, NULL, NULL)) {
857 bsendmsg(ua, "%s", db_strerror(ua->db));
859 bsendmsg(ua, "New Slot is: %d\n", slot);
861 free_pool_memory(query);
864 case 8: /* Volume Files */
866 bsendmsg(ua, _("Warning changing Volume Files can result\n"
867 "in loss of data on your Volume\n\n"));
868 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
869 if (!get_cmd(ua, _("Enter new number of Files for Volume: "))) {
872 VolFiles = atoi(ua->cmd);
874 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
877 if (VolFiles != (int)(mr.VolFiles + 1)) {
878 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
879 if (!get_cmd(ua, _("Continue? (yes/no): ")) ||
880 strcasecmp(ua->cmd, "yes") != 0) {
884 query = get_pool_memory(PM_MESSAGE);
885 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
886 VolFiles, mr.MediaId);
887 if (!db_sql_query(ua->db, query, NULL, NULL)) {
888 bsendmsg(ua, "%s", db_strerror(ua->db));
890 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
892 free_pool_memory(query);
895 default: /* Done or error */
896 bsendmsg(ua, "Selection done.\n");
904 * Update pool record -- pull info from current POOL resource
906 static int update_pool(UAContext *ua)
914 pool = get_pool_resource(ua);
919 memset(&pr, 0, sizeof(pr));
920 strcpy(pr.Name, pool->hdr.name);
921 if (!get_pool_dbr(ua, &pr)) {
925 set_pooldbr_from_poolres(&pr, pool, 0); /* update */
927 id = db_update_pool_record(ua->jcr, ua->db, &pr);
929 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
930 id, db_strerror(ua->db));
932 query = get_pool_memory(PM_MESSAGE);
933 Mmsg(&query, list_pool, pr.PoolId);
934 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, 0);
935 free_pool_memory(query);
936 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
941 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
945 ua->jcr->store = store;
946 /* Try connecting for up to 15 seconds */
947 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
948 store->hdr.name, store->address, store->SDport);
949 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
950 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
953 Dmsg0(120, _("Connected to storage daemon\n"));
954 sd = ua->jcr->store_bsock;
955 bnet_fsend(sd, "setdebug=%d\n", level);
956 if (bnet_recv(sd) >= 0) {
957 bsendmsg(ua, "%s", sd->msg);
959 bnet_sig(sd, BNET_TERMINATE);
961 ua->jcr->store_bsock = NULL;
965 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
969 /* Connect to File daemon */
971 ua->jcr->client = client;
972 /* Try to connect for 15 seconds */
973 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
974 client->hdr.name, client->address, client->FDport);
975 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
976 bsendmsg(ua, _("Failed to connect to Client.\n"));
979 Dmsg0(120, "Connected to file daemon\n");
980 fd = ua->jcr->file_bsock;
981 bnet_fsend(fd, "setdebug=%d\n", level);
982 if (bnet_recv(fd) >= 0) {
983 bsendmsg(ua, "%s", fd->msg);
985 bnet_sig(fd, BNET_TERMINATE);
987 ua->jcr->file_bsock = NULL;
993 static void do_all_setdebug(UAContext *ua, int level)
995 STORE *store, **unique_store;
996 CLIENT *client, **unique_client;
1000 debug_level = level;
1002 /* Count Storage items */
1005 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1007 unique_store = (STORE **) malloc(i * sizeof(STORE));
1008 /* Find Unique Storage address/port */
1009 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1011 unique_store[i++] = store;
1012 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1014 for (j=0; j<i; j++) {
1015 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1016 unique_store[j]->SDport == store->SDport) {
1022 unique_store[i++] = store;
1023 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1028 /* Call each unique Storage daemon */
1029 for (j=0; j<i; j++) {
1030 do_storage_setdebug(ua, unique_store[j], level);
1034 /* Count Client items */
1037 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1039 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1040 /* Find Unique Client address/port */
1041 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1043 unique_client[i++] = client;
1044 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1046 for (j=0; j<i; j++) {
1047 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1048 unique_client[j]->FDport == client->FDport) {
1054 unique_client[i++] = client;
1055 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1060 /* Call each unique File daemon */
1061 for (j=0; j<i; j++) {
1062 do_client_setdebug(ua, unique_client[j], level);
1064 free(unique_client);
1068 * setdebug level=nn all
1070 static int setdebugcmd(UAContext *ua, char *cmd)
1080 Dmsg1(120, "setdebug:%s:\n", cmd);
1083 for (i=1; i<ua->argc; i++) {
1084 if (strcasecmp(ua->argk[i], _("level")) == 0 && ua->argv[i]) {
1085 level = atoi(ua->argv[i]);
1090 if (!get_cmd(ua, _("Enter new debug level: "))) {
1093 level = atoi(ua->cmd);
1096 bsendmsg(ua, _("level cannot be negative.\n"));
1100 /* General debug? */
1101 for (i=1; i<ua->argc; i++) {
1102 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1103 do_all_setdebug(ua, level);
1106 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1107 strcasecmp(ua->argk[i], _("director")) == 0) {
1108 debug_level = level;
1111 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1114 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1116 do_client_setdebug(ua, client, level);
1120 client = select_client_resource(ua);
1122 do_client_setdebug(ua, client, level);
1127 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1128 strcasecmp(ua->argk[i], _("storage")) == 0) {
1131 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1133 do_storage_setdebug(ua, store, level);
1137 store = get_storage_resource(ua, cmd);
1139 do_storage_setdebug(ua, store, level);
1145 * We didn't find an appropriate keyword above, so
1148 start_prompt(ua, _("Available daemons are: \n"));
1149 add_prompt(ua, _("Director"));
1150 add_prompt(ua, _("Storage"));
1151 add_prompt(ua, _("Client"));
1152 add_prompt(ua, _("All"));
1153 switch(do_prompt(ua, _("Select daemon type to set debug level"), NULL, 0)) {
1154 case 0: /* Director */
1155 debug_level = level;
1158 store = get_storage_resource(ua, cmd);
1160 do_storage_setdebug(ua, store, level);
1164 client = select_client_resource(ua);
1166 do_client_setdebug(ua, client, level);
1170 do_all_setdebug(ua, level);
1181 static int timecmd(UAContext *ua, char *cmd)
1184 time_t ttime = time(NULL);
1186 localtime_r(&ttime, &tm);
1187 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1188 bsendmsg(ua, "%s\n", sdt);
1195 * Delete Pool records (should purge Media with it).
1197 * delete pool=<pool-name>
1198 * delete media pool=<pool-name> volume=<name>
1200 static int deletecmd(UAContext *ua, char *cmd)
1202 static char *keywords[] = {
1212 "In general it is not a good idea to delete either a\n"
1213 "Pool or a Volume since they may contain data.\n\n"));
1215 switch (find_arg_keyword(ua, keywords)) {
1225 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1233 bsendmsg(ua, _("Nothing done.\n"));
1240 * Delete media records from database -- dangerous
1242 static int delete_volume(UAContext *ua)
1247 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
1250 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1251 "and all Jobs saved on that volume from the Catalog\n"),
1254 if (!get_cmd(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1257 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1258 db_delete_media_record(ua->jcr, ua->db, &mr);
1264 * Delete a pool record from the database -- dangerous
1266 static int delete_pool(UAContext *ua)
1270 memset(&pr, 0, sizeof(pr));
1272 if (!get_pool_dbr(ua, &pr)) {
1275 if (!get_cmd(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1278 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1279 db_delete_pool_record(ua->jcr, ua->db, &pr);
1285 static void do_mount_cmd(int mount, UAContext *ua, char *cmd)
1289 char dev_name[MAX_NAME_LENGTH];
1295 Dmsg1(120, "mount: %s\n", ua->UA_sock->msg);
1297 store = get_storage_resource(ua, cmd);
1302 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1303 store->media_type, store->dev_name);
1305 ua->jcr->store = store;
1306 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1307 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1310 sd = ua->jcr->store_bsock;
1311 strcpy(dev_name, store->dev_name);
1312 bash_spaces(dev_name);
1314 bnet_fsend(sd, "mount %s", dev_name);
1316 bnet_fsend(sd, "unmount %s", dev_name);
1318 while (bnet_recv(sd) >= 0) {
1319 bsendmsg(ua, "%s", sd->msg);
1320 if (strncmp(sd->msg, "3001 OK mount.", 14) == 0) {
1321 /***** ****FIXME**** fix JobStatus */
1324 bnet_sig(sd, BNET_TERMINATE);
1326 ua->jcr->store_bsock = NULL;
1330 * mount [storage | device] <name>
1332 static int mountcmd(UAContext *ua, char *cmd)
1334 do_mount_cmd(1, ua, cmd); /* mount */
1340 * unmount [storage | device] <name>
1342 static int unmountcmd(UAContext *ua, char *cmd)
1344 do_mount_cmd(0, ua, cmd); /* unmount */
1351 * use catalog=<name>
1353 static int usecmd(UAContext *ua, char *cmd)
1355 CAT *oldcatalog, *catalog;
1358 close_db(ua); /* close any previously open db */
1359 oldcatalog = ua->catalog;
1361 if (!(catalog = get_catalog_resource(ua))) {
1362 ua->catalog = oldcatalog;
1364 ua->catalog = catalog;
1367 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1368 ua->catalog->hdr.name, ua->catalog->db_name);
1373 int quitcmd(UAContext *ua, char *cmd)
1379 static int helpcmd(UAContext *ua, char *cmd)
1384 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1385 for (i=0; i<comsize; i++) {
1386 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1392 static int versioncmd(UAContext *ua, char *cmd)
1394 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1399 /* A bit brain damaged in that if the user has not done
1400 * a "use catalog xxx" command, we simply find the first
1401 * catalog resource and open it.
1403 int open_db(UAContext *ua)
1410 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1413 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1416 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1417 ua->catalog->hdr.name, ua->catalog->db_name);
1421 Dmsg0(150, "Open database\n");
1422 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1423 ua->catalog->db_password, ua->catalog->db_address,
1424 ua->catalog->db_port, ua->catalog->db_socket);
1425 if (!db_open_database(ua->jcr, ua->db)) {
1426 bsendmsg(ua, _("Could not open DB %s: ERR=%s"),
1427 ua->catalog->db_name, db_strerror(ua->db));
1431 ua->jcr->db = ua->db;
1432 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1436 void close_db(UAContext *ua)
1439 db_close_database(ua->jcr, ua->db);