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);
62 /* Forward referenced functions */
63 static int addcmd(UAContext *ua, char *cmd), createcmd(UAContext *ua, char *cmd), cancelcmd(UAContext *ua, char *cmd);
64 static int setdebugcmd(UAContext *ua, char *cmd);
65 static int helpcmd(UAContext *ua, char *cmd);
66 static int deletecmd(UAContext *ua, char *cmd);
67 static int usecmd(UAContext *ua, char *cmd), unmountcmd(UAContext *ua, char *cmd);
68 static int labelcmd(UAContext *ua, char *cmd), mountcmd(UAContext *ua, char *cmd), updatecmd(UAContext *ua, char *cmd);
69 static int versioncmd(UAContext *ua, char *cmd), automountcmd(UAContext *ua, char *cmd);
70 static int timecmd(UAContext *ua, char *cmd);
71 static int update_volume(UAContext *ua);
72 static int update_pool(UAContext *ua);
73 static int delete_volume(UAContext *ua);
74 static int delete_pool(UAContext *ua);
76 int quitcmd(UAContext *ua, char *cmd);
79 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
80 static struct cmdstruct commands[] = {
81 { N_("add"), addcmd, _("add media to a pool")},
82 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
83 { N_("automount"), automountcmd, _("automount [on/off] -- after label")},
84 { N_("cancel"), cancelcmd, _("cancel job=nnn -- cancel a job")},
85 { N_("create"), createcmd, _("create DB Pool from resource")},
86 { N_("delete"), deletecmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
87 { N_("help"), helpcmd, _("print this command")},
88 { N_("label"), labelcmd, _("label a tape")},
89 { N_("list"), listcmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
90 { N_("llist"), llistcmd, _("full or long list like list command")},
91 { N_("messages"), messagescmd, _("messages")},
92 { N_("mount"), mountcmd, _("mount <storage-name>")},
93 { N_("restore"), restorecmd, _("restore files")},
94 { N_("prune"), prunecmd, _("prune expired records from catalog")},
95 { N_("purge"), purgecmd, _("purge records from catalog")},
96 { N_("run"), runcmd, _("run <job-name>")},
97 { N_("setdebug"), setdebugcmd, _("sets debug level")},
98 { N_("show"), showcmd, _("show (resource records) [jobs | pools | ... | all]")},
99 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
100 { N_("status"), statuscmd, _("status [storage | client]=<name>")},
101 { N_("unmount"), unmountcmd, _("unmount <storage-name>")},
102 { N_("update"), updatecmd, _("update Volume or Pool")},
103 { N_("use"), usecmd, _("use catalog xxx")},
104 { N_("version"), versioncmd, _("print Director version")},
105 { N_("quit"), quitcmd, _("quit")},
106 { N_("query"), querycmd, _("query catalog")},
107 { N_("time"), timecmd, _("print current time")},
108 { N_("exit"), quitcmd, _("exit = quit")},
110 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
113 * Execute a command from the UA
115 int do_a_command(UAContext *ua, char *cmd)
124 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
129 len = strlen(ua->argk[0]);
130 for (i=0; i<comsize; i++) /* search for command */
131 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
132 stat = (*commands[i].func)(ua, cmd); /* go execute command */
137 strcat(ua->UA_sock->msg, _(": is an illegal command\n"));
138 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
139 bnet_send(ua->UA_sock);
145 * This is a common routine used to stuff the Pool DB record defaults
146 * into the Media DB record just before creating a media (Volume)
149 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
151 mr->PoolId = pr->PoolId;
152 strcpy(mr->VolStatus, "Append");
153 mr->Recycle = pr->Recycle;
154 mr->VolRetention = pr->VolRetention;
155 mr->VolUseDuration = pr->VolUseDuration;
156 mr->MaxVolJobs = pr->MaxVolJobs;
157 mr->MaxVolFiles = pr->MaxVolFiles;
158 mr->MaxVolBytes = pr->MaxVolBytes;
163 * Add Volumes to an existing Pool
165 static int addcmd(UAContext *ua, char *cmd)
169 int num, i, max, startnum;
171 char name[MAX_NAME_LENGTH];
176 "You probably don't want to be using this command since it\n"
177 "creates database records without labeling the Volumes.\n"
178 "You probably want to use the \"label\" command.\n\n"));
184 memset(&pr, 0, sizeof(pr));
185 memset(&mr, 0, sizeof(mr));
187 if (!get_pool_dbr(ua, &pr)) {
191 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
192 pr.MaxVols, pr.PoolType);
194 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
195 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
197 if (!get_cmd(ua, _("Enter new maximum (zero for unlimited): "))) {
200 pr.MaxVols = atoi(ua->cmd);
201 if (pr.MaxVols < 0) {
202 bsendmsg(ua, _("Max vols must be zero or greater.\n"));
210 if ((store = get_storage_resource(ua, cmd)) != NULL) {
211 strcpy(mr.MediaType, store->media_type);
212 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
216 if (pr.MaxVols == 0) {
219 max = pr.MaxVols - pr.NumVols;
223 sprintf(buf, _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
224 if (!get_cmd(ua, buf)) {
228 if (num < 0 || num > max) {
229 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
236 if (!get_cmd(ua, _("Enter Volume name: "))) {
240 if (!get_cmd(ua, _("Enter base volume name: "))) {
244 /* Don't allow | in Volume name because it is the volume separator character */
245 if (strchr(ua->cmd, '|')) {
246 bsendmsg(ua, _("Illegal character | in a volume name.\n"));
249 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
250 bsendmsg(ua, _("Volume name too long.\n"));
253 if (strlen(ua->cmd) == 0) {
254 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
258 strcpy(name, ua->cmd);
260 strcat(name, "%04d");
263 if (!get_cmd(ua, _("Enter the starting number: "))) {
266 startnum = atoi(ua->cmd);
268 bsendmsg(ua, _("Start number must be greater than zero.\n"));
278 if (store && store->autochanger) {
279 if (!get_cmd(ua, _("Enter slot (0 for none): "))) {
282 slot = atoi(ua->cmd);
285 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
286 for (i=startnum; i < num+startnum; i++) {
287 sprintf(mr.VolumeName, name, i);
289 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
290 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
291 bsendmsg(ua, db_strerror(ua->db));
295 first_id = mr.PoolId;
299 Dmsg0(200, "Update pool record.\n");
300 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
301 bsendmsg(ua, db_strerror(ua->db));
304 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
310 * Turn auto mount on/off
315 int automountcmd(UAContext *ua, char *cmd)
320 if (!get_cmd(ua, _("Turn on or off? "))) {
328 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
336 static int cancelcmd(UAContext *ua, char *cmd)
342 char JobName[MAX_NAME_LENGTH];
348 for (i=1; i<ua->argc; i++) {
349 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
353 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
354 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
358 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
362 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
363 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
369 /* If we still do not have a jcr,
370 * throw up a list and ask the user to select one.
373 /* Count Jobs running */
375 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
376 if (jcr->JobId == 0) { /* this is us */
377 free_locked_jcr(jcr);
381 free_locked_jcr(jcr);
386 bsendmsg(ua, _("No Jobs running.\n"));
389 start_prompt(ua, _("Select Job:\n"));
391 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
392 if (jcr->JobId == 0) { /* this is us */
393 free_locked_jcr(jcr);
396 add_prompt(ua, jcr->Job);
397 free_locked_jcr(jcr);
401 if (do_prompt(ua, _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
405 if (!get_cmd(ua, _("Confirm cancel (yes/no): "))) {
408 if (strcasecmp(ua->cmd, _("yes")) != 0) {
412 jcr = get_jcr_by_full_name(JobName);
414 bsendmsg(ua, _("Job %s not found.\n"), JobName);
419 switch (jcr->JobStatus) {
421 set_jcr_job_status(jcr, JS_Cancelled);
422 bsendmsg(ua, _("JobId %d, Job %s marked to be cancelled.\n"),
423 jcr->JobId, jcr->Job);
424 #ifndef USE_SEMAPHORE
425 workq_remove(&job_wq, jcr->work_item); /* attempt to remove it from queue */
431 set_jcr_job_status(jcr, JS_Cancelled);
432 /* Cancel File daemon */
433 ua->jcr->client = jcr->client;
434 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
435 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
439 Dmsg0(200, "Connected to file daemon\n");
440 fd = ua->jcr->file_bsock;
441 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
442 while (bnet_recv(fd) >= 0) {
443 bsendmsg(ua, "%s", fd->msg);
445 bnet_sig(fd, BNET_TERMINATE);
447 ua->jcr->file_bsock = NULL;
449 /* Cancel Storage daemon */
450 ua->jcr->store = jcr->store;
451 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
452 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
456 Dmsg0(200, "Connected to storage daemon\n");
457 sd = ua->jcr->store_bsock;
458 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
459 while (bnet_recv(sd) >= 0) {
460 bsendmsg(ua, "%s", sd->msg);
462 bnet_sig(sd, BNET_TERMINATE);
464 ua->jcr->store_bsock = NULL;
473 * This is a common routine to create or update a
474 * Pool DB base record from a Pool Resource. We handle
475 * the setting of MaxVols and NumVols slightly differently
476 * depending on if we are creating the Pool or we are
477 * simply bringing it into agreement with the resource (updage).
479 void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, int create)
481 strcpy(pr->PoolType, pool->pool_type);
483 pr->MaxVols = pool->max_volumes;
485 } else { /* update pool */
486 if (pr->MaxVols != pool->max_volumes) {
487 pr->MaxVols = pool->max_volumes;
489 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
490 pr->MaxVols = pr->NumVols;
493 pr->UseOnce = pool->use_volume_once;
494 pr->UseCatalog = pool->use_catalog;
495 pr->AcceptAnyVolume = pool->accept_any_volume;
496 pr->Recycle = pool->Recycle;
497 pr->VolRetention = pool->VolRetention;
498 pr->VolUseDuration = pool->VolUseDuration;
499 pr->MaxVolJobs = pool->MaxVolJobs;
500 pr->MaxVolFiles = pool->MaxVolFiles;
501 pr->MaxVolBytes = pool->MaxVolBytes;
502 if (pool->label_format) {
503 strcpy(pr->LabelFormat, pool->label_format);
505 strcpy(pr->LabelFormat, "*"); /* none */
511 * Create a pool record from a given Pool resource
512 * Also called from backup.c
513 * Returns: -1 on error
514 * 0 record already exists
518 int create_pool(JCR *jcr, B_DB *db, POOL *pool, int create)
522 memset(&pr, 0, sizeof(POOL_DBR));
524 strcpy(pr.Name, pool->hdr.name);
526 if (db_get_pool_record(jcr, db, &pr)) {
528 if (!create) { /* update request */
529 set_pooldbr_from_poolres(&pr, pool, 0);
530 db_update_pool_record(jcr, db, &pr);
532 return 0; /* exists */
535 set_pooldbr_from_poolres(&pr, pool, 1);
537 if (!db_create_pool_record(jcr, db, &pr)) {
538 return -1; /* error */
546 * Create a Pool Record in the database.
547 * It is always created from the Resource record.
549 static int createcmd(UAContext *ua, char *cmd)
557 pool = get_pool_resource(ua);
562 switch (create_pool(ua->jcr, ua->db, pool, 1)) {
564 bsendmsg(ua, _("Error: Pool %s already exists.\n\
565 Use update to change it.\n"), pool->hdr.name);
569 bsendmsg(ua, db_strerror(ua->db));
575 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
583 * Update a Pool Record in the database.
584 * It is always updated from the Resource record.
586 * update pool=<pool-name>
587 * updates pool from Pool resource
588 * update media pool=<pool-name> volume=<volume-name>
589 * changes pool info for volume
591 static int updatecmd(UAContext *ua, char *cmd)
593 static char *kw[] = {
603 switch (find_arg_keyword(ua, kw)) {
615 start_prompt(ua, _("Update choice:\n"));
616 add_prompt(ua, _("Volume parameters"));
617 add_prompt(ua, _("Pool from resource"));
618 switch (do_prompt(ua, _("Choose catalog item to update"), NULL, 0)) {
632 * Update a media record -- allows you to change the
633 * Volume status. E.g. if you want Bacula to stop
634 * writing on the volume, set it to anything other
637 static int update_volume(UAContext *ua)
644 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
648 for (int done=0; !done; ) {
649 if (!db_get_media_record(ua->jcr, ua->db, &mr)) {
650 if (mr.MediaId != 0) {
651 bsendmsg(ua, _("Volume record for MediaId %d not found.\n"), mr.MediaId);
653 bsendmsg(ua, _("Volume record for %s not found.\n"), mr.VolumeName);
657 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
658 start_prompt(ua, _("Parameters to modify:\n"));
659 add_prompt(ua, _("Volume Status"));
660 add_prompt(ua, _("Volume Retention Period"));
661 add_prompt(ua, _("Volume Use Duration"));
662 add_prompt(ua, _("Maximum Volume Jobs"));
663 add_prompt(ua, _("Maximum Volume Files"));
664 add_prompt(ua, _("Maximum Volume Bytes"));
665 add_prompt(ua, _("Recycle Flag"));
666 add_prompt(ua, _("Slot"));
667 add_prompt(ua, _("Volume Files"));
668 add_prompt(ua, _("Done"));
669 switch (do_prompt(ua, _("Select parameter to modify"), NULL, 0)) {
670 case 0: /* Volume Status */
671 /* Modify Volume Status */
672 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
673 start_prompt(ua, _("Possible Values are:\n"));
674 add_prompt(ua, "Append"); /* Better not translate these as */
675 add_prompt(ua, "Archive"); /* They are known in the database code */
676 add_prompt(ua, "Disabled");
677 add_prompt(ua, "Full");
678 add_prompt(ua, "Used");
679 if (strcmp(mr.VolStatus, "Purged") == 0) {
680 add_prompt(ua, "Recycle");
682 add_prompt(ua, "Read-Only");
683 if (do_prompt(ua, _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
686 bstrncpy(mr.VolStatus, ua->cmd, sizeof(mr.VolStatus));
687 query = get_pool_memory(PM_MESSAGE);
688 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
689 mr.VolStatus, mr.MediaId);
690 if (!db_sql_query(ua->db, query, NULL, NULL)) {
691 bsendmsg(ua, "%s", db_strerror(ua->db));
693 bsendmsg(ua, _("New Volume status is: %s\n"), mr.VolStatus);
695 free_pool_memory(query);
697 case 1: /* Retention */
698 bsendmsg(ua, _("Current retention seconds is: %s\n"),
699 edit_utime(mr.VolRetention, ed1));
700 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
703 if (!duration_to_utime(ua->cmd, &mr.VolRetention)) {
704 bsendmsg(ua, _("Invalid retention period specified.\n"));
707 query = get_pool_memory(PM_MESSAGE);
708 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
709 edit_uint64(mr.VolRetention, ed1), mr.MediaId);
710 if (!db_sql_query(ua->db, query, NULL, NULL)) {
711 bsendmsg(ua, "%s", db_strerror(ua->db));
713 bsendmsg(ua, _("New retention seconds is: %s\n"),
714 edit_utime(mr.VolRetention, ed1));
716 free_pool_memory(query);
719 case 2: /* Use Duration */
720 bsendmsg(ua, _("Current use duration is: %s\n"),
721 edit_utime(mr.VolUseDuration, ed1));
722 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
725 if (!duration_to_utime(ua->cmd, &mr.VolUseDuration)) {
726 bsendmsg(ua, _("Invalid use duration specified.\n"));
729 query = get_pool_memory(PM_MESSAGE);
730 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
731 edit_uint64(mr.VolUseDuration, ed1), mr.MediaId);
732 if (!db_sql_query(ua->db, query, NULL, NULL)) {
733 bsendmsg(ua, "%s", db_strerror(ua->db));
735 bsendmsg(ua, _("New use duration is: %s\n"),
736 edit_utime(mr.VolUseDuration, ed1));
738 free_pool_memory(query);
741 case 3: /* Max Jobs */
743 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
744 if (!get_cmd(ua, _("Enter new Maximum Jobs: "))) {
747 maxjobs = atoi(ua->cmd);
749 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
752 query = get_pool_memory(PM_MESSAGE);
753 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%u WHERE MediaId=%u",
754 maxjobs, mr.MediaId);
755 if (!db_sql_query(ua->db, query, NULL, NULL)) {
756 bsendmsg(ua, "%s", db_strerror(ua->db));
758 bsendmsg(ua, _("New max jobs is: %u\n"), maxjobs);
760 free_pool_memory(query);
763 case 4: /* Max Files */
765 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
766 if (!get_cmd(ua, _("Enter new Maximum Files: "))) {
769 maxfiles = atoi(ua->cmd);
771 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
774 query = get_pool_memory(PM_MESSAGE);
775 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%u WHERE MediaId=%u",
776 maxfiles, mr.MediaId);
777 if (!db_sql_query(ua->db, query, NULL, NULL)) {
778 bsendmsg(ua, "%s", db_strerror(ua->db));
780 bsendmsg(ua, _("New max files is: %u\n"), maxfiles);
782 free_pool_memory(query);
785 case 5: /* Max Bytes */
787 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
788 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
791 if (!size_to_uint64(ua->cmd, strlen(ua->cmd), &maxbytes)) {
792 bsendmsg(ua, _("Invalid byte size specification.\n"));
795 query = get_pool_memory(PM_MESSAGE);
796 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
797 edit_uint64(maxbytes, ed1), mr.MediaId);
798 if (!db_sql_query(ua->db, query, NULL, NULL)) {
799 bsendmsg(ua, "%s", db_strerror(ua->db));
801 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
803 free_pool_memory(query);
807 case 6: /* Recycle */
809 bsendmsg(ua, _("Current recycle flag is: %s\n"),
810 mr.Recycle==1?_("yes"):_("no"));
811 if (!get_cmd(ua, _("Enter new Recycle status: "))) {
814 if (strcasecmp(ua->cmd, _("yes")) == 0) {
816 } else if (strcasecmp(ua->cmd, _("no")) == 0) {
819 bsendmsg(ua, _("Invalid recycle status specified.\n"));
822 query = get_pool_memory(PM_MESSAGE);
823 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
824 recycle, mr.MediaId);
825 if (!db_sql_query(ua->db, query, NULL, NULL)) {
826 bsendmsg(ua, "%s", db_strerror(ua->db));
828 bsendmsg(ua, _("New recycle flag is: %s\n"),
829 mr.Recycle==1?_("yes"):_("no"));
831 free_pool_memory(query);
836 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
837 if (!get_cmd(ua, _("Enter new Slot: "))) {
840 slot = atoi(ua->cmd);
842 bsendmsg(ua, _("Invalid slot, it must be 0 or greater\n"));
844 } else if (pr.MaxVols > 0 && slot >(int)pr.MaxVols) {
845 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
849 query = get_pool_memory(PM_MESSAGE);
850 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
852 if (!db_sql_query(ua->db, query, NULL, NULL)) {
853 bsendmsg(ua, "%s", db_strerror(ua->db));
855 bsendmsg(ua, "New Slot is: %d\n", slot);
857 free_pool_memory(query);
860 case 8: /* Volume Files */
862 bsendmsg(ua, _("Warning changing Volume Files can result\n"
863 "in loss of data on your Volume\n\n"));
864 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
865 if (!get_cmd(ua, _("Enter new number of Files for Volume: "))) {
868 VolFiles = atoi(ua->cmd);
870 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
873 if (VolFiles != (int)(mr.VolFiles + 1)) {
874 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
875 if (!get_cmd(ua, _("Continue? (yes/no): ")) ||
876 strcasecmp(ua->cmd, "yes") != 0) {
880 query = get_pool_memory(PM_MESSAGE);
881 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
882 VolFiles, mr.MediaId);
883 if (!db_sql_query(ua->db, query, NULL, NULL)) {
884 bsendmsg(ua, "%s", db_strerror(ua->db));
886 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
888 free_pool_memory(query);
891 default: /* Done or error */
892 bsendmsg(ua, "Selection done.\n");
900 * Update pool record -- pull info from current POOL resource
902 static int update_pool(UAContext *ua)
910 pool = get_pool_resource(ua);
915 memset(&pr, 0, sizeof(pr));
916 strcpy(pr.Name, pool->hdr.name);
917 if (!get_pool_dbr(ua, &pr)) {
921 set_pooldbr_from_poolres(&pr, pool, 0); /* update */
923 id = db_update_pool_record(ua->jcr, ua->db, &pr);
925 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
926 id, db_strerror(ua->db));
928 query = get_pool_memory(PM_MESSAGE);
929 Mmsg(&query, list_pool, pr.PoolId);
930 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, 0);
931 free_pool_memory(query);
932 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
937 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
941 ua->jcr->store = store;
942 /* Try connecting for up to 15 seconds */
943 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
944 store->hdr.name, store->address, store->SDport);
945 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
946 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
949 Dmsg0(120, _("Connected to storage daemon\n"));
950 sd = ua->jcr->store_bsock;
951 bnet_fsend(sd, "setdebug=%d\n", level);
952 if (bnet_recv(sd) >= 0) {
953 bsendmsg(ua, "%s", sd->msg);
955 bnet_sig(sd, BNET_TERMINATE);
957 ua->jcr->store_bsock = NULL;
961 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
965 /* Connect to File daemon */
967 ua->jcr->client = client;
968 /* Try to connect for 15 seconds */
969 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
970 client->hdr.name, client->address, client->FDport);
971 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
972 bsendmsg(ua, _("Failed to connect to Client.\n"));
975 Dmsg0(120, "Connected to file daemon\n");
976 fd = ua->jcr->file_bsock;
977 bnet_fsend(fd, "setdebug=%d\n", level);
978 if (bnet_recv(fd) >= 0) {
979 bsendmsg(ua, "%s", fd->msg);
981 bnet_sig(fd, BNET_TERMINATE);
983 ua->jcr->file_bsock = NULL;
989 static void do_all_setdebug(UAContext *ua, int level)
991 STORE *store, **unique_store;
992 CLIENT *client, **unique_client;
998 /* Count Storage items */
1001 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1003 unique_store = (STORE **) malloc(i * sizeof(STORE));
1004 /* Find Unique Storage address/port */
1005 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1007 unique_store[i++] = store;
1008 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1010 for (j=0; j<i; j++) {
1011 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1012 unique_store[j]->SDport == store->SDport) {
1018 unique_store[i++] = store;
1019 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1024 /* Call each unique Storage daemon */
1025 for (j=0; j<i; j++) {
1026 do_storage_setdebug(ua, unique_store[j], level);
1030 /* Count Client items */
1033 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1035 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1036 /* Find Unique Client address/port */
1037 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1039 unique_client[i++] = client;
1040 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1042 for (j=0; j<i; j++) {
1043 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1044 unique_client[j]->FDport == client->FDport) {
1050 unique_client[i++] = client;
1051 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1056 /* Call each unique File daemon */
1057 for (j=0; j<i; j++) {
1058 do_client_setdebug(ua, unique_client[j], level);
1060 free(unique_client);
1064 * setdebug level=nn all
1066 static int setdebugcmd(UAContext *ua, char *cmd)
1076 Dmsg1(120, "setdebug:%s:\n", cmd);
1079 for (i=1; i<ua->argc; i++) {
1080 if (strcasecmp(ua->argk[i], _("level")) == 0 && ua->argv[i]) {
1081 level = atoi(ua->argv[i]);
1086 if (!get_cmd(ua, _("Enter new debug level: "))) {
1089 level = atoi(ua->cmd);
1092 bsendmsg(ua, _("level cannot be negative.\n"));
1096 /* General debug? */
1097 for (i=1; i<ua->argc; i++) {
1098 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1099 do_all_setdebug(ua, level);
1102 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1103 strcasecmp(ua->argk[i], _("director")) == 0) {
1104 debug_level = level;
1107 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1110 client = (CLIENT *) GetResWithName(R_CLIENT, ua->argv[i]);
1112 do_client_setdebug(ua, client, level);
1116 client = select_client_resource(ua);
1118 do_client_setdebug(ua, client, level);
1122 store = get_storage_resource(ua, cmd);
1124 do_storage_setdebug(ua, store, level);
1130 * We didn't find an appropriate keyword above, so
1133 start_prompt(ua, _("Available daemons are: \n"));
1134 add_prompt(ua, _("Director"));
1135 add_prompt(ua, _("Storage"));
1136 add_prompt(ua, _("Client"));
1137 add_prompt(ua, _("All"));
1138 switch(do_prompt(ua, _("Select daemon type to set debug level"), NULL, 0)) {
1139 case 0: /* Director */
1140 debug_level = level;
1143 store = get_storage_resource(ua, cmd);
1145 do_storage_setdebug(ua, store, level);
1149 client = select_client_resource(ua);
1151 do_client_setdebug(ua, client, level);
1155 do_all_setdebug(ua, level);
1166 static int timecmd(UAContext *ua, char *cmd)
1169 time_t ttime = time(NULL);
1171 localtime_r(&ttime, &tm);
1172 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1173 bsendmsg(ua, "%s\n", sdt);
1180 * Delete Pool records (should purge Media with it).
1182 * delete pool=<pool-name>
1183 * delete media pool=<pool-name> volume=<name>
1185 static int deletecmd(UAContext *ua, char *cmd)
1187 static char *keywords[] = {
1197 "In general it is not a good idea to delete either a\n"
1198 "Pool or a Volume since they may contain data.\n\n"));
1200 switch (find_arg_keyword(ua, keywords)) {
1210 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1218 bsendmsg(ua, _("Nothing done.\n"));
1225 * Delete media records from database -- dangerous
1227 static int delete_volume(UAContext *ua)
1232 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
1235 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1236 "and all Jobs saved on that volume from the Catalog\n"),
1239 if (!get_cmd(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1242 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1243 db_delete_media_record(ua->jcr, ua->db, &mr);
1249 * Delete a pool record from the database -- dangerous
1251 static int delete_pool(UAContext *ua)
1255 memset(&pr, 0, sizeof(pr));
1257 if (!get_pool_dbr(ua, &pr)) {
1260 if (!get_cmd(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1263 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1264 db_delete_pool_record(ua->jcr, ua->db, &pr);
1273 * label storage=xxx volume=vvv
1275 static int labelcmd(UAContext *ua, char *cmd)
1279 char dev_name[MAX_NAME_LENGTH];
1283 int mounted = FALSE;
1286 static char *keyword[] = {
1293 store = get_storage_resource(ua, cmd);
1298 i = find_arg_keyword(ua, keyword);
1299 if (i >=0 && ua->argv[i]) {
1300 strcpy(ua->cmd, ua->argv[i]);
1305 if (!get_cmd(ua, _("Enter new Volume name: "))) {
1309 if (strchr(ua->cmd, '|')) {
1310 bsendmsg(ua, _("Illegal character | in a volume name.\n"));
1313 if (strlen(ua->cmd) >= MAX_NAME_LENGTH) {
1314 bsendmsg(ua, _("Volume name too long.\n"));
1317 if (strlen(ua->cmd) == 0) {
1318 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
1323 memset(&mr, 0, sizeof(mr));
1324 strcpy(mr.VolumeName, ua->cmd);
1325 if (db_get_media_record(ua->jcr, ua->db, &mr)) {
1326 bsendmsg(ua, _("Media record for Volume %s already exists.\n"),
1331 /* Do some more checking on slot ****FIXME**** */
1332 if (store->autochanger) {
1333 if (!get_cmd(ua, _("Enter slot (0 for none): "))) {
1336 slot = atoi(ua->cmd);
1338 strcpy(mr.MediaType, store->media_type);
1341 memset(&pr, 0, sizeof(pr));
1342 if (!select_pool_dbr(ua, &pr)) {
1346 ua->jcr->store = store;
1347 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
1348 store->hdr.name, store->address, store->SDport);
1349 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1350 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1353 sd = ua->jcr->store_bsock;
1354 strcpy(dev_name, store->dev_name);
1355 bash_spaces(dev_name);
1356 bash_spaces(mr.VolumeName);
1357 bash_spaces(mr.MediaType);
1358 bash_spaces(pr.Name);
1359 bnet_fsend(sd, _("label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d"),
1360 dev_name, mr.VolumeName, pr.Name, mr.MediaType, mr.Slot);
1361 bsendmsg(ua, _("Sending label command ...\n"));
1362 while (bget_msg(sd, 0) >= 0) {
1363 bsendmsg(ua, "%s", sd->msg);
1364 if (strncmp(sd->msg, "3000 OK label.", 14) == 0) {
1367 bsendmsg(ua, _("Label command failed.\n"));
1370 ua->jcr->store_bsock = NULL;
1371 unbash_spaces(dev_name);
1372 unbash_spaces(mr.VolumeName);
1373 unbash_spaces(mr.MediaType);
1374 unbash_spaces(pr.Name);
1375 mr.LabelDate = time(NULL);
1377 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
1378 if (db_create_media_record(ua->jcr, ua->db, &mr)) {
1379 bsendmsg(ua, _("Media record for Volume=%s successfully created.\n"),
1381 if (ua->automount) {
1382 bsendmsg(ua, _("Requesting mount %s ...\n"), dev_name);
1383 bash_spaces(dev_name);
1384 bnet_fsend(sd, "mount %s", dev_name);
1385 unbash_spaces(dev_name);
1386 while (bnet_recv(sd) >= 0) {
1387 bsendmsg(ua, "%s", sd->msg);
1389 * 3001 OK mount. Device=xxx or
1390 * 3001 Mounted Volume vvvv
1392 if (strncmp(sd->msg, "3001 ", 5) == 0) {
1394 /***** ****FIXME***** find job waiting for
1395 ***** mount, and change to waiting for SD
1401 bsendmsg(ua, "%s", db_strerror(ua->db));
1405 bsendmsg(ua, _("Do not forget to mount the drive!!!\n"));
1407 bnet_sig(sd, BNET_TERMINATE);
1412 static void do_mount_cmd(int mount, UAContext *ua, char *cmd)
1416 char dev_name[MAX_NAME_LENGTH];
1422 Dmsg1(120, "mount: %s\n", ua->UA_sock->msg);
1424 store = get_storage_resource(ua, cmd);
1429 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1430 store->media_type, store->dev_name);
1432 ua->jcr->store = store;
1433 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1434 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1437 sd = ua->jcr->store_bsock;
1438 strcpy(dev_name, store->dev_name);
1439 bash_spaces(dev_name);
1441 bnet_fsend(sd, "mount %s", dev_name);
1443 bnet_fsend(sd, "unmount %s", dev_name);
1445 while (bnet_recv(sd) >= 0) {
1446 bsendmsg(ua, "%s", sd->msg);
1447 if (strncmp(sd->msg, "3001 OK mount.", 14) == 0) {
1448 /***** ****FIXME**** fix JobStatus */
1451 bnet_sig(sd, BNET_TERMINATE);
1453 ua->jcr->store_bsock = NULL;
1457 * mount [storage | device] <name>
1459 static int mountcmd(UAContext *ua, char *cmd)
1461 do_mount_cmd(1, ua, cmd); /* mount */
1467 * unmount [storage | device] <name>
1469 static int unmountcmd(UAContext *ua, char *cmd)
1471 do_mount_cmd(0, ua, cmd); /* unmount */
1478 * use catalog=<name>
1480 static int usecmd(UAContext *ua, char *cmd)
1482 CAT *oldcatalog, *catalog;
1485 close_db(ua); /* close any previously open db */
1486 oldcatalog = ua->catalog;
1488 if (!(catalog = get_catalog_resource(ua))) {
1489 ua->catalog = oldcatalog;
1491 ua->catalog = catalog;
1494 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1495 ua->catalog->hdr.name, ua->catalog->db_name);
1500 int quitcmd(UAContext *ua, char *cmd)
1506 static int helpcmd(UAContext *ua, char *cmd)
1511 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1512 for (i=0; i<comsize; i++) {
1513 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1519 static int versioncmd(UAContext *ua, char *cmd)
1521 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1526 /* A bit brain damaged in that if the user has not done
1527 * a "use catalog xxx" command, we simply find the first
1528 * catalog resource and open it.
1530 int open_db(UAContext *ua)
1537 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1540 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1543 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1544 ua->catalog->hdr.name, ua->catalog->db_name);
1548 Dmsg0(150, "Open database\n");
1549 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1550 ua->catalog->db_password, ua->catalog->db_address,
1551 ua->catalog->db_port, ua->catalog->db_socket);
1552 if (!db_open_database(ua->jcr, ua->db)) {
1553 bsendmsg(ua, _("Could not open DB %s: ERR=%s"),
1554 ua->catalog->db_name, db_strerror(ua->db));
1558 ua->jcr->db = ua->db;
1559 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1563 void close_db(UAContext *ua)
1566 db_close_database(ua->jcr, ua->db);