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 */
35 /* Imported variables */
38 extern struct s_res resources[];
39 extern char my_name[];
41 extern workq_t job_wq; /* work queue */
44 extern char *list_pool;
46 /* Imported functions */
47 extern int statuscmd(UAContext *ua, char *cmd);
48 extern int listcmd(UAContext *ua, char *cmd);
49 extern int llistcmd(UAContext *ua, char *cmd);
50 extern int show_cmd(UAContext *ua, char *cmd);
51 extern int messagescmd(UAContext *ua, char *cmd);
52 extern int autodisplaycmd(UAContext *ua, char *cmd);
53 extern int sqlquerycmd(UAContext *ua, char *cmd);
54 extern int querycmd(UAContext *ua, char *cmd);
55 extern int runcmd(UAContext *ua, char *cmd);
56 extern int retentioncmd(UAContext *ua, char *cmd);
57 extern int prunecmd(UAContext *ua, char *cmd);
58 extern int purgecmd(UAContext *ua, char *cmd);
59 extern int restorecmd(UAContext *ua, char *cmd);
60 extern int labelcmd(UAContext *ua, char *cmd);
61 extern int relabelcmd(UAContext *ua, char *cmd);
62 extern int update_slots(UAContext *ua); /* ua_label.c */
64 /* Forward referenced functions */
65 static int add_cmd(UAContext *ua, char *cmd), createcmd(UAContext *ua, char *cmd), cancelcmd(UAContext *ua, char *cmd);
66 static int setdebug_cmd(UAContext *ua, char *cmd);
67 static int var_cmd(UAContext *ua, char *cmd);
68 static int estimate_cmd(UAContext *ua, char *cmd);
69 static int help_cmd(UAContext *ua, char *cmd);
70 static int delete_cmd(UAContext *ua, char *cmd);
71 static int use_cmd(UAContext *ua, char *cmd), unmount_cmd(UAContext *ua, char *cmd);
72 static int version_cmd(UAContext *ua, char *cmd), automount_cmd(UAContext *ua, char *cmd);
73 static int time_cmd(UAContext *ua, char *cmd);
74 static int update_volume(UAContext *ua);
75 static int update_pool(UAContext *ua);
76 static int delete_volume(UAContext *ua);
77 static int delete_pool(UAContext *ua);
78 static int mount_cmd(UAContext *ua, char *cmd);
79 static int release_cmd(UAContext *ua, char *cmd);
80 static int update_cmd(UAContext *ua, char *cmd);
81 static int wait_cmd(UAContext *ua, char *cmd);
83 int quit_cmd(UAContext *ua, char *cmd);
86 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
87 static struct cmdstruct commands[] = {
88 { N_("add"), add_cmd, _("add media to a pool")},
89 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
90 { N_("automount"), automount_cmd, _("automount [on/off] -- after label")},
91 { N_("cancel"), cancelcmd, _("cancel job=nnn -- cancel a job")},
92 { N_("create"), createcmd, _("create DB Pool from resource")},
93 { N_("delete"), delete_cmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
94 { N_("help"), help_cmd, _("print this command")},
95 { N_("label"), labelcmd, _("label a tape")},
96 { N_("relabel"), relabelcmd, _("relabel a tape")},
97 { N_("list"), listcmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
98 { N_("llist"), llistcmd, _("full or long list like list command")},
99 { N_("messages"), messagescmd, _("messages")},
100 { N_("mount"), mount_cmd, _("mount <storage-name>")},
101 { N_("restore"), restorecmd, _("restore files")},
102 { N_("prune"), prunecmd, _("prune expired records from catalog")},
103 { N_("purge"), purgecmd, _("purge records from catalog")},
104 { N_("run"), runcmd, _("run <job-name>")},
105 { N_("setdebug"), setdebug_cmd, _("sets debug level")},
106 { N_("estimate"), estimate_cmd, _("performs FileSet estimate debug=1 give full listing")},
107 { N_("var"), var_cmd, _("does variable expansion")},
108 { N_("show"), show_cmd, _("show (resource records) [jobs | pools | ... | all]")},
109 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
110 { N_("status"), statuscmd, _("status [storage | client]=<name>")},
111 { N_("unmount"), unmount_cmd, _("unmount <storage-name>")},
112 { N_("update"), update_cmd, _("update Volume or Pool")},
113 { N_("use"), use_cmd, _("use catalog xxx")},
114 { N_("version"), version_cmd, _("print Director version")},
115 { N_("quit"), quit_cmd, _("quit")},
116 { N_("query"), querycmd, _("query catalog")},
117 { N_("release"), release_cmd, _("release <storage-name>")},
118 { N_("time"), time_cmd, _("print current time")},
119 { N_("exit"), quit_cmd, _("exit = quit")},
120 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
122 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
125 * Execute a command from the UA
127 int do_a_command(UAContext *ua, char *cmd)
136 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
141 len = strlen(ua->argk[0]);
142 for (i=0; i<comsize; i++) { /* search for command */
143 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
144 stat = (*commands[i].func)(ua, cmd); /* go execute command */
150 pm_strcat(&ua->UA_sock->msg, _(": is an illegal command\n"));
151 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
152 bnet_send(ua->UA_sock);
158 * This is a common routine used to stuff the Pool DB record defaults
159 * into the Media DB record just before creating a media (Volume)
162 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
164 mr->PoolId = pr->PoolId;
165 strcpy(mr->VolStatus, "Append");
166 mr->Recycle = pr->Recycle;
167 mr->VolRetention = pr->VolRetention;
168 mr->VolUseDuration = pr->VolUseDuration;
169 mr->MaxVolJobs = pr->MaxVolJobs;
170 mr->MaxVolFiles = pr->MaxVolFiles;
171 mr->MaxVolBytes = pr->MaxVolBytes;
176 * Add Volumes to an existing Pool
178 static int add_cmd(UAContext *ua, char *cmd)
182 int num, i, max, startnum;
184 char name[MAX_NAME_LENGTH];
189 "You probably don't want to be using this command since it\n"
190 "creates database records without labeling the Volumes.\n"
191 "You probably want to use the \"label\" command.\n\n"));
197 memset(&pr, 0, sizeof(pr));
198 memset(&mr, 0, sizeof(mr));
200 if (!get_pool_dbr(ua, &pr)) {
204 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
205 pr.MaxVols, pr.PoolType);
207 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
208 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
210 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
213 pr.MaxVols = ua->pint32_val;
218 if ((store = get_storage_resource(ua, 0)) != NULL) {
219 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
220 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
224 if (pr.MaxVols == 0) {
227 max = pr.MaxVols - pr.NumVols;
231 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
232 if (!get_pint(ua, buf)) {
235 num = ua->pint32_val;
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 (!is_volume_name_legal(ua, ua->cmd)) {
256 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
257 bsendmsg(ua, _("Volume name too long.\n"));
260 if (strlen(ua->cmd) == 0) {
261 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
265 bstrncpy(name, ua->cmd, sizeof(name));
267 strcat(name, "%04d");
270 if (!get_pint(ua, _("Enter the starting number: "))) {
273 startnum = ua->pint32_val;
275 bsendmsg(ua, _("Start number must be greater than zero.\n"));
285 if (store && store->autochanger) {
286 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
289 slot = ua->pint32_val;
292 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
293 for (i=startnum; i < num+startnum; i++) {
294 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
296 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
297 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
298 bsendmsg(ua, db_strerror(ua->db));
302 first_id = mr.PoolId;
306 Dmsg0(200, "Update pool record.\n");
307 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
308 bsendmsg(ua, db_strerror(ua->db));
311 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
317 * Turn auto mount on/off
322 int automount_cmd(UAContext *ua, char *cmd)
327 if (!get_cmd(ua, _("Turn on or off? "))) {
335 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
343 static int cancelcmd(UAContext *ua, char *cmd)
349 char JobName[MAX_NAME_LENGTH];
355 for (i=1; i<ua->argc; i++) {
356 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
360 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
361 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
365 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
369 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
370 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
376 /* If we still do not have a jcr,
377 * throw up a list and ask the user to select one.
380 /* Count Jobs running */
382 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
383 if (jcr->JobId == 0) { /* this is us */
384 free_locked_jcr(jcr);
388 free_locked_jcr(jcr);
393 bsendmsg(ua, _("No Jobs running.\n"));
396 start_prompt(ua, _("Select Job:\n"));
398 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
399 if (jcr->JobId == 0) { /* this is us */
400 free_locked_jcr(jcr);
403 add_prompt(ua, jcr->Job);
404 free_locked_jcr(jcr);
408 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
412 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
416 jcr = get_jcr_by_full_name(JobName);
418 bsendmsg(ua, _("Job %s not found.\n"), JobName);
423 switch (jcr->JobStatus) {
426 case JS_WaitClientRes:
427 case JS_WaitStoreRes:
429 case JS_WaitStartTime:
430 set_jcr_job_status(jcr, JS_Canceled);
431 bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
432 jcr->JobId, jcr->Job);
433 #ifndef USE_SEMAPHORE
434 workq_remove(&job_wq, jcr->work_item); /* attempt to remove it from queue */
440 set_jcr_job_status(jcr, JS_Canceled);
442 /* Cancel File daemon */
443 if (jcr->file_bsock) {
444 ua->jcr->client = jcr->client;
445 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
446 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
450 Dmsg0(200, "Connected to file daemon\n");
451 fd = ua->jcr->file_bsock;
452 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
453 while (bnet_recv(fd) >= 0) {
454 bsendmsg(ua, "%s", fd->msg);
456 bnet_sig(fd, BNET_TERMINATE);
458 ua->jcr->file_bsock = NULL;
461 /* Cancel Storage daemon */
462 if (jcr->store_bsock) {
463 ua->jcr->store = jcr->store;
464 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
465 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
469 Dmsg0(200, "Connected to storage daemon\n");
470 sd = ua->jcr->store_bsock;
471 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
472 while (bnet_recv(sd) >= 0) {
473 bsendmsg(ua, "%s", sd->msg);
475 bnet_sig(sd, BNET_TERMINATE);
477 ua->jcr->store_bsock = NULL;
486 * This is a common routine to create or update a
487 * Pool DB base record from a Pool Resource. We handle
488 * the setting of MaxVols and NumVols slightly differently
489 * depending on if we are creating the Pool or we are
490 * simply bringing it into agreement with the resource (updage).
492 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
494 strcpy(pr->PoolType, pool->pool_type);
495 if (op == POOL_OP_CREATE) {
496 pr->MaxVols = pool->max_volumes;
498 } else { /* update pool */
499 if (pr->MaxVols != pool->max_volumes) {
500 pr->MaxVols = pool->max_volumes;
502 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
503 pr->MaxVols = pr->NumVols;
506 pr->UseOnce = pool->use_volume_once;
507 pr->UseCatalog = pool->use_catalog;
508 pr->AcceptAnyVolume = pool->accept_any_volume;
509 pr->Recycle = pool->Recycle;
510 pr->VolRetention = pool->VolRetention;
511 pr->VolUseDuration = pool->VolUseDuration;
512 pr->MaxVolJobs = pool->MaxVolJobs;
513 pr->MaxVolFiles = pool->MaxVolFiles;
514 pr->MaxVolBytes = pool->MaxVolBytes;
515 pr->AutoPrune = pool->AutoPrune;
516 pr->Recycle = pool->Recycle;
517 if (pool->label_format) {
518 strcpy(pr->LabelFormat, pool->label_format);
520 strcpy(pr->LabelFormat, "*"); /* none */
526 * Create a pool record from a given Pool resource
527 * Also called from backup.c
528 * Returns: -1 on error
529 * 0 record already exists
533 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
537 memset(&pr, 0, sizeof(POOL_DBR));
539 strcpy(pr.Name, pool->hdr.name);
541 if (db_get_pool_record(jcr, db, &pr)) {
543 if (op == POOL_OP_UPDATE) { /* update request */
544 set_pooldbr_from_poolres(&pr, pool, op);
545 db_update_pool_record(jcr, db, &pr);
547 return 0; /* exists */
550 set_pooldbr_from_poolres(&pr, pool, op);
552 if (!db_create_pool_record(jcr, db, &pr)) {
553 return -1; /* error */
561 * Create a Pool Record in the database.
562 * It is always created from the Resource record.
564 static int createcmd(UAContext *ua, char *cmd)
572 pool = get_pool_resource(ua);
577 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
579 bsendmsg(ua, _("Error: Pool %s already exists.\n\
580 Use update to change it.\n"), pool->hdr.name);
584 bsendmsg(ua, db_strerror(ua->db));
590 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
598 * Update a Pool Record in the database.
599 * It is always updated from the Resource record.
601 * update pool=<pool-name>
602 * updates pool from Pool resource
603 * update media pool=<pool-name> volume=<volume-name>
604 * changes pool info for volume
606 static int update_cmd(UAContext *ua, char *cmd)
608 static char *kw[] = {
610 N_("volume"), /* 1 */
619 switch (find_arg_keyword(ua, kw)) {
634 start_prompt(ua, _("Update choice:\n"));
635 add_prompt(ua, _("Volume parameters"));
636 add_prompt(ua, _("Pool from resource"));
637 add_prompt(ua, _("Slots from autochanger"));
638 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
655 * Update a media record -- allows you to change the
656 * Volume status. E.g. if you want Bacula to stop
657 * writing on the volume, set it to anything other
660 static int update_volume(UAContext *ua)
667 for (int done=0; !done; ) {
668 if (!select_media_dbr(ua, &mr)) {
671 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
672 start_prompt(ua, _("Parameters to modify:\n"));
673 add_prompt(ua, _("Volume Status"));
674 add_prompt(ua, _("Volume Retention Period"));
675 add_prompt(ua, _("Volume Use Duration"));
676 add_prompt(ua, _("Maximum Volume Jobs"));
677 add_prompt(ua, _("Maximum Volume Files"));
678 add_prompt(ua, _("Maximum Volume Bytes"));
679 add_prompt(ua, _("Recycle Flag"));
680 add_prompt(ua, _("Slot"));
681 add_prompt(ua, _("Volume Files"));
682 add_prompt(ua, _("Done"));
683 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
684 case 0: /* Volume Status */
685 /* Modify Volume Status */
686 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
687 start_prompt(ua, _("Possible Values are:\n"));
688 add_prompt(ua, "Append"); /* Better not translate these as */
689 add_prompt(ua, "Archive"); /* They are known in the database code */
690 add_prompt(ua, "Disabled");
691 add_prompt(ua, "Full");
692 add_prompt(ua, "Used");
693 add_prompt(ua, "Cleaning");
694 if (strcmp(mr.VolStatus, "Purged") == 0) {
695 add_prompt(ua, "Recycle");
697 add_prompt(ua, "Read-Only");
698 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
701 bstrncpy(mr.VolStatus, ua->cmd, sizeof(mr.VolStatus));
702 query = get_pool_memory(PM_MESSAGE);
703 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
704 mr.VolStatus, mr.MediaId);
705 if (!db_sql_query(ua->db, query, NULL, NULL)) {
706 bsendmsg(ua, "%s", db_strerror(ua->db));
708 bsendmsg(ua, _("New Volume status is: %s\n"), mr.VolStatus);
710 free_pool_memory(query);
712 case 1: /* Retention */
713 bsendmsg(ua, _("Current retention seconds is: %s\n"),
714 edit_utime(mr.VolRetention, ed1));
715 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
718 if (!duration_to_utime(ua->cmd, &mr.VolRetention)) {
719 bsendmsg(ua, _("Invalid retention period specified.\n"));
722 query = get_pool_memory(PM_MESSAGE);
723 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
724 edit_uint64(mr.VolRetention, ed1), mr.MediaId);
725 if (!db_sql_query(ua->db, query, NULL, NULL)) {
726 bsendmsg(ua, "%s", db_strerror(ua->db));
728 bsendmsg(ua, _("New retention seconds is: %s\n"),
729 edit_utime(mr.VolRetention, ed1));
731 free_pool_memory(query);
734 case 2: /* Use Duration */
735 bsendmsg(ua, _("Current use duration is: %s\n"),
736 edit_utime(mr.VolUseDuration, ed1));
737 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
740 if (!duration_to_utime(ua->cmd, &mr.VolUseDuration)) {
741 bsendmsg(ua, _("Invalid use duration specified.\n"));
744 query = get_pool_memory(PM_MESSAGE);
745 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
746 edit_uint64(mr.VolUseDuration, ed1), mr.MediaId);
747 if (!db_sql_query(ua->db, query, NULL, NULL)) {
748 bsendmsg(ua, "%s", db_strerror(ua->db));
750 bsendmsg(ua, _("New use duration is: %s\n"),
751 edit_utime(mr.VolUseDuration, ed1));
753 free_pool_memory(query);
756 case 3: /* Max Jobs */
758 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
759 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
762 maxjobs = ua->pint32_val;
763 query = get_pool_memory(PM_MESSAGE);
764 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%u WHERE MediaId=%u",
765 maxjobs, mr.MediaId);
766 if (!db_sql_query(ua->db, query, NULL, NULL)) {
767 bsendmsg(ua, "%s", db_strerror(ua->db));
769 bsendmsg(ua, _("New max jobs is: %u\n"), maxjobs);
771 free_pool_memory(query);
774 case 4: /* Max Files */
776 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
777 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
780 maxfiles = ua->pint32_val;
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_yesno(ua, _("Enter new Recycle status: "))) {
821 recycle = ua->pint32_val;
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);
838 memset(&pr, 0, sizeof(POOL_DBR));
839 pr.PoolId = mr.PoolId;
840 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
841 bsendmsg(ua, "%s", db_strerror(ua->db));
844 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
845 if (!get_pint(ua, _("Enter new Slot: "))) {
848 slot = ua->pint32_val;
849 if (pr.MaxVols > 0 && slot > (int)pr.MaxVols) {
850 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
854 query = get_pool_memory(PM_MESSAGE);
855 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
857 if (!db_sql_query(ua->db, query, NULL, NULL)) {
858 bsendmsg(ua, "%s", db_strerror(ua->db));
860 bsendmsg(ua, "New Slot is: %d\n", slot);
862 free_pool_memory(query);
865 case 8: /* Volume Files */
867 bsendmsg(ua, _("Warning changing Volume Files can result\n"
868 "in loss of data on your Volume\n\n"));
869 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
870 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
873 VolFiles = ua->pint32_val;
874 if (VolFiles != (int)(mr.VolFiles + 1)) {
875 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
876 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 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, POOL_OP_UPDATE); /* 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, HORZ_LIST);
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 setdebug_cmd(UAContext *ua, char *cmd)
1076 Dmsg1(120, "setdebug:%s:\n", cmd);
1079 i = find_arg_with_value(ua, _("level"));
1081 level = atoi(ua->argv[i]);
1084 if (!get_pint(ua, _("Enter new debug level: "))) {
1087 level = ua->pint32_val;
1090 /* General debug? */
1091 for (i=1; i<ua->argc; i++) {
1092 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1093 do_all_setdebug(ua, level);
1096 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1097 strcasecmp(ua->argk[i], _("director")) == 0) {
1098 debug_level = level;
1101 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1104 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1106 do_client_setdebug(ua, client, level);
1110 client = select_client_resource(ua);
1112 do_client_setdebug(ua, client, level);
1117 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1118 strcasecmp(ua->argk[i], _("storage")) == 0) {
1121 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1123 do_storage_setdebug(ua, store, level);
1127 store = get_storage_resource(ua, 0);
1129 do_storage_setdebug(ua, store, level);
1135 * We didn't find an appropriate keyword above, so
1138 start_prompt(ua, _("Available daemons are: \n"));
1139 add_prompt(ua, _("Director"));
1140 add_prompt(ua, _("Storage"));
1141 add_prompt(ua, _("Client"));
1142 add_prompt(ua, _("All"));
1143 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1144 case 0: /* Director */
1145 debug_level = level;
1148 store = get_storage_resource(ua, 0);
1150 do_storage_setdebug(ua, store, level);
1154 client = select_client_resource(ua);
1156 do_client_setdebug(ua, client, level);
1160 do_all_setdebug(ua, level);
1168 static int var_cmd(UAContext *ua, char *cmd)
1170 POOLMEM *var = get_pool_memory(PM_FNAME);
1171 for (int i=1; i<ua->argc; i++) {
1172 if (ua->argk[i] && variable_expansion(ua->jcr, ua->argk[i], &var)) {
1173 bsendmsg(ua, "%s\n", var);
1176 free_pool_memory(var);
1180 static int estimate_cmd(UAContext *ua, char *cmd)
1183 CLIENT *client = NULL;
1184 FILESET *fileset = NULL;
1187 char since[MAXSTRING];
1189 for (int i=1; i<ua->argc; i++) {
1190 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1192 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1196 if (strcasecmp(ua->argk[i], _("job")) == 0) {
1198 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1202 if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1204 fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1208 if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1212 if (!job && !(client && fileset)) {
1213 if (!(job = select_job_resource(ua))) {
1218 job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1221 client = job->client;
1224 fileset = job->fileset;
1226 ua->jcr->client = client;
1227 ua->jcr->fileset = fileset;
1229 ua->catalog = client->catalog;
1235 get_level_since_time(ua->jcr, since, sizeof(since));
1237 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1238 job->client->hdr.name, job->client->address, job->client->FDport);
1239 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1240 bsendmsg(ua, _("Failed to connect to Client.\n"));
1243 fd = ua->jcr->file_bsock;
1245 if (!send_include_list(ua->jcr)) {
1246 bsendmsg(ua, _("Error sending include list.\n"));
1250 if (!send_exclude_list(ua->jcr)) {
1251 bsendmsg(ua, _("Error sending exclude list.\n"));
1255 if (!send_level_command(ua->jcr)) {
1259 bnet_fsend(fd, "estimate listing=%d\n", listing);
1260 while (bnet_recv(fd) >= 0) {
1261 bsendmsg(ua, "%s", fd->msg);
1264 bnet_sig(fd, BNET_TERMINATE);
1266 ua->jcr->file_bsock = NULL;
1275 static int time_cmd(UAContext *ua, char *cmd)
1278 time_t ttime = time(NULL);
1280 localtime_r(&ttime, &tm);
1281 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1282 bsendmsg(ua, "%s\n", sdt);
1289 * Delete Pool records (should purge Media with it).
1291 * delete pool=<pool-name>
1292 * delete media pool=<pool-name> volume=<name>
1294 static int delete_cmd(UAContext *ua, char *cmd)
1296 static char *keywords[] = {
1306 "In general it is not a good idea to delete either a\n"
1307 "Pool or a Volume since they may contain data.\n\n"));
1309 switch (find_arg_keyword(ua, keywords)) {
1319 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1327 bsendmsg(ua, _("Nothing done.\n"));
1334 * Delete media records from database -- dangerous
1336 static int delete_volume(UAContext *ua)
1340 if (!select_media_dbr(ua, &mr)) {
1343 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1344 "and all Jobs saved on that volume from the Catalog\n"),
1347 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1350 if (ua->pint32_val) {
1351 db_delete_media_record(ua->jcr, ua->db, &mr);
1357 * Delete a pool record from the database -- dangerous
1359 static int delete_pool(UAContext *ua)
1363 memset(&pr, 0, sizeof(pr));
1365 if (!get_pool_dbr(ua, &pr)) {
1368 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1371 if (ua->pint32_val) {
1372 db_delete_pool_record(ua->jcr, ua->db, &pr);
1378 static void do_mount_cmd(UAContext *ua, char *command)
1382 char dev_name[MAX_NAME_LENGTH];
1388 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1390 store = get_storage_resource(ua, 1);
1395 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1396 store->media_type, store->dev_name);
1398 ua->jcr->store = store;
1399 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1400 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1403 sd = ua->jcr->store_bsock;
1404 strcpy(dev_name, store->dev_name);
1405 bash_spaces(dev_name);
1406 bnet_fsend(sd, "%s %s", command, dev_name);
1407 while (bnet_recv(sd) >= 0) {
1408 bsendmsg(ua, "%s", sd->msg);
1410 bnet_sig(sd, BNET_TERMINATE);
1412 ua->jcr->store_bsock = NULL;
1416 * mount [storage | device] <name>
1418 static int mount_cmd(UAContext *ua, char *cmd)
1420 do_mount_cmd(ua, "mount"); /* mount */
1426 * unmount [storage | device] <name>
1428 static int unmount_cmd(UAContext *ua, char *cmd)
1430 do_mount_cmd(ua, "unmount"); /* unmount */
1436 * release [storage | device] <name>
1438 static int release_cmd(UAContext *ua, char *cmd)
1440 do_mount_cmd(ua, "release"); /* release */
1447 * use catalog=<name>
1449 static int use_cmd(UAContext *ua, char *cmd)
1451 CAT *oldcatalog, *catalog;
1454 close_db(ua); /* close any previously open db */
1455 oldcatalog = ua->catalog;
1457 if (!(catalog = get_catalog_resource(ua))) {
1458 ua->catalog = oldcatalog;
1460 ua->catalog = catalog;
1463 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1464 ua->catalog->hdr.name, ua->catalog->db_name);
1469 int quit_cmd(UAContext *ua, char *cmd)
1476 * Wait until no job is running
1478 int wait_cmd(UAContext *ua, char *cmd)
1480 bmicrosleep(0, 200000); /* let job actually start */
1481 for (int running=1; running; ) {
1484 for (JCR *jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
1485 if (jcr->JobId != 0) {
1487 free_locked_jcr(jcr);
1490 free_locked_jcr(jcr);
1501 static int help_cmd(UAContext *ua, char *cmd)
1506 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1507 for (i=0; i<comsize; i++) {
1508 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1514 static int version_cmd(UAContext *ua, char *cmd)
1516 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1521 /* A bit brain damaged in that if the user has not done
1522 * a "use catalog xxx" command, we simply find the first
1523 * catalog resource and open it.
1525 int open_db(UAContext *ua)
1532 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1535 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1538 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1539 ua->catalog->hdr.name, ua->catalog->db_name);
1543 ua->jcr->catalog = ua->catalog;
1545 Dmsg0(150, "Open database\n");
1546 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1547 ua->catalog->db_password, ua->catalog->db_address,
1548 ua->catalog->db_port, ua->catalog->db_socket);
1549 if (!db_open_database(ua->jcr, ua->db)) {
1550 bsendmsg(ua, _("Could not open DB %s: ERR=%s"),
1551 ua->catalog->db_name, db_strerror(ua->db));
1555 ua->jcr->db = ua->db;
1556 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1560 void close_db(UAContext *ua)
1563 db_close_database(ua->jcr, ua->db);