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 statuscmd(UAContext *ua, char *cmd);
46 extern int listcmd(UAContext *ua, char *cmd);
47 extern int llistcmd(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 runcmd(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 restorecmd(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), createcmd(UAContext *ua, char *cmd), cancelcmd(UAContext *ua, char *cmd);
64 static int setdebug_cmd(UAContext *ua, char *cmd);
65 static int var_cmd(UAContext *ua, char *cmd);
66 static int estimate_cmd(UAContext *ua, char *cmd);
67 static int help_cmd(UAContext *ua, char *cmd);
68 static int delete_cmd(UAContext *ua, char *cmd);
69 static int use_cmd(UAContext *ua, char *cmd), unmount_cmd(UAContext *ua, char *cmd);
70 static int version_cmd(UAContext *ua, char *cmd), automount_cmd(UAContext *ua, char *cmd);
71 static int time_cmd(UAContext *ua, char *cmd);
72 static int update_volume(UAContext *ua);
73 static int update_pool(UAContext *ua);
74 static int delete_volume(UAContext *ua);
75 static int delete_pool(UAContext *ua);
76 static int mount_cmd(UAContext *ua, char *cmd);
77 static int release_cmd(UAContext *ua, char *cmd);
78 static int update_cmd(UAContext *ua, char *cmd);
79 static int wait_cmd(UAContext *ua, char *cmd);
81 int quit_cmd(UAContext *ua, char *cmd);
84 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
85 static struct cmdstruct commands[] = {
86 { N_("add"), add_cmd, _("add media to a pool")},
87 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
88 { N_("automount"), automount_cmd, _("automount [on/off] -- after label")},
89 { N_("cancel"), cancelcmd, _("cancel job=nnn -- cancel a job")},
90 { N_("create"), createcmd, _("create DB Pool from resource")},
91 { N_("delete"), delete_cmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
92 { N_("estimate"), estimate_cmd, _("performs FileSet estimate, listing gives full listing")},
93 { N_("exit"), quit_cmd, _("exit = quit")},
94 { N_("help"), help_cmd, _("print this command")},
95 { N_("label"), label_cmd, _("label a tape")},
96 { N_("list"), listcmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
97 { N_("llist"), llistcmd, _("full or long list like list command")},
98 { N_("messages"), messagescmd, _("messages")},
99 { N_("mount"), mount_cmd, _("mount <storage-name>")},
100 { N_("prune"), prunecmd, _("prune expired records from catalog")},
101 { N_("purge"), purgecmd, _("purge records from catalog")},
102 { N_("query"), querycmd, _("query catalog")},
103 { N_("quit"), quit_cmd, _("quit")},
104 { N_("relabel"), relabel_cmd, _("relabel a tape")},
105 { N_("release"), release_cmd, _("release <storage-name>")},
106 { N_("restore"), restorecmd, _("restore files")},
107 { N_("run"), runcmd, _("run <job-name>")},
108 { N_("setdebug"), setdebug_cmd, _("sets debug level")},
109 { N_("show"), show_cmd, _("show (resource records) [jobs | pools | ... | all]")},
110 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
111 { N_("status"), statuscmd, _("status [storage | client]=<name>")},
112 { N_("time"), time_cmd, _("print current time")},
113 { N_("unmount"), unmount_cmd, _("unmount <storage-name>")},
114 { N_("update"), update_cmd, _("update Volume or Pool")},
115 { N_("use"), use_cmd, _("use catalog xxx")},
116 { N_("var"), var_cmd, _("does variable expansion")},
117 { N_("version"), version_cmd, _("print Director version")},
118 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
120 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
123 * Execute a command from the UA
125 int do_a_command(UAContext *ua, char *cmd)
134 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
139 len = strlen(ua->argk[0]);
140 for (i=0; i<comsize; i++) { /* search for command */
141 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
142 stat = (*commands[i].func)(ua, cmd); /* go execute command */
148 pm_strcat(&ua->UA_sock->msg, _(": is an illegal command\n"));
149 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
150 bnet_send(ua->UA_sock);
156 * This is a common routine used to stuff the Pool DB record defaults
157 * into the Media DB record just before creating a media (Volume)
160 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
162 mr->PoolId = pr->PoolId;
163 strcpy(mr->VolStatus, "Append");
164 mr->Recycle = pr->Recycle;
165 mr->VolRetention = pr->VolRetention;
166 mr->VolUseDuration = pr->VolUseDuration;
167 mr->MaxVolJobs = pr->MaxVolJobs;
168 mr->MaxVolFiles = pr->MaxVolFiles;
169 mr->MaxVolBytes = pr->MaxVolBytes;
174 * Add Volumes to an existing Pool
176 static int add_cmd(UAContext *ua, char *cmd)
180 int num, i, max, startnum;
182 char name[MAX_NAME_LENGTH];
187 "You probably don't want to be using this command since it\n"
188 "creates database records without labeling the Volumes.\n"
189 "You probably want to use the \"label\" command.\n\n"));
195 memset(&pr, 0, sizeof(pr));
196 memset(&mr, 0, sizeof(mr));
198 if (!get_pool_dbr(ua, &pr)) {
202 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
203 pr.MaxVols, pr.PoolType);
205 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
206 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
208 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
211 pr.MaxVols = ua->pint32_val;
216 if ((store = get_storage_resource(ua, 0)) != NULL) {
217 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
218 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
222 if (pr.MaxVols == 0) {
225 max = pr.MaxVols - pr.NumVols;
229 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
230 if (!get_pint(ua, buf)) {
233 num = ua->pint32_val;
234 if (num < 0 || num > max) {
235 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
242 if (!get_cmd(ua, _("Enter Volume name: "))) {
246 if (!get_cmd(ua, _("Enter base volume name: "))) {
250 /* Don't allow | in Volume name because it is the volume separator character */
251 if (!is_volume_name_legal(ua, ua->cmd)) {
254 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
255 bsendmsg(ua, _("Volume name too long.\n"));
258 if (strlen(ua->cmd) == 0) {
259 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
263 bstrncpy(name, ua->cmd, sizeof(name));
265 strcat(name, "%04d");
268 if (!get_pint(ua, _("Enter the starting number: "))) {
271 startnum = ua->pint32_val;
273 bsendmsg(ua, _("Start number must be greater than zero.\n"));
283 if (store && store->autochanger) {
284 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
287 slot = ua->pint32_val;
290 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
291 for (i=startnum; i < num+startnum; i++) {
292 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
294 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
295 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
296 bsendmsg(ua, db_strerror(ua->db));
300 first_id = mr.PoolId;
304 Dmsg0(200, "Update pool record.\n");
305 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
306 bsendmsg(ua, db_strerror(ua->db));
309 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
315 * Turn auto mount on/off
320 int automount_cmd(UAContext *ua, char *cmd)
325 if (!get_cmd(ua, _("Turn on or off? "))) {
333 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
341 static int cancelcmd(UAContext *ua, char *cmd)
347 char JobName[MAX_NAME_LENGTH];
353 for (i=1; i<ua->argc; i++) {
354 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
358 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
359 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
363 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
367 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
368 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
374 /* If we still do not have a jcr,
375 * throw up a list and ask the user to select one.
378 /* Count Jobs running */
380 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
381 if (jcr->JobId == 0) { /* this is us */
382 free_locked_jcr(jcr);
386 free_locked_jcr(jcr);
391 bsendmsg(ua, _("No Jobs running.\n"));
394 start_prompt(ua, _("Select Job:\n"));
396 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
397 if (jcr->JobId == 0) { /* this is us */
398 free_locked_jcr(jcr);
401 add_prompt(ua, jcr->Job);
402 free_locked_jcr(jcr);
406 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
410 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
414 jcr = get_jcr_by_full_name(JobName);
416 bsendmsg(ua, _("Job %s not found.\n"), JobName);
421 switch (jcr->JobStatus) {
424 case JS_WaitClientRes:
425 case JS_WaitStoreRes:
426 case JS_WaitPriority:
428 case JS_WaitStartTime:
429 set_jcr_job_status(jcr, JS_Canceled);
430 bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
431 jcr->JobId, jcr->Job);
432 jobq_remove(&job_queue, jcr); /* attempt to remove it from queue */
433 free_jcr(jcr); /* this decrements the use count only */
437 set_jcr_job_status(jcr, JS_Canceled);
439 /* Cancel File daemon */
440 if (jcr->file_bsock) {
441 ua->jcr->client = jcr->client;
442 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
443 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
447 Dmsg0(200, "Connected to file daemon\n");
448 fd = ua->jcr->file_bsock;
449 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
450 while (bnet_recv(fd) >= 0) {
451 bsendmsg(ua, "%s", fd->msg);
453 bnet_sig(fd, BNET_TERMINATE);
455 ua->jcr->file_bsock = NULL;
458 /* Cancel Storage daemon */
459 if (jcr->store_bsock) {
460 ua->jcr->store = jcr->store;
461 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
462 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
466 Dmsg0(200, "Connected to storage daemon\n");
467 sd = ua->jcr->store_bsock;
468 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
469 while (bnet_recv(sd) >= 0) {
470 bsendmsg(ua, "%s", sd->msg);
472 bnet_sig(sd, BNET_TERMINATE);
474 ua->jcr->store_bsock = NULL;
483 * This is a common routine to create or update a
484 * Pool DB base record from a Pool Resource. We handle
485 * the setting of MaxVols and NumVols slightly differently
486 * depending on if we are creating the Pool or we are
487 * simply bringing it into agreement with the resource (updage).
489 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
491 strcpy(pr->PoolType, pool->pool_type);
492 if (op == POOL_OP_CREATE) {
493 pr->MaxVols = pool->max_volumes;
495 } else { /* update pool */
496 if (pr->MaxVols != pool->max_volumes) {
497 pr->MaxVols = pool->max_volumes;
499 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
500 pr->MaxVols = pr->NumVols;
503 pr->UseOnce = pool->use_volume_once;
504 pr->UseCatalog = pool->use_catalog;
505 pr->AcceptAnyVolume = pool->accept_any_volume;
506 pr->Recycle = pool->Recycle;
507 pr->VolRetention = pool->VolRetention;
508 pr->VolUseDuration = pool->VolUseDuration;
509 pr->MaxVolJobs = pool->MaxVolJobs;
510 pr->MaxVolFiles = pool->MaxVolFiles;
511 pr->MaxVolBytes = pool->MaxVolBytes;
512 pr->AutoPrune = pool->AutoPrune;
513 pr->Recycle = pool->Recycle;
514 if (pool->label_format) {
515 strcpy(pr->LabelFormat, pool->label_format);
517 strcpy(pr->LabelFormat, "*"); /* none */
523 * Create a pool record from a given Pool resource
524 * Also called from backup.c
525 * Returns: -1 on error
526 * 0 record already exists
530 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
534 memset(&pr, 0, sizeof(POOL_DBR));
536 strcpy(pr.Name, pool->hdr.name);
538 if (db_get_pool_record(jcr, db, &pr)) {
540 if (op == POOL_OP_UPDATE) { /* update request */
541 set_pooldbr_from_poolres(&pr, pool, op);
542 db_update_pool_record(jcr, db, &pr);
544 return 0; /* exists */
547 set_pooldbr_from_poolres(&pr, pool, op);
549 if (!db_create_pool_record(jcr, db, &pr)) {
550 return -1; /* error */
558 * Create a Pool Record in the database.
559 * It is always created from the Resource record.
561 static int createcmd(UAContext *ua, char *cmd)
569 pool = get_pool_resource(ua);
574 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
576 bsendmsg(ua, _("Error: Pool %s already exists.\n\
577 Use update to change it.\n"), pool->hdr.name);
581 bsendmsg(ua, db_strerror(ua->db));
587 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
595 * Update a Pool Record in the database.
596 * It is always updated from the Resource record.
598 * update pool=<pool-name>
599 * updates pool from Pool resource
600 * update media pool=<pool-name> volume=<volume-name>
601 * changes pool info for volume
603 static int update_cmd(UAContext *ua, char *cmd)
605 static char *kw[] = {
607 N_("volume"), /* 1 */
616 switch (find_arg_keyword(ua, kw)) {
631 start_prompt(ua, _("Update choice:\n"));
632 add_prompt(ua, _("Volume parameters"));
633 add_prompt(ua, _("Pool from resource"));
634 add_prompt(ua, _("Slots from autochanger"));
635 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
651 static void update_volstatus(UAContext *ua, char *val, MEDIA_DBR *mr)
653 POOLMEM *query = get_pool_memory(PM_MESSAGE);
667 for (i=0; kw[i]; i++) {
668 if (strcasecmp(val, kw[i]) == 0) {
674 bsendmsg(ua, _("Invalid VolStatus specified: %s\n"), val);
676 bstrncpy(mr->VolStatus, kw[i], sizeof(mr->VolStatus));
677 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
678 mr->VolStatus, mr->MediaId);
679 if (!db_sql_query(ua->db, query, NULL, NULL)) {
680 bsendmsg(ua, "%s", db_strerror(ua->db));
682 bsendmsg(ua, _("New Volume status is: %s\n"), mr->VolStatus);
685 free_pool_memory(query);
688 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
692 if (!duration_to_utime(val, &mr->VolRetention)) {
693 bsendmsg(ua, _("Invalid retention period specified: %s\n"), val);
696 query = get_pool_memory(PM_MESSAGE);
697 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
698 edit_uint64(mr->VolRetention, ed1), mr->MediaId);
699 if (!db_sql_query(ua->db, query, NULL, NULL)) {
700 bsendmsg(ua, "%s", db_strerror(ua->db));
702 bsendmsg(ua, _("New retention seconds is: %s\n"),
703 edit_utime(mr->VolRetention, ed1));
705 free_pool_memory(query);
708 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
713 if (!duration_to_utime(val, &mr->VolUseDuration)) {
714 bsendmsg(ua, _("Invalid use duration specified: %s\n"), val);
717 query = get_pool_memory(PM_MESSAGE);
718 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
719 edit_uint64(mr->VolUseDuration, ed1), mr->MediaId);
720 if (!db_sql_query(ua->db, query, NULL, NULL)) {
721 bsendmsg(ua, "%s", db_strerror(ua->db));
723 bsendmsg(ua, _("New use duration is: %s\n"),
724 edit_utime(mr->VolUseDuration, ed1));
726 free_pool_memory(query);
729 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
731 POOLMEM *query = get_pool_memory(PM_MESSAGE);
732 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%u",
734 if (!db_sql_query(ua->db, query, NULL, NULL)) {
735 bsendmsg(ua, "%s", db_strerror(ua->db));
737 bsendmsg(ua, _("New max jobs is: %s\n"), val);
739 free_pool_memory(query);
742 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
744 POOLMEM *query = get_pool_memory(PM_MESSAGE);
745 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%u",
747 if (!db_sql_query(ua->db, query, NULL, NULL)) {
748 bsendmsg(ua, "%s", db_strerror(ua->db));
750 bsendmsg(ua, _("New max files is: %s\n"), val);
752 free_pool_memory(query);
755 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
761 if (!size_to_uint64(val, strlen(val), &maxbytes)) {
762 bsendmsg(ua, _("Invalid max. bytes specification: %s\n"), val);
765 query = get_pool_memory(PM_MESSAGE);
766 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
767 edit_uint64(maxbytes, ed1), mr->MediaId);
768 if (!db_sql_query(ua->db, query, NULL, NULL)) {
769 bsendmsg(ua, "%s", db_strerror(ua->db));
771 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
773 free_pool_memory(query);
776 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
780 if (strcasecmp(val, _("yes")) == 0) {
782 } else if (strcasecmp(val, _("no")) == 0) {
785 bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
788 query = get_pool_memory(PM_MESSAGE);
789 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
790 recycle, mr->MediaId);
791 if (!db_sql_query(ua->db, query, NULL, NULL)) {
792 bsendmsg(ua, "%s", db_strerror(ua->db));
794 bsendmsg(ua, _("New recycle flag is: %s\n"),
795 mr->Recycle==1?_("yes"):_("no"));
797 free_pool_memory(query);
801 * Update a media record -- allows you to change the
802 * Volume status. E.g. if you want Bacula to stop
803 * writing on the volume, set it to anything other
806 static int update_volume(UAContext *ua)
813 N_("VolStatus"), /* 0 */
814 N_("VolRetention"), /* 1 */
815 N_("VolUse"), /* 2 */
816 N_("MaxVolJobs"), /* 3 */
817 N_("MaxVolFiles"), /* 4 */
818 N_("MaxVolBytes"), /* 5 */
819 N_("Recycle"), /* 6 */
822 if (!select_media_dbr(ua, &mr)) {
825 for (int i=0; kw[i]; i++) {
827 if ((j=find_arg_with_value(ua, kw[i])) > 0) {
830 update_volstatus(ua, ua->argv[j], &mr);
833 update_volretention(ua, ua->argv[j], &mr);
836 update_voluseduration(ua, ua->argv[j], &mr);
839 update_volmaxjobs(ua, ua->argv[j], &mr);
842 update_volmaxfiles(ua, ua->argv[j], &mr);
845 update_volmaxbytes(ua, ua->argv[j], &mr);
848 update_volrecycle(ua, ua->argv[j], &mr);
856 if (!select_media_dbr(ua, &mr)) {
859 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
860 start_prompt(ua, _("Parameters to modify:\n"));
861 add_prompt(ua, _("Volume Status"));
862 add_prompt(ua, _("Volume Retention Period"));
863 add_prompt(ua, _("Volume Use Duration"));
864 add_prompt(ua, _("Maximum Volume Jobs"));
865 add_prompt(ua, _("Maximum Volume Files"));
866 add_prompt(ua, _("Maximum Volume Bytes"));
867 add_prompt(ua, _("Recycle Flag"));
868 add_prompt(ua, _("Slot"));
869 add_prompt(ua, _("Volume Files"));
870 add_prompt(ua, _("Done"));
871 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
872 case 0: /* Volume Status */
873 /* Modify Volume Status */
874 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
875 start_prompt(ua, _("Possible Values are:\n"));
876 add_prompt(ua, "Append"); /* Better not translate these as */
877 add_prompt(ua, "Archive"); /* They are known in the database code */
878 add_prompt(ua, "Disabled");
879 add_prompt(ua, "Full");
880 add_prompt(ua, "Used");
881 add_prompt(ua, "Cleaning");
882 if (strcmp(mr.VolStatus, "Purged") == 0) {
883 add_prompt(ua, "Recycle");
885 add_prompt(ua, "Read-Only");
886 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
889 update_volstatus(ua, ua->cmd, &mr);
891 case 1: /* Retention */
892 bsendmsg(ua, _("Current retention seconds is: %s\n"),
893 edit_utime(mr.VolRetention, ed1));
894 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
897 update_volretention(ua, ua->cmd, &mr);
900 case 2: /* Use Duration */
901 bsendmsg(ua, _("Current use duration is: %s\n"),
902 edit_utime(mr.VolUseDuration, ed1));
903 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
906 update_voluseduration(ua, ua->cmd, &mr);
909 case 3: /* Max Jobs */
910 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
911 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
914 update_volmaxjobs(ua, ua->cmd, &mr);
917 case 4: /* Max Files */
918 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
919 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
922 update_volmaxfiles(ua, ua->cmd, &mr);
925 case 5: /* Max Bytes */
926 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
927 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
930 update_volmaxbytes(ua, ua->cmd, &mr);
934 case 6: /* Recycle */
935 bsendmsg(ua, _("Current recycle flag is: %s\n"),
936 mr.Recycle==1?_("yes"):_("no"));
937 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
940 update_volrecycle(ua, ua->cmd, &mr);
947 memset(&pr, 0, sizeof(POOL_DBR));
948 pr.PoolId = mr.PoolId;
949 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
950 bsendmsg(ua, "%s", db_strerror(ua->db));
953 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
954 if (!get_pint(ua, _("Enter new Slot: "))) {
957 slot = ua->pint32_val;
958 if (pr.MaxVols > 0 && slot > (int)pr.MaxVols) {
959 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
963 query = get_pool_memory(PM_MESSAGE);
964 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
966 if (!db_sql_query(ua->db, query, NULL, NULL)) {
967 bsendmsg(ua, "%s", db_strerror(ua->db));
969 bsendmsg(ua, "New Slot is: %d\n", slot);
971 free_pool_memory(query);
974 case 8: /* Volume Files */
976 bsendmsg(ua, _("Warning changing Volume Files can result\n"
977 "in loss of data on your Volume\n\n"));
978 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
979 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
982 VolFiles = ua->pint32_val;
983 if (VolFiles != (int)(mr.VolFiles + 1)) {
984 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
985 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
989 query = get_pool_memory(PM_MESSAGE);
990 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
991 VolFiles, mr.MediaId);
992 if (!db_sql_query(ua->db, query, NULL, NULL)) {
993 bsendmsg(ua, "%s", db_strerror(ua->db));
995 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
997 free_pool_memory(query);
1000 default: /* Done or error */
1001 bsendmsg(ua, "Selection done.\n");
1009 * Update pool record -- pull info from current POOL resource
1011 static int update_pool(UAContext *ua)
1018 pool = get_pool_resource(ua);
1023 memset(&pr, 0, sizeof(pr));
1024 strcpy(pr.Name, pool->hdr.name);
1025 if (!get_pool_dbr(ua, &pr)) {
1029 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
1031 id = db_update_pool_record(ua->jcr, ua->db, &pr);
1033 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
1034 id, db_strerror(ua->db));
1036 query = get_pool_memory(PM_MESSAGE);
1037 Mmsg(&query, list_pool, pr.PoolId);
1038 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
1039 free_pool_memory(query);
1040 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
1045 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
1049 ua->jcr->store = store;
1050 /* Try connecting for up to 15 seconds */
1051 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
1052 store->hdr.name, store->address, store->SDport);
1053 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
1054 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1057 Dmsg0(120, _("Connected to storage daemon\n"));
1058 sd = ua->jcr->store_bsock;
1059 bnet_fsend(sd, "setdebug=%d\n", level);
1060 if (bnet_recv(sd) >= 0) {
1061 bsendmsg(ua, "%s", sd->msg);
1063 bnet_sig(sd, BNET_TERMINATE);
1065 ua->jcr->store_bsock = NULL;
1069 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
1073 /* Connect to File daemon */
1075 ua->jcr->client = client;
1076 /* Try to connect for 15 seconds */
1077 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1078 client->hdr.name, client->address, client->FDport);
1079 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1080 bsendmsg(ua, _("Failed to connect to Client.\n"));
1083 Dmsg0(120, "Connected to file daemon\n");
1084 fd = ua->jcr->file_bsock;
1085 bnet_fsend(fd, "setdebug=%d\n", level);
1086 if (bnet_recv(fd) >= 0) {
1087 bsendmsg(ua, "%s", fd->msg);
1089 bnet_sig(fd, BNET_TERMINATE);
1091 ua->jcr->file_bsock = NULL;
1096 static void do_all_setdebug(UAContext *ua, int level)
1098 STORE *store, **unique_store;
1099 CLIENT *client, **unique_client;
1103 debug_level = level;
1105 /* Count Storage items */
1108 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1110 unique_store = (STORE **) malloc(i * sizeof(STORE));
1111 /* Find Unique Storage address/port */
1112 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1114 unique_store[i++] = store;
1115 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1117 for (j=0; j<i; j++) {
1118 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1119 unique_store[j]->SDport == store->SDport) {
1125 unique_store[i++] = store;
1126 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1131 /* Call each unique Storage daemon */
1132 for (j=0; j<i; j++) {
1133 do_storage_setdebug(ua, unique_store[j], level);
1137 /* Count Client items */
1140 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1142 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1143 /* Find Unique Client address/port */
1144 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1146 unique_client[i++] = client;
1147 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1149 for (j=0; j<i; j++) {
1150 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1151 unique_client[j]->FDport == client->FDport) {
1157 unique_client[i++] = client;
1158 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1163 /* Call each unique File daemon */
1164 for (j=0; j<i; j++) {
1165 do_client_setdebug(ua, unique_client[j], level);
1167 free(unique_client);
1171 * setdebug level=nn all
1173 static int setdebug_cmd(UAContext *ua, char *cmd)
1183 Dmsg1(120, "setdebug:%s:\n", cmd);
1186 i = find_arg_with_value(ua, _("level"));
1188 level = atoi(ua->argv[i]);
1191 if (!get_pint(ua, _("Enter new debug level: "))) {
1194 level = ua->pint32_val;
1197 /* General debug? */
1198 for (i=1; i<ua->argc; i++) {
1199 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1200 do_all_setdebug(ua, level);
1203 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1204 strcasecmp(ua->argk[i], _("director")) == 0) {
1205 debug_level = level;
1208 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1211 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1213 do_client_setdebug(ua, client, level);
1217 client = select_client_resource(ua);
1219 do_client_setdebug(ua, client, level);
1224 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1225 strcasecmp(ua->argk[i], _("storage")) == 0) {
1228 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1230 do_storage_setdebug(ua, store, level);
1234 store = get_storage_resource(ua, 0);
1236 do_storage_setdebug(ua, store, level);
1242 * We didn't find an appropriate keyword above, so
1245 start_prompt(ua, _("Available daemons are: \n"));
1246 add_prompt(ua, _("Director"));
1247 add_prompt(ua, _("Storage"));
1248 add_prompt(ua, _("Client"));
1249 add_prompt(ua, _("All"));
1250 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1251 case 0: /* Director */
1252 debug_level = level;
1255 store = get_storage_resource(ua, 0);
1257 do_storage_setdebug(ua, store, level);
1261 client = select_client_resource(ua);
1263 do_client_setdebug(ua, client, level);
1267 do_all_setdebug(ua, level);
1275 static int var_cmd(UAContext *ua, char *cmd)
1277 POOLMEM *var = get_pool_memory(PM_FNAME);
1278 for (int i=1; i<ua->argc; i++) {
1279 if (ua->argk[i] && variable_expansion(ua->jcr, ua->argk[i], &var)) {
1280 bsendmsg(ua, "%s\n", var);
1283 free_pool_memory(var);
1287 static int estimate_cmd(UAContext *ua, char *cmd)
1290 CLIENT *client = NULL;
1291 FILESET *fileset = NULL;
1294 char since[MAXSTRING];
1296 for (int i=1; i<ua->argc; i++) {
1297 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1299 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1303 if (strcasecmp(ua->argk[i], _("job")) == 0) {
1305 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1309 if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1311 fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1315 if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1319 if (!job && !(client && fileset)) {
1320 if (!(job = select_job_resource(ua))) {
1325 job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1328 client = job->client;
1331 fileset = job->fileset;
1333 ua->jcr->client = client;
1334 ua->jcr->fileset = fileset;
1336 ua->catalog = client->catalog;
1342 get_level_since_time(ua->jcr, since, sizeof(since));
1344 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1345 job->client->hdr.name, job->client->address, job->client->FDport);
1346 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1347 bsendmsg(ua, _("Failed to connect to Client.\n"));
1350 fd = ua->jcr->file_bsock;
1352 if (!send_include_list(ua->jcr)) {
1353 bsendmsg(ua, _("Error sending include list.\n"));
1357 if (!send_exclude_list(ua->jcr)) {
1358 bsendmsg(ua, _("Error sending exclude list.\n"));
1362 if (!send_level_command(ua->jcr)) {
1366 bnet_fsend(fd, "estimate listing=%d\n", listing);
1367 while (bnet_recv(fd) >= 0) {
1368 bsendmsg(ua, "%s", fd->msg);
1371 bnet_sig(fd, BNET_TERMINATE);
1373 ua->jcr->file_bsock = NULL;
1382 static int time_cmd(UAContext *ua, char *cmd)
1385 time_t ttime = time(NULL);
1387 localtime_r(&ttime, &tm);
1388 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1389 bsendmsg(ua, "%s\n", sdt);
1396 * Delete Pool records (should purge Media with it).
1398 * delete pool=<pool-name>
1399 * delete media pool=<pool-name> volume=<name>
1401 static int delete_cmd(UAContext *ua, char *cmd)
1403 static char *keywords[] = {
1413 "In general it is not a good idea to delete either a\n"
1414 "Pool or a Volume since they may contain data.\n\n"));
1416 switch (find_arg_keyword(ua, keywords)) {
1426 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1434 bsendmsg(ua, _("Nothing done.\n"));
1441 * Delete media records from database -- dangerous
1443 static int delete_volume(UAContext *ua)
1447 if (!select_media_dbr(ua, &mr)) {
1450 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1451 "and all Jobs saved on that volume from the Catalog\n"),
1454 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1457 if (ua->pint32_val) {
1458 db_delete_media_record(ua->jcr, ua->db, &mr);
1464 * Delete a pool record from the database -- dangerous
1466 static int delete_pool(UAContext *ua)
1470 memset(&pr, 0, sizeof(pr));
1472 if (!get_pool_dbr(ua, &pr)) {
1475 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1478 if (ua->pint32_val) {
1479 db_delete_pool_record(ua->jcr, ua->db, &pr);
1485 static void do_mount_cmd(UAContext *ua, char *command)
1489 char dev_name[MAX_NAME_LENGTH];
1495 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1497 store = get_storage_resource(ua, 1);
1502 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1503 store->media_type, store->dev_name);
1505 ua->jcr->store = store;
1506 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1507 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1510 sd = ua->jcr->store_bsock;
1511 strcpy(dev_name, store->dev_name);
1512 bash_spaces(dev_name);
1513 bnet_fsend(sd, "%s %s", command, dev_name);
1514 while (bnet_recv(sd) >= 0) {
1515 bsendmsg(ua, "%s", sd->msg);
1517 bnet_sig(sd, BNET_TERMINATE);
1519 ua->jcr->store_bsock = NULL;
1523 * mount [storage | device] <name>
1525 static int mount_cmd(UAContext *ua, char *cmd)
1527 do_mount_cmd(ua, "mount"); /* mount */
1533 * unmount [storage | device] <name>
1535 static int unmount_cmd(UAContext *ua, char *cmd)
1537 do_mount_cmd(ua, "unmount"); /* unmount */
1543 * release [storage | device] <name>
1545 static int release_cmd(UAContext *ua, char *cmd)
1547 do_mount_cmd(ua, "release"); /* release */
1554 * use catalog=<name>
1556 static int use_cmd(UAContext *ua, char *cmd)
1558 CAT *oldcatalog, *catalog;
1561 close_db(ua); /* close any previously open db */
1562 oldcatalog = ua->catalog;
1564 if (!(catalog = get_catalog_resource(ua))) {
1565 ua->catalog = oldcatalog;
1567 ua->catalog = catalog;
1570 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1571 ua->catalog->hdr.name, ua->catalog->db_name);
1576 int quit_cmd(UAContext *ua, char *cmd)
1583 * Wait until no job is running
1585 int wait_cmd(UAContext *ua, char *cmd)
1587 bmicrosleep(0, 200000); /* let job actually start */
1588 for (int running=1; running; ) {
1591 for (JCR *jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
1592 if (jcr->JobId != 0) {
1594 free_locked_jcr(jcr);
1597 free_locked_jcr(jcr);
1608 static int help_cmd(UAContext *ua, char *cmd)
1613 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1614 for (i=0; i<comsize; i++) {
1615 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1621 static int version_cmd(UAContext *ua, char *cmd)
1623 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1628 /* A bit brain damaged in that if the user has not done
1629 * a "use catalog xxx" command, we simply find the first
1630 * catalog resource and open it.
1632 int open_db(UAContext *ua)
1639 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1642 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1645 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1646 ua->catalog->hdr.name, ua->catalog->db_name);
1650 ua->jcr->catalog = ua->catalog;
1652 Dmsg0(150, "Open database\n");
1653 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1654 ua->catalog->db_password, ua->catalog->db_address,
1655 ua->catalog->db_port, ua->catalog->db_socket);
1656 if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
1657 bsendmsg(ua, _("Could not open database \"%s\".\n"),
1658 ua->catalog->db_name);
1660 bsendmsg(ua, "%s", db_strerror(ua->db));
1665 ua->jcr->db = ua->db;
1666 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1670 void close_db(UAContext *ua)
1673 db_close_database(ua->jcr, ua->db);