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 jobid=<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];
187 int Slot = 0, InChanger = 0;
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;
291 if (!get_yesno(ua, _("InChanger? yes/no: "))) {
294 InChanger = ua->pint32_val;
297 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
298 for (i=startnum; i < num+startnum; i++) {
299 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
301 mr.InChanger = InChanger;
302 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
303 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
304 bsendmsg(ua, db_strerror(ua->db));
308 first_id = mr.PoolId;
312 Dmsg0(200, "Update pool record.\n");
313 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
314 bsendmsg(ua, db_strerror(ua->db));
317 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
323 * Turn auto mount on/off
328 int automount_cmd(UAContext *ua, char *cmd)
333 if (!get_cmd(ua, _("Turn on or off? "))) {
341 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
349 static int cancel_cmd(UAContext *ua, char *cmd)
354 char JobName[MAX_NAME_LENGTH];
360 for (i=1; i<ua->argc; i++) {
361 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
365 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
366 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
370 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
374 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
375 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
381 /* If we still do not have a jcr,
382 * throw up a list and ask the user to select one.
385 /* Count Jobs running */
387 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
388 if (jcr->JobId == 0) { /* this is us */
389 free_locked_jcr(jcr);
393 free_locked_jcr(jcr);
398 bsendmsg(ua, _("No Jobs running.\n"));
401 start_prompt(ua, _("Select Job:\n"));
403 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
404 if (jcr->JobId == 0) { /* this is us */
405 free_locked_jcr(jcr);
408 add_prompt(ua, jcr->Job);
409 free_locked_jcr(jcr);
413 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
417 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
421 /* NOTE! This increments the ref_count */
422 jcr = get_jcr_by_full_name(JobName);
424 bsendmsg(ua, _("Job %s not found.\n"), JobName);
429 ret = cancel_job(ua, jcr);
436 * This is a common routine to create or update a
437 * Pool DB base record from a Pool Resource. We handle
438 * the setting of MaxVols and NumVols slightly differently
439 * depending on if we are creating the Pool or we are
440 * simply bringing it into agreement with the resource (updage).
442 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
444 strcpy(pr->PoolType, pool->pool_type);
445 if (op == POOL_OP_CREATE) {
446 pr->MaxVols = pool->max_volumes;
448 } else { /* update pool */
449 if (pr->MaxVols != pool->max_volumes) {
450 pr->MaxVols = pool->max_volumes;
452 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
453 pr->MaxVols = pr->NumVols;
456 pr->UseOnce = pool->use_volume_once;
457 pr->UseCatalog = pool->use_catalog;
458 pr->AcceptAnyVolume = pool->accept_any_volume;
459 pr->Recycle = pool->Recycle;
460 pr->VolRetention = pool->VolRetention;
461 pr->VolUseDuration = pool->VolUseDuration;
462 pr->MaxVolJobs = pool->MaxVolJobs;
463 pr->MaxVolFiles = pool->MaxVolFiles;
464 pr->MaxVolBytes = pool->MaxVolBytes;
465 pr->AutoPrune = pool->AutoPrune;
466 pr->Recycle = pool->Recycle;
467 if (pool->label_format) {
468 strcpy(pr->LabelFormat, pool->label_format);
470 strcpy(pr->LabelFormat, "*"); /* none */
476 * Create a pool record from a given Pool resource
477 * Also called from backup.c
478 * Returns: -1 on error
479 * 0 record already exists
483 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
487 memset(&pr, 0, sizeof(POOL_DBR));
489 strcpy(pr.Name, pool->hdr.name);
491 if (db_get_pool_record(jcr, db, &pr)) {
493 if (op == POOL_OP_UPDATE) { /* update request */
494 set_pooldbr_from_poolres(&pr, pool, op);
495 db_update_pool_record(jcr, db, &pr);
497 return 0; /* exists */
500 set_pooldbr_from_poolres(&pr, pool, op);
502 if (!db_create_pool_record(jcr, db, &pr)) {
503 return -1; /* error */
511 * Create a Pool Record in the database.
512 * It is always created from the Resource record.
514 static int create_cmd(UAContext *ua, char *cmd)
522 pool = get_pool_resource(ua);
527 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
529 bsendmsg(ua, _("Error: Pool %s already exists.\n\
530 Use update to change it.\n"), pool->hdr.name);
534 bsendmsg(ua, db_strerror(ua->db));
540 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
548 * Update a Pool Record in the database.
549 * It is always updated from the Resource record.
551 * update pool=<pool-name>
552 * updates pool from Pool resource
553 * update media pool=<pool-name> volume=<volume-name>
554 * changes pool info for volume
556 static int update_cmd(UAContext *ua, char *cmd)
558 static char *kw[] = {
560 N_("volume"), /* 1 */
569 switch (find_arg_keyword(ua, kw)) {
584 start_prompt(ua, _("Update choice:\n"));
585 add_prompt(ua, _("Volume parameters"));
586 add_prompt(ua, _("Pool from resource"));
587 add_prompt(ua, _("Slots from autochanger"));
588 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
604 static void update_volstatus(UAContext *ua, char *val, MEDIA_DBR *mr)
606 POOLMEM *query = get_pool_memory(PM_MESSAGE);
620 for (i=0; kw[i]; i++) {
621 if (strcasecmp(val, kw[i]) == 0) {
627 bsendmsg(ua, _("Invalid VolStatus specified: %s\n"), val);
629 bstrncpy(mr->VolStatus, kw[i], sizeof(mr->VolStatus));
630 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
631 mr->VolStatus, mr->MediaId);
632 if (!db_sql_query(ua->db, query, NULL, NULL)) {
633 bsendmsg(ua, "%s", db_strerror(ua->db));
635 bsendmsg(ua, _("New Volume status is: %s\n"), mr->VolStatus);
638 free_pool_memory(query);
641 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
645 if (!duration_to_utime(val, &mr->VolRetention)) {
646 bsendmsg(ua, _("Invalid retention period specified: %s\n"), val);
649 query = get_pool_memory(PM_MESSAGE);
650 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
651 edit_uint64(mr->VolRetention, ed1), mr->MediaId);
652 if (!db_sql_query(ua->db, query, NULL, NULL)) {
653 bsendmsg(ua, "%s", db_strerror(ua->db));
655 bsendmsg(ua, _("New retention seconds is: %s\n"),
656 edit_utime(mr->VolRetention, ed1));
658 free_pool_memory(query);
661 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
666 if (!duration_to_utime(val, &mr->VolUseDuration)) {
667 bsendmsg(ua, _("Invalid use duration specified: %s\n"), val);
670 query = get_pool_memory(PM_MESSAGE);
671 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
672 edit_uint64(mr->VolUseDuration, ed1), mr->MediaId);
673 if (!db_sql_query(ua->db, query, NULL, NULL)) {
674 bsendmsg(ua, "%s", db_strerror(ua->db));
676 bsendmsg(ua, _("New use duration is: %s\n"),
677 edit_utime(mr->VolUseDuration, ed1));
679 free_pool_memory(query);
682 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
684 POOLMEM *query = get_pool_memory(PM_MESSAGE);
685 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%u",
687 if (!db_sql_query(ua->db, query, NULL, NULL)) {
688 bsendmsg(ua, "%s", db_strerror(ua->db));
690 bsendmsg(ua, _("New max jobs is: %s\n"), val);
692 free_pool_memory(query);
695 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
697 POOLMEM *query = get_pool_memory(PM_MESSAGE);
698 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%u",
700 if (!db_sql_query(ua->db, query, NULL, NULL)) {
701 bsendmsg(ua, "%s", db_strerror(ua->db));
703 bsendmsg(ua, _("New max files is: %s\n"), val);
705 free_pool_memory(query);
708 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
714 if (!size_to_uint64(val, strlen(val), &maxbytes)) {
715 bsendmsg(ua, _("Invalid max. bytes specification: %s\n"), val);
718 query = get_pool_memory(PM_MESSAGE);
719 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
720 edit_uint64(maxbytes, ed1), mr->MediaId);
721 if (!db_sql_query(ua->db, query, NULL, NULL)) {
722 bsendmsg(ua, "%s", db_strerror(ua->db));
724 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
726 free_pool_memory(query);
729 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
733 if (strcasecmp(val, _("yes")) == 0) {
735 } else if (strcasecmp(val, _("no")) == 0) {
738 bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
741 query = get_pool_memory(PM_MESSAGE);
742 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
743 recycle, mr->MediaId);
744 if (!db_sql_query(ua->db, query, NULL, NULL)) {
745 bsendmsg(ua, "%s", db_strerror(ua->db));
747 bsendmsg(ua, _("New Recycle flag is: %s\n"),
748 mr->Recycle==1?_("yes"):_("no"));
750 free_pool_memory(query);
753 /* Modify the Pool in which this Volume is located */
754 static void update_volpool(UAContext *ua, char *val, MEDIA_DBR *mr)
758 memset(&pr, 0, sizeof(pr));
759 bstrncpy(pr.Name, val, sizeof(pr.Name));
760 if (!get_pool_dbr(ua, &pr)) {
763 mr->PoolId = pr.PoolId; /* set new PoolId */
765 * Make sure to use db_update... rather than doing this directly,
766 * so that any Slot is handled correctly.
768 if (!db_update_media_record(ua->jcr, ua->db, mr)) {
769 bsendmsg(ua, _("Error updating media record Pool: ERR=%s"), db_strerror(ua->db));
771 bsendmsg(ua, _("New Pool is: %s\n"), pr.Name);
776 * Update a media record -- allows you to change the
777 * Volume status. E.g. if you want Bacula to stop
778 * writing on the volume, set it to anything other
781 static int update_volume(UAContext *ua)
789 N_("VolStatus"), /* 0 */
790 N_("VolRetention"), /* 1 */
791 N_("VolUse"), /* 2 */
792 N_("MaxVolJobs"), /* 3 */
793 N_("MaxVolFiles"), /* 4 */
794 N_("MaxVolBytes"), /* 5 */
795 N_("Recycle"), /* 6 */
799 for (int i=0; kw[i]; i++) {
801 if ((j=find_arg_with_value(ua, kw[i])) > 0) {
802 if (!select_media_dbr(ua, &mr)) {
807 update_volstatus(ua, ua->argv[j], &mr);
810 update_volretention(ua, ua->argv[j], &mr);
813 update_voluseduration(ua, ua->argv[j], &mr);
816 update_volmaxjobs(ua, ua->argv[j], &mr);
819 update_volmaxfiles(ua, ua->argv[j], &mr);
822 update_volmaxbytes(ua, ua->argv[j], &mr);
825 update_volrecycle(ua, ua->argv[j], &mr);
828 update_volpool(ua, ua->argv[j], &mr);
835 if (!select_media_dbr(ua, &mr)) {
838 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
839 start_prompt(ua, _("Parameters to modify:\n"));
840 add_prompt(ua, _("Volume Status"));
841 add_prompt(ua, _("Volume Retention Period"));
842 add_prompt(ua, _("Volume Use Duration"));
843 add_prompt(ua, _("Maximum Volume Jobs"));
844 add_prompt(ua, _("Maximum Volume Files"));
845 add_prompt(ua, _("Maximum Volume Bytes"));
846 add_prompt(ua, _("Recycle Flag"));
847 add_prompt(ua, _("Slot"));
848 add_prompt(ua, _("InChanger Flag"));
849 add_prompt(ua, _("Volume Files"));
850 add_prompt(ua, _("Pool"));
851 add_prompt(ua, _("Done"));
852 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
853 case 0: /* Volume Status */
854 /* Modify Volume Status */
855 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
856 start_prompt(ua, _("Possible Values are:\n"));
857 add_prompt(ua, "Append"); /* Better not translate these as */
858 add_prompt(ua, "Archive"); /* They are known in the database code */
859 add_prompt(ua, "Disabled");
860 add_prompt(ua, "Full");
861 add_prompt(ua, "Used");
862 add_prompt(ua, "Cleaning");
863 if (strcmp(mr.VolStatus, "Purged") == 0) {
864 add_prompt(ua, "Recycle");
866 add_prompt(ua, "Read-Only");
867 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
870 update_volstatus(ua, ua->cmd, &mr);
872 case 1: /* Retention */
873 bsendmsg(ua, _("Current retention seconds is: %s\n"),
874 edit_utime(mr.VolRetention, ed1));
875 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
878 update_volretention(ua, ua->cmd, &mr);
881 case 2: /* Use Duration */
882 bsendmsg(ua, _("Current use duration is: %s\n"),
883 edit_utime(mr.VolUseDuration, ed1));
884 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
887 update_voluseduration(ua, ua->cmd, &mr);
890 case 3: /* Max Jobs */
891 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
892 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
895 update_volmaxjobs(ua, ua->cmd, &mr);
898 case 4: /* Max Files */
899 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
900 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
903 update_volmaxfiles(ua, ua->cmd, &mr);
906 case 5: /* Max Bytes */
907 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
908 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
911 update_volmaxbytes(ua, ua->cmd, &mr);
915 case 6: /* Recycle */
916 bsendmsg(ua, _("Current recycle flag is: %s\n"),
917 mr.Recycle==1?_("yes"):_("no"));
918 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
921 update_volrecycle(ua, ua->cmd, &mr);
927 memset(&pr, 0, sizeof(POOL_DBR));
928 pr.PoolId = mr.PoolId;
929 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
930 bsendmsg(ua, "%s", db_strerror(ua->db));
933 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
934 if (!get_pint(ua, _("Enter new Slot: "))) {
937 Slot = ua->pint32_val;
938 if (pr.MaxVols > 0 && Slot > (int)pr.MaxVols) {
939 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
945 * Make sure to use db_update... rather than doing this directly,
946 * so that any Slot is handled correctly.
948 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
949 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
951 bsendmsg(ua, _("New Slot is: %s\n"), mr.Slot);
955 case 8: /* InChanger */
956 bsendmsg(ua, _("Current InChanger flag is: %d\n"), mr.InChanger);
957 if (!get_yesno(ua, _("Set InChanger flag? yes/no: "))) {
960 mr.InChanger = ua->pint32_val;
962 * Make sure to use db_update... rather than doing this directly,
963 * so that any Slot is handled correctly.
965 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
966 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
968 bsendmsg(ua, _("New InChanger flag is: %s\n"), mr.InChanger);
973 case 9: /* Volume Files */
975 bsendmsg(ua, _("Warning changing Volume Files can result\n"
976 "in loss of data on your Volume\n\n"));
977 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
978 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
981 VolFiles = ua->pint32_val;
982 if (VolFiles != (int)(mr.VolFiles + 1)) {
983 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
984 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
988 query = get_pool_memory(PM_MESSAGE);
989 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
990 VolFiles, mr.MediaId);
991 if (!db_sql_query(ua->db, query, NULL, NULL)) {
992 bsendmsg(ua, "%s", db_strerror(ua->db));
994 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
996 free_pool_memory(query);
999 case 10: /* Volume's Pool */
1000 memset(&pr, 0, sizeof(POOL_DBR));
1001 pr.PoolId = mr.PoolId;
1002 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1003 bsendmsg(ua, "%s", db_strerror(ua->db));
1006 bsendmsg(ua, _("Current Pool is: %s\n"), pr.Name);
1007 if (!get_cmd(ua, _("Enter new Pool name: "))) {
1010 update_volpool(ua, ua->cmd, &mr);
1013 default: /* Done or error */
1014 bsendmsg(ua, "Selection done.\n");
1022 * Update pool record -- pull info from current POOL resource
1024 static int update_pool(UAContext *ua)
1031 pool = get_pool_resource(ua);
1036 memset(&pr, 0, sizeof(pr));
1037 strcpy(pr.Name, pool->hdr.name);
1038 if (!get_pool_dbr(ua, &pr)) {
1042 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
1044 id = db_update_pool_record(ua->jcr, ua->db, &pr);
1046 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
1047 id, db_strerror(ua->db));
1049 query = get_pool_memory(PM_MESSAGE);
1050 Mmsg(&query, list_pool, pr.PoolId);
1051 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
1052 free_pool_memory(query);
1053 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
1058 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
1062 ua->jcr->store = store;
1063 /* Try connecting for up to 15 seconds */
1064 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
1065 store->hdr.name, store->address, store->SDport);
1066 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
1067 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1070 Dmsg0(120, _("Connected to storage daemon\n"));
1071 sd = ua->jcr->store_bsock;
1072 bnet_fsend(sd, "setdebug=%d\n", level);
1073 if (bnet_recv(sd) >= 0) {
1074 bsendmsg(ua, "%s", sd->msg);
1076 bnet_sig(sd, BNET_TERMINATE);
1078 ua->jcr->store_bsock = NULL;
1082 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
1086 /* Connect to File daemon */
1088 ua->jcr->client = client;
1089 /* Try to connect for 15 seconds */
1090 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1091 client->hdr.name, client->address, client->FDport);
1092 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1093 bsendmsg(ua, _("Failed to connect to Client.\n"));
1096 Dmsg0(120, "Connected to file daemon\n");
1097 fd = ua->jcr->file_bsock;
1098 bnet_fsend(fd, "setdebug=%d\n", level);
1099 if (bnet_recv(fd) >= 0) {
1100 bsendmsg(ua, "%s", fd->msg);
1102 bnet_sig(fd, BNET_TERMINATE);
1104 ua->jcr->file_bsock = NULL;
1109 static void do_all_setdebug(UAContext *ua, int level)
1111 STORE *store, **unique_store;
1112 CLIENT *client, **unique_client;
1116 debug_level = level;
1118 /* Count Storage items */
1121 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1123 unique_store = (STORE **) malloc(i * sizeof(STORE));
1124 /* Find Unique Storage address/port */
1125 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1127 unique_store[i++] = store;
1128 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1130 for (j=0; j<i; j++) {
1131 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1132 unique_store[j]->SDport == store->SDport) {
1138 unique_store[i++] = store;
1139 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1144 /* Call each unique Storage daemon */
1145 for (j=0; j<i; j++) {
1146 do_storage_setdebug(ua, unique_store[j], level);
1150 /* Count Client items */
1153 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1155 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1156 /* Find Unique Client address/port */
1157 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1159 unique_client[i++] = client;
1160 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1162 for (j=0; j<i; j++) {
1163 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1164 unique_client[j]->FDport == client->FDport) {
1170 unique_client[i++] = client;
1171 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1176 /* Call each unique File daemon */
1177 for (j=0; j<i; j++) {
1178 do_client_setdebug(ua, unique_client[j], level);
1180 free(unique_client);
1184 * setdebug level=nn all
1186 static int setdebug_cmd(UAContext *ua, char *cmd)
1196 Dmsg1(120, "setdebug:%s:\n", cmd);
1199 i = find_arg_with_value(ua, _("level"));
1201 level = atoi(ua->argv[i]);
1204 if (!get_pint(ua, _("Enter new debug level: "))) {
1207 level = ua->pint32_val;
1210 /* General debug? */
1211 for (i=1; i<ua->argc; i++) {
1212 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1213 do_all_setdebug(ua, level);
1216 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1217 strcasecmp(ua->argk[i], _("director")) == 0) {
1218 debug_level = level;
1221 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1222 strcasecmp(ua->argk[i], _("fd")) == 0) {
1225 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1227 do_client_setdebug(ua, client, level);
1231 client = select_client_resource(ua);
1233 do_client_setdebug(ua, client, level);
1238 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1239 strcasecmp(ua->argk[i], _("storage")) == 0 ||
1240 strcasecmp(ua->argk[i], _("sd")) == 0) {
1243 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1245 do_storage_setdebug(ua, store, level);
1249 store = get_storage_resource(ua, 0);
1251 do_storage_setdebug(ua, store, level);
1257 * We didn't find an appropriate keyword above, so
1260 start_prompt(ua, _("Available daemons are: \n"));
1261 add_prompt(ua, _("Director"));
1262 add_prompt(ua, _("Storage"));
1263 add_prompt(ua, _("Client"));
1264 add_prompt(ua, _("All"));
1265 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1266 case 0: /* Director */
1267 debug_level = level;
1270 store = get_storage_resource(ua, 0);
1272 do_storage_setdebug(ua, store, level);
1276 client = select_client_resource(ua);
1278 do_client_setdebug(ua, client, level);
1282 do_all_setdebug(ua, level);
1290 static int var_cmd(UAContext *ua, char *cmd)
1292 POOLMEM *val = get_pool_memory(PM_FNAME);
1298 for (var=ua->cmd; *var != ' '; ) { /* skip command */
1301 while (*var == ' ') { /* skip spaces */
1304 Dmsg1(100, "Var=%s:\n", var);
1305 variable_expansion(ua->jcr, var, &val);
1306 bsendmsg(ua, "%s\n", val);
1307 free_pool_memory(val);
1311 static int estimate_cmd(UAContext *ua, char *cmd)
1314 CLIENT *client = NULL;
1315 FILESET *fileset = NULL;
1318 char since[MAXSTRING];
1320 for (int i=1; i<ua->argc; i++) {
1321 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1322 strcasecmp(ua->argk[i], _("fd")) == 0) {
1324 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1328 if (strcasecmp(ua->argk[i], _("job")) == 0) {
1330 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1334 if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1336 fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1340 if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1344 if (!job && !(client && fileset)) {
1345 if (!(job = select_job_resource(ua))) {
1350 job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1353 client = job->client;
1356 fileset = job->fileset;
1358 ua->jcr->client = client;
1359 ua->jcr->fileset = fileset;
1361 ua->catalog = client->catalog;
1367 get_level_since_time(ua->jcr, since, sizeof(since));
1369 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1370 job->client->hdr.name, job->client->address, job->client->FDport);
1371 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1372 bsendmsg(ua, _("Failed to connect to Client.\n"));
1375 fd = ua->jcr->file_bsock;
1377 if (!send_include_list(ua->jcr)) {
1378 bsendmsg(ua, _("Error sending include list.\n"));
1382 if (!send_exclude_list(ua->jcr)) {
1383 bsendmsg(ua, _("Error sending exclude list.\n"));
1387 if (!send_level_command(ua->jcr)) {
1391 bnet_fsend(fd, "estimate listing=%d\n", listing);
1392 while (bnet_recv(fd) >= 0) {
1393 bsendmsg(ua, "%s", fd->msg);
1396 bnet_sig(fd, BNET_TERMINATE);
1398 ua->jcr->file_bsock = NULL;
1407 static int time_cmd(UAContext *ua, char *cmd)
1410 time_t ttime = time(NULL);
1412 localtime_r(&ttime, &tm);
1413 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1414 bsendmsg(ua, "%s\n", sdt);
1421 * Delete Pool records (should purge Media with it).
1423 * delete pool=<pool-name>
1424 * delete media pool=<pool-name> volume=<name>
1426 static int delete_cmd(UAContext *ua, char *cmd)
1428 static char *keywords[] = {
1439 switch (find_arg_keyword(ua, keywords)) {
1454 "In general it is not a good idea to delete either a\n"
1455 "Pool or a Volume since they may contain data.\n\n"));
1457 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1468 bsendmsg(ua, _("Nothing done.\n"));
1474 static int delete_job(UAContext *ua)
1476 POOLMEM *query = get_pool_memory(PM_MESSAGE);
1479 int i = find_arg_with_value(ua, "jobid");
1481 JobId = str_to_int64(ua->argv[i]);
1482 } else if (!get_pint(ua, _("Enter JobId to delete: "))) {
1485 JobId = ua->pint32_val;
1487 Mmsg(&query, "DELETE FROM Job WHERE JobId=%u", JobId);
1488 db_sql_query(ua->db, query, NULL, (void *)NULL);
1489 Mmsg(&query, "DELETE FROM File WHERE JobId=%u", JobId);
1490 db_sql_query(ua->db, query, NULL, (void *)NULL);
1491 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%u", JobId);
1492 db_sql_query(ua->db, query, NULL, (void *)NULL);
1493 free_pool_memory(query);
1494 bsendmsg(ua, _("Job %u and associated records deleted from the catalog.\n"), JobId);
1499 * Delete media records from database -- dangerous
1501 static int delete_volume(UAContext *ua)
1505 if (!select_media_dbr(ua, &mr)) {
1508 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1509 "and all Jobs saved on that volume from the Catalog\n"),
1512 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1515 if (ua->pint32_val) {
1516 db_delete_media_record(ua->jcr, ua->db, &mr);
1522 * Delete a pool record from the database -- dangerous
1524 static int delete_pool(UAContext *ua)
1528 memset(&pr, 0, sizeof(pr));
1530 if (!get_pool_dbr(ua, &pr)) {
1533 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1536 if (ua->pint32_val) {
1537 db_delete_pool_record(ua->jcr, ua->db, &pr);
1543 static void do_mount_cmd(UAContext *ua, char *command)
1547 char dev_name[MAX_NAME_LENGTH];
1553 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1555 store = get_storage_resource(ua, 1);
1560 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1561 store->media_type, store->dev_name);
1563 ua->jcr->store = store;
1564 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1565 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1568 sd = ua->jcr->store_bsock;
1569 strcpy(dev_name, store->dev_name);
1570 bash_spaces(dev_name);
1571 bnet_fsend(sd, "%s %s", command, dev_name);
1572 while (bnet_recv(sd) >= 0) {
1573 bsendmsg(ua, "%s", sd->msg);
1575 bnet_sig(sd, BNET_TERMINATE);
1577 ua->jcr->store_bsock = NULL;
1581 * mount [storage | device] <name>
1583 static int mount_cmd(UAContext *ua, char *cmd)
1585 do_mount_cmd(ua, "mount"); /* mount */
1591 * unmount [storage | device] <name>
1593 static int unmount_cmd(UAContext *ua, char *cmd)
1595 do_mount_cmd(ua, "unmount"); /* unmount */
1601 * release [storage | device] <name>
1603 static int release_cmd(UAContext *ua, char *cmd)
1605 do_mount_cmd(ua, "release"); /* release */
1612 * use catalog=<name>
1614 static int use_cmd(UAContext *ua, char *cmd)
1616 CAT *oldcatalog, *catalog;
1619 close_db(ua); /* close any previously open db */
1620 oldcatalog = ua->catalog;
1622 if (!(catalog = get_catalog_resource(ua))) {
1623 ua->catalog = oldcatalog;
1625 ua->catalog = catalog;
1628 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1629 ua->catalog->hdr.name, ua->catalog->db_name);
1634 int quit_cmd(UAContext *ua, char *cmd)
1641 * Wait until no job is running
1643 int wait_cmd(UAContext *ua, char *cmd)
1645 bmicrosleep(0, 200000); /* let job actually start */
1646 for (int running=1; running; ) {
1649 for (JCR *jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
1650 if (jcr->JobId != 0) {
1652 free_locked_jcr(jcr);
1655 free_locked_jcr(jcr);
1666 static int help_cmd(UAContext *ua, char *cmd)
1670 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1671 for (i=0; i<comsize; i++) {
1672 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1674 bsendmsg(ua, _("\nWhen at a prompt, entering a period cancels the command.\n\n"));
1678 static int version_cmd(UAContext *ua, char *cmd)
1680 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1685 /* A bit brain damaged in that if the user has not done
1686 * a "use catalog xxx" command, we simply find the first
1687 * catalog resource and open it.
1689 int open_db(UAContext *ua)
1696 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1699 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1702 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1703 ua->catalog->hdr.name, ua->catalog->db_name);
1707 ua->jcr->catalog = ua->catalog;
1709 Dmsg0(150, "Open database\n");
1710 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1711 ua->catalog->db_password, ua->catalog->db_address,
1712 ua->catalog->db_port, ua->catalog->db_socket);
1713 if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
1714 bsendmsg(ua, _("Could not open database \"%s\".\n"),
1715 ua->catalog->db_name);
1717 bsendmsg(ua, "%s", db_strerror(ua->db));
1722 ua->jcr->db = ua->db;
1723 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1727 void close_db(UAContext *ua)
1730 db_close_database(ua->jcr, ua->db);