3 * Bacula Director -- User Agent Commands
5 * Kern Sibbald, September MM
11 Copyright (C) 2000-2004 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);
83 static int setip_cmd(UAContext *ua, char *cmd);
85 int quit_cmd(UAContext *ua, char *cmd);
88 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
89 static struct cmdstruct commands[] = {
90 { N_("add"), add_cmd, _("add media to a pool")},
91 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
92 { N_("automount"), automount_cmd, _("automount [on/off] -- after label")},
93 { N_("cancel"), cancel_cmd, _("cancel job=nnn -- cancel a job")},
94 { N_("create"), create_cmd, _("create DB Pool from resource")},
95 { N_("delete"), delete_cmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
96 { N_("estimate"), estimate_cmd, _("performs FileSet estimate, listing gives full listing")},
97 { N_("exit"), quit_cmd, _("exit = quit")},
98 { N_("help"), help_cmd, _("print this command")},
99 { N_("list"), list_cmd, _("list [pools | jobs | jobtotals | media <pool> | files jobid=<nn>]; from catalog")},
100 { N_("label"), label_cmd, _("label a tape")},
101 { N_("llist"), llist_cmd, _("full or long list like list command")},
102 { N_("messages"), messagescmd, _("messages")},
103 { N_("mount"), mount_cmd, _("mount <storage-name>")},
104 { N_("prune"), prunecmd, _("prune expired records from catalog")},
105 { N_("purge"), purgecmd, _("purge records from catalog")},
106 { N_("quit"), quit_cmd, _("quit")},
107 { N_("query"), querycmd, _("query catalog")},
108 { N_("restore"), restore_cmd, _("restore files")},
109 { N_("relabel"), relabel_cmd, _("relabel a tape")},
110 { N_("release"), release_cmd, _("release <storage-name>")},
111 { N_("run"), run_cmd, _("run <job-name>")},
112 { N_("status"), status_cmd, _("status [storage | client]=<name>")},
113 { N_("setdebug"), setdebug_cmd, _("sets debug level")},
114 { N_("setip"), setip_cmd, _("sets new client address -- if authorized")},
115 { N_("show"), show_cmd, _("show (resource records) [jobs | pools | ... | all]")},
116 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
117 { N_("time"), time_cmd, _("print current time")},
118 { N_("unmount"), unmount_cmd, _("unmount <storage-name>")},
119 { N_("update"), update_cmd, _("update Volume or Pool")},
120 { N_("use"), use_cmd, _("use catalog xxx")},
121 { N_("var"), var_cmd, _("does variable expansion")},
122 { N_("version"), version_cmd, _("print Director version")},
123 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
125 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
128 * Execute a command from the UA
130 int do_a_command(UAContext *ua, char *cmd)
138 Dmsg1(200, "Command: %s\n", ua->UA_sock->msg);
143 len = strlen(ua->argk[0]);
144 for (i=0; i<comsize; i++) { /* search for command */
145 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
146 if (!acl_access_ok(ua, Command_ACL, ua->argk[0], len)) {
149 stat = (*commands[i].func)(ua, cmd); /* go execute command */
155 bnet_fsend(ua->UA_sock, _("%s: is an illegal command.\n"), ua->argk[0]);
161 * This is a common routine used to stuff the Pool DB record defaults
162 * into the Media DB record just before creating a media (Volume)
165 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
167 mr->PoolId = pr->PoolId;
168 bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));
169 mr->Recycle = pr->Recycle;
170 mr->VolRetention = pr->VolRetention;
171 mr->VolUseDuration = pr->VolUseDuration;
172 mr->MaxVolJobs = pr->MaxVolJobs;
173 mr->MaxVolFiles = pr->MaxVolFiles;
174 mr->MaxVolBytes = pr->MaxVolBytes;
179 * Add Volumes to an existing Pool
181 static int add_cmd(UAContext *ua, char *cmd)
185 int num, i, max, startnum;
187 char name[MAX_NAME_LENGTH];
189 int Slot = 0, InChanger = 0;
192 "You probably don't want to be using this command since it\n"
193 "creates database records without labeling the Volumes.\n"
194 "You probably want to use the \"label\" command.\n\n"));
200 memset(&pr, 0, sizeof(pr));
201 memset(&mr, 0, sizeof(mr));
203 if (!get_pool_dbr(ua, &pr)) {
207 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
208 pr.MaxVols, pr.PoolType);
210 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
211 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
213 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
216 pr.MaxVols = ua->pint32_val;
221 if ((store = get_storage_resource(ua, 0)) != NULL) {
222 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
223 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
227 if (pr.MaxVols == 0) {
230 max = pr.MaxVols - pr.NumVols;
234 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
235 if (!get_pint(ua, buf)) {
238 num = ua->pint32_val;
239 if (num < 0 || num > max) {
240 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
247 if (!get_cmd(ua, _("Enter Volume name: "))) {
251 if (!get_cmd(ua, _("Enter base volume name: "))) {
255 /* Don't allow | in Volume name because it is the volume separator character */
256 if (!is_volume_name_legal(ua, ua->cmd)) {
259 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
260 bsendmsg(ua, _("Volume name too long.\n"));
263 if (strlen(ua->cmd) == 0) {
264 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
268 bstrncpy(name, ua->cmd, sizeof(name));
270 strcat(name, "%04d");
273 if (!get_pint(ua, _("Enter the starting number: "))) {
276 startnum = ua->pint32_val;
278 bsendmsg(ua, _("Start number must be greater than zero.\n"));
288 if (store && store->autochanger) {
289 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
292 Slot = ua->pint32_val;
293 if (!get_yesno(ua, _("InChanger? yes/no: "))) {
296 InChanger = ua->pint32_val;
299 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
300 for (i=startnum; i < num+startnum; i++) {
301 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
303 mr.InChanger = InChanger;
304 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
305 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
306 bsendmsg(ua, db_strerror(ua->db));
310 first_id = mr.PoolId;
314 Dmsg0(200, "Update pool record.\n");
315 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
316 bsendmsg(ua, db_strerror(ua->db));
319 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
325 * Turn auto mount on/off
330 int automount_cmd(UAContext *ua, char *cmd)
335 if (!get_cmd(ua, _("Turn on or off? "))) {
343 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
351 static int cancel_cmd(UAContext *ua, char *cmd)
356 char JobName[MAX_NAME_LENGTH];
362 for (i=1; i<ua->argc; i++) {
363 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
368 JobId = str_to_int64(ua->argv[i]);
369 if (!(jcr=get_jcr_by_id(JobId))) {
370 bsendmsg(ua, _("JobId %d is not running.\n"), JobId);
374 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
378 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
379 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
385 /* If we still do not have a jcr,
386 * throw up a list and ask the user to select one.
389 /* Count Jobs running */
392 if (jcr->JobId == 0) { /* this is us */
393 free_locked_jcr(jcr);
396 free_locked_jcr(jcr);
402 bsendmsg(ua, _("No Jobs running.\n"));
405 start_prompt(ua, _("Select Job:\n"));
408 if (jcr->JobId == 0) { /* this is us */
409 free_locked_jcr(jcr);
412 add_prompt(ua, jcr->Job);
413 free_locked_jcr(jcr);
417 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
421 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
425 /* NOTE! This increments the ref_count */
426 jcr = get_jcr_by_full_name(JobName);
428 bsendmsg(ua, _("Job %s not found.\n"), JobName);
433 ret = cancel_job(ua, jcr);
440 * This is a common routine to create or update a
441 * Pool DB base record from a Pool Resource. We handle
442 * the setting of MaxVols and NumVols slightly differently
443 * depending on if we are creating the Pool or we are
444 * simply bringing it into agreement with the resource (updage).
446 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
448 strcpy(pr->PoolType, pool->pool_type);
449 if (op == POOL_OP_CREATE) {
450 pr->MaxVols = pool->max_volumes;
452 } else { /* update pool */
453 if (pr->MaxVols != pool->max_volumes) {
454 pr->MaxVols = pool->max_volumes;
456 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
457 pr->MaxVols = pr->NumVols;
460 pr->UseOnce = pool->use_volume_once;
461 pr->UseCatalog = pool->use_catalog;
462 pr->AcceptAnyVolume = pool->accept_any_volume;
463 pr->Recycle = pool->Recycle;
464 pr->VolRetention = pool->VolRetention;
465 pr->VolUseDuration = pool->VolUseDuration;
466 pr->MaxVolJobs = pool->MaxVolJobs;
467 pr->MaxVolFiles = pool->MaxVolFiles;
468 pr->MaxVolBytes = pool->MaxVolBytes;
469 pr->AutoPrune = pool->AutoPrune;
470 pr->Recycle = pool->Recycle;
471 if (pool->label_format) {
472 strcpy(pr->LabelFormat, pool->label_format);
474 strcpy(pr->LabelFormat, "*"); /* none */
480 * Create a pool record from a given Pool resource
481 * Also called from backup.c
482 * Returns: -1 on error
483 * 0 record already exists
487 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
491 memset(&pr, 0, sizeof(POOL_DBR));
493 strcpy(pr.Name, pool->hdr.name);
495 if (db_get_pool_record(jcr, db, &pr)) {
497 if (op == POOL_OP_UPDATE) { /* update request */
498 set_pooldbr_from_poolres(&pr, pool, op);
499 db_update_pool_record(jcr, db, &pr);
501 return 0; /* exists */
504 set_pooldbr_from_poolres(&pr, pool, op);
506 if (!db_create_pool_record(jcr, db, &pr)) {
507 return -1; /* error */
515 * Create a Pool Record in the database.
516 * It is always created from the Resource record.
518 static int create_cmd(UAContext *ua, char *cmd)
526 pool = get_pool_resource(ua);
531 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
533 bsendmsg(ua, _("Error: Pool %s already exists.\n"
534 "Use update to change it.\n"), pool->hdr.name);
538 bsendmsg(ua, db_strerror(ua->db));
544 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
550 * Set a new address in a Client resource. We do this only
551 * if the Console name is the same as the Client name
552 * and the Console can access the client.
554 static int setip_cmd(UAContext *ua, char *cmd)
557 if (!ua->cons || !acl_access_ok(ua, Client_ACL, ua->cons->hdr.name)) {
558 bsendmsg(ua, _("Illegal command from this console.\n"));
561 client = (CLIENT *)GetResWithName(R_CLIENT, ua->cons->hdr.name);
564 bsendmsg(ua, _("Client \"%s\" not found.\n"), ua->cons->hdr.name);
568 if (client->address) {
569 free(client->address);
571 client->address = bstrdup(inet_ntoa(ua->UA_sock->client_addr.sin_addr));
572 bsendmsg(ua, _("Client \"%s\" address set to %s\n"),
573 client->hdr.name, client->address);
580 * Update a Pool Record in the database.
581 * It is always updated from the Resource record.
583 * update pool=<pool-name>
584 * updates pool from Pool resource
585 * update media pool=<pool-name> volume=<volume-name>
586 * changes pool info for volume
588 static int update_cmd(UAContext *ua, char *cmd)
590 static char *kw[] = {
592 N_("volume"), /* 1 */
601 switch (find_arg_keyword(ua, kw)) {
616 start_prompt(ua, _("Update choice:\n"));
617 add_prompt(ua, _("Volume parameters"));
618 add_prompt(ua, _("Pool from resource"));
619 add_prompt(ua, _("Slots from autochanger"));
620 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
636 static void update_volstatus(UAContext *ua, char *val, MEDIA_DBR *mr)
638 POOLMEM *query = get_pool_memory(PM_MESSAGE);
652 for (i=0; kw[i]; i++) {
653 if (strcasecmp(val, kw[i]) == 0) {
659 bsendmsg(ua, _("Invalid VolStatus specified: %s\n"), val);
661 bstrncpy(mr->VolStatus, kw[i], sizeof(mr->VolStatus));
662 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
663 mr->VolStatus, mr->MediaId);
664 if (!db_sql_query(ua->db, query, NULL, NULL)) {
665 bsendmsg(ua, "%s", db_strerror(ua->db));
667 bsendmsg(ua, _("New Volume status is: %s\n"), mr->VolStatus);
670 free_pool_memory(query);
673 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
677 if (!duration_to_utime(val, &mr->VolRetention)) {
678 bsendmsg(ua, _("Invalid retention period specified: %s\n"), val);
681 query = get_pool_memory(PM_MESSAGE);
682 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
683 edit_uint64(mr->VolRetention, ed1), mr->MediaId);
684 if (!db_sql_query(ua->db, query, NULL, NULL)) {
685 bsendmsg(ua, "%s", db_strerror(ua->db));
687 bsendmsg(ua, _("New retention seconds is: %s\n"),
688 edit_utime(mr->VolRetention, ed1));
690 free_pool_memory(query);
693 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
698 if (!duration_to_utime(val, &mr->VolUseDuration)) {
699 bsendmsg(ua, _("Invalid use duration specified: %s\n"), val);
702 query = get_pool_memory(PM_MESSAGE);
703 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
704 edit_uint64(mr->VolUseDuration, ed1), mr->MediaId);
705 if (!db_sql_query(ua->db, query, NULL, NULL)) {
706 bsendmsg(ua, "%s", db_strerror(ua->db));
708 bsendmsg(ua, _("New use duration is: %s\n"),
709 edit_utime(mr->VolUseDuration, ed1));
711 free_pool_memory(query);
714 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
716 POOLMEM *query = get_pool_memory(PM_MESSAGE);
717 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%u",
719 if (!db_sql_query(ua->db, query, NULL, NULL)) {
720 bsendmsg(ua, "%s", db_strerror(ua->db));
722 bsendmsg(ua, _("New max jobs is: %s\n"), val);
724 free_pool_memory(query);
727 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
729 POOLMEM *query = get_pool_memory(PM_MESSAGE);
730 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%u",
732 if (!db_sql_query(ua->db, query, NULL, NULL)) {
733 bsendmsg(ua, "%s", db_strerror(ua->db));
735 bsendmsg(ua, _("New max files is: %s\n"), val);
737 free_pool_memory(query);
740 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
746 if (!size_to_uint64(val, strlen(val), &maxbytes)) {
747 bsendmsg(ua, _("Invalid max. bytes specification: %s\n"), val);
750 query = get_pool_memory(PM_MESSAGE);
751 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
752 edit_uint64(maxbytes, ed1), mr->MediaId);
753 if (!db_sql_query(ua->db, query, NULL, NULL)) {
754 bsendmsg(ua, "%s", db_strerror(ua->db));
756 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
758 free_pool_memory(query);
761 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
765 if (strcasecmp(val, _("yes")) == 0) {
767 } else if (strcasecmp(val, _("no")) == 0) {
770 bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
773 query = get_pool_memory(PM_MESSAGE);
774 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
775 recycle, mr->MediaId);
776 if (!db_sql_query(ua->db, query, NULL, NULL)) {
777 bsendmsg(ua, "%s", db_strerror(ua->db));
779 bsendmsg(ua, _("New Recycle flag is: %s\n"),
780 mr->Recycle==1?_("yes"):_("no"));
782 free_pool_memory(query);
785 /* Modify the Pool in which this Volume is located */
786 static void update_volpool(UAContext *ua, char *val, MEDIA_DBR *mr)
790 memset(&pr, 0, sizeof(pr));
791 bstrncpy(pr.Name, val, sizeof(pr.Name));
792 if (!get_pool_dbr(ua, &pr)) {
795 mr->PoolId = pr.PoolId; /* set new PoolId */
797 * Make sure to use db_update... rather than doing this directly,
798 * so that any Slot is handled correctly.
800 if (!db_update_media_record(ua->jcr, ua->db, mr)) {
801 bsendmsg(ua, _("Error updating media record Pool: ERR=%s"), db_strerror(ua->db));
803 bsendmsg(ua, _("New Pool is: %s\n"), pr.Name);
808 * Update a media record -- allows you to change the
809 * Volume status. E.g. if you want Bacula to stop
810 * writing on the volume, set it to anything other
813 static int update_volume(UAContext *ua)
821 N_("VolStatus"), /* 0 */
822 N_("VolRetention"), /* 1 */
823 N_("VolUse"), /* 2 */
824 N_("MaxVolJobs"), /* 3 */
825 N_("MaxVolFiles"), /* 4 */
826 N_("MaxVolBytes"), /* 5 */
827 N_("Recycle"), /* 6 */
831 for (int i=0; kw[i]; i++) {
833 if ((j=find_arg_with_value(ua, kw[i])) > 0) {
834 if (!select_media_dbr(ua, &mr)) {
839 update_volstatus(ua, ua->argv[j], &mr);
842 update_volretention(ua, ua->argv[j], &mr);
845 update_voluseduration(ua, ua->argv[j], &mr);
848 update_volmaxjobs(ua, ua->argv[j], &mr);
851 update_volmaxfiles(ua, ua->argv[j], &mr);
854 update_volmaxbytes(ua, ua->argv[j], &mr);
857 update_volrecycle(ua, ua->argv[j], &mr);
860 update_volpool(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, _("InChanger Flag"));
881 add_prompt(ua, _("Volume Files"));
882 add_prompt(ua, _("Pool"));
883 add_prompt(ua, _("Done"));
884 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
885 case 0: /* Volume Status */
886 /* Modify Volume Status */
887 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
888 start_prompt(ua, _("Possible Values are:\n"));
889 add_prompt(ua, "Append"); /* Better not translate these as */
890 add_prompt(ua, "Archive"); /* They are known in the database code */
891 add_prompt(ua, "Disabled");
892 add_prompt(ua, "Full");
893 add_prompt(ua, "Used");
894 add_prompt(ua, "Cleaning");
895 if (strcmp(mr.VolStatus, "Purged") == 0) {
896 add_prompt(ua, "Recycle");
898 add_prompt(ua, "Read-Only");
899 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
902 update_volstatus(ua, ua->cmd, &mr);
904 case 1: /* Retention */
905 bsendmsg(ua, _("Current retention seconds is: %s\n"),
906 edit_utime(mr.VolRetention, ed1));
907 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
910 update_volretention(ua, ua->cmd, &mr);
913 case 2: /* Use Duration */
914 bsendmsg(ua, _("Current use duration is: %s\n"),
915 edit_utime(mr.VolUseDuration, ed1));
916 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
919 update_voluseduration(ua, ua->cmd, &mr);
922 case 3: /* Max Jobs */
923 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
924 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
927 update_volmaxjobs(ua, ua->cmd, &mr);
930 case 4: /* Max Files */
931 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
932 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
935 update_volmaxfiles(ua, ua->cmd, &mr);
938 case 5: /* Max Bytes */
939 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
940 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
943 update_volmaxbytes(ua, ua->cmd, &mr);
947 case 6: /* Recycle */
948 bsendmsg(ua, _("Current recycle flag is: %s\n"),
949 mr.Recycle==1?_("yes"):_("no"));
950 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
953 update_volrecycle(ua, ua->cmd, &mr);
959 memset(&pr, 0, sizeof(POOL_DBR));
960 pr.PoolId = mr.PoolId;
961 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
962 bsendmsg(ua, "%s", db_strerror(ua->db));
965 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
966 if (!get_pint(ua, _("Enter new Slot: "))) {
969 Slot = ua->pint32_val;
970 if (pr.MaxVols > 0 && Slot > (int)pr.MaxVols) {
971 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
977 * Make sure to use db_update... rather than doing this directly,
978 * so that any Slot is handled correctly.
980 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
981 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
983 bsendmsg(ua, _("New Slot is: %s\n"), mr.Slot);
987 case 8: /* InChanger */
988 bsendmsg(ua, _("Current InChanger flag is: %d\n"), mr.InChanger);
989 if (!get_yesno(ua, _("Set InChanger flag? yes/no: "))) {
992 mr.InChanger = ua->pint32_val;
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 InChanger flag is: %d\n"), mr.InChanger);
1005 case 9: /* Volume Files */
1007 bsendmsg(ua, _("Warning changing Volume Files can result\n"
1008 "in loss of data on your Volume\n\n"));
1009 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
1010 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
1013 VolFiles = ua->pint32_val;
1014 if (VolFiles != (int)(mr.VolFiles + 1)) {
1015 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
1016 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
1020 query = get_pool_memory(PM_MESSAGE);
1021 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
1022 VolFiles, mr.MediaId);
1023 if (!db_sql_query(ua->db, query, NULL, NULL)) {
1024 bsendmsg(ua, "%s", db_strerror(ua->db));
1026 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
1028 free_pool_memory(query);
1031 case 10: /* Volume's Pool */
1032 memset(&pr, 0, sizeof(POOL_DBR));
1033 pr.PoolId = mr.PoolId;
1034 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1035 bsendmsg(ua, "%s", db_strerror(ua->db));
1038 bsendmsg(ua, _("Current Pool is: %s\n"), pr.Name);
1039 if (!get_cmd(ua, _("Enter new Pool name: "))) {
1042 update_volpool(ua, ua->cmd, &mr);
1045 default: /* Done or error */
1046 bsendmsg(ua, "Selection done.\n");
1054 * Update pool record -- pull info from current POOL resource
1056 static int update_pool(UAContext *ua)
1063 pool = get_pool_resource(ua);
1068 memset(&pr, 0, sizeof(pr));
1069 strcpy(pr.Name, pool->hdr.name);
1070 if (!get_pool_dbr(ua, &pr)) {
1074 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
1076 id = db_update_pool_record(ua->jcr, ua->db, &pr);
1078 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
1079 id, db_strerror(ua->db));
1081 query = get_pool_memory(PM_MESSAGE);
1082 Mmsg(&query, list_pool, pr.PoolId);
1083 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
1084 free_pool_memory(query);
1085 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
1090 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
1094 ua->jcr->store = store;
1095 /* Try connecting for up to 15 seconds */
1096 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
1097 store->hdr.name, store->address, store->SDport);
1098 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
1099 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1102 Dmsg0(120, _("Connected to storage daemon\n"));
1103 sd = ua->jcr->store_bsock;
1104 bnet_fsend(sd, "setdebug=%d\n", level);
1105 if (bnet_recv(sd) >= 0) {
1106 bsendmsg(ua, "%s", sd->msg);
1108 bnet_sig(sd, BNET_TERMINATE);
1110 ua->jcr->store_bsock = NULL;
1114 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
1118 /* Connect to File daemon */
1120 ua->jcr->client = client;
1121 /* Try to connect for 15 seconds */
1122 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1123 client->hdr.name, client->address, client->FDport);
1124 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1125 bsendmsg(ua, _("Failed to connect to Client.\n"));
1128 Dmsg0(120, "Connected to file daemon\n");
1129 fd = ua->jcr->file_bsock;
1130 bnet_fsend(fd, "setdebug=%d\n", level);
1131 if (bnet_recv(fd) >= 0) {
1132 bsendmsg(ua, "%s", fd->msg);
1134 bnet_sig(fd, BNET_TERMINATE);
1136 ua->jcr->file_bsock = NULL;
1141 static void do_all_setdebug(UAContext *ua, int level)
1143 STORE *store, **unique_store;
1144 CLIENT *client, **unique_client;
1148 debug_level = level;
1150 /* Count Storage items */
1153 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1155 unique_store = (STORE **) malloc(i * sizeof(STORE));
1156 /* Find Unique Storage address/port */
1157 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1159 unique_store[i++] = store;
1160 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1162 for (j=0; j<i; j++) {
1163 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1164 unique_store[j]->SDport == store->SDport) {
1170 unique_store[i++] = store;
1171 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1176 /* Call each unique Storage daemon */
1177 for (j=0; j<i; j++) {
1178 do_storage_setdebug(ua, unique_store[j], level);
1182 /* Count Client items */
1185 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1187 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1188 /* Find Unique Client address/port */
1189 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1191 unique_client[i++] = client;
1192 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1194 for (j=0; j<i; j++) {
1195 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1196 unique_client[j]->FDport == client->FDport) {
1202 unique_client[i++] = client;
1203 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1208 /* Call each unique File daemon */
1209 for (j=0; j<i; j++) {
1210 do_client_setdebug(ua, unique_client[j], level);
1212 free(unique_client);
1216 * setdebug level=nn all
1218 static int setdebug_cmd(UAContext *ua, char *cmd)
1228 Dmsg1(120, "setdebug:%s:\n", cmd);
1231 i = find_arg_with_value(ua, _("level"));
1233 level = atoi(ua->argv[i]);
1236 if (!get_pint(ua, _("Enter new debug level: "))) {
1239 level = ua->pint32_val;
1242 /* General debug? */
1243 for (i=1; i<ua->argc; i++) {
1244 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1245 do_all_setdebug(ua, level);
1248 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1249 strcasecmp(ua->argk[i], _("director")) == 0) {
1250 debug_level = level;
1253 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1254 strcasecmp(ua->argk[i], _("fd")) == 0) {
1257 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1259 do_client_setdebug(ua, client, level);
1263 client = select_client_resource(ua);
1265 do_client_setdebug(ua, client, level);
1270 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1271 strcasecmp(ua->argk[i], _("storage")) == 0 ||
1272 strcasecmp(ua->argk[i], _("sd")) == 0) {
1275 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1277 do_storage_setdebug(ua, store, level);
1281 store = get_storage_resource(ua, 0);
1283 do_storage_setdebug(ua, store, level);
1289 * We didn't find an appropriate keyword above, so
1292 start_prompt(ua, _("Available daemons are: \n"));
1293 add_prompt(ua, _("Director"));
1294 add_prompt(ua, _("Storage"));
1295 add_prompt(ua, _("Client"));
1296 add_prompt(ua, _("All"));
1297 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1298 case 0: /* Director */
1299 debug_level = level;
1302 store = get_storage_resource(ua, 0);
1304 do_storage_setdebug(ua, store, level);
1308 client = select_client_resource(ua);
1310 do_client_setdebug(ua, client, level);
1314 do_all_setdebug(ua, level);
1322 static int var_cmd(UAContext *ua, char *cmd)
1324 POOLMEM *val = get_pool_memory(PM_FNAME);
1330 for (var=ua->cmd; *var != ' '; ) { /* skip command */
1333 while (*var == ' ') { /* skip spaces */
1336 Dmsg1(100, "Var=%s:\n", var);
1337 variable_expansion(ua->jcr, var, &val);
1338 bsendmsg(ua, "%s\n", val);
1339 free_pool_memory(val);
1343 static int estimate_cmd(UAContext *ua, char *cmd)
1346 CLIENT *client = NULL;
1347 FILESET *fileset = NULL;
1350 char since[MAXSTRING];
1352 for (int i=1; i<ua->argc; i++) {
1353 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1354 strcasecmp(ua->argk[i], _("fd")) == 0) {
1356 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1360 if (strcasecmp(ua->argk[i], _("job")) == 0) {
1362 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1366 if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1368 fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1372 if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1376 if (!job && !(client && fileset)) {
1377 if (!(job = select_job_resource(ua))) {
1382 job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1385 client = job->client;
1388 fileset = job->fileset;
1390 ua->jcr->client = client;
1391 ua->jcr->fileset = fileset;
1393 ua->catalog = client->catalog;
1399 get_level_since_time(ua->jcr, since, sizeof(since));
1401 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1402 job->client->hdr.name, job->client->address, job->client->FDport);
1403 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1404 bsendmsg(ua, _("Failed to connect to Client.\n"));
1407 fd = ua->jcr->file_bsock;
1409 if (!send_include_list(ua->jcr)) {
1410 bsendmsg(ua, _("Error sending include list.\n"));
1414 if (!send_exclude_list(ua->jcr)) {
1415 bsendmsg(ua, _("Error sending exclude list.\n"));
1419 if (!send_level_command(ua->jcr)) {
1423 bnet_fsend(fd, "estimate listing=%d\n", listing);
1424 while (bnet_recv(fd) >= 0) {
1425 bsendmsg(ua, "%s", fd->msg);
1428 bnet_sig(fd, BNET_TERMINATE);
1430 ua->jcr->file_bsock = NULL;
1439 static int time_cmd(UAContext *ua, char *cmd)
1442 time_t ttime = time(NULL);
1444 localtime_r(&ttime, &tm);
1445 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1446 bsendmsg(ua, "%s\n", sdt);
1453 * Delete Pool records (should purge Media with it).
1455 * delete pool=<pool-name>
1456 * delete media pool=<pool-name> volume=<name>
1458 static int delete_cmd(UAContext *ua, char *cmd)
1460 static char *keywords[] = {
1471 switch (find_arg_keyword(ua, keywords)) {
1486 "In general it is not a good idea to delete either a\n"
1487 "Pool or a Volume since they may contain data.\n\n"));
1489 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1500 bsendmsg(ua, _("Nothing done.\n"));
1506 static int delete_job(UAContext *ua)
1508 POOLMEM *query = get_pool_memory(PM_MESSAGE);
1511 int i = find_arg_with_value(ua, "jobid");
1513 JobId = str_to_int64(ua->argv[i]);
1514 } else if (!get_pint(ua, _("Enter JobId to delete: "))) {
1517 JobId = ua->pint32_val;
1519 Mmsg(&query, "DELETE FROM Job WHERE JobId=%u", JobId);
1520 db_sql_query(ua->db, query, NULL, (void *)NULL);
1521 Mmsg(&query, "DELETE FROM File WHERE JobId=%u", JobId);
1522 db_sql_query(ua->db, query, NULL, (void *)NULL);
1523 Mmsg(&query, "DELETE FROM JobMedia WHERE JobId=%u", JobId);
1524 db_sql_query(ua->db, query, NULL, (void *)NULL);
1525 free_pool_memory(query);
1526 bsendmsg(ua, _("Job %u and associated records deleted from the catalog.\n"), JobId);
1531 * Delete media records from database -- dangerous
1533 static int delete_volume(UAContext *ua)
1537 if (!select_media_dbr(ua, &mr)) {
1540 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1541 "and all Jobs saved on that volume from the Catalog\n"),
1544 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1547 if (ua->pint32_val) {
1548 db_delete_media_record(ua->jcr, ua->db, &mr);
1554 * Delete a pool record from the database -- dangerous
1556 static int delete_pool(UAContext *ua)
1560 memset(&pr, 0, sizeof(pr));
1562 if (!get_pool_dbr(ua, &pr)) {
1565 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1568 if (ua->pint32_val) {
1569 db_delete_pool_record(ua->jcr, ua->db, &pr);
1575 static void do_mount_cmd(UAContext *ua, char *command)
1579 char dev_name[MAX_NAME_LENGTH];
1585 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1587 store = get_storage_resource(ua, 1);
1592 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1593 store->media_type, store->dev_name);
1595 ua->jcr->store = store;
1596 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1597 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1600 sd = ua->jcr->store_bsock;
1601 strcpy(dev_name, store->dev_name);
1602 bash_spaces(dev_name);
1603 bnet_fsend(sd, "%s %s", command, dev_name);
1604 while (bnet_recv(sd) >= 0) {
1605 bsendmsg(ua, "%s", sd->msg);
1607 bnet_sig(sd, BNET_TERMINATE);
1609 ua->jcr->store_bsock = NULL;
1613 * mount [storage | device] <name>
1615 static int mount_cmd(UAContext *ua, char *cmd)
1617 do_mount_cmd(ua, "mount"); /* mount */
1623 * unmount [storage | device] <name>
1625 static int unmount_cmd(UAContext *ua, char *cmd)
1627 do_mount_cmd(ua, "unmount"); /* unmount */
1633 * release [storage | device] <name>
1635 static int release_cmd(UAContext *ua, char *cmd)
1637 do_mount_cmd(ua, "release"); /* release */
1644 * use catalog=<name>
1646 static int use_cmd(UAContext *ua, char *cmd)
1648 CAT *oldcatalog, *catalog;
1651 close_db(ua); /* close any previously open db */
1652 oldcatalog = ua->catalog;
1654 if (!(catalog = get_catalog_resource(ua))) {
1655 ua->catalog = oldcatalog;
1657 ua->catalog = catalog;
1660 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1661 ua->catalog->hdr.name, ua->catalog->db_name);
1666 int quit_cmd(UAContext *ua, char *cmd)
1673 * Wait until no job is running
1675 int wait_cmd(UAContext *ua, char *cmd)
1678 bmicrosleep(0, 200000); /* let job actually start */
1679 for (bool running=true; running; ) {
1683 if (jcr->JobId != 0) {
1685 free_locked_jcr(jcr);
1688 free_locked_jcr(jcr);
1699 static int help_cmd(UAContext *ua, char *cmd)
1703 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1704 for (i=0; i<comsize; i++) {
1705 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1707 bsendmsg(ua, _("\nWhen at a prompt, entering a period cancels the command.\n\n"));
1711 static int version_cmd(UAContext *ua, char *cmd)
1713 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1718 /* A bit brain damaged in that if the user has not done
1719 * a "use catalog xxx" command, we simply find the first
1720 * catalog resource and open it.
1722 int open_db(UAContext *ua)
1729 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1732 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1735 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1736 ua->catalog->hdr.name, ua->catalog->db_name);
1740 ua->jcr->catalog = ua->catalog;
1742 Dmsg0(150, "Open database\n");
1743 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1744 ua->catalog->db_password, ua->catalog->db_address,
1745 ua->catalog->db_port, ua->catalog->db_socket);
1746 if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
1747 bsendmsg(ua, _("Could not open database \"%s\".\n"),
1748 ua->catalog->db_name);
1750 bsendmsg(ua, "%s", db_strerror(ua->db));
1755 ua->jcr->db = ua->db;
1756 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1760 void close_db(UAContext *ua)
1763 db_close_database(ua->jcr, ua->db);