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 help_cmd(UAContext *ua, char *cmd);
68 static int delete_cmd(UAContext *ua, char *cmd);
69 static int use_cmd(UAContext *ua, char *cmd), unmount_cmd(UAContext *ua, char *cmd);
70 static int version_cmd(UAContext *ua, char *cmd), automount_cmd(UAContext *ua, char *cmd);
71 static int time_cmd(UAContext *ua, char *cmd);
72 static int update_volume(UAContext *ua);
73 static int update_pool(UAContext *ua);
74 static int delete_volume(UAContext *ua);
75 static int delete_pool(UAContext *ua);
76 static int mount_cmd(UAContext *ua, char *cmd);
77 static int release_cmd(UAContext *ua, char *cmd);
78 static int update_cmd(UAContext *ua, char *cmd);
79 static int wait_cmd(UAContext *ua, char *cmd);
81 int quit_cmd(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"), add_cmd, _("add media to a pool")},
87 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
88 { N_("automount"), automount_cmd, _("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"), delete_cmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
92 { N_("help"), help_cmd, _("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"), mount_cmd, _("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"), setdebug_cmd, _("sets debug level")},
104 { N_("show"), show_cmd, _("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"), unmount_cmd, _("unmount <storage-name>")},
108 { N_("update"), update_cmd, _("update Volume or Pool")},
109 { N_("use"), use_cmd, _("use catalog xxx")},
110 { N_("version"), version_cmd, _("print Director version")},
111 { N_("quit"), quit_cmd, _("quit")},
112 { N_("query"), querycmd, _("query catalog")},
113 { N_("release"), release_cmd, _("release <storage-name>")},
114 { N_("time"), time_cmd, _("print current time")},
115 { N_("exit"), quit_cmd, _("exit = quit")},
116 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
118 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
121 * Execute a command from the UA
123 int do_a_command(UAContext *ua, char *cmd)
132 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
137 len = strlen(ua->argk[0]);
138 for (i=0; i<comsize; i++) { /* search for command */
139 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
140 stat = (*commands[i].func)(ua, cmd); /* go execute command */
146 pm_strcat(&ua->UA_sock->msg, _(": is an illegal command\n"));
147 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
148 bnet_send(ua->UA_sock);
154 * This is a common routine used to stuff the Pool DB record defaults
155 * into the Media DB record just before creating a media (Volume)
158 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
160 mr->PoolId = pr->PoolId;
161 strcpy(mr->VolStatus, "Append");
162 mr->Recycle = pr->Recycle;
163 mr->VolRetention = pr->VolRetention;
164 mr->VolUseDuration = pr->VolUseDuration;
165 mr->MaxVolJobs = pr->MaxVolJobs;
166 mr->MaxVolFiles = pr->MaxVolFiles;
167 mr->MaxVolBytes = pr->MaxVolBytes;
172 * Add Volumes to an existing Pool
174 static int add_cmd(UAContext *ua, char *cmd)
178 int num, i, max, startnum;
180 char name[MAX_NAME_LENGTH];
185 "You probably don't want to be using this command since it\n"
186 "creates database records without labeling the Volumes.\n"
187 "You probably want to use the \"label\" command.\n\n"));
193 memset(&pr, 0, sizeof(pr));
194 memset(&mr, 0, sizeof(mr));
196 if (!get_pool_dbr(ua, &pr)) {
200 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
201 pr.MaxVols, pr.PoolType);
203 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
204 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
206 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
209 pr.MaxVols = ua->pint32_val;
214 if ((store = get_storage_resource(ua, 0)) != NULL) {
215 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
216 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
220 if (pr.MaxVols == 0) {
223 max = pr.MaxVols - pr.NumVols;
227 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
228 if (!get_pint(ua, buf)) {
231 num = ua->pint32_val;
232 if (num < 0 || num > max) {
233 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
240 if (!get_cmd(ua, _("Enter Volume name: "))) {
244 if (!get_cmd(ua, _("Enter base volume name: "))) {
248 /* Don't allow | in Volume name because it is the volume separator character */
249 if (!is_volume_name_legal(ua, ua->cmd)) {
252 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
253 bsendmsg(ua, _("Volume name too long.\n"));
256 if (strlen(ua->cmd) == 0) {
257 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
261 bstrncpy(name, ua->cmd, sizeof(name));
263 strcat(name, "%04d");
266 if (!get_pint(ua, _("Enter the starting number: "))) {
269 startnum = ua->pint32_val;
271 bsendmsg(ua, _("Start number must be greater than zero.\n"));
281 if (store && store->autochanger) {
282 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
285 slot = ua->pint32_val;
288 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
289 for (i=startnum; i < num+startnum; i++) {
290 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
292 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
293 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
294 bsendmsg(ua, db_strerror(ua->db));
298 first_id = mr.PoolId;
302 Dmsg0(200, "Update pool record.\n");
303 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
304 bsendmsg(ua, db_strerror(ua->db));
307 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
313 * Turn auto mount on/off
318 int automount_cmd(UAContext *ua, char *cmd)
323 if (!get_cmd(ua, _("Turn on or off? "))) {
331 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
339 static int cancelcmd(UAContext *ua, char *cmd)
345 char JobName[MAX_NAME_LENGTH];
351 for (i=1; i<ua->argc; i++) {
352 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
356 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
357 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
361 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
365 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
366 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
372 /* If we still do not have a jcr,
373 * throw up a list and ask the user to select one.
376 /* Count Jobs running */
378 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
379 if (jcr->JobId == 0) { /* this is us */
380 free_locked_jcr(jcr);
384 free_locked_jcr(jcr);
389 bsendmsg(ua, _("No Jobs running.\n"));
392 start_prompt(ua, _("Select Job:\n"));
394 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
395 if (jcr->JobId == 0) { /* this is us */
396 free_locked_jcr(jcr);
399 add_prompt(ua, jcr->Job);
400 free_locked_jcr(jcr);
404 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
408 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
412 jcr = get_jcr_by_full_name(JobName);
414 bsendmsg(ua, _("Job %s not found.\n"), JobName);
419 switch (jcr->JobStatus) {
422 case JS_WaitClientRes:
423 case JS_WaitStoreRes:
425 case JS_WaitStartTime:
426 set_jcr_job_status(jcr, JS_Canceled);
427 bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
428 jcr->JobId, jcr->Job);
429 #ifndef USE_SEMAPHORE
430 workq_remove(&job_wq, jcr->work_item); /* attempt to remove it from queue */
436 set_jcr_job_status(jcr, JS_Canceled);
438 /* Cancel File daemon */
439 if (jcr->file_bsock) {
440 ua->jcr->client = jcr->client;
441 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
442 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
446 Dmsg0(200, "Connected to file daemon\n");
447 fd = ua->jcr->file_bsock;
448 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
449 while (bnet_recv(fd) >= 0) {
450 bsendmsg(ua, "%s", fd->msg);
452 bnet_sig(fd, BNET_TERMINATE);
454 ua->jcr->file_bsock = NULL;
457 /* Cancel Storage daemon */
458 if (jcr->store_bsock) {
459 ua->jcr->store = jcr->store;
460 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
461 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
465 Dmsg0(200, "Connected to storage daemon\n");
466 sd = ua->jcr->store_bsock;
467 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
468 while (bnet_recv(sd) >= 0) {
469 bsendmsg(ua, "%s", sd->msg);
471 bnet_sig(sd, BNET_TERMINATE);
473 ua->jcr->store_bsock = NULL;
482 * This is a common routine to create or update a
483 * Pool DB base record from a Pool Resource. We handle
484 * the setting of MaxVols and NumVols slightly differently
485 * depending on if we are creating the Pool or we are
486 * simply bringing it into agreement with the resource (updage).
488 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
490 strcpy(pr->PoolType, pool->pool_type);
491 if (op == POOL_OP_CREATE) {
492 pr->MaxVols = pool->max_volumes;
494 } else { /* update pool */
495 if (pr->MaxVols != pool->max_volumes) {
496 pr->MaxVols = pool->max_volumes;
498 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
499 pr->MaxVols = pr->NumVols;
502 pr->UseOnce = pool->use_volume_once;
503 pr->UseCatalog = pool->use_catalog;
504 pr->AcceptAnyVolume = pool->accept_any_volume;
505 pr->Recycle = pool->Recycle;
506 pr->VolRetention = pool->VolRetention;
507 pr->VolUseDuration = pool->VolUseDuration;
508 pr->MaxVolJobs = pool->MaxVolJobs;
509 pr->MaxVolFiles = pool->MaxVolFiles;
510 pr->MaxVolBytes = pool->MaxVolBytes;
511 pr->AutoPrune = pool->AutoPrune;
512 pr->Recycle = pool->Recycle;
513 if (pool->label_format) {
514 strcpy(pr->LabelFormat, pool->label_format);
516 strcpy(pr->LabelFormat, "*"); /* none */
522 * Create a pool record from a given Pool resource
523 * Also called from backup.c
524 * Returns: -1 on error
525 * 0 record already exists
529 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
533 memset(&pr, 0, sizeof(POOL_DBR));
535 strcpy(pr.Name, pool->hdr.name);
537 if (db_get_pool_record(jcr, db, &pr)) {
539 if (op == POOL_OP_UPDATE) { /* update request */
540 set_pooldbr_from_poolres(&pr, pool, op);
541 db_update_pool_record(jcr, db, &pr);
543 return 0; /* exists */
546 set_pooldbr_from_poolres(&pr, pool, op);
548 if (!db_create_pool_record(jcr, db, &pr)) {
549 return -1; /* error */
557 * Create a Pool Record in the database.
558 * It is always created from the Resource record.
560 static int createcmd(UAContext *ua, char *cmd)
568 pool = get_pool_resource(ua);
573 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
575 bsendmsg(ua, _("Error: Pool %s already exists.\n\
576 Use update to change it.\n"), pool->hdr.name);
580 bsendmsg(ua, db_strerror(ua->db));
586 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
594 * Update a Pool Record in the database.
595 * It is always updated from the Resource record.
597 * update pool=<pool-name>
598 * updates pool from Pool resource
599 * update media pool=<pool-name> volume=<volume-name>
600 * changes pool info for volume
602 static int update_cmd(UAContext *ua, char *cmd)
604 static char *kw[] = {
606 N_("volume"), /* 1 */
615 switch (find_arg_keyword(ua, kw)) {
630 start_prompt(ua, _("Update choice:\n"));
631 add_prompt(ua, _("Volume parameters"));
632 add_prompt(ua, _("Pool from resource"));
633 add_prompt(ua, _("Slots from autochanger"));
634 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
651 * Update a media record -- allows you to change the
652 * Volume status. E.g. if you want Bacula to stop
653 * writing on the volume, set it to anything other
656 static int update_volume(UAContext *ua)
663 for (int done=0; !done; ) {
664 if (!select_media_dbr(ua, &mr)) {
667 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
668 start_prompt(ua, _("Parameters to modify:\n"));
669 add_prompt(ua, _("Volume Status"));
670 add_prompt(ua, _("Volume Retention Period"));
671 add_prompt(ua, _("Volume Use Duration"));
672 add_prompt(ua, _("Maximum Volume Jobs"));
673 add_prompt(ua, _("Maximum Volume Files"));
674 add_prompt(ua, _("Maximum Volume Bytes"));
675 add_prompt(ua, _("Recycle Flag"));
676 add_prompt(ua, _("Slot"));
677 add_prompt(ua, _("Volume Files"));
678 add_prompt(ua, _("Done"));
679 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
680 case 0: /* Volume Status */
681 /* Modify Volume Status */
682 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
683 start_prompt(ua, _("Possible Values are:\n"));
684 add_prompt(ua, "Append"); /* Better not translate these as */
685 add_prompt(ua, "Archive"); /* They are known in the database code */
686 add_prompt(ua, "Disabled");
687 add_prompt(ua, "Full");
688 add_prompt(ua, "Used");
689 add_prompt(ua, "Cleaning");
690 if (strcmp(mr.VolStatus, "Purged") == 0) {
691 add_prompt(ua, "Recycle");
693 add_prompt(ua, "Read-Only");
694 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
697 bstrncpy(mr.VolStatus, ua->cmd, sizeof(mr.VolStatus));
698 query = get_pool_memory(PM_MESSAGE);
699 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
700 mr.VolStatus, mr.MediaId);
701 if (!db_sql_query(ua->db, query, NULL, NULL)) {
702 bsendmsg(ua, "%s", db_strerror(ua->db));
704 bsendmsg(ua, _("New Volume status is: %s\n"), mr.VolStatus);
706 free_pool_memory(query);
708 case 1: /* Retention */
709 bsendmsg(ua, _("Current retention seconds is: %s\n"),
710 edit_utime(mr.VolRetention, ed1));
711 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
714 if (!duration_to_utime(ua->cmd, &mr.VolRetention)) {
715 bsendmsg(ua, _("Invalid retention period specified.\n"));
718 query = get_pool_memory(PM_MESSAGE);
719 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
720 edit_uint64(mr.VolRetention, ed1), mr.MediaId);
721 if (!db_sql_query(ua->db, query, NULL, NULL)) {
722 bsendmsg(ua, "%s", db_strerror(ua->db));
724 bsendmsg(ua, _("New retention seconds is: %s\n"),
725 edit_utime(mr.VolRetention, ed1));
727 free_pool_memory(query);
730 case 2: /* Use Duration */
731 bsendmsg(ua, _("Current use duration is: %s\n"),
732 edit_utime(mr.VolUseDuration, ed1));
733 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
736 if (!duration_to_utime(ua->cmd, &mr.VolUseDuration)) {
737 bsendmsg(ua, _("Invalid use duration specified.\n"));
740 query = get_pool_memory(PM_MESSAGE);
741 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
742 edit_uint64(mr.VolUseDuration, ed1), mr.MediaId);
743 if (!db_sql_query(ua->db, query, NULL, NULL)) {
744 bsendmsg(ua, "%s", db_strerror(ua->db));
746 bsendmsg(ua, _("New use duration is: %s\n"),
747 edit_utime(mr.VolUseDuration, ed1));
749 free_pool_memory(query);
752 case 3: /* Max Jobs */
754 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
755 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
758 maxjobs = ua->pint32_val;
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_pint(ua, _("Enter new Maximum Files: "))) {
776 maxfiles = ua->pint32_val;
777 query = get_pool_memory(PM_MESSAGE);
778 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%u WHERE MediaId=%u",
779 maxfiles, mr.MediaId);
780 if (!db_sql_query(ua->db, query, NULL, NULL)) {
781 bsendmsg(ua, "%s", db_strerror(ua->db));
783 bsendmsg(ua, _("New max files is: %u\n"), maxfiles);
785 free_pool_memory(query);
788 case 5: /* Max Bytes */
790 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
791 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
794 if (!size_to_uint64(ua->cmd, strlen(ua->cmd), &maxbytes)) {
795 bsendmsg(ua, _("Invalid byte size specification.\n"));
798 query = get_pool_memory(PM_MESSAGE);
799 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
800 edit_uint64(maxbytes, ed1), mr.MediaId);
801 if (!db_sql_query(ua->db, query, NULL, NULL)) {
802 bsendmsg(ua, "%s", db_strerror(ua->db));
804 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
806 free_pool_memory(query);
810 case 6: /* Recycle */
812 bsendmsg(ua, _("Current recycle flag is: %s\n"),
813 mr.Recycle==1?_("yes"):_("no"));
814 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
817 recycle = ua->pint32_val;
818 query = get_pool_memory(PM_MESSAGE);
819 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
820 recycle, mr.MediaId);
821 if (!db_sql_query(ua->db, query, NULL, NULL)) {
822 bsendmsg(ua, "%s", db_strerror(ua->db));
824 bsendmsg(ua, _("New recycle flag is: %s\n"),
825 mr.Recycle==1?_("yes"):_("no"));
827 free_pool_memory(query);
834 memset(&pr, 0, sizeof(POOL_DBR));
835 pr.PoolId = mr.PoolId;
836 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
837 bsendmsg(ua, "%s", db_strerror(ua->db));
840 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
841 if (!get_pint(ua, _("Enter new Slot: "))) {
844 slot = ua->pint32_val;
845 if (pr.MaxVols > 0 && slot > (int)pr.MaxVols) {
846 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
850 query = get_pool_memory(PM_MESSAGE);
851 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
853 if (!db_sql_query(ua->db, query, NULL, NULL)) {
854 bsendmsg(ua, "%s", db_strerror(ua->db));
856 bsendmsg(ua, "New Slot is: %d\n", slot);
858 free_pool_memory(query);
861 case 8: /* Volume Files */
863 bsendmsg(ua, _("Warning changing Volume Files can result\n"
864 "in loss of data on your Volume\n\n"));
865 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
866 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
869 VolFiles = ua->pint32_val;
870 if (VolFiles != (int)(mr.VolFiles + 1)) {
871 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
872 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
876 query = get_pool_memory(PM_MESSAGE);
877 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
878 VolFiles, mr.MediaId);
879 if (!db_sql_query(ua->db, query, NULL, NULL)) {
880 bsendmsg(ua, "%s", db_strerror(ua->db));
882 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
884 free_pool_memory(query);
887 default: /* Done or error */
888 bsendmsg(ua, "Selection done.\n");
896 * Update pool record -- pull info from current POOL resource
898 static int update_pool(UAContext *ua)
906 pool = get_pool_resource(ua);
911 memset(&pr, 0, sizeof(pr));
912 strcpy(pr.Name, pool->hdr.name);
913 if (!get_pool_dbr(ua, &pr)) {
917 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
919 id = db_update_pool_record(ua->jcr, ua->db, &pr);
921 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
922 id, db_strerror(ua->db));
924 query = get_pool_memory(PM_MESSAGE);
925 Mmsg(&query, list_pool, pr.PoolId);
926 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
927 free_pool_memory(query);
928 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
933 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
937 ua->jcr->store = store;
938 /* Try connecting for up to 15 seconds */
939 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
940 store->hdr.name, store->address, store->SDport);
941 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
942 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
945 Dmsg0(120, _("Connected to storage daemon\n"));
946 sd = ua->jcr->store_bsock;
947 bnet_fsend(sd, "setdebug=%d\n", level);
948 if (bnet_recv(sd) >= 0) {
949 bsendmsg(ua, "%s", sd->msg);
951 bnet_sig(sd, BNET_TERMINATE);
953 ua->jcr->store_bsock = NULL;
957 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
961 /* Connect to File daemon */
963 ua->jcr->client = client;
964 /* Try to connect for 15 seconds */
965 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
966 client->hdr.name, client->address, client->FDport);
967 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
968 bsendmsg(ua, _("Failed to connect to Client.\n"));
971 Dmsg0(120, "Connected to file daemon\n");
972 fd = ua->jcr->file_bsock;
973 bnet_fsend(fd, "setdebug=%d\n", level);
974 if (bnet_recv(fd) >= 0) {
975 bsendmsg(ua, "%s", fd->msg);
977 bnet_sig(fd, BNET_TERMINATE);
979 ua->jcr->file_bsock = NULL;
985 static void do_all_setdebug(UAContext *ua, int level)
987 STORE *store, **unique_store;
988 CLIENT *client, **unique_client;
994 /* Count Storage items */
997 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
999 unique_store = (STORE **) malloc(i * sizeof(STORE));
1000 /* Find Unique Storage address/port */
1001 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1003 unique_store[i++] = store;
1004 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1006 for (j=0; j<i; j++) {
1007 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1008 unique_store[j]->SDport == store->SDport) {
1014 unique_store[i++] = store;
1015 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1020 /* Call each unique Storage daemon */
1021 for (j=0; j<i; j++) {
1022 do_storage_setdebug(ua, unique_store[j], level);
1026 /* Count Client items */
1029 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1031 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1032 /* Find Unique Client address/port */
1033 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1035 unique_client[i++] = client;
1036 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1038 for (j=0; j<i; j++) {
1039 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1040 unique_client[j]->FDport == client->FDport) {
1046 unique_client[i++] = client;
1047 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1052 /* Call each unique File daemon */
1053 for (j=0; j<i; j++) {
1054 do_client_setdebug(ua, unique_client[j], level);
1056 free(unique_client);
1060 * setdebug level=nn all
1062 static int setdebug_cmd(UAContext *ua, char *cmd)
1072 Dmsg1(120, "setdebug:%s:\n", cmd);
1075 i = find_arg_with_value(ua, _("level"));
1077 level = atoi(ua->argv[i]);
1080 if (!get_pint(ua, _("Enter new debug level: "))) {
1083 level = ua->pint32_val;
1086 /* General debug? */
1087 for (i=1; i<ua->argc; i++) {
1088 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1089 do_all_setdebug(ua, level);
1092 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1093 strcasecmp(ua->argk[i], _("director")) == 0) {
1094 debug_level = level;
1097 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1100 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1102 do_client_setdebug(ua, client, level);
1106 client = select_client_resource(ua);
1108 do_client_setdebug(ua, client, level);
1113 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1114 strcasecmp(ua->argk[i], _("storage")) == 0) {
1117 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1119 do_storage_setdebug(ua, store, level);
1123 store = get_storage_resource(ua, 0);
1125 do_storage_setdebug(ua, store, level);
1131 * We didn't find an appropriate keyword above, so
1134 start_prompt(ua, _("Available daemons are: \n"));
1135 add_prompt(ua, _("Director"));
1136 add_prompt(ua, _("Storage"));
1137 add_prompt(ua, _("Client"));
1138 add_prompt(ua, _("All"));
1139 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1140 case 0: /* Director */
1141 debug_level = level;
1144 store = get_storage_resource(ua, 0);
1146 do_storage_setdebug(ua, store, level);
1150 client = select_client_resource(ua);
1152 do_client_setdebug(ua, client, level);
1156 do_all_setdebug(ua, level);
1167 static int time_cmd(UAContext *ua, char *cmd)
1170 time_t ttime = time(NULL);
1172 localtime_r(&ttime, &tm);
1173 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1174 bsendmsg(ua, "%s\n", sdt);
1181 * Delete Pool records (should purge Media with it).
1183 * delete pool=<pool-name>
1184 * delete media pool=<pool-name> volume=<name>
1186 static int delete_cmd(UAContext *ua, char *cmd)
1188 static char *keywords[] = {
1198 "In general it is not a good idea to delete either a\n"
1199 "Pool or a Volume since they may contain data.\n\n"));
1201 switch (find_arg_keyword(ua, keywords)) {
1211 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1219 bsendmsg(ua, _("Nothing done.\n"));
1226 * Delete media records from database -- dangerous
1228 static int delete_volume(UAContext *ua)
1232 if (!select_media_dbr(ua, &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_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1242 if (ua->pint32_val) {
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_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1263 if (ua->pint32_val) {
1264 db_delete_pool_record(ua->jcr, ua->db, &pr);
1270 static void do_mount_cmd(UAContext *ua, char *command)
1274 char dev_name[MAX_NAME_LENGTH];
1280 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1282 store = get_storage_resource(ua, 1);
1287 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1288 store->media_type, store->dev_name);
1290 ua->jcr->store = store;
1291 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1292 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1295 sd = ua->jcr->store_bsock;
1296 strcpy(dev_name, store->dev_name);
1297 bash_spaces(dev_name);
1298 bnet_fsend(sd, "%s %s", command, dev_name);
1299 while (bnet_recv(sd) >= 0) {
1300 bsendmsg(ua, "%s", sd->msg);
1302 bnet_sig(sd, BNET_TERMINATE);
1304 ua->jcr->store_bsock = NULL;
1308 * mount [storage | device] <name>
1310 static int mount_cmd(UAContext *ua, char *cmd)
1312 do_mount_cmd(ua, "mount"); /* mount */
1318 * unmount [storage | device] <name>
1320 static int unmount_cmd(UAContext *ua, char *cmd)
1322 do_mount_cmd(ua, "unmount"); /* unmount */
1328 * release [storage | device] <name>
1330 static int release_cmd(UAContext *ua, char *cmd)
1332 do_mount_cmd(ua, "release"); /* release */
1339 * use catalog=<name>
1341 static int use_cmd(UAContext *ua, char *cmd)
1343 CAT *oldcatalog, *catalog;
1346 close_db(ua); /* close any previously open db */
1347 oldcatalog = ua->catalog;
1349 if (!(catalog = get_catalog_resource(ua))) {
1350 ua->catalog = oldcatalog;
1352 ua->catalog = catalog;
1355 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1356 ua->catalog->hdr.name, ua->catalog->db_name);
1361 int quit_cmd(UAContext *ua, char *cmd)
1368 * Wait until no job is running
1370 int wait_cmd(UAContext *ua, char *cmd)
1372 bmicrosleep(0, 200000); /* let job actually start */
1373 for (int running=1; running; ) {
1376 for (JCR *jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
1377 if (jcr->JobId != 0) {
1379 free_locked_jcr(jcr);
1382 free_locked_jcr(jcr);
1393 static int help_cmd(UAContext *ua, char *cmd)
1398 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1399 for (i=0; i<comsize; i++) {
1400 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1406 static int version_cmd(UAContext *ua, char *cmd)
1408 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1413 /* A bit brain damaged in that if the user has not done
1414 * a "use catalog xxx" command, we simply find the first
1415 * catalog resource and open it.
1417 int open_db(UAContext *ua)
1424 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1427 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1430 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1431 ua->catalog->hdr.name, ua->catalog->db_name);
1435 Dmsg0(150, "Open database\n");
1436 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1437 ua->catalog->db_password, ua->catalog->db_address,
1438 ua->catalog->db_port, ua->catalog->db_socket);
1439 if (!db_open_database(ua->jcr, ua->db)) {
1440 bsendmsg(ua, _("Could not open DB %s: ERR=%s"),
1441 ua->catalog->db_name, db_strerror(ua->db));
1445 ua->jcr->db = ua->db;
1446 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1450 void close_db(UAContext *ua)
1453 db_close_database(ua->jcr, ua->db);