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[];
42 extern jobq_t job_queue; /* job queue */
44 extern workq_t job_wq; /* work queue */
48 extern char *list_pool;
50 /* Imported functions */
51 extern int statuscmd(UAContext *ua, char *cmd);
52 extern int listcmd(UAContext *ua, char *cmd);
53 extern int llistcmd(UAContext *ua, char *cmd);
54 extern int show_cmd(UAContext *ua, char *cmd);
55 extern int messagescmd(UAContext *ua, char *cmd);
56 extern int autodisplaycmd(UAContext *ua, char *cmd);
57 extern int sqlquerycmd(UAContext *ua, char *cmd);
58 extern int querycmd(UAContext *ua, char *cmd);
59 extern int runcmd(UAContext *ua, char *cmd);
60 extern int retentioncmd(UAContext *ua, char *cmd);
61 extern int prunecmd(UAContext *ua, char *cmd);
62 extern int purgecmd(UAContext *ua, char *cmd);
63 extern int restorecmd(UAContext *ua, char *cmd);
64 extern int label_cmd(UAContext *ua, char *cmd);
65 extern int relabel_cmd(UAContext *ua, char *cmd);
66 extern int update_slots(UAContext *ua); /* ua_label.c */
68 /* Forward referenced functions */
69 static int add_cmd(UAContext *ua, char *cmd), createcmd(UAContext *ua, char *cmd), cancelcmd(UAContext *ua, char *cmd);
70 static int setdebug_cmd(UAContext *ua, char *cmd);
71 static int var_cmd(UAContext *ua, char *cmd);
72 static int estimate_cmd(UAContext *ua, char *cmd);
73 static int help_cmd(UAContext *ua, char *cmd);
74 static int delete_cmd(UAContext *ua, char *cmd);
75 static int use_cmd(UAContext *ua, char *cmd), unmount_cmd(UAContext *ua, char *cmd);
76 static int version_cmd(UAContext *ua, char *cmd), automount_cmd(UAContext *ua, char *cmd);
77 static int time_cmd(UAContext *ua, char *cmd);
78 static int update_volume(UAContext *ua);
79 static int update_pool(UAContext *ua);
80 static int delete_volume(UAContext *ua);
81 static int delete_pool(UAContext *ua);
82 static int mount_cmd(UAContext *ua, char *cmd);
83 static int release_cmd(UAContext *ua, char *cmd);
84 static int update_cmd(UAContext *ua, char *cmd);
85 static int wait_cmd(UAContext *ua, char *cmd);
87 int quit_cmd(UAContext *ua, char *cmd);
90 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
91 static struct cmdstruct commands[] = {
92 { N_("add"), add_cmd, _("add media to a pool")},
93 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
94 { N_("automount"), automount_cmd, _("automount [on/off] -- after label")},
95 { N_("cancel"), cancelcmd, _("cancel job=nnn -- cancel a job")},
96 { N_("create"), createcmd, _("create DB Pool from resource")},
97 { N_("delete"), delete_cmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
98 { N_("estimate"), estimate_cmd, _("performs FileSet estimate debug=1 give full listing")},
99 { N_("exit"), quit_cmd, _("exit = quit")},
100 { N_("help"), help_cmd, _("print this command")},
101 { N_("label"), label_cmd, _("label a tape")},
102 { N_("list"), listcmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
103 { N_("llist"), llistcmd, _("full or long list like list command")},
104 { N_("messages"), messagescmd, _("messages")},
105 { N_("mount"), mount_cmd, _("mount <storage-name>")},
106 { N_("prune"), prunecmd, _("prune expired records from catalog")},
107 { N_("purge"), purgecmd, _("purge records from catalog")},
108 { N_("query"), querycmd, _("query catalog")},
109 { N_("quit"), quit_cmd, _("quit")},
110 { N_("relabel"), relabel_cmd, _("relabel a tape")},
111 { N_("release"), release_cmd, _("release <storage-name>")},
112 { N_("restore"), restorecmd, _("restore files")},
113 { N_("run"), runcmd, _("run <job-name>")},
114 { N_("setdebug"), setdebug_cmd, _("sets debug level")},
115 { N_("show"), show_cmd, _("show (resource records) [jobs | pools | ... | all]")},
116 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
117 { N_("status"), statuscmd, _("status [storage | client]=<name>")},
118 { N_("time"), time_cmd, _("print current time")},
119 { N_("unmount"), unmount_cmd, _("unmount <storage-name>")},
120 { N_("update"), update_cmd, _("update Volume or Pool")},
121 { N_("use"), use_cmd, _("use catalog xxx")},
122 { N_("var"), var_cmd, _("does variable expansion")},
123 { N_("version"), version_cmd, _("print Director version")},
124 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
126 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
129 * Execute a command from the UA
131 int do_a_command(UAContext *ua, char *cmd)
140 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
145 len = strlen(ua->argk[0]);
146 for (i=0; i<comsize; i++) { /* search for command */
147 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
148 stat = (*commands[i].func)(ua, cmd); /* go execute command */
154 pm_strcat(&ua->UA_sock->msg, _(": is an illegal command\n"));
155 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
156 bnet_send(ua->UA_sock);
162 * This is a common routine used to stuff the Pool DB record defaults
163 * into the Media DB record just before creating a media (Volume)
166 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
168 mr->PoolId = pr->PoolId;
169 strcpy(mr->VolStatus, "Append");
170 mr->Recycle = pr->Recycle;
171 mr->VolRetention = pr->VolRetention;
172 mr->VolUseDuration = pr->VolUseDuration;
173 mr->MaxVolJobs = pr->MaxVolJobs;
174 mr->MaxVolFiles = pr->MaxVolFiles;
175 mr->MaxVolBytes = pr->MaxVolBytes;
180 * Add Volumes to an existing Pool
182 static int add_cmd(UAContext *ua, char *cmd)
186 int num, i, max, startnum;
188 char name[MAX_NAME_LENGTH];
193 "You probably don't want to be using this command since it\n"
194 "creates database records without labeling the Volumes.\n"
195 "You probably want to use the \"label\" command.\n\n"));
201 memset(&pr, 0, sizeof(pr));
202 memset(&mr, 0, sizeof(mr));
204 if (!get_pool_dbr(ua, &pr)) {
208 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
209 pr.MaxVols, pr.PoolType);
211 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
212 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
214 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
217 pr.MaxVols = ua->pint32_val;
222 if ((store = get_storage_resource(ua, 0)) != NULL) {
223 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
224 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
228 if (pr.MaxVols == 0) {
231 max = pr.MaxVols - pr.NumVols;
235 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
236 if (!get_pint(ua, buf)) {
239 num = ua->pint32_val;
240 if (num < 0 || num > max) {
241 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
248 if (!get_cmd(ua, _("Enter Volume name: "))) {
252 if (!get_cmd(ua, _("Enter base volume name: "))) {
256 /* Don't allow | in Volume name because it is the volume separator character */
257 if (!is_volume_name_legal(ua, ua->cmd)) {
260 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
261 bsendmsg(ua, _("Volume name too long.\n"));
264 if (strlen(ua->cmd) == 0) {
265 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
269 bstrncpy(name, ua->cmd, sizeof(name));
271 strcat(name, "%04d");
274 if (!get_pint(ua, _("Enter the starting number: "))) {
277 startnum = ua->pint32_val;
279 bsendmsg(ua, _("Start number must be greater than zero.\n"));
289 if (store && store->autochanger) {
290 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
293 slot = ua->pint32_val;
296 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
297 for (i=startnum; i < num+startnum; i++) {
298 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
300 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
301 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
302 bsendmsg(ua, db_strerror(ua->db));
306 first_id = mr.PoolId;
310 Dmsg0(200, "Update pool record.\n");
311 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
312 bsendmsg(ua, db_strerror(ua->db));
315 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
321 * Turn auto mount on/off
326 int automount_cmd(UAContext *ua, char *cmd)
331 if (!get_cmd(ua, _("Turn on or off? "))) {
339 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
347 static int cancelcmd(UAContext *ua, char *cmd)
353 char JobName[MAX_NAME_LENGTH];
359 for (i=1; i<ua->argc; i++) {
360 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
364 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
365 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
369 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
373 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
374 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
380 /* If we still do not have a jcr,
381 * throw up a list and ask the user to select one.
384 /* Count Jobs running */
386 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
387 if (jcr->JobId == 0) { /* this is us */
388 free_locked_jcr(jcr);
392 free_locked_jcr(jcr);
397 bsendmsg(ua, _("No Jobs running.\n"));
400 start_prompt(ua, _("Select Job:\n"));
402 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
403 if (jcr->JobId == 0) { /* this is us */
404 free_locked_jcr(jcr);
407 add_prompt(ua, jcr->Job);
408 free_locked_jcr(jcr);
412 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
416 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
420 jcr = get_jcr_by_full_name(JobName);
422 bsendmsg(ua, _("Job %s not found.\n"), JobName);
427 switch (jcr->JobStatus) {
430 case JS_WaitClientRes:
431 case JS_WaitStoreRes:
432 case JS_WaitPriority:
434 case JS_WaitStartTime:
435 set_jcr_job_status(jcr, JS_Canceled);
436 bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
437 jcr->JobId, jcr->Job);
438 #ifndef USE_SEMAPHORE
440 jobq_remove(&job_queue, jcr); /* attempt to remove it from queue */
442 workq_remove(&job_wq, jcr->work_item); /* attempt to remove it from queue */
449 set_jcr_job_status(jcr, JS_Canceled);
451 /* Cancel File daemon */
452 if (jcr->file_bsock) {
453 ua->jcr->client = jcr->client;
454 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
455 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
459 Dmsg0(200, "Connected to file daemon\n");
460 fd = ua->jcr->file_bsock;
461 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
462 while (bnet_recv(fd) >= 0) {
463 bsendmsg(ua, "%s", fd->msg);
465 bnet_sig(fd, BNET_TERMINATE);
467 ua->jcr->file_bsock = NULL;
470 /* Cancel Storage daemon */
471 if (jcr->store_bsock) {
472 ua->jcr->store = jcr->store;
473 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
474 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
478 Dmsg0(200, "Connected to storage daemon\n");
479 sd = ua->jcr->store_bsock;
480 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
481 while (bnet_recv(sd) >= 0) {
482 bsendmsg(ua, "%s", sd->msg);
484 bnet_sig(sd, BNET_TERMINATE);
486 ua->jcr->store_bsock = NULL;
495 * This is a common routine to create or update a
496 * Pool DB base record from a Pool Resource. We handle
497 * the setting of MaxVols and NumVols slightly differently
498 * depending on if we are creating the Pool or we are
499 * simply bringing it into agreement with the resource (updage).
501 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
503 strcpy(pr->PoolType, pool->pool_type);
504 if (op == POOL_OP_CREATE) {
505 pr->MaxVols = pool->max_volumes;
507 } else { /* update pool */
508 if (pr->MaxVols != pool->max_volumes) {
509 pr->MaxVols = pool->max_volumes;
511 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
512 pr->MaxVols = pr->NumVols;
515 pr->UseOnce = pool->use_volume_once;
516 pr->UseCatalog = pool->use_catalog;
517 pr->AcceptAnyVolume = pool->accept_any_volume;
518 pr->Recycle = pool->Recycle;
519 pr->VolRetention = pool->VolRetention;
520 pr->VolUseDuration = pool->VolUseDuration;
521 pr->MaxVolJobs = pool->MaxVolJobs;
522 pr->MaxVolFiles = pool->MaxVolFiles;
523 pr->MaxVolBytes = pool->MaxVolBytes;
524 pr->AutoPrune = pool->AutoPrune;
525 pr->Recycle = pool->Recycle;
526 if (pool->label_format) {
527 strcpy(pr->LabelFormat, pool->label_format);
529 strcpy(pr->LabelFormat, "*"); /* none */
535 * Create a pool record from a given Pool resource
536 * Also called from backup.c
537 * Returns: -1 on error
538 * 0 record already exists
542 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
546 memset(&pr, 0, sizeof(POOL_DBR));
548 strcpy(pr.Name, pool->hdr.name);
550 if (db_get_pool_record(jcr, db, &pr)) {
552 if (op == POOL_OP_UPDATE) { /* update request */
553 set_pooldbr_from_poolres(&pr, pool, op);
554 db_update_pool_record(jcr, db, &pr);
556 return 0; /* exists */
559 set_pooldbr_from_poolres(&pr, pool, op);
561 if (!db_create_pool_record(jcr, db, &pr)) {
562 return -1; /* error */
570 * Create a Pool Record in the database.
571 * It is always created from the Resource record.
573 static int createcmd(UAContext *ua, char *cmd)
581 pool = get_pool_resource(ua);
586 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
588 bsendmsg(ua, _("Error: Pool %s already exists.\n\
589 Use update to change it.\n"), pool->hdr.name);
593 bsendmsg(ua, db_strerror(ua->db));
599 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
607 * Update a Pool Record in the database.
608 * It is always updated from the Resource record.
610 * update pool=<pool-name>
611 * updates pool from Pool resource
612 * update media pool=<pool-name> volume=<volume-name>
613 * changes pool info for volume
615 static int update_cmd(UAContext *ua, char *cmd)
617 static char *kw[] = {
619 N_("volume"), /* 1 */
628 switch (find_arg_keyword(ua, kw)) {
643 start_prompt(ua, _("Update choice:\n"));
644 add_prompt(ua, _("Volume parameters"));
645 add_prompt(ua, _("Pool from resource"));
646 add_prompt(ua, _("Slots from autochanger"));
647 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
663 static void update_volstatus(UAContext *ua, char *val, MEDIA_DBR *mr)
665 POOLMEM *query = get_pool_memory(PM_MESSAGE);
678 for (i=0; kw[i]; i++) {
679 if (strcasecmp(val, kw[i]) == 0) {
685 bsendmsg(ua, _("Invalid VolStatus specified: %s\n"), val);
687 bstrncpy(mr->VolStatus, kw[i], sizeof(mr->VolStatus));
688 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
689 mr->VolStatus, mr->MediaId);
690 if (!db_sql_query(ua->db, query, NULL, NULL)) {
691 bsendmsg(ua, "%s", db_strerror(ua->db));
693 bsendmsg(ua, _("New Volume status is: %s\n"), mr->VolStatus);
696 free_pool_memory(query);
699 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
703 if (!duration_to_utime(val, &mr->VolRetention)) {
704 bsendmsg(ua, _("Invalid retention period specified: %s\n"), val);
707 query = get_pool_memory(PM_MESSAGE);
708 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
709 edit_uint64(mr->VolRetention, ed1), mr->MediaId);
710 if (!db_sql_query(ua->db, query, NULL, NULL)) {
711 bsendmsg(ua, "%s", db_strerror(ua->db));
713 bsendmsg(ua, _("New retention seconds is: %s\n"),
714 edit_utime(mr->VolRetention, ed1));
716 free_pool_memory(query);
719 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
724 if (!duration_to_utime(val, &mr->VolUseDuration)) {
725 bsendmsg(ua, _("Invalid use duration specified: %s\n"), val);
728 query = get_pool_memory(PM_MESSAGE);
729 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
730 edit_uint64(mr->VolUseDuration, ed1), mr->MediaId);
731 if (!db_sql_query(ua->db, query, NULL, NULL)) {
732 bsendmsg(ua, "%s", db_strerror(ua->db));
734 bsendmsg(ua, _("New use duration is: %s\n"),
735 edit_utime(mr->VolUseDuration, ed1));
737 free_pool_memory(query);
740 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
742 POOLMEM *query = get_pool_memory(PM_MESSAGE);
743 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%u",
745 if (!db_sql_query(ua->db, query, NULL, NULL)) {
746 bsendmsg(ua, "%s", db_strerror(ua->db));
748 bsendmsg(ua, _("New max jobs is: %s\n"), val);
750 free_pool_memory(query);
753 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
755 POOLMEM *query = get_pool_memory(PM_MESSAGE);
756 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%u",
758 if (!db_sql_query(ua->db, query, NULL, NULL)) {
759 bsendmsg(ua, "%s", db_strerror(ua->db));
761 bsendmsg(ua, _("New max files is: %s\n"), val);
763 free_pool_memory(query);
766 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
772 if (!size_to_uint64(val, strlen(val), &maxbytes)) {
773 bsendmsg(ua, _("Invalid max. bytes specification: %s\n"), val);
776 query = get_pool_memory(PM_MESSAGE);
777 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
778 edit_uint64(maxbytes, ed1), mr->MediaId);
779 if (!db_sql_query(ua->db, query, NULL, NULL)) {
780 bsendmsg(ua, "%s", db_strerror(ua->db));
782 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
784 free_pool_memory(query);
787 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
791 if (strcasecmp(val, _("yes")) == 0) {
793 } else if (strcasecmp(val, _("no")) == 0) {
796 bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
799 query = get_pool_memory(PM_MESSAGE);
800 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
801 recycle, mr->MediaId);
802 if (!db_sql_query(ua->db, query, NULL, NULL)) {
803 bsendmsg(ua, "%s", db_strerror(ua->db));
805 bsendmsg(ua, _("New recycle flag is: %s\n"),
806 mr->Recycle==1?_("yes"):_("no"));
808 free_pool_memory(query);
812 * Update a media record -- allows you to change the
813 * Volume status. E.g. if you want Bacula to stop
814 * writing on the volume, set it to anything other
817 static int update_volume(UAContext *ua)
824 N_("VolStatus"), /* 0 */
825 N_("VolRetention"), /* 1 */
826 N_("VolUse"), /* 2 */
827 N_("MaxVolJobs"), /* 3 */
828 N_("MaxVolFiles"), /* 4 */
829 N_("MaxVolBytes"), /* 5 */
830 N_("Recycle"), /* 6 */
833 if (!select_media_dbr(ua, &mr)) {
836 for (int i=0; kw[i]; i++) {
838 if ((j=find_arg_with_value(ua, kw[i])) > 0) {
841 update_volstatus(ua, ua->argv[j], &mr);
844 update_volretention(ua, ua->argv[j], &mr);
847 update_voluseduration(ua, ua->argv[j], &mr);
850 update_volmaxjobs(ua, ua->argv[j], &mr);
853 update_volmaxfiles(ua, ua->argv[j], &mr);
856 update_volmaxbytes(ua, ua->argv[j], &mr);
859 update_volrecycle(ua, ua->argv[j], &mr);
867 if (!select_media_dbr(ua, &mr)) {
870 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
871 start_prompt(ua, _("Parameters to modify:\n"));
872 add_prompt(ua, _("Volume Status"));
873 add_prompt(ua, _("Volume Retention Period"));
874 add_prompt(ua, _("Volume Use Duration"));
875 add_prompt(ua, _("Maximum Volume Jobs"));
876 add_prompt(ua, _("Maximum Volume Files"));
877 add_prompt(ua, _("Maximum Volume Bytes"));
878 add_prompt(ua, _("Recycle Flag"));
879 add_prompt(ua, _("Slot"));
880 add_prompt(ua, _("Volume Files"));
881 add_prompt(ua, _("Done"));
882 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
883 case 0: /* Volume Status */
884 /* Modify Volume Status */
885 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
886 start_prompt(ua, _("Possible Values are:\n"));
887 add_prompt(ua, "Append"); /* Better not translate these as */
888 add_prompt(ua, "Archive"); /* They are known in the database code */
889 add_prompt(ua, "Disabled");
890 add_prompt(ua, "Full");
891 add_prompt(ua, "Used");
892 add_prompt(ua, "Cleaning");
893 if (strcmp(mr.VolStatus, "Purged") == 0) {
894 add_prompt(ua, "Recycle");
896 add_prompt(ua, "Read-Only");
897 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
900 update_volstatus(ua, ua->cmd, &mr);
902 case 1: /* Retention */
903 bsendmsg(ua, _("Current retention seconds is: %s\n"),
904 edit_utime(mr.VolRetention, ed1));
905 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
908 update_volretention(ua, ua->cmd, &mr);
911 case 2: /* Use Duration */
912 bsendmsg(ua, _("Current use duration is: %s\n"),
913 edit_utime(mr.VolUseDuration, ed1));
914 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
917 update_voluseduration(ua, ua->cmd, &mr);
920 case 3: /* Max Jobs */
921 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
922 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
925 update_volmaxjobs(ua, ua->cmd, &mr);
928 case 4: /* Max Files */
929 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
930 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
933 update_volmaxfiles(ua, ua->cmd, &mr);
936 case 5: /* Max Bytes */
937 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
938 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
941 update_volmaxbytes(ua, ua->cmd, &mr);
945 case 6: /* Recycle */
946 bsendmsg(ua, _("Current recycle flag is: %s\n"),
947 mr.Recycle==1?_("yes"):_("no"));
948 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
951 update_volrecycle(ua, ua->cmd, &mr);
958 memset(&pr, 0, sizeof(POOL_DBR));
959 pr.PoolId = mr.PoolId;
960 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
961 bsendmsg(ua, "%s", db_strerror(ua->db));
964 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
965 if (!get_pint(ua, _("Enter new Slot: "))) {
968 slot = ua->pint32_val;
969 if (pr.MaxVols > 0 && slot > (int)pr.MaxVols) {
970 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
974 query = get_pool_memory(PM_MESSAGE);
975 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
977 if (!db_sql_query(ua->db, query, NULL, NULL)) {
978 bsendmsg(ua, "%s", db_strerror(ua->db));
980 bsendmsg(ua, "New Slot is: %d\n", slot);
982 free_pool_memory(query);
985 case 8: /* Volume Files */
987 bsendmsg(ua, _("Warning changing Volume Files can result\n"
988 "in loss of data on your Volume\n\n"));
989 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
990 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
993 VolFiles = ua->pint32_val;
994 if (VolFiles != (int)(mr.VolFiles + 1)) {
995 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
996 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
1000 query = get_pool_memory(PM_MESSAGE);
1001 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
1002 VolFiles, mr.MediaId);
1003 if (!db_sql_query(ua->db, query, NULL, NULL)) {
1004 bsendmsg(ua, "%s", db_strerror(ua->db));
1006 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
1008 free_pool_memory(query);
1011 default: /* Done or error */
1012 bsendmsg(ua, "Selection done.\n");
1020 * Update pool record -- pull info from current POOL resource
1022 static int update_pool(UAContext *ua)
1029 pool = get_pool_resource(ua);
1034 memset(&pr, 0, sizeof(pr));
1035 strcpy(pr.Name, pool->hdr.name);
1036 if (!get_pool_dbr(ua, &pr)) {
1040 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
1042 id = db_update_pool_record(ua->jcr, ua->db, &pr);
1044 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
1045 id, db_strerror(ua->db));
1047 query = get_pool_memory(PM_MESSAGE);
1048 Mmsg(&query, list_pool, pr.PoolId);
1049 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
1050 free_pool_memory(query);
1051 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
1056 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
1060 ua->jcr->store = store;
1061 /* Try connecting for up to 15 seconds */
1062 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
1063 store->hdr.name, store->address, store->SDport);
1064 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
1065 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1068 Dmsg0(120, _("Connected to storage daemon\n"));
1069 sd = ua->jcr->store_bsock;
1070 bnet_fsend(sd, "setdebug=%d\n", level);
1071 if (bnet_recv(sd) >= 0) {
1072 bsendmsg(ua, "%s", sd->msg);
1074 bnet_sig(sd, BNET_TERMINATE);
1076 ua->jcr->store_bsock = NULL;
1080 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
1084 /* Connect to File daemon */
1086 ua->jcr->client = client;
1087 /* Try to connect for 15 seconds */
1088 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1089 client->hdr.name, client->address, client->FDport);
1090 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1091 bsendmsg(ua, _("Failed to connect to Client.\n"));
1094 Dmsg0(120, "Connected to file daemon\n");
1095 fd = ua->jcr->file_bsock;
1096 bnet_fsend(fd, "setdebug=%d\n", level);
1097 if (bnet_recv(fd) >= 0) {
1098 bsendmsg(ua, "%s", fd->msg);
1100 bnet_sig(fd, BNET_TERMINATE);
1102 ua->jcr->file_bsock = NULL;
1107 static void do_all_setdebug(UAContext *ua, int level)
1109 STORE *store, **unique_store;
1110 CLIENT *client, **unique_client;
1114 debug_level = level;
1116 /* Count Storage items */
1119 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1121 unique_store = (STORE **) malloc(i * sizeof(STORE));
1122 /* Find Unique Storage address/port */
1123 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1125 unique_store[i++] = store;
1126 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1128 for (j=0; j<i; j++) {
1129 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1130 unique_store[j]->SDport == store->SDport) {
1136 unique_store[i++] = store;
1137 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1142 /* Call each unique Storage daemon */
1143 for (j=0; j<i; j++) {
1144 do_storage_setdebug(ua, unique_store[j], level);
1148 /* Count Client items */
1151 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1153 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1154 /* Find Unique Client address/port */
1155 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1157 unique_client[i++] = client;
1158 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1160 for (j=0; j<i; j++) {
1161 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1162 unique_client[j]->FDport == client->FDport) {
1168 unique_client[i++] = client;
1169 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1174 /* Call each unique File daemon */
1175 for (j=0; j<i; j++) {
1176 do_client_setdebug(ua, unique_client[j], level);
1178 free(unique_client);
1182 * setdebug level=nn all
1184 static int setdebug_cmd(UAContext *ua, char *cmd)
1194 Dmsg1(120, "setdebug:%s:\n", cmd);
1197 i = find_arg_with_value(ua, _("level"));
1199 level = atoi(ua->argv[i]);
1202 if (!get_pint(ua, _("Enter new debug level: "))) {
1205 level = ua->pint32_val;
1208 /* General debug? */
1209 for (i=1; i<ua->argc; i++) {
1210 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1211 do_all_setdebug(ua, level);
1214 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1215 strcasecmp(ua->argk[i], _("director")) == 0) {
1216 debug_level = level;
1219 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1222 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1224 do_client_setdebug(ua, client, level);
1228 client = select_client_resource(ua);
1230 do_client_setdebug(ua, client, level);
1235 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1236 strcasecmp(ua->argk[i], _("storage")) == 0) {
1239 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1241 do_storage_setdebug(ua, store, level);
1245 store = get_storage_resource(ua, 0);
1247 do_storage_setdebug(ua, store, level);
1253 * We didn't find an appropriate keyword above, so
1256 start_prompt(ua, _("Available daemons are: \n"));
1257 add_prompt(ua, _("Director"));
1258 add_prompt(ua, _("Storage"));
1259 add_prompt(ua, _("Client"));
1260 add_prompt(ua, _("All"));
1261 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1262 case 0: /* Director */
1263 debug_level = level;
1266 store = get_storage_resource(ua, 0);
1268 do_storage_setdebug(ua, store, level);
1272 client = select_client_resource(ua);
1274 do_client_setdebug(ua, client, level);
1278 do_all_setdebug(ua, level);
1286 static int var_cmd(UAContext *ua, char *cmd)
1288 POOLMEM *var = get_pool_memory(PM_FNAME);
1289 for (int i=1; i<ua->argc; i++) {
1290 if (ua->argk[i] && variable_expansion(ua->jcr, ua->argk[i], &var)) {
1291 bsendmsg(ua, "%s\n", var);
1294 free_pool_memory(var);
1298 static int estimate_cmd(UAContext *ua, char *cmd)
1301 CLIENT *client = NULL;
1302 FILESET *fileset = NULL;
1305 char since[MAXSTRING];
1307 for (int i=1; i<ua->argc; i++) {
1308 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1310 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1314 if (strcasecmp(ua->argk[i], _("job")) == 0) {
1316 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1320 if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1322 fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1326 if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1330 if (!job && !(client && fileset)) {
1331 if (!(job = select_job_resource(ua))) {
1336 job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1339 client = job->client;
1342 fileset = job->fileset;
1344 ua->jcr->client = client;
1345 ua->jcr->fileset = fileset;
1347 ua->catalog = client->catalog;
1353 get_level_since_time(ua->jcr, since, sizeof(since));
1355 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1356 job->client->hdr.name, job->client->address, job->client->FDport);
1357 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1358 bsendmsg(ua, _("Failed to connect to Client.\n"));
1361 fd = ua->jcr->file_bsock;
1363 if (!send_include_list(ua->jcr)) {
1364 bsendmsg(ua, _("Error sending include list.\n"));
1368 if (!send_exclude_list(ua->jcr)) {
1369 bsendmsg(ua, _("Error sending exclude list.\n"));
1373 if (!send_level_command(ua->jcr)) {
1377 bnet_fsend(fd, "estimate listing=%d\n", listing);
1378 while (bnet_recv(fd) >= 0) {
1379 bsendmsg(ua, "%s", fd->msg);
1382 bnet_sig(fd, BNET_TERMINATE);
1384 ua->jcr->file_bsock = NULL;
1393 static int time_cmd(UAContext *ua, char *cmd)
1396 time_t ttime = time(NULL);
1398 localtime_r(&ttime, &tm);
1399 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1400 bsendmsg(ua, "%s\n", sdt);
1407 * Delete Pool records (should purge Media with it).
1409 * delete pool=<pool-name>
1410 * delete media pool=<pool-name> volume=<name>
1412 static int delete_cmd(UAContext *ua, char *cmd)
1414 static char *keywords[] = {
1424 "In general it is not a good idea to delete either a\n"
1425 "Pool or a Volume since they may contain data.\n\n"));
1427 switch (find_arg_keyword(ua, keywords)) {
1437 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1445 bsendmsg(ua, _("Nothing done.\n"));
1452 * Delete media records from database -- dangerous
1454 static int delete_volume(UAContext *ua)
1458 if (!select_media_dbr(ua, &mr)) {
1461 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1462 "and all Jobs saved on that volume from the Catalog\n"),
1465 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1468 if (ua->pint32_val) {
1469 db_delete_media_record(ua->jcr, ua->db, &mr);
1475 * Delete a pool record from the database -- dangerous
1477 static int delete_pool(UAContext *ua)
1481 memset(&pr, 0, sizeof(pr));
1483 if (!get_pool_dbr(ua, &pr)) {
1486 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1489 if (ua->pint32_val) {
1490 db_delete_pool_record(ua->jcr, ua->db, &pr);
1496 static void do_mount_cmd(UAContext *ua, char *command)
1500 char dev_name[MAX_NAME_LENGTH];
1506 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1508 store = get_storage_resource(ua, 1);
1513 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1514 store->media_type, store->dev_name);
1516 ua->jcr->store = store;
1517 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1518 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1521 sd = ua->jcr->store_bsock;
1522 strcpy(dev_name, store->dev_name);
1523 bash_spaces(dev_name);
1524 bnet_fsend(sd, "%s %s", command, dev_name);
1525 while (bnet_recv(sd) >= 0) {
1526 bsendmsg(ua, "%s", sd->msg);
1528 bnet_sig(sd, BNET_TERMINATE);
1530 ua->jcr->store_bsock = NULL;
1534 * mount [storage | device] <name>
1536 static int mount_cmd(UAContext *ua, char *cmd)
1538 do_mount_cmd(ua, "mount"); /* mount */
1544 * unmount [storage | device] <name>
1546 static int unmount_cmd(UAContext *ua, char *cmd)
1548 do_mount_cmd(ua, "unmount"); /* unmount */
1554 * release [storage | device] <name>
1556 static int release_cmd(UAContext *ua, char *cmd)
1558 do_mount_cmd(ua, "release"); /* release */
1565 * use catalog=<name>
1567 static int use_cmd(UAContext *ua, char *cmd)
1569 CAT *oldcatalog, *catalog;
1572 close_db(ua); /* close any previously open db */
1573 oldcatalog = ua->catalog;
1575 if (!(catalog = get_catalog_resource(ua))) {
1576 ua->catalog = oldcatalog;
1578 ua->catalog = catalog;
1581 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1582 ua->catalog->hdr.name, ua->catalog->db_name);
1587 int quit_cmd(UAContext *ua, char *cmd)
1594 * Wait until no job is running
1596 int wait_cmd(UAContext *ua, char *cmd)
1598 bmicrosleep(0, 200000); /* let job actually start */
1599 for (int running=1; running; ) {
1602 for (JCR *jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
1603 if (jcr->JobId != 0) {
1605 free_locked_jcr(jcr);
1608 free_locked_jcr(jcr);
1619 static int help_cmd(UAContext *ua, char *cmd)
1624 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1625 for (i=0; i<comsize; i++) {
1626 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1632 static int version_cmd(UAContext *ua, char *cmd)
1634 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1639 /* A bit brain damaged in that if the user has not done
1640 * a "use catalog xxx" command, we simply find the first
1641 * catalog resource and open it.
1643 int open_db(UAContext *ua)
1650 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1653 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1656 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1657 ua->catalog->hdr.name, ua->catalog->db_name);
1661 ua->jcr->catalog = ua->catalog;
1663 Dmsg0(150, "Open database\n");
1664 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1665 ua->catalog->db_password, ua->catalog->db_address,
1666 ua->catalog->db_port, ua->catalog->db_socket);
1667 if (!db_open_database(ua->jcr, ua->db)) {
1668 bsendmsg(ua, _("Could not open DB %s: ERR=%s"),
1669 ua->catalog->db_name, db_strerror(ua->db));
1673 ua->jcr->db = ua->db;
1674 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1678 void close_db(UAContext *ua)
1681 db_close_database(ua->jcr, ua->db);