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);
79 static int waitcmd(UAContext *ua, char *cmd);
81 int quitcmd(UAContext *ua, char *cmd);
84 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
85 static struct cmdstruct commands[] = {
86 { N_("add"), addcmd, _("add media to a pool")},
87 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
88 { N_("automount"), automountcmd, _("automount [on/off] -- after label")},
89 { N_("cancel"), cancelcmd, _("cancel job=nnn -- cancel a job")},
90 { N_("create"), createcmd, _("create DB Pool from resource")},
91 { N_("delete"), deletecmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
92 { N_("help"), helpcmd, _("print this command")},
93 { N_("label"), labelcmd, _("label a tape")},
94 { N_("relabel"), relabelcmd, _("relabel a tape")},
95 { N_("list"), listcmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
96 { N_("llist"), llistcmd, _("full or long list like list command")},
97 { N_("messages"), messagescmd, _("messages")},
98 { N_("mount"), mountcmd, _("mount <storage-name>")},
99 { N_("restore"), restorecmd, _("restore files")},
100 { N_("prune"), prunecmd, _("prune expired records from catalog")},
101 { N_("purge"), purgecmd, _("purge records from catalog")},
102 { N_("run"), runcmd, _("run <job-name>")},
103 { N_("setdebug"), setdebugcmd, _("sets debug level")},
104 { N_("show"), showcmd, _("show (resource records) [jobs | pools | ... | all]")},
105 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
106 { N_("status"), statuscmd, _("status [storage | client]=<name>")},
107 { N_("unmount"), unmountcmd, _("unmount <storage-name>")},
108 { N_("update"), updatecmd, _("update Volume or Pool")},
109 { N_("use"), usecmd, _("use catalog xxx")},
110 { N_("version"), versioncmd, _("print Director version")},
111 { N_("quit"), quitcmd, _("quit")},
112 { N_("query"), querycmd, _("query catalog")},
113 { N_("time"), timecmd, _("print current time")},
114 { N_("exit"), quitcmd, _("exit = quit")},
115 { N_("wait"), waitcmd, _("wait until no jobs are running")},
117 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
120 * Execute a command from the UA
122 int do_a_command(UAContext *ua, char *cmd)
131 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
136 len = strlen(ua->argk[0]);
137 for (i=0; i<comsize; i++) { /* search for command */
138 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
139 stat = (*commands[i].func)(ua, cmd); /* go execute command */
145 pm_strcat(&ua->UA_sock->msg, _(": is an illegal command\n"));
146 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
147 bnet_send(ua->UA_sock);
153 * This is a common routine used to stuff the Pool DB record defaults
154 * into the Media DB record just before creating a media (Volume)
157 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
159 mr->PoolId = pr->PoolId;
160 strcpy(mr->VolStatus, "Append");
161 mr->Recycle = pr->Recycle;
162 mr->VolRetention = pr->VolRetention;
163 mr->VolUseDuration = pr->VolUseDuration;
164 mr->MaxVolJobs = pr->MaxVolJobs;
165 mr->MaxVolFiles = pr->MaxVolFiles;
166 mr->MaxVolBytes = pr->MaxVolBytes;
171 * Add Volumes to an existing Pool
173 static int addcmd(UAContext *ua, char *cmd)
177 int num, i, max, startnum;
179 char name[MAX_NAME_LENGTH];
184 "You probably don't want to be using this command since it\n"
185 "creates database records without labeling the Volumes.\n"
186 "You probably want to use the \"label\" command.\n\n"));
192 memset(&pr, 0, sizeof(pr));
193 memset(&mr, 0, sizeof(mr));
195 if (!get_pool_dbr(ua, &pr)) {
199 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
200 pr.MaxVols, pr.PoolType);
202 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
203 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
205 if (!get_cmd(ua, _("Enter new maximum (zero for unlimited): "))) {
208 pr.MaxVols = atoi(ua->cmd);
209 if (pr.MaxVols < 0) {
210 bsendmsg(ua, _("Max vols must be zero or greater.\n"));
218 if ((store = get_storage_resource(ua, 0)) != NULL) {
219 strcpy(mr.MediaType, store->media_type);
220 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
224 if (pr.MaxVols == 0) {
227 max = pr.MaxVols - pr.NumVols;
231 sprintf(buf, _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
232 if (!get_cmd(ua, buf)) {
236 if (num < 0 || num > max) {
237 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
244 if (!get_cmd(ua, _("Enter Volume name: "))) {
248 if (!get_cmd(ua, _("Enter base volume name: "))) {
252 /* Don't allow | in Volume name because it is the volume separator character */
253 if (strchr(ua->cmd, '|')) {
254 bsendmsg(ua, _("Illegal character | in a volume name.\n"));
257 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
258 bsendmsg(ua, _("Volume name too long.\n"));
261 if (strlen(ua->cmd) == 0) {
262 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
266 strcpy(name, ua->cmd);
268 strcat(name, "%04d");
271 if (!get_cmd(ua, _("Enter the starting number: "))) {
274 startnum = atoi(ua->cmd);
276 bsendmsg(ua, _("Start number must be greater than zero.\n"));
286 if (store && store->autochanger) {
287 if (!get_cmd(ua, _("Enter slot (0 for none): "))) {
290 slot = atoi(ua->cmd);
293 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
294 for (i=startnum; i < num+startnum; i++) {
295 sprintf(mr.VolumeName, name, i);
297 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
298 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
299 bsendmsg(ua, db_strerror(ua->db));
303 first_id = mr.PoolId;
307 Dmsg0(200, "Update pool record.\n");
308 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
309 bsendmsg(ua, db_strerror(ua->db));
312 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
318 * Turn auto mount on/off
323 int automountcmd(UAContext *ua, char *cmd)
328 if (!get_cmd(ua, _("Turn on or off? "))) {
336 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
344 static int cancelcmd(UAContext *ua, char *cmd)
350 char JobName[MAX_NAME_LENGTH];
356 for (i=1; i<ua->argc; i++) {
357 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
361 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
362 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
366 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
370 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
371 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
377 /* If we still do not have a jcr,
378 * throw up a list and ask the user to select one.
381 /* Count Jobs running */
383 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
384 if (jcr->JobId == 0) { /* this is us */
385 free_locked_jcr(jcr);
389 free_locked_jcr(jcr);
394 bsendmsg(ua, _("No Jobs running.\n"));
397 start_prompt(ua, _("Select Job:\n"));
399 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
400 if (jcr->JobId == 0) { /* this is us */
401 free_locked_jcr(jcr);
404 add_prompt(ua, jcr->Job);
405 free_locked_jcr(jcr);
409 if (do_prompt(ua, _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
413 if (!get_cmd(ua, _("Confirm cancel (yes/no): "))) {
416 if (strcasecmp(ua->cmd, _("yes")) != 0) {
420 jcr = get_jcr_by_full_name(JobName);
422 bsendmsg(ua, _("Job %s not found.\n"), JobName);
427 switch (jcr->JobStatus) {
429 set_jcr_job_status(jcr, JS_Canceled);
430 bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
431 jcr->JobId, jcr->Job);
432 #ifndef USE_SEMAPHORE
433 workq_remove(&job_wq, jcr->work_item); /* attempt to remove it from queue */
439 set_jcr_job_status(jcr, JS_Canceled);
440 /* Cancel File daemon */
441 ua->jcr->client = jcr->client;
442 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
443 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
447 Dmsg0(200, "Connected to file daemon\n");
448 fd = ua->jcr->file_bsock;
449 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
450 while (bnet_recv(fd) >= 0) {
451 bsendmsg(ua, "%s", fd->msg);
453 bnet_sig(fd, BNET_TERMINATE);
455 ua->jcr->file_bsock = NULL;
457 /* Cancel Storage daemon */
458 ua->jcr->store = jcr->store;
459 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
460 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
464 Dmsg0(200, "Connected to storage daemon\n");
465 sd = ua->jcr->store_bsock;
466 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
467 while (bnet_recv(sd) >= 0) {
468 bsendmsg(ua, "%s", sd->msg);
470 bnet_sig(sd, BNET_TERMINATE);
472 ua->jcr->store_bsock = NULL;
481 * This is a common routine to create or update a
482 * Pool DB base record from a Pool Resource. We handle
483 * the setting of MaxVols and NumVols slightly differently
484 * depending on if we are creating the Pool or we are
485 * simply bringing it into agreement with the resource (updage).
487 void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, int create)
489 strcpy(pr->PoolType, pool->pool_type);
491 pr->MaxVols = pool->max_volumes;
493 } else { /* update pool */
494 if (pr->MaxVols != pool->max_volumes) {
495 pr->MaxVols = pool->max_volumes;
497 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
498 pr->MaxVols = pr->NumVols;
501 pr->UseOnce = pool->use_volume_once;
502 pr->UseCatalog = pool->use_catalog;
503 pr->AcceptAnyVolume = pool->accept_any_volume;
504 pr->Recycle = pool->Recycle;
505 pr->VolRetention = pool->VolRetention;
506 pr->VolUseDuration = pool->VolUseDuration;
507 pr->MaxVolJobs = pool->MaxVolJobs;
508 pr->MaxVolFiles = pool->MaxVolFiles;
509 pr->MaxVolBytes = pool->MaxVolBytes;
510 if (pool->label_format) {
511 strcpy(pr->LabelFormat, pool->label_format);
513 strcpy(pr->LabelFormat, "*"); /* none */
519 * Create a pool record from a given Pool resource
520 * Also called from backup.c
521 * Returns: -1 on error
522 * 0 record already exists
526 int create_pool(JCR *jcr, B_DB *db, POOL *pool, int create)
530 memset(&pr, 0, sizeof(POOL_DBR));
532 strcpy(pr.Name, pool->hdr.name);
534 if (db_get_pool_record(jcr, db, &pr)) {
536 if (!create) { /* update request */
537 set_pooldbr_from_poolres(&pr, pool, 0);
538 db_update_pool_record(jcr, db, &pr);
540 return 0; /* exists */
543 set_pooldbr_from_poolres(&pr, pool, 1);
545 if (!db_create_pool_record(jcr, db, &pr)) {
546 return -1; /* error */
554 * Create a Pool Record in the database.
555 * It is always created from the Resource record.
557 static int createcmd(UAContext *ua, char *cmd)
565 pool = get_pool_resource(ua);
570 switch (create_pool(ua->jcr, ua->db, pool, 1)) {
572 bsendmsg(ua, _("Error: Pool %s already exists.\n\
573 Use update to change it.\n"), pool->hdr.name);
577 bsendmsg(ua, db_strerror(ua->db));
583 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
591 * Update a Pool Record in the database.
592 * It is always updated from the Resource record.
594 * update pool=<pool-name>
595 * updates pool from Pool resource
596 * update media pool=<pool-name> volume=<volume-name>
597 * changes pool info for volume
599 static int updatecmd(UAContext *ua, char *cmd)
601 static char *kw[] = {
603 N_("volume"), /* 1 */
612 switch (find_arg_keyword(ua, kw)) {
627 start_prompt(ua, _("Update choice:\n"));
628 add_prompt(ua, _("Volume parameters"));
629 add_prompt(ua, _("Pool from resource"));
630 add_prompt(ua, _("Slots from autochanger"));
631 switch (do_prompt(ua, _("Choose catalog item to update"), NULL, 0)) {
648 * Update a media record -- allows you to change the
649 * Volume status. E.g. if you want Bacula to stop
650 * writing on the volume, set it to anything other
653 static int update_volume(UAContext *ua)
660 for (int done=0; !done; ) {
661 if (!select_media_dbr(ua, &mr)) {
664 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
665 start_prompt(ua, _("Parameters to modify:\n"));
666 add_prompt(ua, _("Volume Status"));
667 add_prompt(ua, _("Volume Retention Period"));
668 add_prompt(ua, _("Volume Use Duration"));
669 add_prompt(ua, _("Maximum Volume Jobs"));
670 add_prompt(ua, _("Maximum Volume Files"));
671 add_prompt(ua, _("Maximum Volume Bytes"));
672 add_prompt(ua, _("Recycle Flag"));
673 add_prompt(ua, _("Slot"));
674 add_prompt(ua, _("Volume Files"));
675 add_prompt(ua, _("Done"));
676 switch (do_prompt(ua, _("Select parameter to modify"), NULL, 0)) {
677 case 0: /* Volume Status */
678 /* Modify Volume Status */
679 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
680 start_prompt(ua, _("Possible Values are:\n"));
681 add_prompt(ua, "Append"); /* Better not translate these as */
682 add_prompt(ua, "Archive"); /* They are known in the database code */
683 add_prompt(ua, "Disabled");
684 add_prompt(ua, "Full");
685 add_prompt(ua, "Used");
686 if (strcmp(mr.VolStatus, "Purged") == 0) {
687 add_prompt(ua, "Recycle");
689 add_prompt(ua, "Read-Only");
690 if (do_prompt(ua, _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
693 bstrncpy(mr.VolStatus, ua->cmd, sizeof(mr.VolStatus));
694 query = get_pool_memory(PM_MESSAGE);
695 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
696 mr.VolStatus, mr.MediaId);
697 if (!db_sql_query(ua->db, query, NULL, NULL)) {
698 bsendmsg(ua, "%s", db_strerror(ua->db));
700 bsendmsg(ua, _("New Volume status is: %s\n"), mr.VolStatus);
702 free_pool_memory(query);
704 case 1: /* Retention */
705 bsendmsg(ua, _("Current retention seconds is: %s\n"),
706 edit_utime(mr.VolRetention, ed1));
707 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
710 if (!duration_to_utime(ua->cmd, &mr.VolRetention)) {
711 bsendmsg(ua, _("Invalid retention period specified.\n"));
714 query = get_pool_memory(PM_MESSAGE);
715 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
716 edit_uint64(mr.VolRetention, ed1), mr.MediaId);
717 if (!db_sql_query(ua->db, query, NULL, NULL)) {
718 bsendmsg(ua, "%s", db_strerror(ua->db));
720 bsendmsg(ua, _("New retention seconds is: %s\n"),
721 edit_utime(mr.VolRetention, ed1));
723 free_pool_memory(query);
726 case 2: /* Use Duration */
727 bsendmsg(ua, _("Current use duration is: %s\n"),
728 edit_utime(mr.VolUseDuration, ed1));
729 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
732 if (!duration_to_utime(ua->cmd, &mr.VolUseDuration)) {
733 bsendmsg(ua, _("Invalid use duration specified.\n"));
736 query = get_pool_memory(PM_MESSAGE);
737 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
738 edit_uint64(mr.VolUseDuration, ed1), mr.MediaId);
739 if (!db_sql_query(ua->db, query, NULL, NULL)) {
740 bsendmsg(ua, "%s", db_strerror(ua->db));
742 bsendmsg(ua, _("New use duration is: %s\n"),
743 edit_utime(mr.VolUseDuration, ed1));
745 free_pool_memory(query);
748 case 3: /* Max Jobs */
750 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
751 if (!get_cmd(ua, _("Enter new Maximum Jobs: "))) {
754 maxjobs = atoi(ua->cmd);
756 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
759 query = get_pool_memory(PM_MESSAGE);
760 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%u WHERE MediaId=%u",
761 maxjobs, mr.MediaId);
762 if (!db_sql_query(ua->db, query, NULL, NULL)) {
763 bsendmsg(ua, "%s", db_strerror(ua->db));
765 bsendmsg(ua, _("New max jobs is: %u\n"), maxjobs);
767 free_pool_memory(query);
770 case 4: /* Max Files */
772 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
773 if (!get_cmd(ua, _("Enter new Maximum Files: "))) {
776 maxfiles = atoi(ua->cmd);
778 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
781 query = get_pool_memory(PM_MESSAGE);
782 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%u WHERE MediaId=%u",
783 maxfiles, mr.MediaId);
784 if (!db_sql_query(ua->db, query, NULL, NULL)) {
785 bsendmsg(ua, "%s", db_strerror(ua->db));
787 bsendmsg(ua, _("New max files is: %u\n"), maxfiles);
789 free_pool_memory(query);
792 case 5: /* Max Bytes */
794 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
795 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
798 if (!size_to_uint64(ua->cmd, strlen(ua->cmd), &maxbytes)) {
799 bsendmsg(ua, _("Invalid byte size specification.\n"));
802 query = get_pool_memory(PM_MESSAGE);
803 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
804 edit_uint64(maxbytes, ed1), mr.MediaId);
805 if (!db_sql_query(ua->db, query, NULL, NULL)) {
806 bsendmsg(ua, "%s", db_strerror(ua->db));
808 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
810 free_pool_memory(query);
814 case 6: /* Recycle */
816 bsendmsg(ua, _("Current recycle flag is: %s\n"),
817 mr.Recycle==1?_("yes"):_("no"));
818 if (!get_cmd(ua, _("Enter new Recycle status: "))) {
821 if (strcasecmp(ua->cmd, _("yes")) == 0) {
823 } else if (strcasecmp(ua->cmd, _("no")) == 0) {
826 bsendmsg(ua, _("Invalid recycle status specified.\n"));
829 query = get_pool_memory(PM_MESSAGE);
830 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
831 recycle, mr.MediaId);
832 if (!db_sql_query(ua->db, query, NULL, NULL)) {
833 bsendmsg(ua, "%s", db_strerror(ua->db));
835 bsendmsg(ua, _("New recycle flag is: %s\n"),
836 mr.Recycle==1?_("yes"):_("no"));
838 free_pool_memory(query);
845 memset(&pr, 0, sizeof(POOL_DBR));
846 pr.PoolId = mr.PoolId;
847 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
848 bsendmsg(ua, "%s", db_strerror(ua->db));
851 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
852 if (!get_cmd(ua, _("Enter new Slot: "))) {
855 slot = atoi(ua->cmd);
857 bsendmsg(ua, _("Invalid slot, it must be 0 or greater\n"));
859 } else if (pr.MaxVols > 0 && slot >(int)pr.MaxVols) {
860 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
864 query = get_pool_memory(PM_MESSAGE);
865 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
867 if (!db_sql_query(ua->db, query, NULL, NULL)) {
868 bsendmsg(ua, "%s", db_strerror(ua->db));
870 bsendmsg(ua, "New Slot is: %d\n", slot);
872 free_pool_memory(query);
875 case 8: /* Volume Files */
877 bsendmsg(ua, _("Warning changing Volume Files can result\n"
878 "in loss of data on your Volume\n\n"));
879 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
880 if (!get_cmd(ua, _("Enter new number of Files for Volume: "))) {
883 VolFiles = atoi(ua->cmd);
885 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
888 if (VolFiles != (int)(mr.VolFiles + 1)) {
889 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
890 if (!get_cmd(ua, _("Continue? (yes/no): ")) ||
891 strcasecmp(ua->cmd, "yes") != 0) {
895 query = get_pool_memory(PM_MESSAGE);
896 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
897 VolFiles, mr.MediaId);
898 if (!db_sql_query(ua->db, query, NULL, NULL)) {
899 bsendmsg(ua, "%s", db_strerror(ua->db));
901 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
903 free_pool_memory(query);
906 default: /* Done or error */
907 bsendmsg(ua, "Selection done.\n");
915 * Update pool record -- pull info from current POOL resource
917 static int update_pool(UAContext *ua)
925 pool = get_pool_resource(ua);
930 memset(&pr, 0, sizeof(pr));
931 strcpy(pr.Name, pool->hdr.name);
932 if (!get_pool_dbr(ua, &pr)) {
936 set_pooldbr_from_poolres(&pr, pool, 0); /* update */
938 id = db_update_pool_record(ua->jcr, ua->db, &pr);
940 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
941 id, db_strerror(ua->db));
943 query = get_pool_memory(PM_MESSAGE);
944 Mmsg(&query, list_pool, pr.PoolId);
945 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, 0);
946 free_pool_memory(query);
947 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
952 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
956 ua->jcr->store = store;
957 /* Try connecting for up to 15 seconds */
958 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
959 store->hdr.name, store->address, store->SDport);
960 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
961 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
964 Dmsg0(120, _("Connected to storage daemon\n"));
965 sd = ua->jcr->store_bsock;
966 bnet_fsend(sd, "setdebug=%d\n", level);
967 if (bnet_recv(sd) >= 0) {
968 bsendmsg(ua, "%s", sd->msg);
970 bnet_sig(sd, BNET_TERMINATE);
972 ua->jcr->store_bsock = NULL;
976 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
980 /* Connect to File daemon */
982 ua->jcr->client = client;
983 /* Try to connect for 15 seconds */
984 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
985 client->hdr.name, client->address, client->FDport);
986 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
987 bsendmsg(ua, _("Failed to connect to Client.\n"));
990 Dmsg0(120, "Connected to file daemon\n");
991 fd = ua->jcr->file_bsock;
992 bnet_fsend(fd, "setdebug=%d\n", level);
993 if (bnet_recv(fd) >= 0) {
994 bsendmsg(ua, "%s", fd->msg);
996 bnet_sig(fd, BNET_TERMINATE);
998 ua->jcr->file_bsock = NULL;
1004 static void do_all_setdebug(UAContext *ua, int level)
1006 STORE *store, **unique_store;
1007 CLIENT *client, **unique_client;
1011 debug_level = level;
1013 /* Count Storage items */
1016 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1018 unique_store = (STORE **) malloc(i * sizeof(STORE));
1019 /* Find Unique Storage address/port */
1020 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1022 unique_store[i++] = store;
1023 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1025 for (j=0; j<i; j++) {
1026 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1027 unique_store[j]->SDport == store->SDport) {
1033 unique_store[i++] = store;
1034 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1039 /* Call each unique Storage daemon */
1040 for (j=0; j<i; j++) {
1041 do_storage_setdebug(ua, unique_store[j], level);
1045 /* Count Client items */
1048 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1050 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1051 /* Find Unique Client address/port */
1052 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1054 unique_client[i++] = client;
1055 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1057 for (j=0; j<i; j++) {
1058 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1059 unique_client[j]->FDport == client->FDport) {
1065 unique_client[i++] = client;
1066 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1071 /* Call each unique File daemon */
1072 for (j=0; j<i; j++) {
1073 do_client_setdebug(ua, unique_client[j], level);
1075 free(unique_client);
1079 * setdebug level=nn all
1081 static int setdebugcmd(UAContext *ua, char *cmd)
1091 Dmsg1(120, "setdebug:%s:\n", cmd);
1094 for (i=1; i<ua->argc; i++) {
1095 if (strcasecmp(ua->argk[i], _("level")) == 0 && ua->argv[i]) {
1096 level = atoi(ua->argv[i]);
1101 if (!get_cmd(ua, _("Enter new debug level: "))) {
1104 level = atoi(ua->cmd);
1107 bsendmsg(ua, _("level cannot be negative.\n"));
1111 /* General debug? */
1112 for (i=1; i<ua->argc; i++) {
1113 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1114 do_all_setdebug(ua, level);
1117 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1118 strcasecmp(ua->argk[i], _("director")) == 0) {
1119 debug_level = level;
1122 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1125 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1127 do_client_setdebug(ua, client, level);
1131 client = select_client_resource(ua);
1133 do_client_setdebug(ua, client, level);
1138 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1139 strcasecmp(ua->argk[i], _("storage")) == 0) {
1142 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1144 do_storage_setdebug(ua, store, level);
1148 store = get_storage_resource(ua, 0);
1150 do_storage_setdebug(ua, store, level);
1156 * We didn't find an appropriate keyword above, so
1159 start_prompt(ua, _("Available daemons are: \n"));
1160 add_prompt(ua, _("Director"));
1161 add_prompt(ua, _("Storage"));
1162 add_prompt(ua, _("Client"));
1163 add_prompt(ua, _("All"));
1164 switch(do_prompt(ua, _("Select daemon type to set debug level"), NULL, 0)) {
1165 case 0: /* Director */
1166 debug_level = level;
1169 store = get_storage_resource(ua, 0);
1171 do_storage_setdebug(ua, store, level);
1175 client = select_client_resource(ua);
1177 do_client_setdebug(ua, client, level);
1181 do_all_setdebug(ua, level);
1192 static int timecmd(UAContext *ua, char *cmd)
1195 time_t ttime = time(NULL);
1197 localtime_r(&ttime, &tm);
1198 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1199 bsendmsg(ua, "%s\n", sdt);
1206 * Delete Pool records (should purge Media with it).
1208 * delete pool=<pool-name>
1209 * delete media pool=<pool-name> volume=<name>
1211 static int deletecmd(UAContext *ua, char *cmd)
1213 static char *keywords[] = {
1223 "In general it is not a good idea to delete either a\n"
1224 "Pool or a Volume since they may contain data.\n\n"));
1226 switch (find_arg_keyword(ua, keywords)) {
1236 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1244 bsendmsg(ua, _("Nothing done.\n"));
1251 * Delete media records from database -- dangerous
1253 static int delete_volume(UAContext *ua)
1257 if (!select_media_dbr(ua, &mr)) {
1260 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1261 "and all Jobs saved on that volume from the Catalog\n"),
1264 if (!get_cmd(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1267 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1268 db_delete_media_record(ua->jcr, ua->db, &mr);
1274 * Delete a pool record from the database -- dangerous
1276 static int delete_pool(UAContext *ua)
1280 memset(&pr, 0, sizeof(pr));
1282 if (!get_pool_dbr(ua, &pr)) {
1285 if (!get_cmd(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1288 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1289 db_delete_pool_record(ua->jcr, ua->db, &pr);
1295 static void do_mount_cmd(int mount, UAContext *ua, char *cmd)
1299 char dev_name[MAX_NAME_LENGTH];
1305 Dmsg1(120, "mount: %s\n", ua->UA_sock->msg);
1307 store = get_storage_resource(ua, 1);
1312 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1313 store->media_type, store->dev_name);
1315 ua->jcr->store = store;
1316 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1317 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1320 sd = ua->jcr->store_bsock;
1321 strcpy(dev_name, store->dev_name);
1322 bash_spaces(dev_name);
1324 bnet_fsend(sd, "mount %s", dev_name);
1326 bnet_fsend(sd, "unmount %s", dev_name);
1328 while (bnet_recv(sd) >= 0) {
1329 bsendmsg(ua, "%s", sd->msg);
1330 if (strncmp(sd->msg, "3001 OK mount.", 14) == 0) {
1331 /***** ****FIXME**** fix JobStatus */
1334 bnet_sig(sd, BNET_TERMINATE);
1336 ua->jcr->store_bsock = NULL;
1340 * mount [storage | device] <name>
1342 static int mountcmd(UAContext *ua, char *cmd)
1344 do_mount_cmd(1, ua, cmd); /* mount */
1350 * unmount [storage | device] <name>
1352 static int unmountcmd(UAContext *ua, char *cmd)
1354 do_mount_cmd(0, ua, cmd); /* unmount */
1361 * use catalog=<name>
1363 static int usecmd(UAContext *ua, char *cmd)
1365 CAT *oldcatalog, *catalog;
1368 close_db(ua); /* close any previously open db */
1369 oldcatalog = ua->catalog;
1371 if (!(catalog = get_catalog_resource(ua))) {
1372 ua->catalog = oldcatalog;
1374 ua->catalog = catalog;
1377 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1378 ua->catalog->hdr.name, ua->catalog->db_name);
1383 int quitcmd(UAContext *ua, char *cmd)
1390 * Wait until no job is running
1392 int waitcmd(UAContext *ua, char *cmd)
1395 for (int running=1; running; ) {
1398 for (JCR *jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
1399 if (jcr->JobId != 0 && jcr->JobStatus != JS_Created) {
1401 free_locked_jcr(jcr);
1404 free_locked_jcr(jcr);
1415 static int helpcmd(UAContext *ua, char *cmd)
1420 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1421 for (i=0; i<comsize; i++) {
1422 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1428 static int versioncmd(UAContext *ua, char *cmd)
1430 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1435 /* A bit brain damaged in that if the user has not done
1436 * a "use catalog xxx" command, we simply find the first
1437 * catalog resource and open it.
1439 int open_db(UAContext *ua)
1446 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1449 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1452 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1453 ua->catalog->hdr.name, ua->catalog->db_name);
1457 Dmsg0(150, "Open database\n");
1458 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1459 ua->catalog->db_password, ua->catalog->db_address,
1460 ua->catalog->db_port, ua->catalog->db_socket);
1461 if (!db_open_database(ua->jcr, ua->db)) {
1462 bsendmsg(ua, _("Could not open DB %s: ERR=%s"),
1463 ua->catalog->db_name, db_strerror(ua->db));
1467 ua->jcr->db = ua->db;
1468 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1472 void close_db(UAContext *ua)
1475 db_close_database(ua->jcr, ua->db);