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 add_cmd(UAContext *ua, char *cmd), createcmd(UAContext *ua, char *cmd), cancelcmd(UAContext *ua, char *cmd);
67 static int setdebug_cmd(UAContext *ua, char *cmd);
68 static int help_cmd(UAContext *ua, char *cmd);
69 static int delete_cmd(UAContext *ua, char *cmd);
70 static int use_cmd(UAContext *ua, char *cmd), unmount_cmd(UAContext *ua, char *cmd);
71 static int version_cmd(UAContext *ua, char *cmd), automount_cmd(UAContext *ua, char *cmd);
72 static int time_cmd(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 mount_cmd(UAContext *ua, char *cmd);
78 static int release_cmd(UAContext *ua, char *cmd);
79 static int update_cmd(UAContext *ua, char *cmd);
80 static int wait_cmd(UAContext *ua, char *cmd);
82 int quit_cmd(UAContext *ua, char *cmd);
85 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
86 static struct cmdstruct commands[] = {
87 { N_("add"), add_cmd, _("add media to a pool")},
88 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
89 { N_("automount"), automount_cmd, _("automount [on/off] -- after label")},
90 { N_("cancel"), cancelcmd, _("cancel job=nnn -- cancel a job")},
91 { N_("create"), createcmd, _("create DB Pool from resource")},
92 { N_("delete"), delete_cmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
93 { N_("help"), help_cmd, _("print this command")},
94 { N_("label"), labelcmd, _("label a tape")},
95 { N_("relabel"), relabelcmd, _("relabel a tape")},
96 { N_("list"), listcmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
97 { N_("llist"), llistcmd, _("full or long list like list command")},
98 { N_("messages"), messagescmd, _("messages")},
99 { N_("mount"), mount_cmd, _("mount <storage-name>")},
100 { N_("restore"), restorecmd, _("restore files")},
101 { N_("prune"), prunecmd, _("prune expired records from catalog")},
102 { N_("purge"), purgecmd, _("purge records from catalog")},
103 { N_("run"), runcmd, _("run <job-name>")},
104 { N_("setdebug"), setdebug_cmd, _("sets debug level")},
105 { N_("show"), showcmd, _("show (resource records) [jobs | pools | ... | all]")},
106 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
107 { N_("status"), statuscmd, _("status [storage | client]=<name>")},
108 { N_("unmount"), unmount_cmd, _("unmount <storage-name>")},
109 { N_("update"), update_cmd, _("update Volume or Pool")},
110 { N_("use"), use_cmd, _("use catalog xxx")},
111 { N_("version"), version_cmd, _("print Director version")},
112 { N_("quit"), quit_cmd, _("quit")},
113 { N_("query"), querycmd, _("query catalog")},
114 { N_("release"), release_cmd, _("release <storage-name>")},
115 { N_("time"), time_cmd, _("print current time")},
116 { N_("exit"), quit_cmd, _("exit = quit")},
117 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
119 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
122 * Execute a command from the UA
124 int do_a_command(UAContext *ua, char *cmd)
133 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
138 len = strlen(ua->argk[0]);
139 for (i=0; i<comsize; i++) { /* search for command */
140 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
141 stat = (*commands[i].func)(ua, cmd); /* go execute command */
147 pm_strcat(&ua->UA_sock->msg, _(": is an illegal command\n"));
148 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
149 bnet_send(ua->UA_sock);
155 * This is a common routine used to stuff the Pool DB record defaults
156 * into the Media DB record just before creating a media (Volume)
159 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
161 mr->PoolId = pr->PoolId;
162 strcpy(mr->VolStatus, "Append");
163 mr->Recycle = pr->Recycle;
164 mr->VolRetention = pr->VolRetention;
165 mr->VolUseDuration = pr->VolUseDuration;
166 mr->MaxVolJobs = pr->MaxVolJobs;
167 mr->MaxVolFiles = pr->MaxVolFiles;
168 mr->MaxVolBytes = pr->MaxVolBytes;
173 * Add Volumes to an existing Pool
175 static int add_cmd(UAContext *ua, char *cmd)
179 int num, i, max, startnum;
181 char name[MAX_NAME_LENGTH];
186 "You probably don't want to be using this command since it\n"
187 "creates database records without labeling the Volumes.\n"
188 "You probably want to use the \"label\" command.\n\n"));
194 memset(&pr, 0, sizeof(pr));
195 memset(&mr, 0, sizeof(mr));
197 if (!get_pool_dbr(ua, &pr)) {
201 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
202 pr.MaxVols, pr.PoolType);
204 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
205 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
207 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
210 pr.MaxVols = ua->pint32_val;
215 if ((store = get_storage_resource(ua, 0)) != NULL) {
216 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
217 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
221 if (pr.MaxVols == 0) {
224 max = pr.MaxVols - pr.NumVols;
228 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
229 if (!get_pint(ua, buf)) {
232 num = ua->pint32_val;
233 if (num < 0 || num > max) {
234 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
241 if (!get_cmd(ua, _("Enter Volume name: "))) {
245 if (!get_cmd(ua, _("Enter base volume name: "))) {
249 /* Don't allow | in Volume name because it is the volume separator character */
250 if (!is_volume_name_legal(ua, ua->cmd)) {
253 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
254 bsendmsg(ua, _("Volume name too long.\n"));
257 if (strlen(ua->cmd) == 0) {
258 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
262 bstrncpy(name, ua->cmd, sizeof(name));
264 strcat(name, "%04d");
267 if (!get_pint(ua, _("Enter the starting number: "))) {
270 startnum = ua->pint32_val;
272 bsendmsg(ua, _("Start number must be greater than zero.\n"));
282 if (store && store->autochanger) {
283 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
286 slot = ua->pint32_val;
289 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
290 for (i=startnum; i < num+startnum; i++) {
291 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
293 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
294 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
295 bsendmsg(ua, db_strerror(ua->db));
299 first_id = mr.PoolId;
303 Dmsg0(200, "Update pool record.\n");
304 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
305 bsendmsg(ua, db_strerror(ua->db));
308 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
314 * Turn auto mount on/off
319 int automount_cmd(UAContext *ua, char *cmd)
324 if (!get_cmd(ua, _("Turn on or off? "))) {
332 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
340 static int cancelcmd(UAContext *ua, char *cmd)
346 char JobName[MAX_NAME_LENGTH];
352 for (i=1; i<ua->argc; i++) {
353 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
357 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
358 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
362 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
366 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
367 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
373 /* If we still do not have a jcr,
374 * throw up a list and ask the user to select one.
377 /* Count Jobs running */
379 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
380 if (jcr->JobId == 0) { /* this is us */
381 free_locked_jcr(jcr);
385 free_locked_jcr(jcr);
390 bsendmsg(ua, _("No Jobs running.\n"));
393 start_prompt(ua, _("Select Job:\n"));
395 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
396 if (jcr->JobId == 0) { /* this is us */
397 free_locked_jcr(jcr);
400 add_prompt(ua, jcr->Job);
401 free_locked_jcr(jcr);
405 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
409 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
413 jcr = get_jcr_by_full_name(JobName);
415 bsendmsg(ua, _("Job %s not found.\n"), JobName);
420 switch (jcr->JobStatus) {
423 case JS_WaitClientRes:
424 case JS_WaitStoreRes:
426 case JS_WaitStartTime:
427 set_jcr_job_status(jcr, JS_Canceled);
428 bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
429 jcr->JobId, jcr->Job);
430 #ifndef USE_SEMAPHORE
431 workq_remove(&job_wq, jcr->work_item); /* attempt to remove it from queue */
437 set_jcr_job_status(jcr, JS_Canceled);
439 /* Cancel File daemon */
440 if (jcr->file_bsock) {
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;
458 /* Cancel Storage daemon */
459 if (jcr->store_bsock) {
460 ua->jcr->store = jcr->store;
461 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
462 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
466 Dmsg0(200, "Connected to storage daemon\n");
467 sd = ua->jcr->store_bsock;
468 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
469 while (bnet_recv(sd) >= 0) {
470 bsendmsg(ua, "%s", sd->msg);
472 bnet_sig(sd, BNET_TERMINATE);
474 ua->jcr->store_bsock = NULL;
483 * This is a common routine to create or update a
484 * Pool DB base record from a Pool Resource. We handle
485 * the setting of MaxVols and NumVols slightly differently
486 * depending on if we are creating the Pool or we are
487 * simply bringing it into agreement with the resource (updage).
489 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
491 strcpy(pr->PoolType, pool->pool_type);
492 if (op == POOL_OP_CREATE) {
493 pr->MaxVols = pool->max_volumes;
495 } else { /* update pool */
496 if (pr->MaxVols != pool->max_volumes) {
497 pr->MaxVols = pool->max_volumes;
499 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
500 pr->MaxVols = pr->NumVols;
503 pr->UseOnce = pool->use_volume_once;
504 pr->UseCatalog = pool->use_catalog;
505 pr->AcceptAnyVolume = pool->accept_any_volume;
506 pr->Recycle = pool->Recycle;
507 pr->VolRetention = pool->VolRetention;
508 pr->VolUseDuration = pool->VolUseDuration;
509 pr->MaxVolJobs = pool->MaxVolJobs;
510 pr->MaxVolFiles = pool->MaxVolFiles;
511 pr->MaxVolBytes = pool->MaxVolBytes;
512 pr->AutoPrune = pool->AutoPrune;
513 pr->Recycle = pool->Recycle;
514 if (pool->label_format) {
515 strcpy(pr->LabelFormat, pool->label_format);
517 strcpy(pr->LabelFormat, "*"); /* none */
523 * Create a pool record from a given Pool resource
524 * Also called from backup.c
525 * Returns: -1 on error
526 * 0 record already exists
530 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
534 memset(&pr, 0, sizeof(POOL_DBR));
536 strcpy(pr.Name, pool->hdr.name);
538 if (db_get_pool_record(jcr, db, &pr)) {
540 if (op == POOL_OP_UPDATE) { /* update request */
541 set_pooldbr_from_poolres(&pr, pool, op);
542 db_update_pool_record(jcr, db, &pr);
544 return 0; /* exists */
547 set_pooldbr_from_poolres(&pr, pool, op);
549 if (!db_create_pool_record(jcr, db, &pr)) {
550 return -1; /* error */
558 * Create a Pool Record in the database.
559 * It is always created from the Resource record.
561 static int createcmd(UAContext *ua, char *cmd)
569 pool = get_pool_resource(ua);
574 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
576 bsendmsg(ua, _("Error: Pool %s already exists.\n\
577 Use update to change it.\n"), pool->hdr.name);
581 bsendmsg(ua, db_strerror(ua->db));
587 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
595 * Update a Pool Record in the database.
596 * It is always updated from the Resource record.
598 * update pool=<pool-name>
599 * updates pool from Pool resource
600 * update media pool=<pool-name> volume=<volume-name>
601 * changes pool info for volume
603 static int update_cmd(UAContext *ua, char *cmd)
605 static char *kw[] = {
607 N_("volume"), /* 1 */
616 switch (find_arg_keyword(ua, kw)) {
631 start_prompt(ua, _("Update choice:\n"));
632 add_prompt(ua, _("Volume parameters"));
633 add_prompt(ua, _("Pool from resource"));
634 add_prompt(ua, _("Slots from autochanger"));
635 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
652 * Update a media record -- allows you to change the
653 * Volume status. E.g. if you want Bacula to stop
654 * writing on the volume, set it to anything other
657 static int update_volume(UAContext *ua)
664 for (int done=0; !done; ) {
665 if (!select_media_dbr(ua, &mr)) {
668 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
669 start_prompt(ua, _("Parameters to modify:\n"));
670 add_prompt(ua, _("Volume Status"));
671 add_prompt(ua, _("Volume Retention Period"));
672 add_prompt(ua, _("Volume Use Duration"));
673 add_prompt(ua, _("Maximum Volume Jobs"));
674 add_prompt(ua, _("Maximum Volume Files"));
675 add_prompt(ua, _("Maximum Volume Bytes"));
676 add_prompt(ua, _("Recycle Flag"));
677 add_prompt(ua, _("Slot"));
678 add_prompt(ua, _("Volume Files"));
679 add_prompt(ua, _("Done"));
680 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
681 case 0: /* Volume Status */
682 /* Modify Volume Status */
683 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
684 start_prompt(ua, _("Possible Values are:\n"));
685 add_prompt(ua, "Append"); /* Better not translate these as */
686 add_prompt(ua, "Archive"); /* They are known in the database code */
687 add_prompt(ua, "Disabled");
688 add_prompt(ua, "Full");
689 add_prompt(ua, "Used");
690 add_prompt(ua, "Cleaning");
691 if (strcmp(mr.VolStatus, "Purged") == 0) {
692 add_prompt(ua, "Recycle");
694 add_prompt(ua, "Read-Only");
695 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
698 bstrncpy(mr.VolStatus, ua->cmd, sizeof(mr.VolStatus));
699 query = get_pool_memory(PM_MESSAGE);
700 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
701 mr.VolStatus, mr.MediaId);
702 if (!db_sql_query(ua->db, query, NULL, NULL)) {
703 bsendmsg(ua, "%s", db_strerror(ua->db));
705 bsendmsg(ua, _("New Volume status is: %s\n"), mr.VolStatus);
707 free_pool_memory(query);
709 case 1: /* Retention */
710 bsendmsg(ua, _("Current retention seconds is: %s\n"),
711 edit_utime(mr.VolRetention, ed1));
712 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
715 if (!duration_to_utime(ua->cmd, &mr.VolRetention)) {
716 bsendmsg(ua, _("Invalid retention period specified.\n"));
719 query = get_pool_memory(PM_MESSAGE);
720 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
721 edit_uint64(mr.VolRetention, ed1), mr.MediaId);
722 if (!db_sql_query(ua->db, query, NULL, NULL)) {
723 bsendmsg(ua, "%s", db_strerror(ua->db));
725 bsendmsg(ua, _("New retention seconds is: %s\n"),
726 edit_utime(mr.VolRetention, ed1));
728 free_pool_memory(query);
731 case 2: /* Use Duration */
732 bsendmsg(ua, _("Current use duration is: %s\n"),
733 edit_utime(mr.VolUseDuration, ed1));
734 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
737 if (!duration_to_utime(ua->cmd, &mr.VolUseDuration)) {
738 bsendmsg(ua, _("Invalid use duration specified.\n"));
741 query = get_pool_memory(PM_MESSAGE);
742 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
743 edit_uint64(mr.VolUseDuration, ed1), mr.MediaId);
744 if (!db_sql_query(ua->db, query, NULL, NULL)) {
745 bsendmsg(ua, "%s", db_strerror(ua->db));
747 bsendmsg(ua, _("New use duration is: %s\n"),
748 edit_utime(mr.VolUseDuration, ed1));
750 free_pool_memory(query);
753 case 3: /* Max Jobs */
755 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
756 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
759 maxjobs = ua->pint32_val;
760 query = get_pool_memory(PM_MESSAGE);
761 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%u WHERE MediaId=%u",
762 maxjobs, mr.MediaId);
763 if (!db_sql_query(ua->db, query, NULL, NULL)) {
764 bsendmsg(ua, "%s", db_strerror(ua->db));
766 bsendmsg(ua, _("New max jobs is: %u\n"), maxjobs);
768 free_pool_memory(query);
771 case 4: /* Max Files */
773 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
774 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
777 maxfiles = ua->pint32_val;
778 query = get_pool_memory(PM_MESSAGE);
779 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%u WHERE MediaId=%u",
780 maxfiles, mr.MediaId);
781 if (!db_sql_query(ua->db, query, NULL, NULL)) {
782 bsendmsg(ua, "%s", db_strerror(ua->db));
784 bsendmsg(ua, _("New max files is: %u\n"), maxfiles);
786 free_pool_memory(query);
789 case 5: /* Max Bytes */
791 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
792 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
795 if (!size_to_uint64(ua->cmd, strlen(ua->cmd), &maxbytes)) {
796 bsendmsg(ua, _("Invalid byte size specification.\n"));
799 query = get_pool_memory(PM_MESSAGE);
800 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
801 edit_uint64(maxbytes, ed1), mr.MediaId);
802 if (!db_sql_query(ua->db, query, NULL, NULL)) {
803 bsendmsg(ua, "%s", db_strerror(ua->db));
805 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
807 free_pool_memory(query);
811 case 6: /* Recycle */
813 bsendmsg(ua, _("Current recycle flag is: %s\n"),
814 mr.Recycle==1?_("yes"):_("no"));
815 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
818 recycle = ua->pint32_val;
819 query = get_pool_memory(PM_MESSAGE);
820 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
821 recycle, mr.MediaId);
822 if (!db_sql_query(ua->db, query, NULL, NULL)) {
823 bsendmsg(ua, "%s", db_strerror(ua->db));
825 bsendmsg(ua, _("New recycle flag is: %s\n"),
826 mr.Recycle==1?_("yes"):_("no"));
828 free_pool_memory(query);
835 memset(&pr, 0, sizeof(POOL_DBR));
836 pr.PoolId = mr.PoolId;
837 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
838 bsendmsg(ua, "%s", db_strerror(ua->db));
841 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
842 if (!get_pint(ua, _("Enter new Slot: "))) {
845 slot = ua->pint32_val;
846 if (pr.MaxVols > 0 && slot > (int)pr.MaxVols) {
847 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
851 query = get_pool_memory(PM_MESSAGE);
852 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
854 if (!db_sql_query(ua->db, query, NULL, NULL)) {
855 bsendmsg(ua, "%s", db_strerror(ua->db));
857 bsendmsg(ua, "New Slot is: %d\n", slot);
859 free_pool_memory(query);
862 case 8: /* Volume Files */
864 bsendmsg(ua, _("Warning changing Volume Files can result\n"
865 "in loss of data on your Volume\n\n"));
866 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
867 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
870 VolFiles = ua->pint32_val;
871 if (VolFiles != (int)(mr.VolFiles + 1)) {
872 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
873 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
877 query = get_pool_memory(PM_MESSAGE);
878 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
879 VolFiles, mr.MediaId);
880 if (!db_sql_query(ua->db, query, NULL, NULL)) {
881 bsendmsg(ua, "%s", db_strerror(ua->db));
883 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
885 free_pool_memory(query);
888 default: /* Done or error */
889 bsendmsg(ua, "Selection done.\n");
897 * Update pool record -- pull info from current POOL resource
899 static int update_pool(UAContext *ua)
907 pool = get_pool_resource(ua);
912 memset(&pr, 0, sizeof(pr));
913 strcpy(pr.Name, pool->hdr.name);
914 if (!get_pool_dbr(ua, &pr)) {
918 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
920 id = db_update_pool_record(ua->jcr, ua->db, &pr);
922 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
923 id, db_strerror(ua->db));
925 query = get_pool_memory(PM_MESSAGE);
926 Mmsg(&query, list_pool, pr.PoolId);
927 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
928 free_pool_memory(query);
929 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
934 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
938 ua->jcr->store = store;
939 /* Try connecting for up to 15 seconds */
940 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
941 store->hdr.name, store->address, store->SDport);
942 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
943 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
946 Dmsg0(120, _("Connected to storage daemon\n"));
947 sd = ua->jcr->store_bsock;
948 bnet_fsend(sd, "setdebug=%d\n", level);
949 if (bnet_recv(sd) >= 0) {
950 bsendmsg(ua, "%s", sd->msg);
952 bnet_sig(sd, BNET_TERMINATE);
954 ua->jcr->store_bsock = NULL;
958 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
962 /* Connect to File daemon */
964 ua->jcr->client = client;
965 /* Try to connect for 15 seconds */
966 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
967 client->hdr.name, client->address, client->FDport);
968 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
969 bsendmsg(ua, _("Failed to connect to Client.\n"));
972 Dmsg0(120, "Connected to file daemon\n");
973 fd = ua->jcr->file_bsock;
974 bnet_fsend(fd, "setdebug=%d\n", level);
975 if (bnet_recv(fd) >= 0) {
976 bsendmsg(ua, "%s", fd->msg);
978 bnet_sig(fd, BNET_TERMINATE);
980 ua->jcr->file_bsock = NULL;
986 static void do_all_setdebug(UAContext *ua, int level)
988 STORE *store, **unique_store;
989 CLIENT *client, **unique_client;
995 /* Count Storage items */
998 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1000 unique_store = (STORE **) malloc(i * sizeof(STORE));
1001 /* Find Unique Storage address/port */
1002 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1004 unique_store[i++] = store;
1005 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1007 for (j=0; j<i; j++) {
1008 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1009 unique_store[j]->SDport == store->SDport) {
1015 unique_store[i++] = store;
1016 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1021 /* Call each unique Storage daemon */
1022 for (j=0; j<i; j++) {
1023 do_storage_setdebug(ua, unique_store[j], level);
1027 /* Count Client items */
1030 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1032 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1033 /* Find Unique Client address/port */
1034 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1036 unique_client[i++] = client;
1037 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1039 for (j=0; j<i; j++) {
1040 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1041 unique_client[j]->FDport == client->FDport) {
1047 unique_client[i++] = client;
1048 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1053 /* Call each unique File daemon */
1054 for (j=0; j<i; j++) {
1055 do_client_setdebug(ua, unique_client[j], level);
1057 free(unique_client);
1061 * setdebug level=nn all
1063 static int setdebug_cmd(UAContext *ua, char *cmd)
1073 Dmsg1(120, "setdebug:%s:\n", cmd);
1076 i = find_arg_with_value(ua, _("level"));
1078 level = atoi(ua->argv[i]);
1081 if (!get_pint(ua, _("Enter new debug level: "))) {
1084 level = ua->pint32_val;
1087 /* General debug? */
1088 for (i=1; i<ua->argc; i++) {
1089 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1090 do_all_setdebug(ua, level);
1093 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1094 strcasecmp(ua->argk[i], _("director")) == 0) {
1095 debug_level = level;
1098 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1101 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1103 do_client_setdebug(ua, client, level);
1107 client = select_client_resource(ua);
1109 do_client_setdebug(ua, client, level);
1114 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1115 strcasecmp(ua->argk[i], _("storage")) == 0) {
1118 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1120 do_storage_setdebug(ua, store, level);
1124 store = get_storage_resource(ua, 0);
1126 do_storage_setdebug(ua, store, level);
1132 * We didn't find an appropriate keyword above, so
1135 start_prompt(ua, _("Available daemons are: \n"));
1136 add_prompt(ua, _("Director"));
1137 add_prompt(ua, _("Storage"));
1138 add_prompt(ua, _("Client"));
1139 add_prompt(ua, _("All"));
1140 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1141 case 0: /* Director */
1142 debug_level = level;
1145 store = get_storage_resource(ua, 0);
1147 do_storage_setdebug(ua, store, level);
1151 client = select_client_resource(ua);
1153 do_client_setdebug(ua, client, level);
1157 do_all_setdebug(ua, level);
1168 static int time_cmd(UAContext *ua, char *cmd)
1171 time_t ttime = time(NULL);
1173 localtime_r(&ttime, &tm);
1174 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1175 bsendmsg(ua, "%s\n", sdt);
1182 * Delete Pool records (should purge Media with it).
1184 * delete pool=<pool-name>
1185 * delete media pool=<pool-name> volume=<name>
1187 static int delete_cmd(UAContext *ua, char *cmd)
1189 static char *keywords[] = {
1199 "In general it is not a good idea to delete either a\n"
1200 "Pool or a Volume since they may contain data.\n\n"));
1202 switch (find_arg_keyword(ua, keywords)) {
1212 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1220 bsendmsg(ua, _("Nothing done.\n"));
1227 * Delete media records from database -- dangerous
1229 static int delete_volume(UAContext *ua)
1233 if (!select_media_dbr(ua, &mr)) {
1236 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1237 "and all Jobs saved on that volume from the Catalog\n"),
1240 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1243 if (ua->pint32_val) {
1244 db_delete_media_record(ua->jcr, ua->db, &mr);
1250 * Delete a pool record from the database -- dangerous
1252 static int delete_pool(UAContext *ua)
1256 memset(&pr, 0, sizeof(pr));
1258 if (!get_pool_dbr(ua, &pr)) {
1261 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1264 if (ua->pint32_val) {
1265 db_delete_pool_record(ua->jcr, ua->db, &pr);
1271 static void do_mount_cmd(UAContext *ua, char *command)
1275 char dev_name[MAX_NAME_LENGTH];
1281 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1283 store = get_storage_resource(ua, 1);
1288 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1289 store->media_type, store->dev_name);
1291 ua->jcr->store = store;
1292 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1293 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1296 sd = ua->jcr->store_bsock;
1297 strcpy(dev_name, store->dev_name);
1298 bash_spaces(dev_name);
1299 bnet_fsend(sd, "%s %s", command, dev_name);
1300 while (bnet_recv(sd) >= 0) {
1301 bsendmsg(ua, "%s", sd->msg);
1303 bnet_sig(sd, BNET_TERMINATE);
1305 ua->jcr->store_bsock = NULL;
1309 * mount [storage | device] <name>
1311 static int mount_cmd(UAContext *ua, char *cmd)
1313 do_mount_cmd(ua, "mount"); /* mount */
1319 * unmount [storage | device] <name>
1321 static int unmount_cmd(UAContext *ua, char *cmd)
1323 do_mount_cmd(ua, "unmount"); /* unmount */
1329 * release [storage | device] <name>
1331 static int release_cmd(UAContext *ua, char *cmd)
1333 do_mount_cmd(ua, "release"); /* release */
1340 * use catalog=<name>
1342 static int use_cmd(UAContext *ua, char *cmd)
1344 CAT *oldcatalog, *catalog;
1347 close_db(ua); /* close any previously open db */
1348 oldcatalog = ua->catalog;
1350 if (!(catalog = get_catalog_resource(ua))) {
1351 ua->catalog = oldcatalog;
1353 ua->catalog = catalog;
1356 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1357 ua->catalog->hdr.name, ua->catalog->db_name);
1362 int quit_cmd(UAContext *ua, char *cmd)
1369 * Wait until no job is running
1371 int wait_cmd(UAContext *ua, char *cmd)
1373 bmicrosleep(0, 200000); /* let job actually start */
1374 for (int running=1; running; ) {
1377 for (JCR *jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
1378 if (jcr->JobId != 0) {
1380 free_locked_jcr(jcr);
1383 free_locked_jcr(jcr);
1394 static int help_cmd(UAContext *ua, char *cmd)
1399 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1400 for (i=0; i<comsize; i++) {
1401 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1407 static int version_cmd(UAContext *ua, char *cmd)
1409 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1414 /* A bit brain damaged in that if the user has not done
1415 * a "use catalog xxx" command, we simply find the first
1416 * catalog resource and open it.
1418 int open_db(UAContext *ua)
1425 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1428 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1431 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1432 ua->catalog->hdr.name, ua->catalog->db_name);
1436 Dmsg0(150, "Open database\n");
1437 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1438 ua->catalog->db_password, ua->catalog->db_address,
1439 ua->catalog->db_port, ua->catalog->db_socket);
1440 if (!db_open_database(ua->jcr, ua->db)) {
1441 bsendmsg(ua, _("Could not open DB %s: ERR=%s"),
1442 ua->catalog->db_name, db_strerror(ua->db));
1446 ua->jcr->db = ua->db;
1447 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1451 void close_db(UAContext *ua)
1454 db_close_database(ua->jcr, ua->db);