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);
62 /* Forward referenced functions */
63 static int addcmd(UAContext *ua, char *cmd), createcmd(UAContext *ua, char *cmd), cancelcmd(UAContext *ua, char *cmd);
64 static int setdebugcmd(UAContext *ua, char *cmd);
65 static int helpcmd(UAContext *ua, char *cmd);
66 static int deletecmd(UAContext *ua, char *cmd);
67 static int usecmd(UAContext *ua, char *cmd), unmountcmd(UAContext *ua, char *cmd);
68 static int labelcmd(UAContext *ua, char *cmd), mountcmd(UAContext *ua, char *cmd), updatecmd(UAContext *ua, char *cmd);
69 static int relabelcmd(UAContext *ua, char *cmd), mountcmd(UAContext *ua, char *cmd), updatecmd(UAContext *ua, char *cmd);
70 static int versioncmd(UAContext *ua, char *cmd), automountcmd(UAContext *ua, char *cmd);
71 static int timecmd(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 do_label(UAContext *ua, char *cmd, int relabel);
78 int quitcmd(UAContext *ua, char *cmd);
81 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; };
82 static struct cmdstruct commands[] = {
83 { N_("add"), addcmd, _("add media to a pool")},
84 { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
85 { N_("automount"), automountcmd, _("automount [on/off] -- after label")},
86 { N_("cancel"), cancelcmd, _("cancel job=nnn -- cancel a job")},
87 { N_("create"), createcmd, _("create DB Pool from resource")},
88 { N_("delete"), deletecmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
89 { N_("help"), helpcmd, _("print this command")},
90 { N_("label"), labelcmd, _("label a tape")},
91 { N_("relabel"), relabelcmd, _("relabel a tape")},
92 { N_("list"), listcmd, _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
93 { N_("llist"), llistcmd, _("full or long list like list command")},
94 { N_("messages"), messagescmd, _("messages")},
95 { N_("mount"), mountcmd, _("mount <storage-name>")},
96 { N_("restore"), restorecmd, _("restore files")},
97 { N_("prune"), prunecmd, _("prune expired records from catalog")},
98 { N_("purge"), purgecmd, _("purge records from catalog")},
99 { N_("run"), runcmd, _("run <job-name>")},
100 { N_("setdebug"), setdebugcmd, _("sets debug level")},
101 { N_("show"), showcmd, _("show (resource records) [jobs | pools | ... | all]")},
102 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
103 { N_("status"), statuscmd, _("status [storage | client]=<name>")},
104 { N_("unmount"), unmountcmd, _("unmount <storage-name>")},
105 { N_("update"), updatecmd, _("update Volume or Pool")},
106 { N_("use"), usecmd, _("use catalog xxx")},
107 { N_("version"), versioncmd, _("print Director version")},
108 { N_("quit"), quitcmd, _("quit")},
109 { N_("query"), querycmd, _("query catalog")},
110 { N_("time"), timecmd, _("print current time")},
111 { N_("exit"), quitcmd, _("exit = quit")},
113 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
116 * Execute a command from the UA
118 int do_a_command(UAContext *ua, char *cmd)
127 Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
132 len = strlen(ua->argk[0]);
133 for (i=0; i<comsize; i++) /* search for command */
134 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
135 stat = (*commands[i].func)(ua, cmd); /* go execute command */
140 strcat(ua->UA_sock->msg, _(": is an illegal command\n"));
141 ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
142 bnet_send(ua->UA_sock);
148 * This is a common routine used to stuff the Pool DB record defaults
149 * into the Media DB record just before creating a media (Volume)
152 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
154 mr->PoolId = pr->PoolId;
155 strcpy(mr->VolStatus, "Append");
156 mr->Recycle = pr->Recycle;
157 mr->VolRetention = pr->VolRetention;
158 mr->VolUseDuration = pr->VolUseDuration;
159 mr->MaxVolJobs = pr->MaxVolJobs;
160 mr->MaxVolFiles = pr->MaxVolFiles;
161 mr->MaxVolBytes = pr->MaxVolBytes;
166 * Add Volumes to an existing Pool
168 static int addcmd(UAContext *ua, char *cmd)
172 int num, i, max, startnum;
174 char name[MAX_NAME_LENGTH];
179 "You probably don't want to be using this command since it\n"
180 "creates database records without labeling the Volumes.\n"
181 "You probably want to use the \"label\" command.\n\n"));
187 memset(&pr, 0, sizeof(pr));
188 memset(&mr, 0, sizeof(mr));
190 if (!get_pool_dbr(ua, &pr)) {
194 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
195 pr.MaxVols, pr.PoolType);
197 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
198 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
200 if (!get_cmd(ua, _("Enter new maximum (zero for unlimited): "))) {
203 pr.MaxVols = atoi(ua->cmd);
204 if (pr.MaxVols < 0) {
205 bsendmsg(ua, _("Max vols must be zero or greater.\n"));
213 if ((store = get_storage_resource(ua, cmd)) != NULL) {
214 strcpy(mr.MediaType, store->media_type);
215 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
219 if (pr.MaxVols == 0) {
222 max = pr.MaxVols - pr.NumVols;
226 sprintf(buf, _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
227 if (!get_cmd(ua, buf)) {
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 (strchr(ua->cmd, '|')) {
249 bsendmsg(ua, _("Illegal character | in a volume name.\n"));
252 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
253 bsendmsg(ua, _("Volume name too long.\n"));
256 if (strlen(ua->cmd) == 0) {
257 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
261 strcpy(name, ua->cmd);
263 strcat(name, "%04d");
266 if (!get_cmd(ua, _("Enter the starting number: "))) {
269 startnum = atoi(ua->cmd);
271 bsendmsg(ua, _("Start number must be greater than zero.\n"));
281 if (store && store->autochanger) {
282 if (!get_cmd(ua, _("Enter slot (0 for none): "))) {
285 slot = atoi(ua->cmd);
288 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
289 for (i=startnum; i < num+startnum; i++) {
290 sprintf(mr.VolumeName, name, i);
292 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
293 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
294 bsendmsg(ua, db_strerror(ua->db));
298 first_id = mr.PoolId;
302 Dmsg0(200, "Update pool record.\n");
303 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
304 bsendmsg(ua, db_strerror(ua->db));
307 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
313 * Turn auto mount on/off
318 int automountcmd(UAContext *ua, char *cmd)
323 if (!get_cmd(ua, _("Turn on or off? "))) {
331 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
339 static int cancelcmd(UAContext *ua, char *cmd)
345 char JobName[MAX_NAME_LENGTH];
351 for (i=1; i<ua->argc; i++) {
352 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
356 if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
357 bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
361 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
365 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
366 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
372 /* If we still do not have a jcr,
373 * throw up a list and ask the user to select one.
376 /* Count Jobs running */
378 for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
379 if (jcr->JobId == 0) { /* this is us */
380 free_locked_jcr(jcr);
384 free_locked_jcr(jcr);
389 bsendmsg(ua, _("No Jobs running.\n"));
392 start_prompt(ua, _("Select Job:\n"));
394 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
395 if (jcr->JobId == 0) { /* this is us */
396 free_locked_jcr(jcr);
399 add_prompt(ua, jcr->Job);
400 free_locked_jcr(jcr);
404 if (do_prompt(ua, _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
408 if (!get_cmd(ua, _("Confirm cancel (yes/no): "))) {
411 if (strcasecmp(ua->cmd, _("yes")) != 0) {
415 jcr = get_jcr_by_full_name(JobName);
417 bsendmsg(ua, _("Job %s not found.\n"), JobName);
422 switch (jcr->JobStatus) {
424 set_jcr_job_status(jcr, JS_Cancelled);
425 bsendmsg(ua, _("JobId %d, Job %s marked to be cancelled.\n"),
426 jcr->JobId, jcr->Job);
427 #ifndef USE_SEMAPHORE
428 workq_remove(&job_wq, jcr->work_item); /* attempt to remove it from queue */
434 set_jcr_job_status(jcr, JS_Cancelled);
435 /* Cancel File daemon */
436 ua->jcr->client = jcr->client;
437 if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
438 bsendmsg(ua, _("Failed to connect to File daemon.\n"));
442 Dmsg0(200, "Connected to file daemon\n");
443 fd = ua->jcr->file_bsock;
444 bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
445 while (bnet_recv(fd) >= 0) {
446 bsendmsg(ua, "%s", fd->msg);
448 bnet_sig(fd, BNET_TERMINATE);
450 ua->jcr->file_bsock = NULL;
452 /* Cancel Storage daemon */
453 ua->jcr->store = jcr->store;
454 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
455 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
459 Dmsg0(200, "Connected to storage daemon\n");
460 sd = ua->jcr->store_bsock;
461 bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
462 while (bnet_recv(sd) >= 0) {
463 bsendmsg(ua, "%s", sd->msg);
465 bnet_sig(sd, BNET_TERMINATE);
467 ua->jcr->store_bsock = NULL;
476 * This is a common routine to create or update a
477 * Pool DB base record from a Pool Resource. We handle
478 * the setting of MaxVols and NumVols slightly differently
479 * depending on if we are creating the Pool or we are
480 * simply bringing it into agreement with the resource (updage).
482 void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, int create)
484 strcpy(pr->PoolType, pool->pool_type);
486 pr->MaxVols = pool->max_volumes;
488 } else { /* update pool */
489 if (pr->MaxVols != pool->max_volumes) {
490 pr->MaxVols = pool->max_volumes;
492 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
493 pr->MaxVols = pr->NumVols;
496 pr->UseOnce = pool->use_volume_once;
497 pr->UseCatalog = pool->use_catalog;
498 pr->AcceptAnyVolume = pool->accept_any_volume;
499 pr->Recycle = pool->Recycle;
500 pr->VolRetention = pool->VolRetention;
501 pr->VolUseDuration = pool->VolUseDuration;
502 pr->MaxVolJobs = pool->MaxVolJobs;
503 pr->MaxVolFiles = pool->MaxVolFiles;
504 pr->MaxVolBytes = pool->MaxVolBytes;
505 if (pool->label_format) {
506 strcpy(pr->LabelFormat, pool->label_format);
508 strcpy(pr->LabelFormat, "*"); /* none */
514 * Create a pool record from a given Pool resource
515 * Also called from backup.c
516 * Returns: -1 on error
517 * 0 record already exists
521 int create_pool(JCR *jcr, B_DB *db, POOL *pool, int create)
525 memset(&pr, 0, sizeof(POOL_DBR));
527 strcpy(pr.Name, pool->hdr.name);
529 if (db_get_pool_record(jcr, db, &pr)) {
531 if (!create) { /* update request */
532 set_pooldbr_from_poolres(&pr, pool, 0);
533 db_update_pool_record(jcr, db, &pr);
535 return 0; /* exists */
538 set_pooldbr_from_poolres(&pr, pool, 1);
540 if (!db_create_pool_record(jcr, db, &pr)) {
541 return -1; /* error */
549 * Create a Pool Record in the database.
550 * It is always created from the Resource record.
552 static int createcmd(UAContext *ua, char *cmd)
560 pool = get_pool_resource(ua);
565 switch (create_pool(ua->jcr, ua->db, pool, 1)) {
567 bsendmsg(ua, _("Error: Pool %s already exists.\n\
568 Use update to change it.\n"), pool->hdr.name);
572 bsendmsg(ua, db_strerror(ua->db));
578 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
586 * Update a Pool Record in the database.
587 * It is always updated from the Resource record.
589 * update pool=<pool-name>
590 * updates pool from Pool resource
591 * update media pool=<pool-name> volume=<volume-name>
592 * changes pool info for volume
594 static int updatecmd(UAContext *ua, char *cmd)
596 static char *kw[] = {
606 switch (find_arg_keyword(ua, kw)) {
618 start_prompt(ua, _("Update choice:\n"));
619 add_prompt(ua, _("Volume parameters"));
620 add_prompt(ua, _("Pool from resource"));
621 switch (do_prompt(ua, _("Choose catalog item to update"), NULL, 0)) {
635 * Update a media record -- allows you to change the
636 * Volume status. E.g. if you want Bacula to stop
637 * writing on the volume, set it to anything other
640 static int update_volume(UAContext *ua)
647 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
651 for (int done=0; !done; ) {
652 if (!db_get_media_record(ua->jcr, ua->db, &mr)) {
653 if (mr.MediaId != 0) {
654 bsendmsg(ua, _("Volume record for MediaId %d not found.\n"), mr.MediaId);
656 bsendmsg(ua, _("Volume record for %s not found.\n"), mr.VolumeName);
660 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
661 start_prompt(ua, _("Parameters to modify:\n"));
662 add_prompt(ua, _("Volume Status"));
663 add_prompt(ua, _("Volume Retention Period"));
664 add_prompt(ua, _("Volume Use Duration"));
665 add_prompt(ua, _("Maximum Volume Jobs"));
666 add_prompt(ua, _("Maximum Volume Files"));
667 add_prompt(ua, _("Maximum Volume Bytes"));
668 add_prompt(ua, _("Recycle Flag"));
669 add_prompt(ua, _("Slot"));
670 add_prompt(ua, _("Volume Files"));
671 add_prompt(ua, _("Done"));
672 switch (do_prompt(ua, _("Select parameter to modify"), NULL, 0)) {
673 case 0: /* Volume Status */
674 /* Modify Volume Status */
675 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
676 start_prompt(ua, _("Possible Values are:\n"));
677 add_prompt(ua, "Append"); /* Better not translate these as */
678 add_prompt(ua, "Archive"); /* They are known in the database code */
679 add_prompt(ua, "Disabled");
680 add_prompt(ua, "Full");
681 add_prompt(ua, "Used");
682 if (strcmp(mr.VolStatus, "Purged") == 0) {
683 add_prompt(ua, "Recycle");
685 add_prompt(ua, "Read-Only");
686 if (do_prompt(ua, _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
689 bstrncpy(mr.VolStatus, ua->cmd, sizeof(mr.VolStatus));
690 query = get_pool_memory(PM_MESSAGE);
691 Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
692 mr.VolStatus, mr.MediaId);
693 if (!db_sql_query(ua->db, query, NULL, NULL)) {
694 bsendmsg(ua, "%s", db_strerror(ua->db));
696 bsendmsg(ua, _("New Volume status is: %s\n"), mr.VolStatus);
698 free_pool_memory(query);
700 case 1: /* Retention */
701 bsendmsg(ua, _("Current retention seconds is: %s\n"),
702 edit_utime(mr.VolRetention, ed1));
703 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
706 if (!duration_to_utime(ua->cmd, &mr.VolRetention)) {
707 bsendmsg(ua, _("Invalid retention period specified.\n"));
710 query = get_pool_memory(PM_MESSAGE);
711 Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
712 edit_uint64(mr.VolRetention, ed1), mr.MediaId);
713 if (!db_sql_query(ua->db, query, NULL, NULL)) {
714 bsendmsg(ua, "%s", db_strerror(ua->db));
716 bsendmsg(ua, _("New retention seconds is: %s\n"),
717 edit_utime(mr.VolRetention, ed1));
719 free_pool_memory(query);
722 case 2: /* Use Duration */
723 bsendmsg(ua, _("Current use duration is: %s\n"),
724 edit_utime(mr.VolUseDuration, ed1));
725 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
728 if (!duration_to_utime(ua->cmd, &mr.VolUseDuration)) {
729 bsendmsg(ua, _("Invalid use duration specified.\n"));
732 query = get_pool_memory(PM_MESSAGE);
733 Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
734 edit_uint64(mr.VolUseDuration, ed1), mr.MediaId);
735 if (!db_sql_query(ua->db, query, NULL, NULL)) {
736 bsendmsg(ua, "%s", db_strerror(ua->db));
738 bsendmsg(ua, _("New use duration is: %s\n"),
739 edit_utime(mr.VolUseDuration, ed1));
741 free_pool_memory(query);
744 case 3: /* Max Jobs */
746 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
747 if (!get_cmd(ua, _("Enter new Maximum Jobs: "))) {
750 maxjobs = atoi(ua->cmd);
752 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
755 query = get_pool_memory(PM_MESSAGE);
756 Mmsg(&query, "UPDATE Media SET MaxVolJobs=%u WHERE MediaId=%u",
757 maxjobs, mr.MediaId);
758 if (!db_sql_query(ua->db, query, NULL, NULL)) {
759 bsendmsg(ua, "%s", db_strerror(ua->db));
761 bsendmsg(ua, _("New max jobs is: %u\n"), maxjobs);
763 free_pool_memory(query);
766 case 4: /* Max Files */
768 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
769 if (!get_cmd(ua, _("Enter new Maximum Files: "))) {
772 maxfiles = atoi(ua->cmd);
774 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
777 query = get_pool_memory(PM_MESSAGE);
778 Mmsg(&query, "UPDATE Media SET MaxVolFiles=%u WHERE MediaId=%u",
779 maxfiles, mr.MediaId);
780 if (!db_sql_query(ua->db, query, NULL, NULL)) {
781 bsendmsg(ua, "%s", db_strerror(ua->db));
783 bsendmsg(ua, _("New max files is: %u\n"), maxfiles);
785 free_pool_memory(query);
788 case 5: /* Max Bytes */
790 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
791 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
794 if (!size_to_uint64(ua->cmd, strlen(ua->cmd), &maxbytes)) {
795 bsendmsg(ua, _("Invalid byte size specification.\n"));
798 query = get_pool_memory(PM_MESSAGE);
799 Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
800 edit_uint64(maxbytes, ed1), mr.MediaId);
801 if (!db_sql_query(ua->db, query, NULL, NULL)) {
802 bsendmsg(ua, "%s", db_strerror(ua->db));
804 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
806 free_pool_memory(query);
810 case 6: /* Recycle */
812 bsendmsg(ua, _("Current recycle flag is: %s\n"),
813 mr.Recycle==1?_("yes"):_("no"));
814 if (!get_cmd(ua, _("Enter new Recycle status: "))) {
817 if (strcasecmp(ua->cmd, _("yes")) == 0) {
819 } else if (strcasecmp(ua->cmd, _("no")) == 0) {
822 bsendmsg(ua, _("Invalid recycle status specified.\n"));
825 query = get_pool_memory(PM_MESSAGE);
826 Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
827 recycle, mr.MediaId);
828 if (!db_sql_query(ua->db, query, NULL, NULL)) {
829 bsendmsg(ua, "%s", db_strerror(ua->db));
831 bsendmsg(ua, _("New recycle flag is: %s\n"),
832 mr.Recycle==1?_("yes"):_("no"));
834 free_pool_memory(query);
839 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
840 if (!get_cmd(ua, _("Enter new Slot: "))) {
843 slot = atoi(ua->cmd);
845 bsendmsg(ua, _("Invalid slot, it must be 0 or greater\n"));
847 } else if (pr.MaxVols > 0 && slot >(int)pr.MaxVols) {
848 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
852 query = get_pool_memory(PM_MESSAGE);
853 Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
855 if (!db_sql_query(ua->db, query, NULL, NULL)) {
856 bsendmsg(ua, "%s", db_strerror(ua->db));
858 bsendmsg(ua, "New Slot is: %d\n", slot);
860 free_pool_memory(query);
863 case 8: /* Volume Files */
865 bsendmsg(ua, _("Warning changing Volume Files can result\n"
866 "in loss of data on your Volume\n\n"));
867 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
868 if (!get_cmd(ua, _("Enter new number of Files for Volume: "))) {
871 VolFiles = atoi(ua->cmd);
873 bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
876 if (VolFiles != (int)(mr.VolFiles + 1)) {
877 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
878 if (!get_cmd(ua, _("Continue? (yes/no): ")) ||
879 strcasecmp(ua->cmd, "yes") != 0) {
883 query = get_pool_memory(PM_MESSAGE);
884 Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
885 VolFiles, mr.MediaId);
886 if (!db_sql_query(ua->db, query, NULL, NULL)) {
887 bsendmsg(ua, "%s", db_strerror(ua->db));
889 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
891 free_pool_memory(query);
894 default: /* Done or error */
895 bsendmsg(ua, "Selection done.\n");
903 * Update pool record -- pull info from current POOL resource
905 static int update_pool(UAContext *ua)
913 pool = get_pool_resource(ua);
918 memset(&pr, 0, sizeof(pr));
919 strcpy(pr.Name, pool->hdr.name);
920 if (!get_pool_dbr(ua, &pr)) {
924 set_pooldbr_from_poolres(&pr, pool, 0); /* update */
926 id = db_update_pool_record(ua->jcr, ua->db, &pr);
928 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
929 id, db_strerror(ua->db));
931 query = get_pool_memory(PM_MESSAGE);
932 Mmsg(&query, list_pool, pr.PoolId);
933 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, 0);
934 free_pool_memory(query);
935 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
940 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
944 ua->jcr->store = store;
945 /* Try connecting for up to 15 seconds */
946 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
947 store->hdr.name, store->address, store->SDport);
948 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
949 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
952 Dmsg0(120, _("Connected to storage daemon\n"));
953 sd = ua->jcr->store_bsock;
954 bnet_fsend(sd, "setdebug=%d\n", level);
955 if (bnet_recv(sd) >= 0) {
956 bsendmsg(ua, "%s", sd->msg);
958 bnet_sig(sd, BNET_TERMINATE);
960 ua->jcr->store_bsock = NULL;
964 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
968 /* Connect to File daemon */
970 ua->jcr->client = client;
971 /* Try to connect for 15 seconds */
972 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
973 client->hdr.name, client->address, client->FDport);
974 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
975 bsendmsg(ua, _("Failed to connect to Client.\n"));
978 Dmsg0(120, "Connected to file daemon\n");
979 fd = ua->jcr->file_bsock;
980 bnet_fsend(fd, "setdebug=%d\n", level);
981 if (bnet_recv(fd) >= 0) {
982 bsendmsg(ua, "%s", fd->msg);
984 bnet_sig(fd, BNET_TERMINATE);
986 ua->jcr->file_bsock = NULL;
992 static void do_all_setdebug(UAContext *ua, int level)
994 STORE *store, **unique_store;
995 CLIENT *client, **unique_client;
1001 /* Count Storage items */
1004 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1006 unique_store = (STORE **) malloc(i * sizeof(STORE));
1007 /* Find Unique Storage address/port */
1008 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1010 unique_store[i++] = store;
1011 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1013 for (j=0; j<i; j++) {
1014 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1015 unique_store[j]->SDport == store->SDport) {
1021 unique_store[i++] = store;
1022 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1027 /* Call each unique Storage daemon */
1028 for (j=0; j<i; j++) {
1029 do_storage_setdebug(ua, unique_store[j], level);
1033 /* Count Client items */
1036 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1038 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1039 /* Find Unique Client address/port */
1040 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1042 unique_client[i++] = client;
1043 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1045 for (j=0; j<i; j++) {
1046 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1047 unique_client[j]->FDport == client->FDport) {
1053 unique_client[i++] = client;
1054 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1059 /* Call each unique File daemon */
1060 for (j=0; j<i; j++) {
1061 do_client_setdebug(ua, unique_client[j], level);
1063 free(unique_client);
1067 * setdebug level=nn all
1069 static int setdebugcmd(UAContext *ua, char *cmd)
1079 Dmsg1(120, "setdebug:%s:\n", cmd);
1082 for (i=1; i<ua->argc; i++) {
1083 if (strcasecmp(ua->argk[i], _("level")) == 0 && ua->argv[i]) {
1084 level = atoi(ua->argv[i]);
1089 if (!get_cmd(ua, _("Enter new debug level: "))) {
1092 level = atoi(ua->cmd);
1095 bsendmsg(ua, _("level cannot be negative.\n"));
1099 /* General debug? */
1100 for (i=1; i<ua->argc; i++) {
1101 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1102 do_all_setdebug(ua, level);
1105 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1106 strcasecmp(ua->argk[i], _("director")) == 0) {
1107 debug_level = level;
1110 if (strcasecmp(ua->argk[i], _("client")) == 0) {
1113 client = (CLIENT *) GetResWithName(R_CLIENT, ua->argv[i]);
1115 do_client_setdebug(ua, client, level);
1119 client = select_client_resource(ua);
1121 do_client_setdebug(ua, client, level);
1125 store = get_storage_resource(ua, cmd);
1127 do_storage_setdebug(ua, store, level);
1133 * We didn't find an appropriate keyword above, so
1136 start_prompt(ua, _("Available daemons are: \n"));
1137 add_prompt(ua, _("Director"));
1138 add_prompt(ua, _("Storage"));
1139 add_prompt(ua, _("Client"));
1140 add_prompt(ua, _("All"));
1141 switch(do_prompt(ua, _("Select daemon type to set debug level"), NULL, 0)) {
1142 case 0: /* Director */
1143 debug_level = level;
1146 store = get_storage_resource(ua, cmd);
1148 do_storage_setdebug(ua, store, level);
1152 client = select_client_resource(ua);
1154 do_client_setdebug(ua, client, level);
1158 do_all_setdebug(ua, level);
1169 static int timecmd(UAContext *ua, char *cmd)
1172 time_t ttime = time(NULL);
1174 localtime_r(&ttime, &tm);
1175 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1176 bsendmsg(ua, "%s\n", sdt);
1183 * Delete Pool records (should purge Media with it).
1185 * delete pool=<pool-name>
1186 * delete media pool=<pool-name> volume=<name>
1188 static int deletecmd(UAContext *ua, char *cmd)
1190 static char *keywords[] = {
1200 "In general it is not a good idea to delete either a\n"
1201 "Pool or a Volume since they may contain data.\n\n"));
1203 switch (find_arg_keyword(ua, keywords)) {
1213 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1221 bsendmsg(ua, _("Nothing done.\n"));
1228 * Delete media records from database -- dangerous
1230 static int delete_volume(UAContext *ua)
1235 if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
1238 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1239 "and all Jobs saved on that volume from the Catalog\n"),
1242 if (!get_cmd(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1245 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1246 db_delete_media_record(ua->jcr, ua->db, &mr);
1252 * Delete a pool record from the database -- dangerous
1254 static int delete_pool(UAContext *ua)
1258 memset(&pr, 0, sizeof(pr));
1260 if (!get_pool_dbr(ua, &pr)) {
1263 if (!get_cmd(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1266 if (strcasecmp(ua->cmd, _("yes")) == 0) {
1267 db_delete_pool_record(ua->jcr, ua->db, &pr);
1276 * label storage=xxx volume=vvv
1278 static int labelcmd(UAContext *ua, char *cmd)
1280 return do_label(ua, cmd, 0); /* standard label */
1283 static int relabelcmd(UAContext *ua, char *cmd)
1285 return do_label(ua, cmd, 1); /* relabel tape */
1290 * Common routine for both label and relabel
1292 static int do_label(UAContext *ua, char *cmd, int relabel)
1296 char dev_name[MAX_NAME_LENGTH];
1300 int mounted = FALSE;
1303 static char *name_keyword[] = {
1307 static char *vol_keyword[] = {
1315 store = get_storage_resource(ua, cmd);
1320 /* If relabel get name of Volume to relabel */
1322 i = find_arg_keyword(ua, vol_keyword);
1323 if (i >= 0 && ua->argv[i]) {
1324 memset(&omr, 0, sizeof(omr));
1325 bstrncpy(omr.VolumeName, ua->argv[i], sizeof(omr.VolumeName));
1326 if (!db_get_media_record(ua->jcr, ua->db, &omr)) {
1327 bsendmsg(ua, "%s", db_strerror(ua->db));
1333 if (!select_pool_and_media_dbr(ua, &pr, &omr)) {
1338 if (strcmp(omr.VolStatus, "Purged") != 0) {
1339 bsendmsg(ua, _("Volume \"%s\" has VolStatus %s. It must be purged before relabeling.\n"),
1340 omr.VolumeName, omr.VolStatus);
1345 i = find_arg_keyword(ua, name_keyword);
1346 if (i >=0 && ua->argv[i]) {
1347 strcpy(ua->cmd, ua->argv[i]);
1352 if (!get_cmd(ua, _("Enter new Volume name: "))) {
1356 /* ****FIXME*** be much more restrictive in the name */
1357 if (strpbrk(ua->cmd, "`~!@#$%^&*()[]{}|\\;'\"<>?,/")) {
1358 bsendmsg(ua, _("Illegal character | in a volume name.\n"));
1361 if (strlen(ua->cmd) >= MAX_NAME_LENGTH) {
1362 bsendmsg(ua, _("Volume name too long.\n"));
1365 if (strlen(ua->cmd) == 0) {
1366 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
1370 memset(&mr, 0, sizeof(mr));
1371 strcpy(mr.VolumeName, ua->cmd);
1372 if (db_get_media_record(ua->jcr, ua->db, &mr)) {
1373 bsendmsg(ua, _("Media record for new Volume \"%s\" already exists.\n"),
1378 /* Do some more checking on slot ****FIXME**** */
1379 if (store->autochanger) {
1380 if (!get_cmd(ua, _("Enter slot (0 for none): "))) {
1383 slot = atoi(ua->cmd);
1385 strcpy(mr.MediaType, store->media_type);
1388 memset(&pr, 0, sizeof(pr));
1389 if (!select_pool_dbr(ua, &pr)) {
1393 ua->jcr->store = store;
1394 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
1395 store->hdr.name, store->address, store->SDport);
1396 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1397 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1400 sd = ua->jcr->store_bsock;
1401 strcpy(dev_name, store->dev_name);
1402 bash_spaces(dev_name);
1403 bash_spaces(mr.VolumeName);
1404 bash_spaces(mr.MediaType);
1405 bash_spaces(pr.Name);
1407 bash_spaces(omr.VolumeName);
1408 bnet_fsend(sd, _("relabel %s OldName=%s NewName=%s PoolName=%s MediaType=%s Slot=%d"),
1409 dev_name, omr.VolumeName, mr.VolumeName, pr.Name, mr.MediaType, mr.Slot);
1410 bsendmsg(ua, _("Sending relabel command ...\n"));
1412 bnet_fsend(sd, _("label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d"),
1413 dev_name, mr.VolumeName, pr.Name, mr.MediaType, mr.Slot);
1414 bsendmsg(ua, _("Sending label command ...\n"));
1416 while (bget_msg(sd, 0) >= 0) {
1417 bsendmsg(ua, "%s", sd->msg);
1418 if (strncmp(sd->msg, "3000 OK label.", 14) == 0) {
1421 bsendmsg(ua, _("Label command failed.\n"));
1424 ua->jcr->store_bsock = NULL;
1425 unbash_spaces(dev_name);
1426 unbash_spaces(mr.VolumeName);
1427 unbash_spaces(mr.MediaType);
1428 unbash_spaces(pr.Name);
1429 mr.LabelDate = time(NULL);
1431 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
1432 if (db_create_media_record(ua->jcr, ua->db, &mr)) {
1433 bsendmsg(ua, _("Media record for Volume \"%s\" successfully created.\n"),
1435 if (ua->automount) {
1436 bsendmsg(ua, _("Requesting mount %s ...\n"), dev_name);
1437 bash_spaces(dev_name);
1438 bnet_fsend(sd, "mount %s", dev_name);
1439 unbash_spaces(dev_name);
1440 while (bnet_recv(sd) >= 0) {
1441 bsendmsg(ua, "%s", sd->msg);
1443 * 3001 OK mount. Device=xxx or
1444 * 3001 Mounted Volume vvvv
1446 if (strncmp(sd->msg, "3001 ", 5) == 0) {
1448 /***** ****FIXME***** find job waiting for
1449 ***** mount, and change to waiting for SD
1455 bsendmsg(ua, "%s", db_strerror(ua->db));
1459 bsendmsg(ua, _("Do not forget to mount the drive!!!\n"));
1461 bnet_sig(sd, BNET_TERMINATE);
1466 static void do_mount_cmd(int mount, UAContext *ua, char *cmd)
1470 char dev_name[MAX_NAME_LENGTH];
1476 Dmsg1(120, "mount: %s\n", ua->UA_sock->msg);
1478 store = get_storage_resource(ua, cmd);
1483 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1484 store->media_type, store->dev_name);
1486 ua->jcr->store = store;
1487 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1488 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1491 sd = ua->jcr->store_bsock;
1492 strcpy(dev_name, store->dev_name);
1493 bash_spaces(dev_name);
1495 bnet_fsend(sd, "mount %s", dev_name);
1497 bnet_fsend(sd, "unmount %s", dev_name);
1499 while (bnet_recv(sd) >= 0) {
1500 bsendmsg(ua, "%s", sd->msg);
1501 if (strncmp(sd->msg, "3001 OK mount.", 14) == 0) {
1502 /***** ****FIXME**** fix JobStatus */
1505 bnet_sig(sd, BNET_TERMINATE);
1507 ua->jcr->store_bsock = NULL;
1511 * mount [storage | device] <name>
1513 static int mountcmd(UAContext *ua, char *cmd)
1515 do_mount_cmd(1, ua, cmd); /* mount */
1521 * unmount [storage | device] <name>
1523 static int unmountcmd(UAContext *ua, char *cmd)
1525 do_mount_cmd(0, ua, cmd); /* unmount */
1532 * use catalog=<name>
1534 static int usecmd(UAContext *ua, char *cmd)
1536 CAT *oldcatalog, *catalog;
1539 close_db(ua); /* close any previously open db */
1540 oldcatalog = ua->catalog;
1542 if (!(catalog = get_catalog_resource(ua))) {
1543 ua->catalog = oldcatalog;
1545 ua->catalog = catalog;
1548 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1549 ua->catalog->hdr.name, ua->catalog->db_name);
1554 int quitcmd(UAContext *ua, char *cmd)
1560 static int helpcmd(UAContext *ua, char *cmd)
1565 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1566 for (i=0; i<comsize; i++) {
1567 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1573 static int versioncmd(UAContext *ua, char *cmd)
1575 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1580 /* A bit brain damaged in that if the user has not done
1581 * a "use catalog xxx" command, we simply find the first
1582 * catalog resource and open it.
1584 int open_db(UAContext *ua)
1591 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1594 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1597 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1598 ua->catalog->hdr.name, ua->catalog->db_name);
1602 Dmsg0(150, "Open database\n");
1603 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1604 ua->catalog->db_password, ua->catalog->db_address,
1605 ua->catalog->db_port, ua->catalog->db_socket);
1606 if (!db_open_database(ua->jcr, ua->db)) {
1607 bsendmsg(ua, _("Could not open DB %s: ERR=%s"),
1608 ua->catalog->db_name, db_strerror(ua->db));
1612 ua->jcr->db = ua->db;
1613 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1617 void close_db(UAContext *ua)
1620 db_close_database(ua->jcr, ua->db);