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[];
40 extern jobq_t job_queue; /* job queue */
42 extern char *list_pool;
44 /* Imported functions */
45 extern int status_cmd(UAContext *ua, char *cmd);
46 extern int list_cmd(UAContext *ua, char *cmd);
47 extern int llist_cmd(UAContext *ua, char *cmd);
48 extern int show_cmd(UAContext *ua, char *cmd);
49 extern int messagescmd(UAContext *ua, char *cmd);
50 extern int autodisplaycmd(UAContext *ua, char *cmd);
51 extern int sqlquerycmd(UAContext *ua, char *cmd);
52 extern int querycmd(UAContext *ua, char *cmd);
53 extern int run_cmd(UAContext *ua, char *cmd);
54 extern int retentioncmd(UAContext *ua, char *cmd);
55 extern int prunecmd(UAContext *ua, char *cmd);
56 extern int purgecmd(UAContext *ua, char *cmd);
57 extern int restore_cmd(UAContext *ua, char *cmd);
58 extern int label_cmd(UAContext *ua, char *cmd);
59 extern int relabel_cmd(UAContext *ua, char *cmd);
60 extern int update_slots(UAContext *ua); /* ua_label.c */
62 /* Forward referenced functions */
63 static int add_cmd(UAContext *ua, char *cmd);
64 static int create_cmd(UAContext *ua, char *cmd);
65 static int cancel_cmd(UAContext *ua, char *cmd);
66 static int setdebug_cmd(UAContext *ua, char *cmd);
67 static int var_cmd(UAContext *ua, char *cmd);
68 static int estimate_cmd(UAContext *ua, char *cmd);
69 static int help_cmd(UAContext *ua, char *cmd);
70 static int delete_cmd(UAContext *ua, char *cmd);
71 static int use_cmd(UAContext *ua, char *cmd), unmount_cmd(UAContext *ua, char *cmd);
72 static int version_cmd(UAContext *ua, char *cmd), automount_cmd(UAContext *ua, char *cmd);
73 static int time_cmd(UAContext *ua, char *cmd);
74 static int update_volume(UAContext *ua);
75 static int update_pool(UAContext *ua);
76 static int delete_volume(UAContext *ua);
77 static int delete_pool(UAContext *ua);
78 static int delete_job(UAContext *ua);
79 static int mount_cmd(UAContext *ua, char *cmd);
80 static int release_cmd(UAContext *ua, char *cmd);
81 static int update_cmd(UAContext *ua, char *cmd);
82 static int wait_cmd(UAContext *ua, char *cmd);
84 int quit_cmd(UAContext *ua, char *cmd);
87 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
88 static struct cmdstruct commands[] = {
89 { N_("add"), add_cmd, _("add media to a pool")},
90 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
91 { N_("automount"), automount_cmd, _("automount [on/off] -- after label")},
92 { N_("cancel"), cancel_cmd, _("cancel job=nnn -- cancel a job")},
93 { N_("create"), create_cmd, _("create DB Pool from resource")},
94 { N_("delete"), delete_cmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
95 { N_("estimate"), estimate_cmd, _("performs FileSet estimate, listing gives full listing")},
96 { N_("exit"), quit_cmd, _("exit = quit")},
97 { N_("help"), help_cmd, _("print this command")},
98 { N_("list"), list_cmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
99 { N_("label"), label_cmd, _("label a tape")},
100 { N_("llist"), llist_cmd, _("full or long list like list command")},
101 { N_("messages"), messagescmd, _("messages")},
102 { N_("mount"), mount_cmd, _("mount <storage-name>")},
103 { N_("prune"), prunecmd, _("prune expired records from catalog")},
104 { N_("purge"), purgecmd, _("purge records from catalog")},
105 { N_("quit"), quit_cmd, _("quit")},
106 { N_("query"), querycmd, _("query catalog")},
107 { N_("restore"), restore_cmd, _("restore files")},
108 { N_("relabel"), relabel_cmd, _("relabel a tape")},
109 { N_("release"), release_cmd, _("release <storage-name>")},
110 { N_("run"), run_cmd, _("run <job-name>")},
111 { N_("status"), status_cmd, _("status [storage | client]=<name>")},
112 { N_("setdebug"), setdebug_cmd, _("sets debug level")},
113 { N_("show"), show_cmd, _("show (resource records) [jobs | pools | ... | all]")},
114 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
115 { N_("time"), time_cmd, _("print current time")},
116 { N_("unmount"), unmount_cmd, _("unmount <storage-name>")},
117 { N_("update"), update_cmd, _("update Volume or Pool")},
118 { N_("use"), use_cmd, _("use catalog xxx")},
119 { N_("var"), var_cmd, _("does variable expansion")},
120 { N_("version"), version_cmd, _("print Director version")},
121 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
123 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
126 * Execute a command from the UA
128 int do_a_command(UAContext *ua, char *cmd)
137 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
142 len = strlen(ua->argk[0]);
143 for (i=0; i<comsize; i++) { /* search for command */
144 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
145 stat = (*commands[i].func)(ua, cmd); /* go execute command */
151 pm_strcat(&ua->UA_sock->msg, _(": is an illegal command\n"));
152 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
153 bnet_send(ua->UA_sock);
159 * This is a common routine used to stuff the Pool DB record defaults
160 * into the Media DB record just before creating a media (Volume)
163 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
165 mr->PoolId = pr->PoolId;
166 bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));
167 mr->Recycle = pr->Recycle;
168 mr->VolRetention = pr->VolRetention;
169 mr->VolUseDuration = pr->VolUseDuration;
170 mr->MaxVolJobs = pr->MaxVolJobs;
171 mr->MaxVolFiles = pr->MaxVolFiles;
172 mr->MaxVolBytes = pr->MaxVolBytes;
177 * Add Volumes to an existing Pool
179 static int add_cmd(UAContext *ua, char *cmd)
183 int num, i, max, startnum;
185 char name[MAX_NAME_LENGTH];
190 "You probably don't want to be using this command since it\n"
191 "creates database records without labeling the Volumes.\n"
192 "You probably want to use the \"label\" command.\n\n"));
198 memset(&pr, 0, sizeof(pr));
199 memset(&mr, 0, sizeof(mr));
201 if (!get_pool_dbr(ua, &pr)) {
205 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
206 pr.MaxVols, pr.PoolType);
208 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
209 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
211 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
214 pr.MaxVols = ua->pint32_val;
219 if ((store = get_storage_resource(ua, 0)) != NULL) {
220 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
221 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
225 if (pr.MaxVols == 0) {
228 max = pr.MaxVols - pr.NumVols;
232 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
233 if (!get_pint(ua, buf)) {
236 num = ua->pint32_val;
237 if (num < 0 || num > max) {
238 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
245 if (!get_cmd(ua, _("Enter Volume name: "))) {
249 if (!get_cmd(ua, _("Enter base volume name: "))) {
253 /* Don't allow | in Volume name because it is the volume separator character */
254 if (!is_volume_name_legal(ua, ua->cmd)) {
257 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
258 bsendmsg(ua, _("Volume name too long.\n"));
261 if (strlen(ua->cmd) == 0) {
262 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
266 bstrncpy(name, ua->cmd, sizeof(name));
268 strcat(name, "%04d");
271 if (!get_pint(ua, _("Enter the starting number: "))) {
274 startnum = ua->pint32_val;
276 bsendmsg(ua, _("Start number must be greater than zero.\n"));
286 if (store && store->autochanger) {
287 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
290 slot = ua->pint32_val;
293 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
294 for (i=startnum; i < num+startnum; i++) {
295 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
297 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
298 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
299 bsendmsg(ua, db_strerror(ua->db));
303 first_id = mr.PoolId;
307 Dmsg0(200, "Update pool record.\n");
308 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
309 bsendmsg(ua, db_strerror(ua->db));
312 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
318 * Turn auto mount on/off
323 int automount_cmd(UAContext *ua, char *cmd)
328 if (!get_cmd(ua, _("Turn on or off? "))) {
336 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
344 static int cancel_cmd(UAContext *ua, char *cmd)
350 char JobName[MAX_NAME_LENGTH];
356 for (i=1; i<ua->argc; i++) {
357 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
361 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
362 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
366 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
370 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
371 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
377 /* If we still do not have a jcr,
378 * throw up a list and ask the user to select one.
381 /* Count Jobs running */
383 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
384 if (jcr->JobId == 0) { /* this is us */
385 free_locked_jcr(jcr);
389 free_locked_jcr(jcr);
394 bsendmsg(ua, _("No Jobs running.\n"));
397 start_prompt(ua, _("Select Job:\n"));
399 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
400 if (jcr->JobId == 0) { /* this is us */
401 free_locked_jcr(jcr);
404 add_prompt(ua, jcr->Job);
405 free_locked_jcr(jcr);
409 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
413 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
417 jcr = get_jcr_by_full_name(JobName);
419 bsendmsg(ua, _("Job %s not found.\n"), JobName);
424 switch (jcr->JobStatus) {
427 case JS_WaitClientRes:
428 case JS_WaitStoreRes:
429 case JS_WaitPriority:
431 case JS_WaitStartTime:
432 set_jcr_job_status(jcr, JS_Canceled);
433 bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
434 jcr->JobId, jcr->Job);
435 jobq_remove(&job_queue, jcr); /* attempt to remove it from queue */
436 free_jcr(jcr); /* this decrements the use count only */
440 set_jcr_job_status(jcr, JS_Canceled);
442 /* Cancel File daemon */
443 if (jcr->file_bsock) {
444 ua->jcr->client = jcr->client;
445 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
446 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
450 Dmsg0(200, "Connected to file daemon\n");
451 fd = ua->jcr->file_bsock;
452 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
453 while (bnet_recv(fd) >= 0) {
454 bsendmsg(ua, "%s", fd->msg);
456 bnet_sig(fd, BNET_TERMINATE);
458 ua->jcr->file_bsock = NULL;
461 /* Cancel Storage daemon */
462 if (jcr->store_bsock) {
463 ua->jcr->store = jcr->store;
464 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
465 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
469 Dmsg0(200, "Connected to storage daemon\n");
470 sd = ua->jcr->store_bsock;
471 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
472 while (bnet_recv(sd) >= 0) {
473 bsendmsg(ua, "%s", sd->msg);
475 bnet_sig(sd, BNET_TERMINATE);
477 ua->jcr->store_bsock = NULL;
486 * This is a common routine to create or update a
487 * Pool DB base record from a Pool Resource. We handle
488 * the setting of MaxVols and NumVols slightly differently
489 * depending on if we are creating the Pool or we are
490 * simply bringing it into agreement with the resource (updage).
492 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
494 strcpy(pr->PoolType, pool->pool_type);
495 if (op == POOL_OP_CREATE) {
496 pr->MaxVols = pool->max_volumes;
498 } else { /* update pool */
499 if (pr->MaxVols != pool->max_volumes) {
500 pr->MaxVols = pool->max_volumes;
502 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
503 pr->MaxVols = pr->NumVols;
506 pr->UseOnce = pool->use_volume_once;
507 pr->UseCatalog = pool->use_catalog;
508 pr->AcceptAnyVolume = pool->accept_any_volume;
509 pr->Recycle = pool->Recycle;
510 pr->VolRetention = pool->VolRetention;
511 pr->VolUseDuration = pool->VolUseDuration;
512 pr->MaxVolJobs = pool->MaxVolJobs;
513 pr->MaxVolFiles = pool->MaxVolFiles;
514 pr->MaxVolBytes = pool->MaxVolBytes;
515 pr->AutoPrune = pool->AutoPrune;
516 pr->Recycle = pool->Recycle;
517 if (pool->label_format) {
518 strcpy(pr->LabelFormat, pool->label_format);
520 strcpy(pr->LabelFormat, "*"); /* none */
526 * Create a pool record from a given Pool resource
527 * Also called from backup.c
528 * Returns: -1 on error
529 * 0 record already exists
533 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
537 memset(&pr, 0, sizeof(POOL_DBR));
539 strcpy(pr.Name, pool->hdr.name);
541 if (db_get_pool_record(jcr, db, &pr)) {
543 if (op == POOL_OP_UPDATE) { /* update request */
544 set_pooldbr_from_poolres(&pr, pool, op);
545 db_update_pool_record(jcr, db, &pr);
547 return 0; /* exists */
550 set_pooldbr_from_poolres(&pr, pool, op);
552 if (!db_create_pool_record(jcr, db, &pr)) {
553 return -1; /* error */
561 * Create a Pool Record in the database.
562 * It is always created from the Resource record.
564 static int create_cmd(UAContext *ua, char *cmd)
572 pool = get_pool_resource(ua);
577 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
579 bsendmsg(ua, _("Error: Pool %s already exists.\n\
580 Use update to change it.\n"), pool->hdr.name);
584 bsendmsg(ua, db_strerror(ua->db));
590 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
598 * Update a Pool Record in the database.
599 * It is always updated from the Resource record.
601 * update pool=<pool-name>
602 * updates pool from Pool resource
603 * update media pool=<pool-name> volume=<volume-name>
604 * changes pool info for volume
606 static int update_cmd(UAContext *ua, char *cmd)
608 static char *kw[] = {
610 N_("volume"), /* 1 */
619 switch (find_arg_keyword(ua, kw)) {
634 start_prompt(ua, _("Update choice:\n"));
635 add_prompt(ua, _("Volume parameters"));
636 add_prompt(ua, _("Pool from resource"));
637 add_prompt(ua, _("Slots from autochanger"));
638 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
654 static void update_volstatus(UAContext *ua, char *val, MEDIA_DBR *mr)
656 POOLMEM *query = get_pool_memory(PM_MESSAGE);
670 for (i=0; kw[i]; i++) {
671 if (strcasecmp(val, kw[i]) == 0) {
677 bsendmsg(ua, _("Invalid VolStatus specified: %s\n"), val);
679 bstrncpy(mr->VolStatus, kw[i], sizeof(mr->VolStatus));
680 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
681 mr->VolStatus, mr->MediaId);
682 if (!db_sql_query(ua->db, query, NULL, NULL)) {
683 bsendmsg(ua, "%s", db_strerror(ua->db));
685 bsendmsg(ua, _("New Volume status is: %s\n"), mr->VolStatus);
688 free_pool_memory(query);
691 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
695 if (!duration_to_utime(val, &mr->VolRetention)) {
696 bsendmsg(ua, _("Invalid retention period specified: %s\n"), val);
699 query = get_pool_memory(PM_MESSAGE);
700 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
701 edit_uint64(mr->VolRetention, ed1), mr->MediaId);
702 if (!db_sql_query(ua->db, query, NULL, NULL)) {
703 bsendmsg(ua, "%s", db_strerror(ua->db));
705 bsendmsg(ua, _("New retention seconds is: %s\n"),
706 edit_utime(mr->VolRetention, ed1));
708 free_pool_memory(query);
711 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
716 if (!duration_to_utime(val, &mr->VolUseDuration)) {
717 bsendmsg(ua, _("Invalid use duration specified: %s\n"), val);
720 query = get_pool_memory(PM_MESSAGE);
721 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
722 edit_uint64(mr->VolUseDuration, ed1), mr->MediaId);
723 if (!db_sql_query(ua->db, query, NULL, NULL)) {
724 bsendmsg(ua, "%s", db_strerror(ua->db));
726 bsendmsg(ua, _("New use duration is: %s\n"),
727 edit_utime(mr->VolUseDuration, ed1));
729 free_pool_memory(query);
732 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
734 POOLMEM *query = get_pool_memory(PM_MESSAGE);
735 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%u",
737 if (!db_sql_query(ua->db, query, NULL, NULL)) {
738 bsendmsg(ua, "%s", db_strerror(ua->db));
740 bsendmsg(ua, _("New max jobs is: %s\n"), val);
742 free_pool_memory(query);
745 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
747 POOLMEM *query = get_pool_memory(PM_MESSAGE);
748 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%u",
750 if (!db_sql_query(ua->db, query, NULL, NULL)) {
751 bsendmsg(ua, "%s", db_strerror(ua->db));
753 bsendmsg(ua, _("New max files is: %s\n"), val);
755 free_pool_memory(query);
758 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
764 if (!size_to_uint64(val, strlen(val), &maxbytes)) {
765 bsendmsg(ua, _("Invalid max. bytes specification: %s\n"), val);
768 query = get_pool_memory(PM_MESSAGE);
769 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
770 edit_uint64(maxbytes, ed1), mr->MediaId);
771 if (!db_sql_query(ua->db, query, NULL, NULL)) {
772 bsendmsg(ua, "%s", db_strerror(ua->db));
774 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
776 free_pool_memory(query);
779 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
783 if (strcasecmp(val, _("yes")) == 0) {
785 } else if (strcasecmp(val, _("no")) == 0) {
788 bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
791 query = get_pool_memory(PM_MESSAGE);
792 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
793 recycle, mr->MediaId);
794 if (!db_sql_query(ua->db, query, NULL, NULL)) {
795 bsendmsg(ua, "%s", db_strerror(ua->db));
797 bsendmsg(ua, _("New recycle flag is: %s\n"),
798 mr->Recycle==1?_("yes"):_("no"));
800 free_pool_memory(query);
803 /* Modify the Pool in which this Volume is located */
804 static void update_volpool(UAContext *ua, char *val, MEDIA_DBR *mr)
808 memset(&pr, 0, sizeof(pr));
809 bstrncpy(pr.Name, val, sizeof(pr.Name));
810 if (!get_pool_dbr(ua, &pr)) {
813 mr->PoolId = pr.PoolId; /* set new PoolId */
815 * Make sure to use db_update... rather than doing this directly,
816 * so that any Slot is handled correctly.
818 if (!db_update_media_record(ua->jcr, ua->db, mr)) {
819 bsendmsg(ua, _("Error updating media record Pool: ERR=%s"), db_strerror(ua->db));
821 bsendmsg(ua, _("New Pool is: %s\n"), pr.Name);
826 * Update a media record -- allows you to change the
827 * Volume status. E.g. if you want Bacula to stop
828 * writing on the volume, set it to anything other
831 static int update_volume(UAContext *ua)
839 N_("VolStatus"), /* 0 */
840 N_("VolRetention"), /* 1 */
841 N_("VolUse"), /* 2 */
842 N_("MaxVolJobs"), /* 3 */
843 N_("MaxVolFiles"), /* 4 */
844 N_("MaxVolBytes"), /* 5 */
845 N_("Recycle"), /* 6 */
849 for (int i=0; kw[i]; i++) {
851 if ((j=find_arg_with_value(ua, kw[i])) > 0) {
852 if (!select_media_dbr(ua, &mr)) {
857 update_volstatus(ua, ua->argv[j], &mr);
860 update_volretention(ua, ua->argv[j], &mr);
863 update_voluseduration(ua, ua->argv[j], &mr);
866 update_volmaxjobs(ua, ua->argv[j], &mr);
869 update_volmaxfiles(ua, ua->argv[j], &mr);
872 update_volmaxbytes(ua, ua->argv[j], &mr);
875 update_volrecycle(ua, ua->argv[j], &mr);
878 update_volpool(ua, ua->argv[j], &mr);
885 if (!select_media_dbr(ua, &mr)) {
888 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
889 start_prompt(ua, _("Parameters to modify:\n"));
890 add_prompt(ua, _("Volume Status"));
891 add_prompt(ua, _("Volume Retention Period"));
892 add_prompt(ua, _("Volume Use Duration"));
893 add_prompt(ua, _("Maximum Volume Jobs"));
894 add_prompt(ua, _("Maximum Volume Files"));
895 add_prompt(ua, _("Maximum Volume Bytes"));
896 add_prompt(ua, _("Recycle Flag"));
897 add_prompt(ua, _("Slot"));
898 add_prompt(ua, _("Volume Files"));
899 add_prompt(ua, _("Pool"));
900 add_prompt(ua, _("Done"));
901 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
902 case 0: /* Volume Status */
903 /* Modify Volume Status */
904 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
905 start_prompt(ua, _("Possible Values are:\n"));
906 add_prompt(ua, "Append"); /* Better not translate these as */
907 add_prompt(ua, "Archive"); /* They are known in the database code */
908 add_prompt(ua, "Disabled");
909 add_prompt(ua, "Full");
910 add_prompt(ua, "Used");
911 add_prompt(ua, "Cleaning");
912 if (strcmp(mr.VolStatus, "Purged") == 0) {
913 add_prompt(ua, "Recycle");
915 add_prompt(ua, "Read-Only");
916 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
919 update_volstatus(ua, ua->cmd, &mr);
921 case 1: /* Retention */
922 bsendmsg(ua, _("Current retention seconds is: %s\n"),
923 edit_utime(mr.VolRetention, ed1));
924 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
927 update_volretention(ua, ua->cmd, &mr);
930 case 2: /* Use Duration */
931 bsendmsg(ua, _("Current use duration is: %s\n"),
932 edit_utime(mr.VolUseDuration, ed1));
933 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
936 update_voluseduration(ua, ua->cmd, &mr);
939 case 3: /* Max Jobs */
940 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
941 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
944 update_volmaxjobs(ua, ua->cmd, &mr);
947 case 4: /* Max Files */
948 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
949 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
952 update_volmaxfiles(ua, ua->cmd, &mr);
955 case 5: /* Max Bytes */
956 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
957 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
960 update_volmaxbytes(ua, ua->cmd, &mr);
964 case 6: /* Recycle */
965 bsendmsg(ua, _("Current recycle flag is: %s\n"),
966 mr.Recycle==1?_("yes"):_("no"));
967 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
970 update_volrecycle(ua, ua->cmd, &mr);
976 memset(&pr, 0, sizeof(POOL_DBR));
977 pr.PoolId = mr.PoolId;
978 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
979 bsendmsg(ua, "%s", db_strerror(ua->db));
982 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
983 if (!get_pint(ua, _("Enter new Slot: "))) {
986 slot = ua->pint32_val;
987 if (pr.MaxVols > 0 && slot > (int)pr.MaxVols) {
988 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
994 * Make sure to use db_update... rather than doing this directly,
995 * so that any Slot is handled correctly.
997 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
998 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1000 bsendmsg(ua, _("New Slot is: %s\n"), mr.Slot);
1004 case 8: /* Volume Files */
1006 bsendmsg(ua, _("Warning changing Volume Files can result\n"
1007 "in loss of data on your Volume\n\n"));
1008 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
1009 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
1012 VolFiles = ua->pint32_val;
1013 if (VolFiles != (int)(mr.VolFiles + 1)) {
1014 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
1015 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
1019 query = get_pool_memory(PM_MESSAGE);
1020 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
1021 VolFiles, mr.MediaId);
1022 if (!db_sql_query(ua->db, query, NULL, NULL)) {
1023 bsendmsg(ua, "%s", db_strerror(ua->db));
1025 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
1027 free_pool_memory(query);
1030 case 9: /* Volume's Pool */
1031 memset(&pr, 0, sizeof(POOL_DBR));
1032 pr.PoolId = mr.PoolId;
1033 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1034 bsendmsg(ua, "%s", db_strerror(ua->db));
1037 bsendmsg(ua, _("Current Pool is: %s\n"), pr.Name);
1038 if (!get_cmd(ua, _("Enter new Pool name: "))) {
1041 update_volpool(ua, ua->cmd, &mr);
1043 default: /* Done or error */
1044 bsendmsg(ua, "Selection done.\n");
1052 * Update pool record -- pull info from current POOL resource
1054 static int update_pool(UAContext *ua)
1061 pool = get_pool_resource(ua);
1066 memset(&pr, 0, sizeof(pr));
1067 strcpy(pr.Name, pool->hdr.name);
1068 if (!get_pool_dbr(ua, &pr)) {
1072 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
1074 id = db_update_pool_record(ua->jcr, ua->db, &pr);
1076 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
1077 id, db_strerror(ua->db));
1079 query = get_pool_memory(PM_MESSAGE);
1080 Mmsg(&query, list_pool, pr.PoolId);
1081 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
1082 free_pool_memory(query);
1083 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
1088 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
1092 ua->jcr->store = store;
1093 /* Try connecting for up to 15 seconds */
1094 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
1095 store->hdr.name, store->address, store->SDport);
1096 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
1097 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1100 Dmsg0(120, _("Connected to storage daemon\n"));
1101 sd = ua->jcr->store_bsock;
1102 bnet_fsend(sd, "setdebug=%d\n", level);
1103 if (bnet_recv(sd) >= 0) {
1104 bsendmsg(ua, "%s", sd->msg);
1106 bnet_sig(sd, BNET_TERMINATE);
1108 ua->jcr->store_bsock = NULL;
1112 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
1116 /* Connect to File daemon */
1118 ua->jcr->client = client;
1119 /* Try to connect for 15 seconds */
1120 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1121 client->hdr.name, client->address, client->FDport);
1122 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1123 bsendmsg(ua, _("Failed to connect to Client.\n"));
1126 Dmsg0(120, "Connected to file daemon\n");
1127 fd = ua->jcr->file_bsock;
1128 bnet_fsend(fd, "setdebug=%d\n", level);
1129 if (bnet_recv(fd) >= 0) {
1130 bsendmsg(ua, "%s", fd->msg);
1132 bnet_sig(fd, BNET_TERMINATE);
1134 ua->jcr->file_bsock = NULL;
1139 static void do_all_setdebug(UAContext *ua, int level)
1141 STORE *store, **unique_store;
1142 CLIENT *client, **unique_client;
1146 debug_level = level;
1148 /* Count Storage items */
1151 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1153 unique_store = (STORE **) malloc(i * sizeof(STORE));
1154 /* Find Unique Storage address/port */
1155 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1157 unique_store[i++] = store;
1158 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1160 for (j=0; j<i; j++) {
1161 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1162 unique_store[j]->SDport == store->SDport) {
1168 unique_store[i++] = store;
1169 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1174 /* Call each unique Storage daemon */
1175 for (j=0; j<i; j++) {
1176 do_storage_setdebug(ua, unique_store[j], level);
1180 /* Count Client items */
1183 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1185 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1186 /* Find Unique Client address/port */
1187 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1189 unique_client[i++] = client;
1190 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1192 for (j=0; j<i; j++) {
1193 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1194 unique_client[j]->FDport == client->FDport) {
1200 unique_client[i++] = client;
1201 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1206 /* Call each unique File daemon */
1207 for (j=0; j<i; j++) {
1208 do_client_setdebug(ua, unique_client[j], level);
1210 free(unique_client);
1214 * setdebug level=nn all
1216 static int setdebug_cmd(UAContext *ua, char *cmd)
1226 Dmsg1(120, "setdebug:%s:\n", cmd);
1229 i = find_arg_with_value(ua, _("level"));
1231 level = atoi(ua->argv[i]);
1234 if (!get_pint(ua, _("Enter new debug level: "))) {
1237 level = ua->pint32_val;
1240 /* General debug? */
1241 for (i=1; i<ua->argc; i++) {
1242 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1243 do_all_setdebug(ua, level);
1246 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1247 strcasecmp(ua->argk[i], _("director")) == 0) {
1248 debug_level = level;
1251 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1252 strcasecmp(ua->argk[i], _("fd")) == 0) {
1255 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1257 do_client_setdebug(ua, client, level);
1261 client = select_client_resource(ua);
1263 do_client_setdebug(ua, client, level);
1268 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1269 strcasecmp(ua->argk[i], _("storage")) == 0 ||
1270 strcasecmp(ua->argk[i], _("sd")) == 0) {
1273 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1275 do_storage_setdebug(ua, store, level);
1279 store = get_storage_resource(ua, 0);
1281 do_storage_setdebug(ua, store, level);
1287 * We didn't find an appropriate keyword above, so
1290 start_prompt(ua, _("Available daemons are: \n"));
1291 add_prompt(ua, _("Director"));
1292 add_prompt(ua, _("Storage"));
1293 add_prompt(ua, _("Client"));
1294 add_prompt(ua, _("All"));
1295 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1296 case 0: /* Director */
1297 debug_level = level;
1300 store = get_storage_resource(ua, 0);
1302 do_storage_setdebug(ua, store, level);
1306 client = select_client_resource(ua);
1308 do_client_setdebug(ua, client, level);
1312 do_all_setdebug(ua, level);
1320 static int var_cmd(UAContext *ua, char *cmd)
1322 POOLMEM *val = get_pool_memory(PM_FNAME);
1328 for (var=ua->cmd; *var != ' '; ) { /* skip command */
1331 while (*var == ' ') { /* skip spaces */
1334 Dmsg1(100, "Var=%s:\n", var);
1335 variable_expansion(ua->jcr, var, &val);
1336 bsendmsg(ua, "%s\n", val);
1337 free_pool_memory(val);
1341 static int estimate_cmd(UAContext *ua, char *cmd)
1344 CLIENT *client = NULL;
1345 FILESET *fileset = NULL;
1348 char since[MAXSTRING];
1350 for (int i=1; i<ua->argc; i++) {
1351 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1352 strcasecmp(ua->argk[i], _("fd")) == 0) {
1354 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1358 if (strcasecmp(ua->argk[i], _("job")) == 0) {
1360 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1364 if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1366 fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1370 if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1374 if (!job && !(client && fileset)) {
1375 if (!(job = select_job_resource(ua))) {
1380 job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1383 client = job->client;
1386 fileset = job->fileset;
1388 ua->jcr->client = client;
1389 ua->jcr->fileset = fileset;
1391 ua->catalog = client->catalog;
1397 get_level_since_time(ua->jcr, since, sizeof(since));
1399 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1400 job->client->hdr.name, job->client->address, job->client->FDport);
1401 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1402 bsendmsg(ua, _("Failed to connect to Client.\n"));
1405 fd = ua->jcr->file_bsock;
1407 if (!send_include_list(ua->jcr)) {
1408 bsendmsg(ua, _("Error sending include list.\n"));
1412 if (!send_exclude_list(ua->jcr)) {
1413 bsendmsg(ua, _("Error sending exclude list.\n"));
1417 if (!send_level_command(ua->jcr)) {
1421 bnet_fsend(fd, "estimate listing=%d\n", listing);
1422 while (bnet_recv(fd) >= 0) {
1423 bsendmsg(ua, "%s", fd->msg);
1426 bnet_sig(fd, BNET_TERMINATE);
1428 ua->jcr->file_bsock = NULL;
1437 static int time_cmd(UAContext *ua, char *cmd)
1440 time_t ttime = time(NULL);
1442 localtime_r(&ttime, &tm);
1443 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1444 bsendmsg(ua, "%s\n", sdt);
1451 * Delete Pool records (should purge Media with it).
1453 * delete pool=<pool-name>
1454 * delete media pool=<pool-name> volume=<name>
1456 static int delete_cmd(UAContext *ua, char *cmd)
1458 static char *keywords[] = {
1469 switch (find_arg_keyword(ua, keywords)) {
1484 "In general it is not a good idea to delete either a\n"
1485 "Pool or a Volume since they may contain data.\n\n"));
1487 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1498 bsendmsg(ua, _("Nothing done.\n"));
1504 static int delete_job(UAContext *ua)
1506 POOLMEM *query = get_pool_memory(PM_MESSAGE);
1509 int i = find_arg_with_value(ua, "jobid");
1511 JobId = str_to_int64(ua->argv[i]);
1512 } else if (!get_pint(ua, _("Enter JobId to delete: "))) {
1515 JobId = ua->pint32_val;
1517 Mmsg(&query, "DELETE FROM Job WHERE JobId=%u", JobId);
1518 db_sql_query(ua->db, query, NULL, (void *)NULL);
1519 Mmsg(&query, "DELETE FROM File WHERE JobId=%u", JobId);
1520 db_sql_query(ua->db, query, NULL, (void *)NULL);
1521 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%u", JobId);
1522 db_sql_query(ua->db, query, NULL, (void *)NULL);
1523 free_pool_memory(query);
1524 bsendmsg(ua, _("Job %u and associated records deleted from the catalog.\n"), JobId);
1529 * Delete media records from database -- dangerous
1531 static int delete_volume(UAContext *ua)
1535 if (!select_media_dbr(ua, &mr)) {
1538 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1539 "and all Jobs saved on that volume from the Catalog\n"),
1542 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1545 if (ua->pint32_val) {
1546 db_delete_media_record(ua->jcr, ua->db, &mr);
1552 * Delete a pool record from the database -- dangerous
1554 static int delete_pool(UAContext *ua)
1558 memset(&pr, 0, sizeof(pr));
1560 if (!get_pool_dbr(ua, &pr)) {
1563 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1566 if (ua->pint32_val) {
1567 db_delete_pool_record(ua->jcr, ua->db, &pr);
1573 static void do_mount_cmd(UAContext *ua, char *command)
1577 char dev_name[MAX_NAME_LENGTH];
1583 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1585 store = get_storage_resource(ua, 1);
1590 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1591 store->media_type, store->dev_name);
1593 ua->jcr->store = store;
1594 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1595 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1598 sd = ua->jcr->store_bsock;
1599 strcpy(dev_name, store->dev_name);
1600 bash_spaces(dev_name);
1601 bnet_fsend(sd, "%s %s", command, dev_name);
1602 while (bnet_recv(sd) >= 0) {
1603 bsendmsg(ua, "%s", sd->msg);
1605 bnet_sig(sd, BNET_TERMINATE);
1607 ua->jcr->store_bsock = NULL;
1611 * mount [storage | device] <name>
1613 static int mount_cmd(UAContext *ua, char *cmd)
1615 do_mount_cmd(ua, "mount"); /* mount */
1621 * unmount [storage | device] <name>
1623 static int unmount_cmd(UAContext *ua, char *cmd)
1625 do_mount_cmd(ua, "unmount"); /* unmount */
1631 * release [storage | device] <name>
1633 static int release_cmd(UAContext *ua, char *cmd)
1635 do_mount_cmd(ua, "release"); /* release */
1642 * use catalog=<name>
1644 static int use_cmd(UAContext *ua, char *cmd)
1646 CAT *oldcatalog, *catalog;
1649 close_db(ua); /* close any previously open db */
1650 oldcatalog = ua->catalog;
1652 if (!(catalog = get_catalog_resource(ua))) {
1653 ua->catalog = oldcatalog;
1655 ua->catalog = catalog;
1658 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1659 ua->catalog->hdr.name, ua->catalog->db_name);
1664 int quit_cmd(UAContext *ua, char *cmd)
1671 * Wait until no job is running
1673 int wait_cmd(UAContext *ua, char *cmd)
1675 bmicrosleep(0, 200000); /* let job actually start */
1676 for (int running=1; running; ) {
1679 for (JCR *jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
1680 if (jcr->JobId != 0) {
1682 free_locked_jcr(jcr);
1685 free_locked_jcr(jcr);
1696 static int help_cmd(UAContext *ua, char *cmd)
1700 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1701 for (i=0; i<comsize; i++) {
1702 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1704 bsendmsg(ua, _("\nWhen at a prompt, entering a period cancels the command.\n\n"));
1708 static int version_cmd(UAContext *ua, char *cmd)
1710 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1715 /* A bit brain damaged in that if the user has not done
1716 * a "use catalog xxx" command, we simply find the first
1717 * catalog resource and open it.
1719 int open_db(UAContext *ua)
1726 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1729 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1732 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1733 ua->catalog->hdr.name, ua->catalog->db_name);
1737 ua->jcr->catalog = ua->catalog;
1739 Dmsg0(150, "Open database\n");
1740 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1741 ua->catalog->db_password, ua->catalog->db_address,
1742 ua->catalog->db_port, ua->catalog->db_socket);
1743 if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
1744 bsendmsg(ua, _("Could not open database \"%s\".\n"),
1745 ua->catalog->db_name);
1747 bsendmsg(ua, "%s", db_strerror(ua->db));
1752 ua->jcr->db = ua->db;
1753 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1757 void close_db(UAContext *ua)
1760 db_close_database(ua->jcr, ua->db);