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 mount_cmd(UAContext *ua, char *cmd);
79 static int release_cmd(UAContext *ua, char *cmd);
80 static int update_cmd(UAContext *ua, char *cmd);
81 static int wait_cmd(UAContext *ua, char *cmd);
83 int quit_cmd(UAContext *ua, char *cmd);
86 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
87 static struct cmdstruct commands[] = {
88 { N_("add"), add_cmd, _("add media to a pool")},
89 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
90 { N_("automount"), automount_cmd, _("automount [on/off] -- after label")},
91 { N_("cancel"), cancel_cmd, _("cancel job=nnn -- cancel a job")},
92 { N_("create"), create_cmd, _("create DB Pool from resource")},
93 { N_("delete"), delete_cmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
94 { N_("estimate"), estimate_cmd, _("performs FileSet estimate, listing gives full listing")},
95 { N_("exit"), quit_cmd, _("exit = quit")},
96 { N_("help"), help_cmd, _("print this command")},
97 { N_("label"), label_cmd, _("label a tape")},
98 { N_("list"), list_cmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
99 { N_("llist"), llist_cmd, _("full or long list like list command")},
100 { N_("messages"), messagescmd, _("messages")},
101 { N_("mount"), mount_cmd, _("mount <storage-name>")},
102 { N_("prune"), prunecmd, _("prune expired records from catalog")},
103 { N_("purge"), purgecmd, _("purge records from catalog")},
104 { N_("query"), querycmd, _("query catalog")},
105 { N_("quit"), quit_cmd, _("quit")},
106 { N_("relabel"), relabel_cmd, _("relabel a tape")},
107 { N_("release"), release_cmd, _("release <storage-name>")},
108 { N_("restore"), restore_cmd, _("restore files")},
109 { N_("run"), run_cmd, _("run <job-name>")},
110 { N_("setdebug"), setdebug_cmd, _("sets debug level")},
111 { N_("show"), show_cmd, _("show (resource records) [jobs | pools | ... | all]")},
112 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
113 { N_("status"), status_cmd, _("status [storage | client]=<name>")},
114 { N_("time"), time_cmd, _("print current time")},
115 { N_("unmount"), unmount_cmd, _("unmount <storage-name>")},
116 { N_("update"), update_cmd, _("update Volume or Pool")},
117 { N_("use"), use_cmd, _("use catalog xxx")},
118 { N_("var"), var_cmd, _("does variable expansion")},
119 { N_("version"), version_cmd, _("print Director version")},
120 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
122 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
125 * Execute a command from the UA
127 int do_a_command(UAContext *ua, char *cmd)
136 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
141 len = strlen(ua->argk[0]);
142 for (i=0; i<comsize; i++) { /* search for command */
143 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
144 stat = (*commands[i].func)(ua, cmd); /* go execute command */
150 pm_strcat(&ua->UA_sock->msg, _(": is an illegal command\n"));
151 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
152 bnet_send(ua->UA_sock);
158 * This is a common routine used to stuff the Pool DB record defaults
159 * into the Media DB record just before creating a media (Volume)
162 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
164 mr->PoolId = pr->PoolId;
165 bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));
166 mr->Recycle = pr->Recycle;
167 mr->VolRetention = pr->VolRetention;
168 mr->VolUseDuration = pr->VolUseDuration;
169 mr->MaxVolJobs = pr->MaxVolJobs;
170 mr->MaxVolFiles = pr->MaxVolFiles;
171 mr->MaxVolBytes = pr->MaxVolBytes;
176 * Add Volumes to an existing Pool
178 static int add_cmd(UAContext *ua, char *cmd)
182 int num, i, max, startnum;
184 char name[MAX_NAME_LENGTH];
189 "You probably don't want to be using this command since it\n"
190 "creates database records without labeling the Volumes.\n"
191 "You probably want to use the \"label\" command.\n\n"));
197 memset(&pr, 0, sizeof(pr));
198 memset(&mr, 0, sizeof(mr));
200 if (!get_pool_dbr(ua, &pr)) {
204 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
205 pr.MaxVols, pr.PoolType);
207 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
208 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
210 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
213 pr.MaxVols = ua->pint32_val;
218 if ((store = get_storage_resource(ua, 0)) != NULL) {
219 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
220 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
224 if (pr.MaxVols == 0) {
227 max = pr.MaxVols - pr.NumVols;
231 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
232 if (!get_pint(ua, buf)) {
235 num = ua->pint32_val;
236 if (num < 0 || num > max) {
237 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
244 if (!get_cmd(ua, _("Enter Volume name: "))) {
248 if (!get_cmd(ua, _("Enter base volume name: "))) {
252 /* Don't allow | in Volume name because it is the volume separator character */
253 if (!is_volume_name_legal(ua, ua->cmd)) {
256 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
257 bsendmsg(ua, _("Volume name too long.\n"));
260 if (strlen(ua->cmd) == 0) {
261 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
265 bstrncpy(name, ua->cmd, sizeof(name));
267 strcat(name, "%04d");
270 if (!get_pint(ua, _("Enter the starting number: "))) {
273 startnum = ua->pint32_val;
275 bsendmsg(ua, _("Start number must be greater than zero.\n"));
285 if (store && store->autochanger) {
286 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
289 slot = ua->pint32_val;
292 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
293 for (i=startnum; i < num+startnum; i++) {
294 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
296 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
297 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
298 bsendmsg(ua, db_strerror(ua->db));
302 first_id = mr.PoolId;
306 Dmsg0(200, "Update pool record.\n");
307 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
308 bsendmsg(ua, db_strerror(ua->db));
311 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
317 * Turn auto mount on/off
322 int automount_cmd(UAContext *ua, char *cmd)
327 if (!get_cmd(ua, _("Turn on or off? "))) {
335 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
343 static int cancel_cmd(UAContext *ua, char *cmd)
349 char JobName[MAX_NAME_LENGTH];
355 for (i=1; i<ua->argc; i++) {
356 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
360 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
361 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
365 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
369 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
370 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
376 /* If we still do not have a jcr,
377 * throw up a list and ask the user to select one.
380 /* Count Jobs running */
382 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
383 if (jcr->JobId == 0) { /* this is us */
384 free_locked_jcr(jcr);
388 free_locked_jcr(jcr);
393 bsendmsg(ua, _("No Jobs running.\n"));
396 start_prompt(ua, _("Select Job:\n"));
398 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
399 if (jcr->JobId == 0) { /* this is us */
400 free_locked_jcr(jcr);
403 add_prompt(ua, jcr->Job);
404 free_locked_jcr(jcr);
408 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
412 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
416 jcr = get_jcr_by_full_name(JobName);
418 bsendmsg(ua, _("Job %s not found.\n"), JobName);
423 switch (jcr->JobStatus) {
426 case JS_WaitClientRes:
427 case JS_WaitStoreRes:
428 case JS_WaitPriority:
430 case JS_WaitStartTime:
431 set_jcr_job_status(jcr, JS_Canceled);
432 bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
433 jcr->JobId, jcr->Job);
434 jobq_remove(&job_queue, jcr); /* attempt to remove it from queue */
435 free_jcr(jcr); /* this decrements the use count only */
439 set_jcr_job_status(jcr, JS_Canceled);
441 /* Cancel File daemon */
442 if (jcr->file_bsock) {
443 ua->jcr->client = jcr->client;
444 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
445 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
449 Dmsg0(200, "Connected to file daemon\n");
450 fd = ua->jcr->file_bsock;
451 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
452 while (bnet_recv(fd) >= 0) {
453 bsendmsg(ua, "%s", fd->msg);
455 bnet_sig(fd, BNET_TERMINATE);
457 ua->jcr->file_bsock = NULL;
460 /* Cancel Storage daemon */
461 if (jcr->store_bsock) {
462 ua->jcr->store = jcr->store;
463 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
464 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
468 Dmsg0(200, "Connected to storage daemon\n");
469 sd = ua->jcr->store_bsock;
470 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
471 while (bnet_recv(sd) >= 0) {
472 bsendmsg(ua, "%s", sd->msg);
474 bnet_sig(sd, BNET_TERMINATE);
476 ua->jcr->store_bsock = NULL;
485 * This is a common routine to create or update a
486 * Pool DB base record from a Pool Resource. We handle
487 * the setting of MaxVols and NumVols slightly differently
488 * depending on if we are creating the Pool or we are
489 * simply bringing it into agreement with the resource (updage).
491 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
493 strcpy(pr->PoolType, pool->pool_type);
494 if (op == POOL_OP_CREATE) {
495 pr->MaxVols = pool->max_volumes;
497 } else { /* update pool */
498 if (pr->MaxVols != pool->max_volumes) {
499 pr->MaxVols = pool->max_volumes;
501 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
502 pr->MaxVols = pr->NumVols;
505 pr->UseOnce = pool->use_volume_once;
506 pr->UseCatalog = pool->use_catalog;
507 pr->AcceptAnyVolume = pool->accept_any_volume;
508 pr->Recycle = pool->Recycle;
509 pr->VolRetention = pool->VolRetention;
510 pr->VolUseDuration = pool->VolUseDuration;
511 pr->MaxVolJobs = pool->MaxVolJobs;
512 pr->MaxVolFiles = pool->MaxVolFiles;
513 pr->MaxVolBytes = pool->MaxVolBytes;
514 pr->AutoPrune = pool->AutoPrune;
515 pr->Recycle = pool->Recycle;
516 if (pool->label_format) {
517 strcpy(pr->LabelFormat, pool->label_format);
519 strcpy(pr->LabelFormat, "*"); /* none */
525 * Create a pool record from a given Pool resource
526 * Also called from backup.c
527 * Returns: -1 on error
528 * 0 record already exists
532 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
536 memset(&pr, 0, sizeof(POOL_DBR));
538 strcpy(pr.Name, pool->hdr.name);
540 if (db_get_pool_record(jcr, db, &pr)) {
542 if (op == POOL_OP_UPDATE) { /* update request */
543 set_pooldbr_from_poolres(&pr, pool, op);
544 db_update_pool_record(jcr, db, &pr);
546 return 0; /* exists */
549 set_pooldbr_from_poolres(&pr, pool, op);
551 if (!db_create_pool_record(jcr, db, &pr)) {
552 return -1; /* error */
560 * Create a Pool Record in the database.
561 * It is always created from the Resource record.
563 static int create_cmd(UAContext *ua, char *cmd)
571 pool = get_pool_resource(ua);
576 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
578 bsendmsg(ua, _("Error: Pool %s already exists.\n\
579 Use update to change it.\n"), pool->hdr.name);
583 bsendmsg(ua, db_strerror(ua->db));
589 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
597 * Update a Pool Record in the database.
598 * It is always updated from the Resource record.
600 * update pool=<pool-name>
601 * updates pool from Pool resource
602 * update media pool=<pool-name> volume=<volume-name>
603 * changes pool info for volume
605 static int update_cmd(UAContext *ua, char *cmd)
607 static char *kw[] = {
609 N_("volume"), /* 1 */
618 switch (find_arg_keyword(ua, kw)) {
633 start_prompt(ua, _("Update choice:\n"));
634 add_prompt(ua, _("Volume parameters"));
635 add_prompt(ua, _("Pool from resource"));
636 add_prompt(ua, _("Slots from autochanger"));
637 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
653 static void update_volstatus(UAContext *ua, char *val, MEDIA_DBR *mr)
655 POOLMEM *query = get_pool_memory(PM_MESSAGE);
669 for (i=0; kw[i]; i++) {
670 if (strcasecmp(val, kw[i]) == 0) {
676 bsendmsg(ua, _("Invalid VolStatus specified: %s\n"), val);
678 bstrncpy(mr->VolStatus, kw[i], sizeof(mr->VolStatus));
679 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
680 mr->VolStatus, mr->MediaId);
681 if (!db_sql_query(ua->db, query, NULL, NULL)) {
682 bsendmsg(ua, "%s", db_strerror(ua->db));
684 bsendmsg(ua, _("New Volume status is: %s\n"), mr->VolStatus);
687 free_pool_memory(query);
690 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
694 if (!duration_to_utime(val, &mr->VolRetention)) {
695 bsendmsg(ua, _("Invalid retention period specified: %s\n"), val);
698 query = get_pool_memory(PM_MESSAGE);
699 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
700 edit_uint64(mr->VolRetention, ed1), mr->MediaId);
701 if (!db_sql_query(ua->db, query, NULL, NULL)) {
702 bsendmsg(ua, "%s", db_strerror(ua->db));
704 bsendmsg(ua, _("New retention seconds is: %s\n"),
705 edit_utime(mr->VolRetention, ed1));
707 free_pool_memory(query);
710 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
715 if (!duration_to_utime(val, &mr->VolUseDuration)) {
716 bsendmsg(ua, _("Invalid use duration specified: %s\n"), val);
719 query = get_pool_memory(PM_MESSAGE);
720 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
721 edit_uint64(mr->VolUseDuration, ed1), mr->MediaId);
722 if (!db_sql_query(ua->db, query, NULL, NULL)) {
723 bsendmsg(ua, "%s", db_strerror(ua->db));
725 bsendmsg(ua, _("New use duration is: %s\n"),
726 edit_utime(mr->VolUseDuration, ed1));
728 free_pool_memory(query);
731 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
733 POOLMEM *query = get_pool_memory(PM_MESSAGE);
734 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%u",
736 if (!db_sql_query(ua->db, query, NULL, NULL)) {
737 bsendmsg(ua, "%s", db_strerror(ua->db));
739 bsendmsg(ua, _("New max jobs is: %s\n"), val);
741 free_pool_memory(query);
744 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
746 POOLMEM *query = get_pool_memory(PM_MESSAGE);
747 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%u",
749 if (!db_sql_query(ua->db, query, NULL, NULL)) {
750 bsendmsg(ua, "%s", db_strerror(ua->db));
752 bsendmsg(ua, _("New max files is: %s\n"), val);
754 free_pool_memory(query);
757 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
763 if (!size_to_uint64(val, strlen(val), &maxbytes)) {
764 bsendmsg(ua, _("Invalid max. bytes specification: %s\n"), val);
767 query = get_pool_memory(PM_MESSAGE);
768 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
769 edit_uint64(maxbytes, ed1), mr->MediaId);
770 if (!db_sql_query(ua->db, query, NULL, NULL)) {
771 bsendmsg(ua, "%s", db_strerror(ua->db));
773 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
775 free_pool_memory(query);
778 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
782 if (strcasecmp(val, _("yes")) == 0) {
784 } else if (strcasecmp(val, _("no")) == 0) {
787 bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
790 query = get_pool_memory(PM_MESSAGE);
791 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
792 recycle, mr->MediaId);
793 if (!db_sql_query(ua->db, query, NULL, NULL)) {
794 bsendmsg(ua, "%s", db_strerror(ua->db));
796 bsendmsg(ua, _("New recycle flag is: %s\n"),
797 mr->Recycle==1?_("yes"):_("no"));
799 free_pool_memory(query);
802 /* Modify the Pool in which this Volume is located */
803 static void update_volpool(UAContext *ua, char *val, MEDIA_DBR *mr)
807 memset(&pr, 0, sizeof(pr));
808 bstrncpy(pr.Name, val, sizeof(pr.Name));
809 if (!get_pool_dbr(ua, &pr)) {
812 query = get_pool_memory(PM_MESSAGE);
813 Mmsg(&query, "UPDATE Media SET PoolId=%u WHERE MediaId=%u", pr.PoolId, mr->MediaId);
814 if (!db_sql_query(ua->db, query, NULL, NULL)) {
815 bsendmsg(ua, "%s", db_strerror(ua->db));
817 bsendmsg(ua, _("New Pool is: %s\n"), pr.Name);
819 free_pool_memory(query);
823 * Update a media record -- allows you to change the
824 * Volume status. E.g. if you want Bacula to stop
825 * writing on the volume, set it to anything other
828 static int update_volume(UAContext *ua)
836 N_("VolStatus"), /* 0 */
837 N_("VolRetention"), /* 1 */
838 N_("VolUse"), /* 2 */
839 N_("MaxVolJobs"), /* 3 */
840 N_("MaxVolFiles"), /* 4 */
841 N_("MaxVolBytes"), /* 5 */
842 N_("Recycle"), /* 6 */
846 for (int i=0; kw[i]; i++) {
848 if ((j=find_arg_with_value(ua, kw[i])) > 0) {
849 if (!select_media_dbr(ua, &mr)) {
854 update_volstatus(ua, ua->argv[j], &mr);
857 update_volretention(ua, ua->argv[j], &mr);
860 update_voluseduration(ua, ua->argv[j], &mr);
863 update_volmaxjobs(ua, ua->argv[j], &mr);
866 update_volmaxfiles(ua, ua->argv[j], &mr);
869 update_volmaxbytes(ua, ua->argv[j], &mr);
872 update_volrecycle(ua, ua->argv[j], &mr);
875 update_volpool(ua, ua->argv[j], &mr);
882 if (!select_media_dbr(ua, &mr)) {
885 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
886 start_prompt(ua, _("Parameters to modify:\n"));
887 add_prompt(ua, _("Volume Status"));
888 add_prompt(ua, _("Volume Retention Period"));
889 add_prompt(ua, _("Volume Use Duration"));
890 add_prompt(ua, _("Maximum Volume Jobs"));
891 add_prompt(ua, _("Maximum Volume Files"));
892 add_prompt(ua, _("Maximum Volume Bytes"));
893 add_prompt(ua, _("Recycle Flag"));
894 add_prompt(ua, _("Slot"));
895 add_prompt(ua, _("Volume Files"));
896 add_prompt(ua, _("Pool"));
897 add_prompt(ua, _("Done"));
898 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
899 case 0: /* Volume Status */
900 /* Modify Volume Status */
901 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
902 start_prompt(ua, _("Possible Values are:\n"));
903 add_prompt(ua, "Append"); /* Better not translate these as */
904 add_prompt(ua, "Archive"); /* They are known in the database code */
905 add_prompt(ua, "Disabled");
906 add_prompt(ua, "Full");
907 add_prompt(ua, "Used");
908 add_prompt(ua, "Cleaning");
909 if (strcmp(mr.VolStatus, "Purged") == 0) {
910 add_prompt(ua, "Recycle");
912 add_prompt(ua, "Read-Only");
913 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
916 update_volstatus(ua, ua->cmd, &mr);
918 case 1: /* Retention */
919 bsendmsg(ua, _("Current retention seconds is: %s\n"),
920 edit_utime(mr.VolRetention, ed1));
921 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
924 update_volretention(ua, ua->cmd, &mr);
927 case 2: /* Use Duration */
928 bsendmsg(ua, _("Current use duration is: %s\n"),
929 edit_utime(mr.VolUseDuration, ed1));
930 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
933 update_voluseduration(ua, ua->cmd, &mr);
936 case 3: /* Max Jobs */
937 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
938 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
941 update_volmaxjobs(ua, ua->cmd, &mr);
944 case 4: /* Max Files */
945 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
946 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
949 update_volmaxfiles(ua, ua->cmd, &mr);
952 case 5: /* Max Bytes */
953 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
954 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
957 update_volmaxbytes(ua, ua->cmd, &mr);
961 case 6: /* Recycle */
962 bsendmsg(ua, _("Current recycle flag is: %s\n"),
963 mr.Recycle==1?_("yes"):_("no"));
964 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
967 update_volrecycle(ua, ua->cmd, &mr);
973 memset(&pr, 0, sizeof(POOL_DBR));
974 pr.PoolId = mr.PoolId;
975 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
976 bsendmsg(ua, "%s", db_strerror(ua->db));
979 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
980 if (!get_pint(ua, _("Enter new Slot: "))) {
983 slot = ua->pint32_val;
984 if (pr.MaxVols > 0 && slot > (int)pr.MaxVols) {
985 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
989 query = get_pool_memory(PM_MESSAGE);
990 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
992 if (!db_sql_query(ua->db, query, NULL, NULL)) {
993 bsendmsg(ua, "%s", db_strerror(ua->db));
995 bsendmsg(ua, "New Slot is: %d\n", slot);
997 free_pool_memory(query);
1000 case 8: /* Volume Files */
1002 bsendmsg(ua, _("Warning changing Volume Files can result\n"
1003 "in loss of data on your Volume\n\n"));
1004 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
1005 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
1008 VolFiles = ua->pint32_val;
1009 if (VolFiles != (int)(mr.VolFiles + 1)) {
1010 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
1011 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
1015 query = get_pool_memory(PM_MESSAGE);
1016 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
1017 VolFiles, mr.MediaId);
1018 if (!db_sql_query(ua->db, query, NULL, NULL)) {
1019 bsendmsg(ua, "%s", db_strerror(ua->db));
1021 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
1023 free_pool_memory(query);
1026 case 9: /* Volume's Pool */
1027 memset(&pr, 0, sizeof(POOL_DBR));
1028 pr.PoolId = mr.PoolId;
1029 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1030 bsendmsg(ua, "%s", db_strerror(ua->db));
1033 bsendmsg(ua, _("Current Pool is: %s\n"), pr.Name);
1034 if (!get_cmd(ua, _("Enter new Pool name: "))) {
1037 update_volpool(ua, ua->cmd, &mr);
1039 default: /* Done or error */
1040 bsendmsg(ua, "Selection done.\n");
1048 * Update pool record -- pull info from current POOL resource
1050 static int update_pool(UAContext *ua)
1057 pool = get_pool_resource(ua);
1062 memset(&pr, 0, sizeof(pr));
1063 strcpy(pr.Name, pool->hdr.name);
1064 if (!get_pool_dbr(ua, &pr)) {
1068 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
1070 id = db_update_pool_record(ua->jcr, ua->db, &pr);
1072 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
1073 id, db_strerror(ua->db));
1075 query = get_pool_memory(PM_MESSAGE);
1076 Mmsg(&query, list_pool, pr.PoolId);
1077 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
1078 free_pool_memory(query);
1079 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
1084 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
1088 ua->jcr->store = store;
1089 /* Try connecting for up to 15 seconds */
1090 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
1091 store->hdr.name, store->address, store->SDport);
1092 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
1093 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1096 Dmsg0(120, _("Connected to storage daemon\n"));
1097 sd = ua->jcr->store_bsock;
1098 bnet_fsend(sd, "setdebug=%d\n", level);
1099 if (bnet_recv(sd) >= 0) {
1100 bsendmsg(ua, "%s", sd->msg);
1102 bnet_sig(sd, BNET_TERMINATE);
1104 ua->jcr->store_bsock = NULL;
1108 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
1112 /* Connect to File daemon */
1114 ua->jcr->client = client;
1115 /* Try to connect for 15 seconds */
1116 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1117 client->hdr.name, client->address, client->FDport);
1118 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1119 bsendmsg(ua, _("Failed to connect to Client.\n"));
1122 Dmsg0(120, "Connected to file daemon\n");
1123 fd = ua->jcr->file_bsock;
1124 bnet_fsend(fd, "setdebug=%d\n", level);
1125 if (bnet_recv(fd) >= 0) {
1126 bsendmsg(ua, "%s", fd->msg);
1128 bnet_sig(fd, BNET_TERMINATE);
1130 ua->jcr->file_bsock = NULL;
1135 static void do_all_setdebug(UAContext *ua, int level)
1137 STORE *store, **unique_store;
1138 CLIENT *client, **unique_client;
1142 debug_level = level;
1144 /* Count Storage items */
1147 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1149 unique_store = (STORE **) malloc(i * sizeof(STORE));
1150 /* Find Unique Storage address/port */
1151 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1153 unique_store[i++] = store;
1154 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1156 for (j=0; j<i; j++) {
1157 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1158 unique_store[j]->SDport == store->SDport) {
1164 unique_store[i++] = store;
1165 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1170 /* Call each unique Storage daemon */
1171 for (j=0; j<i; j++) {
1172 do_storage_setdebug(ua, unique_store[j], level);
1176 /* Count Client items */
1179 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1181 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1182 /* Find Unique Client address/port */
1183 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1185 unique_client[i++] = client;
1186 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1188 for (j=0; j<i; j++) {
1189 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1190 unique_client[j]->FDport == client->FDport) {
1196 unique_client[i++] = client;
1197 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1202 /* Call each unique File daemon */
1203 for (j=0; j<i; j++) {
1204 do_client_setdebug(ua, unique_client[j], level);
1206 free(unique_client);
1210 * setdebug level=nn all
1212 static int setdebug_cmd(UAContext *ua, char *cmd)
1222 Dmsg1(120, "setdebug:%s:\n", cmd);
1225 i = find_arg_with_value(ua, _("level"));
1227 level = atoi(ua->argv[i]);
1230 if (!get_pint(ua, _("Enter new debug level: "))) {
1233 level = ua->pint32_val;
1236 /* General debug? */
1237 for (i=1; i<ua->argc; i++) {
1238 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1239 do_all_setdebug(ua, level);
1242 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1243 strcasecmp(ua->argk[i], _("director")) == 0) {
1244 debug_level = level;
1247 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1250 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1252 do_client_setdebug(ua, client, level);
1256 client = select_client_resource(ua);
1258 do_client_setdebug(ua, client, level);
1263 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1264 strcasecmp(ua->argk[i], _("storage")) == 0) {
1267 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1269 do_storage_setdebug(ua, store, level);
1273 store = get_storage_resource(ua, 0);
1275 do_storage_setdebug(ua, store, level);
1281 * We didn't find an appropriate keyword above, so
1284 start_prompt(ua, _("Available daemons are: \n"));
1285 add_prompt(ua, _("Director"));
1286 add_prompt(ua, _("Storage"));
1287 add_prompt(ua, _("Client"));
1288 add_prompt(ua, _("All"));
1289 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1290 case 0: /* Director */
1291 debug_level = level;
1294 store = get_storage_resource(ua, 0);
1296 do_storage_setdebug(ua, store, level);
1300 client = select_client_resource(ua);
1302 do_client_setdebug(ua, client, level);
1306 do_all_setdebug(ua, level);
1314 static int var_cmd(UAContext *ua, char *cmd)
1316 POOLMEM *val = get_pool_memory(PM_FNAME);
1322 for (var=ua->cmd; *var != ' '; ) { /* skip command */
1325 while (*var == ' ') { /* skip spaces */
1328 Dmsg1(100, "Var=%s:\n", var);
1329 variable_expansion(ua->jcr, var, &val);
1330 bsendmsg(ua, "%s\n", val);
1331 free_pool_memory(val);
1335 static int estimate_cmd(UAContext *ua, char *cmd)
1338 CLIENT *client = NULL;
1339 FILESET *fileset = NULL;
1342 char since[MAXSTRING];
1344 for (int i=1; i<ua->argc; i++) {
1345 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1347 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1351 if (strcasecmp(ua->argk[i], _("job")) == 0) {
1353 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1357 if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1359 fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1363 if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1367 if (!job && !(client && fileset)) {
1368 if (!(job = select_job_resource(ua))) {
1373 job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1376 client = job->client;
1379 fileset = job->fileset;
1381 ua->jcr->client = client;
1382 ua->jcr->fileset = fileset;
1384 ua->catalog = client->catalog;
1390 get_level_since_time(ua->jcr, since, sizeof(since));
1392 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1393 job->client->hdr.name, job->client->address, job->client->FDport);
1394 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1395 bsendmsg(ua, _("Failed to connect to Client.\n"));
1398 fd = ua->jcr->file_bsock;
1400 if (!send_include_list(ua->jcr)) {
1401 bsendmsg(ua, _("Error sending include list.\n"));
1405 if (!send_exclude_list(ua->jcr)) {
1406 bsendmsg(ua, _("Error sending exclude list.\n"));
1410 if (!send_level_command(ua->jcr)) {
1414 bnet_fsend(fd, "estimate listing=%d\n", listing);
1415 while (bnet_recv(fd) >= 0) {
1416 bsendmsg(ua, "%s", fd->msg);
1419 bnet_sig(fd, BNET_TERMINATE);
1421 ua->jcr->file_bsock = NULL;
1430 static int time_cmd(UAContext *ua, char *cmd)
1433 time_t ttime = time(NULL);
1435 localtime_r(&ttime, &tm);
1436 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1437 bsendmsg(ua, "%s\n", sdt);
1444 * Delete Pool records (should purge Media with it).
1446 * delete pool=<pool-name>
1447 * delete media pool=<pool-name> volume=<name>
1449 static int delete_cmd(UAContext *ua, char *cmd)
1451 static char *keywords[] = {
1461 "In general it is not a good idea to delete either a\n"
1462 "Pool or a Volume since they may contain data.\n\n"));
1464 switch (find_arg_keyword(ua, keywords)) {
1474 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1482 bsendmsg(ua, _("Nothing done.\n"));
1489 * Delete media records from database -- dangerous
1491 static int delete_volume(UAContext *ua)
1495 if (!select_media_dbr(ua, &mr)) {
1498 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1499 "and all Jobs saved on that volume from the Catalog\n"),
1502 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1505 if (ua->pint32_val) {
1506 db_delete_media_record(ua->jcr, ua->db, &mr);
1512 * Delete a pool record from the database -- dangerous
1514 static int delete_pool(UAContext *ua)
1518 memset(&pr, 0, sizeof(pr));
1520 if (!get_pool_dbr(ua, &pr)) {
1523 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1526 if (ua->pint32_val) {
1527 db_delete_pool_record(ua->jcr, ua->db, &pr);
1533 static void do_mount_cmd(UAContext *ua, char *command)
1537 char dev_name[MAX_NAME_LENGTH];
1543 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1545 store = get_storage_resource(ua, 1);
1550 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1551 store->media_type, store->dev_name);
1553 ua->jcr->store = store;
1554 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1555 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1558 sd = ua->jcr->store_bsock;
1559 strcpy(dev_name, store->dev_name);
1560 bash_spaces(dev_name);
1561 bnet_fsend(sd, "%s %s", command, dev_name);
1562 while (bnet_recv(sd) >= 0) {
1563 bsendmsg(ua, "%s", sd->msg);
1565 bnet_sig(sd, BNET_TERMINATE);
1567 ua->jcr->store_bsock = NULL;
1571 * mount [storage | device] <name>
1573 static int mount_cmd(UAContext *ua, char *cmd)
1575 do_mount_cmd(ua, "mount"); /* mount */
1581 * unmount [storage | device] <name>
1583 static int unmount_cmd(UAContext *ua, char *cmd)
1585 do_mount_cmd(ua, "unmount"); /* unmount */
1591 * release [storage | device] <name>
1593 static int release_cmd(UAContext *ua, char *cmd)
1595 do_mount_cmd(ua, "release"); /* release */
1602 * use catalog=<name>
1604 static int use_cmd(UAContext *ua, char *cmd)
1606 CAT *oldcatalog, *catalog;
1609 close_db(ua); /* close any previously open db */
1610 oldcatalog = ua->catalog;
1612 if (!(catalog = get_catalog_resource(ua))) {
1613 ua->catalog = oldcatalog;
1615 ua->catalog = catalog;
1618 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1619 ua->catalog->hdr.name, ua->catalog->db_name);
1624 int quit_cmd(UAContext *ua, char *cmd)
1631 * Wait until no job is running
1633 int wait_cmd(UAContext *ua, char *cmd)
1635 bmicrosleep(0, 200000); /* let job actually start */
1636 for (int running=1; running; ) {
1639 for (JCR *jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
1640 if (jcr->JobId != 0) {
1642 free_locked_jcr(jcr);
1645 free_locked_jcr(jcr);
1656 static int help_cmd(UAContext *ua, char *cmd)
1661 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1662 for (i=0; i<comsize; i++) {
1663 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1669 static int version_cmd(UAContext *ua, char *cmd)
1671 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1676 /* A bit brain damaged in that if the user has not done
1677 * a "use catalog xxx" command, we simply find the first
1678 * catalog resource and open it.
1680 int open_db(UAContext *ua)
1687 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1690 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1693 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1694 ua->catalog->hdr.name, ua->catalog->db_name);
1698 ua->jcr->catalog = ua->catalog;
1700 Dmsg0(150, "Open database\n");
1701 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1702 ua->catalog->db_password, ua->catalog->db_address,
1703 ua->catalog->db_port, ua->catalog->db_socket);
1704 if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
1705 bsendmsg(ua, _("Could not open database \"%s\".\n"),
1706 ua->catalog->db_name);
1708 bsendmsg(ua, "%s", db_strerror(ua->db));
1713 ua->jcr->db = ua->db;
1714 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1718 void close_db(UAContext *ua)
1721 db_close_database(ua->jcr, ua->db);