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 */
34 extern void run_job(JCR *jcr);
36 /* Imported variables */
39 extern struct s_res resources[];
40 extern char my_name[];
42 extern workq_t job_wq; /* work queue */
45 extern char *list_pool;
47 /* Imported functions */
48 extern int statuscmd(UAContext *ua, char *cmd);
49 extern int listcmd(UAContext *ua, char *cmd);
50 extern int llistcmd(UAContext *ua, char *cmd);
51 extern int showcmd(UAContext *ua, char *cmd);
52 extern int messagescmd(UAContext *ua, char *cmd);
53 extern int autodisplaycmd(UAContext *ua, char *cmd);
54 extern int sqlquerycmd(UAContext *ua, char *cmd);
55 extern int querycmd(UAContext *ua, char *cmd);
56 extern int runcmd(UAContext *ua, char *cmd);
57 extern int retentioncmd(UAContext *ua, char *cmd);
58 extern int prunecmd(UAContext *ua, char *cmd);
59 extern int purgecmd(UAContext *ua, char *cmd);
60 extern int restorecmd(UAContext *ua, char *cmd);
61 extern int labelcmd(UAContext *ua, char *cmd);
62 extern int relabelcmd(UAContext *ua, char *cmd);
63 extern int update_slots(UAContext *ua); /* ua_label.c */
65 /* Forward referenced functions */
66 static int addcmd(UAContext *ua, char *cmd), createcmd(UAContext *ua, char *cmd), cancelcmd(UAContext *ua, char *cmd);
67 static int setdebugcmd(UAContext *ua, char *cmd);
68 static int helpcmd(UAContext *ua, char *cmd);
69 static int deletecmd(UAContext *ua, char *cmd);
70 static int usecmd(UAContext *ua, char *cmd), unmountcmd(UAContext *ua, char *cmd);
71 static int versioncmd(UAContext *ua, char *cmd), automountcmd(UAContext *ua, char *cmd);
72 static int timecmd(UAContext *ua, char *cmd);
73 static int update_volume(UAContext *ua);
74 static int update_pool(UAContext *ua);
75 static int delete_volume(UAContext *ua);
76 static int delete_pool(UAContext *ua);
77 static int mountcmd(UAContext *ua, char *cmd);
78 static int updatecmd(UAContext *ua, char *cmd);
79 static int waitcmd(UAContext *ua, char *cmd);
81 int quitcmd(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"), addcmd, _("add media to a pool")},
87 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
88 { N_("automount"), automountcmd, _("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"), deletecmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
92 { N_("help"), helpcmd, _("print this command")},
93 { N_("label"), labelcmd, _("label a tape")},
94 { N_("relabel"), relabelcmd, _("relabel a tape")},
95 { N_("list"), listcmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
96 { N_("llist"), llistcmd, _("full or long list like list command")},
97 { N_("messages"), messagescmd, _("messages")},
98 { N_("mount"), mountcmd, _("mount <storage-name>")},
99 { N_("restore"), restorecmd, _("restore files")},
100 { N_("prune"), prunecmd, _("prune expired records from catalog")},
101 { N_("purge"), purgecmd, _("purge records from catalog")},
102 { N_("run"), runcmd, _("run <job-name>")},
103 { N_("setdebug"), setdebugcmd, _("sets debug level")},
104 { N_("show"), showcmd, _("show (resource records) [jobs | pools | ... | all]")},
105 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
106 { N_("status"), statuscmd, _("status [storage | client]=<name>")},
107 { N_("unmount"), unmountcmd, _("unmount <storage-name>")},
108 { N_("update"), updatecmd, _("update Volume or Pool")},
109 { N_("use"), usecmd, _("use catalog xxx")},
110 { N_("version"), versioncmd, _("print Director version")},
111 { N_("quit"), quitcmd, _("quit")},
112 { N_("query"), querycmd, _("query catalog")},
113 { N_("time"), timecmd, _("print current time")},
114 { N_("exit"), quitcmd, _("exit = quit")},
115 { N_("wait"), waitcmd, _("wait until no jobs are running")},
117 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
120 * Execute a command from the UA
122 int do_a_command(UAContext *ua, char *cmd)
131 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
136 len = strlen(ua->argk[0]);
137 for (i=0; i<comsize; i++) { /* search for command */
138 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
139 stat = (*commands[i].func)(ua, cmd); /* go execute command */
145 pm_strcat(&ua->UA_sock->msg, _(": is an illegal command\n"));
146 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
147 bnet_send(ua->UA_sock);
153 * This is a common routine used to stuff the Pool DB record defaults
154 * into the Media DB record just before creating a media (Volume)
157 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
159 mr->PoolId = pr->PoolId;
160 strcpy(mr->VolStatus, "Append");
161 mr->Recycle = pr->Recycle;
162 mr->VolRetention = pr->VolRetention;
163 mr->VolUseDuration = pr->VolUseDuration;
164 mr->MaxVolJobs = pr->MaxVolJobs;
165 mr->MaxVolFiles = pr->MaxVolFiles;
166 mr->MaxVolBytes = pr->MaxVolBytes;
171 * Add Volumes to an existing Pool
173 static int addcmd(UAContext *ua, char *cmd)
177 int num, i, max, startnum;
179 char name[MAX_NAME_LENGTH];
184 "You probably don't want to be using this command since it\n"
185 "creates database records without labeling the Volumes.\n"
186 "You probably want to use the \"label\" command.\n\n"));
192 memset(&pr, 0, sizeof(pr));
193 memset(&mr, 0, sizeof(mr));
195 if (!get_pool_dbr(ua, &pr)) {
199 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
200 pr.MaxVols, pr.PoolType);
202 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
203 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
205 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
208 pr.MaxVols = ua->pint32_val;
213 if ((store = get_storage_resource(ua, 0)) != NULL) {
214 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
215 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
219 if (pr.MaxVols == 0) {
222 max = pr.MaxVols - pr.NumVols;
226 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
227 if (!get_pint(ua, buf)) {
230 num = ua->pint32_val;
231 if (num < 0 || num > max) {
232 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
239 if (!get_cmd(ua, _("Enter Volume name: "))) {
243 if (!get_cmd(ua, _("Enter base volume name: "))) {
247 /* Don't allow | in Volume name because it is the volume separator character */
248 if (!is_volume_name_legal(ua, ua->cmd)) {
251 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
252 bsendmsg(ua, _("Volume name too long.\n"));
255 if (strlen(ua->cmd) == 0) {
256 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
260 bstrncpy(name, ua->cmd, sizeof(name));
262 strcat(name, "%04d");
265 if (!get_pint(ua, _("Enter the starting number: "))) {
268 startnum = ua->pint32_val;
270 bsendmsg(ua, _("Start number must be greater than zero.\n"));
280 if (store && store->autochanger) {
281 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
284 slot = ua->pint32_val;
287 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
288 for (i=startnum; i < num+startnum; i++) {
289 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
291 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
292 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
293 bsendmsg(ua, db_strerror(ua->db));
297 first_id = mr.PoolId;
301 Dmsg0(200, "Update pool record.\n");
302 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
303 bsendmsg(ua, db_strerror(ua->db));
306 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
312 * Turn auto mount on/off
317 int automountcmd(UAContext *ua, char *cmd)
322 if (!get_cmd(ua, _("Turn on or off? "))) {
330 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
338 static int cancelcmd(UAContext *ua, char *cmd)
344 char JobName[MAX_NAME_LENGTH];
350 for (i=1; i<ua->argc; i++) {
351 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
355 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
356 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
360 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
364 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
365 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
371 /* If we still do not have a jcr,
372 * throw up a list and ask the user to select one.
375 /* Count Jobs running */
377 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
378 if (jcr->JobId == 0) { /* this is us */
379 free_locked_jcr(jcr);
383 free_locked_jcr(jcr);
388 bsendmsg(ua, _("No Jobs running.\n"));
391 start_prompt(ua, _("Select Job:\n"));
393 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
394 if (jcr->JobId == 0) { /* this is us */
395 free_locked_jcr(jcr);
398 add_prompt(ua, jcr->Job);
399 free_locked_jcr(jcr);
403 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
407 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
411 jcr = get_jcr_by_full_name(JobName);
413 bsendmsg(ua, _("Job %s not found.\n"), JobName);
418 switch (jcr->JobStatus) {
421 case JS_WaitClientRes:
422 case JS_WaitStoreRes:
424 case JS_WaitStartTime:
425 set_jcr_job_status(jcr, JS_Canceled);
426 bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
427 jcr->JobId, jcr->Job);
428 #ifndef USE_SEMAPHORE
429 workq_remove(&job_wq, jcr->work_item); /* attempt to remove it from queue */
435 set_jcr_job_status(jcr, JS_Canceled);
437 /* Cancel File daemon */
438 if (jcr->file_bsock) {
439 ua->jcr->client = jcr->client;
440 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
441 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
445 Dmsg0(200, "Connected to file daemon\n");
446 fd = ua->jcr->file_bsock;
447 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
448 while (bnet_recv(fd) >= 0) {
449 bsendmsg(ua, "%s", fd->msg);
451 bnet_sig(fd, BNET_TERMINATE);
453 ua->jcr->file_bsock = NULL;
456 /* Cancel Storage daemon */
457 if (jcr->store_bsock) {
458 ua->jcr->store = jcr->store;
459 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
460 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
464 Dmsg0(200, "Connected to storage daemon\n");
465 sd = ua->jcr->store_bsock;
466 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
467 while (bnet_recv(sd) >= 0) {
468 bsendmsg(ua, "%s", sd->msg);
470 bnet_sig(sd, BNET_TERMINATE);
472 ua->jcr->store_bsock = NULL;
481 * This is a common routine to create or update a
482 * Pool DB base record from a Pool Resource. We handle
483 * the setting of MaxVols and NumVols slightly differently
484 * depending on if we are creating the Pool or we are
485 * simply bringing it into agreement with the resource (updage).
487 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
489 strcpy(pr->PoolType, pool->pool_type);
490 if (op == POOL_OP_CREATE) {
491 pr->MaxVols = pool->max_volumes;
493 } else { /* update pool */
494 if (pr->MaxVols != pool->max_volumes) {
495 pr->MaxVols = pool->max_volumes;
497 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
498 pr->MaxVols = pr->NumVols;
501 pr->UseOnce = pool->use_volume_once;
502 pr->UseCatalog = pool->use_catalog;
503 pr->AcceptAnyVolume = pool->accept_any_volume;
504 pr->Recycle = pool->Recycle;
505 pr->VolRetention = pool->VolRetention;
506 pr->VolUseDuration = pool->VolUseDuration;
507 pr->MaxVolJobs = pool->MaxVolJobs;
508 pr->MaxVolFiles = pool->MaxVolFiles;
509 pr->MaxVolBytes = pool->MaxVolBytes;
510 pr->AutoPrune = pool->AutoPrune;
511 pr->Recycle = pool->Recycle;
512 if (pool->label_format) {
513 strcpy(pr->LabelFormat, pool->label_format);
515 strcpy(pr->LabelFormat, "*"); /* none */
521 * Create a pool record from a given Pool resource
522 * Also called from backup.c
523 * Returns: -1 on error
524 * 0 record already exists
528 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
532 memset(&pr, 0, sizeof(POOL_DBR));
534 strcpy(pr.Name, pool->hdr.name);
536 if (db_get_pool_record(jcr, db, &pr)) {
538 if (op == POOL_OP_UPDATE) { /* update request */
539 set_pooldbr_from_poolres(&pr, pool, op);
540 db_update_pool_record(jcr, db, &pr);
542 return 0; /* exists */
545 set_pooldbr_from_poolres(&pr, pool, op);
547 if (!db_create_pool_record(jcr, db, &pr)) {
548 return -1; /* error */
556 * Create a Pool Record in the database.
557 * It is always created from the Resource record.
559 static int createcmd(UAContext *ua, char *cmd)
567 pool = get_pool_resource(ua);
572 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
574 bsendmsg(ua, _("Error: Pool %s already exists.\n\
575 Use update to change it.\n"), pool->hdr.name);
579 bsendmsg(ua, db_strerror(ua->db));
585 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
593 * Update a Pool Record in the database.
594 * It is always updated from the Resource record.
596 * update pool=<pool-name>
597 * updates pool from Pool resource
598 * update media pool=<pool-name> volume=<volume-name>
599 * changes pool info for volume
601 static int updatecmd(UAContext *ua, char *cmd)
603 static char *kw[] = {
605 N_("volume"), /* 1 */
614 switch (find_arg_keyword(ua, kw)) {
629 start_prompt(ua, _("Update choice:\n"));
630 add_prompt(ua, _("Volume parameters"));
631 add_prompt(ua, _("Pool from resource"));
632 add_prompt(ua, _("Slots from autochanger"));
633 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
650 * Update a media record -- allows you to change the
651 * Volume status. E.g. if you want Bacula to stop
652 * writing on the volume, set it to anything other
655 static int update_volume(UAContext *ua)
662 for (int done=0; !done; ) {
663 if (!select_media_dbr(ua, &mr)) {
666 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
667 start_prompt(ua, _("Parameters to modify:\n"));
668 add_prompt(ua, _("Volume Status"));
669 add_prompt(ua, _("Volume Retention Period"));
670 add_prompt(ua, _("Volume Use Duration"));
671 add_prompt(ua, _("Maximum Volume Jobs"));
672 add_prompt(ua, _("Maximum Volume Files"));
673 add_prompt(ua, _("Maximum Volume Bytes"));
674 add_prompt(ua, _("Recycle Flag"));
675 add_prompt(ua, _("Slot"));
676 add_prompt(ua, _("Volume Files"));
677 add_prompt(ua, _("Done"));
678 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
679 case 0: /* Volume Status */
680 /* Modify Volume Status */
681 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
682 start_prompt(ua, _("Possible Values are:\n"));
683 add_prompt(ua, "Append"); /* Better not translate these as */
684 add_prompt(ua, "Archive"); /* They are known in the database code */
685 add_prompt(ua, "Disabled");
686 add_prompt(ua, "Full");
687 add_prompt(ua, "Used");
688 add_prompt(ua, "Cleaning");
689 if (strcmp(mr.VolStatus, "Purged") == 0) {
690 add_prompt(ua, "Recycle");
692 add_prompt(ua, "Read-Only");
693 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
696 bstrncpy(mr.VolStatus, ua->cmd, sizeof(mr.VolStatus));
697 query = get_pool_memory(PM_MESSAGE);
698 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
699 mr.VolStatus, mr.MediaId);
700 if (!db_sql_query(ua->db, query, NULL, NULL)) {
701 bsendmsg(ua, "%s", db_strerror(ua->db));
703 bsendmsg(ua, _("New Volume status is: %s\n"), mr.VolStatus);
705 free_pool_memory(query);
707 case 1: /* Retention */
708 bsendmsg(ua, _("Current retention seconds is: %s\n"),
709 edit_utime(mr.VolRetention, ed1));
710 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
713 if (!duration_to_utime(ua->cmd, &mr.VolRetention)) {
714 bsendmsg(ua, _("Invalid retention period specified.\n"));
717 query = get_pool_memory(PM_MESSAGE);
718 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
719 edit_uint64(mr.VolRetention, 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 retention seconds is: %s\n"),
724 edit_utime(mr.VolRetention, ed1));
726 free_pool_memory(query);
729 case 2: /* Use Duration */
730 bsendmsg(ua, _("Current use duration is: %s\n"),
731 edit_utime(mr.VolUseDuration, ed1));
732 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
735 if (!duration_to_utime(ua->cmd, &mr.VolUseDuration)) {
736 bsendmsg(ua, _("Invalid use duration specified.\n"));
739 query = get_pool_memory(PM_MESSAGE);
740 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
741 edit_uint64(mr.VolUseDuration, ed1), mr.MediaId);
742 if (!db_sql_query(ua->db, query, NULL, NULL)) {
743 bsendmsg(ua, "%s", db_strerror(ua->db));
745 bsendmsg(ua, _("New use duration is: %s\n"),
746 edit_utime(mr.VolUseDuration, ed1));
748 free_pool_memory(query);
751 case 3: /* Max Jobs */
753 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
754 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
757 maxjobs = ua->pint32_val;
758 query = get_pool_memory(PM_MESSAGE);
759 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%u WHERE MediaId=%u",
760 maxjobs, mr.MediaId);
761 if (!db_sql_query(ua->db, query, NULL, NULL)) {
762 bsendmsg(ua, "%s", db_strerror(ua->db));
764 bsendmsg(ua, _("New max jobs is: %u\n"), maxjobs);
766 free_pool_memory(query);
769 case 4: /* Max Files */
771 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
772 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
775 maxfiles = ua->pint32_val;
776 query = get_pool_memory(PM_MESSAGE);
777 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%u WHERE MediaId=%u",
778 maxfiles, mr.MediaId);
779 if (!db_sql_query(ua->db, query, NULL, NULL)) {
780 bsendmsg(ua, "%s", db_strerror(ua->db));
782 bsendmsg(ua, _("New max files is: %u\n"), maxfiles);
784 free_pool_memory(query);
787 case 5: /* Max Bytes */
789 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
790 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
793 if (!size_to_uint64(ua->cmd, strlen(ua->cmd), &maxbytes)) {
794 bsendmsg(ua, _("Invalid byte size specification.\n"));
797 query = get_pool_memory(PM_MESSAGE);
798 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
799 edit_uint64(maxbytes, ed1), mr.MediaId);
800 if (!db_sql_query(ua->db, query, NULL, NULL)) {
801 bsendmsg(ua, "%s", db_strerror(ua->db));
803 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
805 free_pool_memory(query);
809 case 6: /* Recycle */
811 bsendmsg(ua, _("Current recycle flag is: %s\n"),
812 mr.Recycle==1?_("yes"):_("no"));
813 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
816 recycle = ua->pint32_val;
817 query = get_pool_memory(PM_MESSAGE);
818 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
819 recycle, mr.MediaId);
820 if (!db_sql_query(ua->db, query, NULL, NULL)) {
821 bsendmsg(ua, "%s", db_strerror(ua->db));
823 bsendmsg(ua, _("New recycle flag is: %s\n"),
824 mr.Recycle==1?_("yes"):_("no"));
826 free_pool_memory(query);
833 memset(&pr, 0, sizeof(POOL_DBR));
834 pr.PoolId = mr.PoolId;
835 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
836 bsendmsg(ua, "%s", db_strerror(ua->db));
839 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
840 if (!get_pint(ua, _("Enter new Slot: "))) {
843 slot = ua->pint32_val;
844 if (pr.MaxVols > 0 && slot > (int)pr.MaxVols) {
845 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
849 query = get_pool_memory(PM_MESSAGE);
850 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
852 if (!db_sql_query(ua->db, query, NULL, NULL)) {
853 bsendmsg(ua, "%s", db_strerror(ua->db));
855 bsendmsg(ua, "New Slot is: %d\n", slot);
857 free_pool_memory(query);
860 case 8: /* Volume Files */
862 bsendmsg(ua, _("Warning changing Volume Files can result\n"
863 "in loss of data on your Volume\n\n"));
864 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
865 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
868 VolFiles = ua->pint32_val;
869 if (VolFiles != (int)(mr.VolFiles + 1)) {
870 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
871 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
875 query = get_pool_memory(PM_MESSAGE);
876 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
877 VolFiles, mr.MediaId);
878 if (!db_sql_query(ua->db, query, NULL, NULL)) {
879 bsendmsg(ua, "%s", db_strerror(ua->db));
881 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
883 free_pool_memory(query);
886 default: /* Done or error */
887 bsendmsg(ua, "Selection done.\n");
895 * Update pool record -- pull info from current POOL resource
897 static int update_pool(UAContext *ua)
905 pool = get_pool_resource(ua);
910 memset(&pr, 0, sizeof(pr));
911 strcpy(pr.Name, pool->hdr.name);
912 if (!get_pool_dbr(ua, &pr)) {
916 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
918 id = db_update_pool_record(ua->jcr, ua->db, &pr);
920 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
921 id, db_strerror(ua->db));
923 query = get_pool_memory(PM_MESSAGE);
924 Mmsg(&query, list_pool, pr.PoolId);
925 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
926 free_pool_memory(query);
927 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
932 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
936 ua->jcr->store = store;
937 /* Try connecting for up to 15 seconds */
938 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
939 store->hdr.name, store->address, store->SDport);
940 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
941 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
944 Dmsg0(120, _("Connected to storage daemon\n"));
945 sd = ua->jcr->store_bsock;
946 bnet_fsend(sd, "setdebug=%d\n", level);
947 if (bnet_recv(sd) >= 0) {
948 bsendmsg(ua, "%s", sd->msg);
950 bnet_sig(sd, BNET_TERMINATE);
952 ua->jcr->store_bsock = NULL;
956 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
960 /* Connect to File daemon */
962 ua->jcr->client = client;
963 /* Try to connect for 15 seconds */
964 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
965 client->hdr.name, client->address, client->FDport);
966 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
967 bsendmsg(ua, _("Failed to connect to Client.\n"));
970 Dmsg0(120, "Connected to file daemon\n");
971 fd = ua->jcr->file_bsock;
972 bnet_fsend(fd, "setdebug=%d\n", level);
973 if (bnet_recv(fd) >= 0) {
974 bsendmsg(ua, "%s", fd->msg);
976 bnet_sig(fd, BNET_TERMINATE);
978 ua->jcr->file_bsock = NULL;
984 static void do_all_setdebug(UAContext *ua, int level)
986 STORE *store, **unique_store;
987 CLIENT *client, **unique_client;
993 /* Count Storage items */
996 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
998 unique_store = (STORE **) malloc(i * sizeof(STORE));
999 /* Find Unique Storage address/port */
1000 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1002 unique_store[i++] = store;
1003 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1005 for (j=0; j<i; j++) {
1006 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1007 unique_store[j]->SDport == store->SDport) {
1013 unique_store[i++] = store;
1014 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1019 /* Call each unique Storage daemon */
1020 for (j=0; j<i; j++) {
1021 do_storage_setdebug(ua, unique_store[j], level);
1025 /* Count Client items */
1028 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1030 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1031 /* Find Unique Client address/port */
1032 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1034 unique_client[i++] = client;
1035 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1037 for (j=0; j<i; j++) {
1038 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1039 unique_client[j]->FDport == client->FDport) {
1045 unique_client[i++] = client;
1046 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1051 /* Call each unique File daemon */
1052 for (j=0; j<i; j++) {
1053 do_client_setdebug(ua, unique_client[j], level);
1055 free(unique_client);
1059 * setdebug level=nn all
1061 static int setdebugcmd(UAContext *ua, char *cmd)
1071 Dmsg1(120, "setdebug:%s:\n", cmd);
1074 i = find_arg_with_value(ua, _("level"));
1076 level = atoi(ua->argv[i]);
1079 if (!get_pint(ua, _("Enter new debug level: "))) {
1082 level = ua->pint32_val;
1085 /* General debug? */
1086 for (i=1; i<ua->argc; i++) {
1087 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1088 do_all_setdebug(ua, level);
1091 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1092 strcasecmp(ua->argk[i], _("director")) == 0) {
1093 debug_level = level;
1096 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1099 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1101 do_client_setdebug(ua, client, level);
1105 client = select_client_resource(ua);
1107 do_client_setdebug(ua, client, level);
1112 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1113 strcasecmp(ua->argk[i], _("storage")) == 0) {
1116 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1118 do_storage_setdebug(ua, store, level);
1122 store = get_storage_resource(ua, 0);
1124 do_storage_setdebug(ua, store, level);
1130 * We didn't find an appropriate keyword above, so
1133 start_prompt(ua, _("Available daemons are: \n"));
1134 add_prompt(ua, _("Director"));
1135 add_prompt(ua, _("Storage"));
1136 add_prompt(ua, _("Client"));
1137 add_prompt(ua, _("All"));
1138 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1139 case 0: /* Director */
1140 debug_level = level;
1143 store = get_storage_resource(ua, 0);
1145 do_storage_setdebug(ua, store, level);
1149 client = select_client_resource(ua);
1151 do_client_setdebug(ua, client, level);
1155 do_all_setdebug(ua, level);
1166 static int timecmd(UAContext *ua, char *cmd)
1169 time_t ttime = time(NULL);
1171 localtime_r(&ttime, &tm);
1172 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1173 bsendmsg(ua, "%s\n", sdt);
1180 * Delete Pool records (should purge Media with it).
1182 * delete pool=<pool-name>
1183 * delete media pool=<pool-name> volume=<name>
1185 static int deletecmd(UAContext *ua, char *cmd)
1187 static char *keywords[] = {
1197 "In general it is not a good idea to delete either a\n"
1198 "Pool or a Volume since they may contain data.\n\n"));
1200 switch (find_arg_keyword(ua, keywords)) {
1210 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1218 bsendmsg(ua, _("Nothing done.\n"));
1225 * Delete media records from database -- dangerous
1227 static int delete_volume(UAContext *ua)
1231 if (!select_media_dbr(ua, &mr)) {
1234 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1235 "and all Jobs saved on that volume from the Catalog\n"),
1238 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1241 if (ua->pint32_val) {
1242 db_delete_media_record(ua->jcr, ua->db, &mr);
1248 * Delete a pool record from the database -- dangerous
1250 static int delete_pool(UAContext *ua)
1254 memset(&pr, 0, sizeof(pr));
1256 if (!get_pool_dbr(ua, &pr)) {
1259 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1262 if (ua->pint32_val) {
1263 db_delete_pool_record(ua->jcr, ua->db, &pr);
1269 static void do_mount_cmd(int mount, UAContext *ua, char *cmd)
1273 char dev_name[MAX_NAME_LENGTH];
1279 Dmsg1(120, "mount: %s\n", ua->UA_sock->msg);
1281 store = get_storage_resource(ua, 1);
1286 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1287 store->media_type, store->dev_name);
1289 ua->jcr->store = store;
1290 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1291 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1294 sd = ua->jcr->store_bsock;
1295 strcpy(dev_name, store->dev_name);
1296 bash_spaces(dev_name);
1298 bnet_fsend(sd, "mount %s", dev_name);
1300 bnet_fsend(sd, "unmount %s", dev_name);
1302 while (bnet_recv(sd) >= 0) {
1303 bsendmsg(ua, "%s", sd->msg);
1304 if (strncmp(sd->msg, "3001 OK mount.", 14) == 0) {
1305 /***** ****FIXME**** fix JobStatus */
1308 bnet_sig(sd, BNET_TERMINATE);
1310 ua->jcr->store_bsock = NULL;
1314 * mount [storage | device] <name>
1316 static int mountcmd(UAContext *ua, char *cmd)
1318 do_mount_cmd(1, ua, cmd); /* mount */
1324 * unmount [storage | device] <name>
1326 static int unmountcmd(UAContext *ua, char *cmd)
1328 do_mount_cmd(0, ua, cmd); /* unmount */
1335 * use catalog=<name>
1337 static int usecmd(UAContext *ua, char *cmd)
1339 CAT *oldcatalog, *catalog;
1342 close_db(ua); /* close any previously open db */
1343 oldcatalog = ua->catalog;
1345 if (!(catalog = get_catalog_resource(ua))) {
1346 ua->catalog = oldcatalog;
1348 ua->catalog = catalog;
1351 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1352 ua->catalog->hdr.name, ua->catalog->db_name);
1357 int quitcmd(UAContext *ua, char *cmd)
1364 * Wait until no job is running
1366 int waitcmd(UAContext *ua, char *cmd)
1368 bmicrosleep(0, 200000); /* let job actually start */
1369 for (int running=1; running; ) {
1372 for (JCR *jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
1373 if (jcr->JobId != 0) {
1375 free_locked_jcr(jcr);
1378 free_locked_jcr(jcr);
1389 static int helpcmd(UAContext *ua, char *cmd)
1394 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1395 for (i=0; i<comsize; i++) {
1396 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1402 static int versioncmd(UAContext *ua, char *cmd)
1404 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1409 /* A bit brain damaged in that if the user has not done
1410 * a "use catalog xxx" command, we simply find the first
1411 * catalog resource and open it.
1413 int open_db(UAContext *ua)
1420 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1423 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1426 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1427 ua->catalog->hdr.name, ua->catalog->db_name);
1431 Dmsg0(150, "Open database\n");
1432 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1433 ua->catalog->db_password, ua->catalog->db_address,
1434 ua->catalog->db_port, ua->catalog->db_socket);
1435 if (!db_open_database(ua->jcr, ua->db)) {
1436 bsendmsg(ua, _("Could not open DB %s: ERR=%s"),
1437 ua->catalog->db_name, db_strerror(ua->db));
1441 ua->jcr->db = ua->db;
1442 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1446 void close_db(UAContext *ua)
1449 db_close_database(ua->jcr, ua->db);