3 * Bacula Director -- User Agent Commands
5 * Kern Sibbald, September MM
11 Copyright (C) 2000-2004 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 /* Imported subroutines */
35 /* Imported variables */
38 extern struct s_res resources[];
39 extern char my_name[];
40 extern jobq_t job_queue; /* job queue */
43 extern char *list_pool;
45 /* Imported functions */
46 extern int status_cmd(UAContext *ua, const char *cmd);
47 extern int list_cmd(UAContext *ua, const char *cmd);
48 extern int llist_cmd(UAContext *ua, const char *cmd);
49 extern int show_cmd(UAContext *ua, const char *cmd);
50 extern int messagescmd(UAContext *ua, const char *cmd);
51 extern int autodisplay_cmd(UAContext *ua, const char *cmd);
52 extern int gui_cmd(UAContext *ua, const char *cmd);
53 extern int sqlquerycmd(UAContext *ua, const char *cmd);
54 extern int querycmd(UAContext *ua, const char *cmd);
55 extern int run_cmd(UAContext *ua, const char *cmd);
56 extern int retentioncmd(UAContext *ua, const char *cmd);
57 extern int prunecmd(UAContext *ua, const char *cmd);
58 extern int purgecmd(UAContext *ua, const char *cmd);
59 extern int restore_cmd(UAContext *ua, const char *cmd);
60 extern int label_cmd(UAContext *ua, const char *cmd);
61 extern int relabel_cmd(UAContext *ua, const char *cmd);
62 extern int update_slots(UAContext *ua); /* ua_label.c */
64 /* Forward referenced functions */
65 static int add_cmd(UAContext *ua, const char *cmd);
66 static int create_cmd(UAContext *ua, const char *cmd);
67 static int cancel_cmd(UAContext *ua, const char *cmd);
68 static int setdebug_cmd(UAContext *ua, const char *cmd);
69 static int trace_cmd(UAContext *ua, const char *cmd);
70 static int var_cmd(UAContext *ua, const char *cmd);
71 static int estimate_cmd(UAContext *ua, const char *cmd);
72 static int help_cmd(UAContext *ua, const char *cmd);
73 static int delete_cmd(UAContext *ua, const char *cmd);
74 static int use_cmd(UAContext *ua, const char *cmd);
75 static int unmount_cmd(UAContext *ua, const char *cmd);
76 static int version_cmd(UAContext *ua, const char *cmd);
77 static int automount_cmd(UAContext *ua, const char *cmd);
78 static int time_cmd(UAContext *ua, const char *cmd);
79 static int reload_cmd(UAContext *ua, const char *cmd);
80 static int update_volume(UAContext *ua);
81 static int update_pool(UAContext *ua);
82 static int delete_volume(UAContext *ua);
83 static int delete_pool(UAContext *ua);
84 static void delete_job(UAContext *ua);
85 static int mount_cmd(UAContext *ua, const char *cmd);
86 static int release_cmd(UAContext *ua, const char *cmd);
87 static int update_cmd(UAContext *ua, const char *cmd);
88 static int wait_cmd(UAContext *ua, const char *cmd);
89 static int setip_cmd(UAContext *ua, const char *cmd);
90 static void do_job_delete(UAContext *ua, JobId_t JobId);
91 static void delete_job_id_range(UAContext *ua, char *tok);
93 int qhelp_cmd(UAContext *ua, const char *cmd);
94 int quit_cmd(UAContext *ua, const char *cmd);
97 struct cmdstruct { const char *key; int (*func)(UAContext *ua, const char *cmd); const char *help; };
98 static struct cmdstruct commands[] = {
99 { N_("add"), add_cmd, _("add media to a pool")},
100 { N_("autodisplay"), autodisplay_cmd, _("autodisplay [on/off] -- console messages")},
101 { N_("automount"), automount_cmd, _("automount [on/off] -- after label")},
102 { N_("cancel"), cancel_cmd, _("cancel job=nnn -- cancel a job")},
103 { N_("create"), create_cmd, _("create DB Pool from resource")},
104 { N_("delete"), delete_cmd, _("delete [pool=<pool-name> | media volume=<volume-name>]")},
105 { N_("estimate"), estimate_cmd, _("performs FileSet estimate, listing gives full listing")},
106 { N_("exit"), quit_cmd, _("exit = quit")},
107 { N_("gui"), gui_cmd, _("gui [on/off] -- non-interactive gui mode")},
108 { N_("help"), help_cmd, _("print this command")},
109 { N_("list"), list_cmd, _("list [pools | jobs | jobtotals | media <pool> | files jobid=<nn>]; from catalog")},
110 { N_("label"), label_cmd, _("label a tape")},
111 { N_("llist"), llist_cmd, _("full or long list like list command")},
112 { N_("messages"), messagescmd, _("messages")},
113 { N_("mount"), mount_cmd, _("mount <storage-name>")},
114 { N_("prune"), prunecmd, _("prune expired records from catalog")},
115 { N_("purge"), purgecmd, _("purge records from catalog")},
116 { N_("quit"), quit_cmd, _("quit")},
117 { N_("query"), querycmd, _("query catalog")},
118 { N_("restore"), restore_cmd, _("restore files")},
119 { N_("relabel"), relabel_cmd, _("relabel a tape")},
120 { N_("release"), release_cmd, _("release <storage-name>")},
121 { N_("reload"), reload_cmd, _("reload conf file")},
122 { N_("run"), run_cmd, _("run <job-name>")},
123 { N_("status"), status_cmd, _("status [storage | client]=<name>")},
124 { N_("setdebug"), setdebug_cmd, _("sets debug level")},
125 { N_("setip"), setip_cmd, _("sets new client address -- if authorized")},
126 { N_("show"), show_cmd, _("show (resource records) [jobs | pools | ... | all]")},
127 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
128 { N_("time"), time_cmd, _("print current time")},
129 { N_("trace"), trace_cmd, _("turn on/off trace to file")},
130 { N_("unmount"), unmount_cmd, _("unmount <storage-name>")},
131 { N_("update"), update_cmd, _("update Volume or Pool")},
132 { N_("use"), use_cmd, _("use catalog xxx")},
133 { N_("var"), var_cmd, _("does variable expansion")},
134 { N_("version"), version_cmd, _("print Director version")},
135 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
137 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
140 * Execute a command from the UA
142 int do_a_command(UAContext *ua, const char *cmd)
150 Dmsg1(200, "Command: %s\n", ua->UA_sock->msg);
155 len = strlen(ua->argk[0]);
156 for (i=0; i<comsize; i++) { /* search for command */
157 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
158 if (!acl_access_ok(ua, Command_ACL, ua->argk[0], len)) {
161 stat = (*commands[i].func)(ua, cmd); /* go execute command */
167 bnet_fsend(ua->UA_sock, _("%s: is an illegal command.\n"), ua->argk[0]);
173 * This is a common routine used to stuff the Pool DB record defaults
174 * into the Media DB record just before creating a media (Volume)
177 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
179 mr->PoolId = pr->PoolId;
180 bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));
181 mr->Recycle = pr->Recycle;
182 mr->VolRetention = pr->VolRetention;
183 mr->VolUseDuration = pr->VolUseDuration;
184 mr->MaxVolJobs = pr->MaxVolJobs;
185 mr->MaxVolFiles = pr->MaxVolFiles;
186 mr->MaxVolBytes = pr->MaxVolBytes;
191 * Add Volumes to an existing Pool
193 static int add_cmd(UAContext *ua, const char *cmd)
197 int num, i, max, startnum;
199 char name[MAX_NAME_LENGTH];
201 int Slot = 0, InChanger = 0;
204 "You probably don't want to be using this command since it\n"
205 "creates database records without labeling the Volumes.\n"
206 "You probably want to use the \"label\" command.\n\n"));
212 memset(&pr, 0, sizeof(pr));
213 memset(&mr, 0, sizeof(mr));
215 if (!get_pool_dbr(ua, &pr)) {
219 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
220 pr.MaxVols, pr.PoolType);
222 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
223 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
225 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
228 pr.MaxVols = ua->pint32_val;
233 if ((store = get_storage_resource(ua, 0)) != NULL) {
234 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
235 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
239 if (pr.MaxVols == 0) {
242 max = pr.MaxVols - pr.NumVols;
246 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
247 if (!get_pint(ua, buf)) {
250 num = ua->pint32_val;
251 if (num < 0 || num > max) {
252 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
259 if (!get_cmd(ua, _("Enter Volume name: "))) {
263 if (!get_cmd(ua, _("Enter base volume name: "))) {
267 /* Don't allow | in Volume name because it is the volume separator character */
268 if (!is_volume_name_legal(ua, ua->cmd)) {
271 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
272 bsendmsg(ua, _("Volume name too long.\n"));
275 if (strlen(ua->cmd) == 0) {
276 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
280 bstrncpy(name, ua->cmd, sizeof(name));
282 strcat(name, "%04d");
285 if (!get_pint(ua, _("Enter the starting number: "))) {
288 startnum = ua->pint32_val;
290 bsendmsg(ua, _("Start number must be greater than zero.\n"));
300 if (store && store->autochanger) {
301 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
304 Slot = ua->pint32_val;
305 if (!get_yesno(ua, _("InChanger? yes/no: "))) {
308 InChanger = ua->pint32_val;
311 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
312 for (i=startnum; i < num+startnum; i++) {
313 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
315 mr.InChanger = InChanger;
316 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
317 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
318 bsendmsg(ua, "%s", db_strerror(ua->db));
322 first_id = mr.PoolId;
326 Dmsg0(200, "Update pool record.\n");
327 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
328 bsendmsg(ua, "%s", db_strerror(ua->db));
331 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
337 * Turn auto mount on/off
342 int automount_cmd(UAContext *ua, const char *cmd)
347 if (!get_cmd(ua, _("Turn on or off? "))) {
355 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
363 static int cancel_cmd(UAContext *ua, const char *cmd)
368 char JobName[MAX_NAME_LENGTH];
374 for (i=1; i<ua->argc; i++) {
375 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
380 JobId = str_to_int64(ua->argv[i]);
381 if (!(jcr=get_jcr_by_id(JobId))) {
382 bsendmsg(ua, _("JobId %d is not running.\n"), JobId);
386 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
390 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
391 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
397 /* If we still do not have a jcr,
398 * throw up a list and ask the user to select one.
401 /* Count Jobs running */
404 if (jcr->JobId == 0) { /* this is us */
405 free_locked_jcr(jcr);
408 free_locked_jcr(jcr);
414 bsendmsg(ua, _("No Jobs running.\n"));
417 start_prompt(ua, _("Select Job:\n"));
420 if (jcr->JobId == 0) { /* this is us */
421 free_locked_jcr(jcr);
424 add_prompt(ua, jcr->Job);
425 free_locked_jcr(jcr);
429 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
433 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
437 /* NOTE! This increments the ref_count */
438 jcr = get_jcr_by_full_name(JobName);
440 bsendmsg(ua, _("Job %s not found.\n"), JobName);
445 ret = cancel_job(ua, jcr);
452 * This is a common routine to create or update a
453 * Pool DB base record from a Pool Resource. We handle
454 * the setting of MaxVols and NumVols slightly differently
455 * depending on if we are creating the Pool or we are
456 * simply bringing it into agreement with the resource (updage).
458 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
460 strcpy(pr->PoolType, pool->pool_type);
461 if (op == POOL_OP_CREATE) {
462 pr->MaxVols = pool->max_volumes;
464 } else { /* update pool */
465 if (pr->MaxVols != pool->max_volumes) {
466 pr->MaxVols = pool->max_volumes;
468 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
469 pr->MaxVols = pr->NumVols;
472 pr->UseOnce = pool->use_volume_once;
473 pr->UseCatalog = pool->use_catalog;
474 pr->AcceptAnyVolume = pool->accept_any_volume;
475 pr->Recycle = pool->Recycle;
476 pr->VolRetention = pool->VolRetention;
477 pr->VolUseDuration = pool->VolUseDuration;
478 pr->MaxVolJobs = pool->MaxVolJobs;
479 pr->MaxVolFiles = pool->MaxVolFiles;
480 pr->MaxVolBytes = pool->MaxVolBytes;
481 pr->AutoPrune = pool->AutoPrune;
482 pr->Recycle = pool->Recycle;
483 if (pool->label_format) {
484 strcpy(pr->LabelFormat, pool->label_format);
486 strcpy(pr->LabelFormat, "*"); /* none */
492 * Create a pool record from a given Pool resource
493 * Also called from backup.c
494 * Returns: -1 on error
495 * 0 record already exists
499 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
503 memset(&pr, 0, sizeof(POOL_DBR));
505 strcpy(pr.Name, pool->hdr.name);
507 if (db_get_pool_record(jcr, db, &pr)) {
509 if (op == POOL_OP_UPDATE) { /* update request */
510 set_pooldbr_from_poolres(&pr, pool, op);
511 db_update_pool_record(jcr, db, &pr);
513 return 0; /* exists */
516 set_pooldbr_from_poolres(&pr, pool, op);
518 if (!db_create_pool_record(jcr, db, &pr)) {
519 return -1; /* error */
527 * Create a Pool Record in the database.
528 * It is always created from the Resource record.
530 static int create_cmd(UAContext *ua, const char *cmd)
538 pool = get_pool_resource(ua);
543 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
545 bsendmsg(ua, _("Error: Pool %s already exists.\n"
546 "Use update to change it.\n"), pool->hdr.name);
550 bsendmsg(ua, "%s", db_strerror(ua->db));
556 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
562 * Set a new address in a Client resource. We do this only
563 * if the Console name is the same as the Client name
564 * and the Console can access the client.
566 static int setip_cmd(UAContext *ua, const char *cmd)
570 if (!ua->cons || !acl_access_ok(ua, Client_ACL, ua->cons->hdr.name)) {
571 bsendmsg(ua, _("Illegal command from this console.\n"));
575 client = (CLIENT *)GetResWithName(R_CLIENT, ua->cons->hdr.name);
578 bsendmsg(ua, _("Client \"%s\" not found.\n"), ua->cons->hdr.name);
581 if (client->address) {
582 free(client->address);
584 /* MA Bug 6 remove ifdef */
585 sockaddr_to_ascii(&(ua->UA_sock->client_addr), buf, sizeof(buf));
586 client->address = bstrdup(buf);
587 bsendmsg(ua, _("Client \"%s\" address set to %s\n"),
588 client->hdr.name, client->address);
596 * Update a Pool Record in the database.
597 * It is always updated from the Resource record.
599 * update pool=<pool-name>
600 * updates pool from Pool resource
601 * update media pool=<pool-name> volume=<volume-name>
602 * changes pool info for volume
603 * update slots [scan=...]
604 * updates autochanger slots
606 static int update_cmd(UAContext *ua, const char *cmd)
608 static const char *kw[] = {
610 N_("volume"), /* 1 */
619 switch (find_arg_keyword(ua, kw)) {
634 start_prompt(ua, _("Update choice:\n"));
635 add_prompt(ua, _("Volume parameters"));
636 add_prompt(ua, _("Pool from resource"));
637 add_prompt(ua, _("Slots from autochanger"));
638 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
654 static void update_volstatus(UAContext *ua, const char *val, MEDIA_DBR *mr)
656 POOLMEM *query = get_pool_memory(PM_MESSAGE);
670 for (i=0; kw[i]; i++) {
671 if (strcasecmp(val, kw[i]) == 0) {
677 bsendmsg(ua, _("Invalid VolStatus specified: %s\n"), val);
679 bstrncpy(mr->VolStatus, kw[i], sizeof(mr->VolStatus));
680 Mmsg(query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
681 mr->VolStatus, mr->MediaId);
682 if (!db_sql_query(ua->db, query, NULL, NULL)) {
683 bsendmsg(ua, "%s", db_strerror(ua->db));
685 bsendmsg(ua, _("New Volume status is: %s\n"), mr->VolStatus);
688 free_pool_memory(query);
691 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
695 if (!duration_to_utime(val, &mr->VolRetention)) {
696 bsendmsg(ua, _("Invalid retention period specified: %s\n"), val);
699 query = get_pool_memory(PM_MESSAGE);
700 Mmsg(query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
701 edit_uint64(mr->VolRetention, ed1), mr->MediaId);
702 if (!db_sql_query(ua->db, query, NULL, NULL)) {
703 bsendmsg(ua, "%s", db_strerror(ua->db));
705 bsendmsg(ua, _("New retention period is: %s\n"),
706 edit_utime(mr->VolRetention, ed1, sizeof(ed1)));
708 free_pool_memory(query);
711 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
716 if (!duration_to_utime(val, &mr->VolUseDuration)) {
717 bsendmsg(ua, _("Invalid use duration specified: %s\n"), val);
720 query = get_pool_memory(PM_MESSAGE);
721 Mmsg(query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
722 edit_uint64(mr->VolUseDuration, ed1), mr->MediaId);
723 if (!db_sql_query(ua->db, query, NULL, NULL)) {
724 bsendmsg(ua, "%s", db_strerror(ua->db));
726 bsendmsg(ua, _("New use duration is: %s\n"),
727 edit_utime(mr->VolUseDuration, ed1, sizeof(ed1)));
729 free_pool_memory(query);
732 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
734 POOLMEM *query = get_pool_memory(PM_MESSAGE);
735 Mmsg(query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%u",
737 if (!db_sql_query(ua->db, query, NULL, NULL)) {
738 bsendmsg(ua, "%s", db_strerror(ua->db));
740 bsendmsg(ua, _("New max jobs is: %s\n"), val);
742 free_pool_memory(query);
745 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
747 POOLMEM *query = get_pool_memory(PM_MESSAGE);
748 Mmsg(query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%u",
750 if (!db_sql_query(ua->db, query, NULL, NULL)) {
751 bsendmsg(ua, "%s", db_strerror(ua->db));
753 bsendmsg(ua, _("New max files is: %s\n"), val);
755 free_pool_memory(query);
758 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
764 if (!size_to_uint64(val, strlen(val), &maxbytes)) {
765 bsendmsg(ua, _("Invalid max. bytes specification: %s\n"), val);
768 query = get_pool_memory(PM_MESSAGE);
769 Mmsg(query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
770 edit_uint64(maxbytes, ed1), mr->MediaId);
771 if (!db_sql_query(ua->db, query, NULL, NULL)) {
772 bsendmsg(ua, "%s", db_strerror(ua->db));
774 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
776 free_pool_memory(query);
779 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
783 if (strcasecmp(val, _("yes")) == 0) {
785 } else if (strcasecmp(val, _("no")) == 0) {
788 bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
791 query = get_pool_memory(PM_MESSAGE);
792 Mmsg(query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
793 recycle, mr->MediaId);
794 if (!db_sql_query(ua->db, query, NULL, NULL)) {
795 bsendmsg(ua, "%s", db_strerror(ua->db));
797 bsendmsg(ua, _("New Recycle flag is: %s\n"),
798 mr->Recycle==1?_("yes"):_("no"));
800 free_pool_memory(query);
803 /* Modify the Pool in which this Volume is located */
804 static void update_vol_pool(UAContext *ua, char *val, MEDIA_DBR *mr, POOL_DBR *opr)
809 memset(&pr, 0, sizeof(pr));
810 bstrncpy(pr.Name, val, sizeof(pr.Name));
811 if (!get_pool_dbr(ua, &pr)) {
814 mr->PoolId = pr.PoolId; /* set new PoolId */
817 query = get_pool_memory(PM_MESSAGE);
819 Mmsg(query, "UPDATE Media SET PoolId=%d WHERE MediaId=%u",
820 mr->PoolId, mr->MediaId);
821 if (!db_sql_query(ua->db, query, NULL, NULL)) {
822 bsendmsg(ua, "%s", db_strerror(ua->db));
824 bsendmsg(ua, _("New Pool is: %s\n"), pr.Name);
826 if (!db_update_pool_record(ua->jcr, ua->db, opr)) {
827 bsendmsg(ua, "%s", db_strerror(ua->db));
830 if (!db_update_pool_record(ua->jcr, ua->db, &pr)) {
831 bsendmsg(ua, "%s", db_strerror(ua->db));
833 db_make_inchanger_unique(ua->jcr, ua->db, mr);
836 free_pool_memory(query);
840 * Refresh the Volume information from the Pool record
842 static void update_volfrompool(UAContext *ua, MEDIA_DBR *mr)
846 memset(&pr, 0, sizeof(pr));
847 pr.PoolId = mr->PoolId;
848 if (!get_pool_dbr(ua, &pr)) {
851 set_pool_dbr_defaults_in_media_dbr(mr, &pr);
852 if (!db_update_media_defaults(ua->jcr, ua->db, mr)) {
853 bsendmsg(ua, _("Error updating Volume record: ERR=%s"), db_strerror(ua->db));
855 bsendmsg(ua, _("Volume defaults updated from Pool record.\n"));
860 * Refresh the Volume information from the Pool record
863 static void update_all_vols_from_pool(UAContext *ua)
868 memset(&pr, 0, sizeof(pr));
869 if (!get_pool_dbr(ua, &pr)) {
872 memset(&mr, 0, sizeof(mr));
873 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
874 mr.PoolId = pr.PoolId;
875 if (!db_update_media_defaults(ua->jcr, ua->db, &mr)) {
876 bsendmsg(ua, _("Error updating Volume records: ERR=%s"), db_strerror(ua->db));
878 bsendmsg(ua, _("All Volume defaults updated from Pool record.\n"));
884 * Update a media record -- allows you to change the
885 * Volume status. E.g. if you want Bacula to stop
886 * writing on the volume, set it to anything other
889 static int update_volume(UAContext *ua)
897 N_("VolStatus"), /* 0 */
898 N_("VolRetention"), /* 1 */
899 N_("VolUse"), /* 2 */
900 N_("MaxVolJobs"), /* 3 */
901 N_("MaxVolFiles"), /* 4 */
902 N_("MaxVolBytes"), /* 5 */
903 N_("Recycle"), /* 6 */
905 N_("FromPool"), /* 8 */
906 N_("AllFromPool"), /* 9 */
909 for (int i=0; kw[i]; i++) {
912 if ((j=find_arg_with_value(ua, kw[i])) > 0) {
913 if (i != 9 && !select_media_dbr(ua, &mr)) {
918 update_volstatus(ua, ua->argv[j], &mr);
921 update_volretention(ua, ua->argv[j], &mr);
924 update_voluseduration(ua, ua->argv[j], &mr);
927 update_volmaxjobs(ua, ua->argv[j], &mr);
930 update_volmaxfiles(ua, ua->argv[j], &mr);
933 update_volmaxbytes(ua, ua->argv[j], &mr);
936 update_volrecycle(ua, ua->argv[j], &mr);
939 memset(&pr, 0, sizeof(POOL_DBR));
940 pr.PoolId = mr.PoolId;
941 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
942 bsendmsg(ua, "%s", db_strerror(ua->db));
945 update_vol_pool(ua, ua->argv[j], &mr, &pr);
948 update_volfrompool(ua, &mr);
951 update_all_vols_from_pool(ua);
958 if (!select_media_dbr(ua, &mr)) {
961 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
962 start_prompt(ua, _("Parameters to modify:\n"));
963 add_prompt(ua, _("Volume Status"));
964 add_prompt(ua, _("Volume Retention Period"));
965 add_prompt(ua, _("Volume Use Duration"));
966 add_prompt(ua, _("Maximum Volume Jobs"));
967 add_prompt(ua, _("Maximum Volume Files"));
968 add_prompt(ua, _("Maximum Volume Bytes"));
969 add_prompt(ua, _("Recycle Flag"));
970 add_prompt(ua, _("Slot"));
971 add_prompt(ua, _("InChanger Flag"));
972 add_prompt(ua, _("Volume Files"));
973 add_prompt(ua, _("Pool"));
974 add_prompt(ua, _("Volume from Pool"));
975 add_prompt(ua, _("All Volumes from Pool"));
976 add_prompt(ua, _("Done"));
977 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
978 case 0: /* Volume Status */
979 /* Modify Volume Status */
980 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
981 start_prompt(ua, _("Possible Values are:\n"));
982 add_prompt(ua, "Append"); /* Better not translate these as */
983 add_prompt(ua, "Archive"); /* They are known in the database code */
984 add_prompt(ua, "Disabled");
985 add_prompt(ua, "Full");
986 add_prompt(ua, "Used");
987 add_prompt(ua, "Cleaning");
988 if (strcmp(mr.VolStatus, "Purged") == 0) {
989 add_prompt(ua, "Recycle");
991 add_prompt(ua, "Read-Only");
992 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
995 update_volstatus(ua, ua->cmd, &mr);
997 case 1: /* Retention */
998 bsendmsg(ua, _("Current retention period is: %s\n"),
999 edit_utime(mr.VolRetention, ed1, sizeof(ed1)));
1000 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
1003 update_volretention(ua, ua->cmd, &mr);
1006 case 2: /* Use Duration */
1007 bsendmsg(ua, _("Current use duration is: %s\n"),
1008 edit_utime(mr.VolUseDuration, ed1, sizeof(ed1)));
1009 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
1012 update_voluseduration(ua, ua->cmd, &mr);
1015 case 3: /* Max Jobs */
1016 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
1017 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
1020 update_volmaxjobs(ua, ua->cmd, &mr);
1023 case 4: /* Max Files */
1024 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
1025 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
1028 update_volmaxfiles(ua, ua->cmd, &mr);
1031 case 5: /* Max Bytes */
1032 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
1033 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
1036 update_volmaxbytes(ua, ua->cmd, &mr);
1040 case 6: /* Recycle */
1041 bsendmsg(ua, _("Current recycle flag is: %s\n"),
1042 mr.Recycle==1?_("yes"):_("no"));
1043 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
1046 update_volrecycle(ua, ua->cmd, &mr);
1052 memset(&pr, 0, sizeof(POOL_DBR));
1053 pr.PoolId = mr.PoolId;
1054 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1055 bsendmsg(ua, "%s", db_strerror(ua->db));
1058 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
1059 if (!get_pint(ua, _("Enter new Slot: "))) {
1062 Slot = ua->pint32_val;
1063 if (pr.MaxVols > 0 && Slot > (int)pr.MaxVols) {
1064 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
1070 * Make sure to use db_update... rather than doing this directly,
1071 * so that any Slot is handled correctly.
1073 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1074 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1076 bsendmsg(ua, _("New Slot is: %d\n"), mr.Slot);
1080 case 8: /* InChanger */
1081 bsendmsg(ua, _("Current InChanger flag is: %d\n"), mr.InChanger);
1082 if (!get_yesno(ua, _("Set InChanger flag? yes/no: "))) {
1085 mr.InChanger = ua->pint32_val;
1087 * Make sure to use db_update... rather than doing this directly,
1088 * so that any Slot is handled correctly.
1090 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1091 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1093 bsendmsg(ua, _("New InChanger flag is: %d\n"), mr.InChanger);
1098 case 9: /* Volume Files */
1100 bsendmsg(ua, _("Warning changing Volume Files can result\n"
1101 "in loss of data on your Volume\n\n"));
1102 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
1103 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
1106 VolFiles = ua->pint32_val;
1107 if (VolFiles != (int)(mr.VolFiles + 1)) {
1108 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
1109 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
1113 query = get_pool_memory(PM_MESSAGE);
1114 Mmsg(query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
1115 VolFiles, mr.MediaId);
1116 if (!db_sql_query(ua->db, query, NULL, NULL)) {
1117 bsendmsg(ua, "%s", db_strerror(ua->db));
1119 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
1121 free_pool_memory(query);
1124 case 10: /* Volume's Pool */
1125 memset(&pr, 0, sizeof(POOL_DBR));
1126 pr.PoolId = mr.PoolId;
1127 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1128 bsendmsg(ua, "%s", db_strerror(ua->db));
1131 bsendmsg(ua, _("Current Pool is: %s\n"), pr.Name);
1132 if (!get_cmd(ua, _("Enter new Pool name: "))) {
1135 update_vol_pool(ua, ua->cmd, &mr, &pr);
1139 update_volfrompool(ua, &mr);
1142 update_all_vols_from_pool(ua);
1144 default: /* Done or error */
1145 bsendmsg(ua, "Selection done.\n");
1153 * Update pool record -- pull info from current POOL resource
1155 static int update_pool(UAContext *ua)
1162 pool = get_pool_resource(ua);
1167 memset(&pr, 0, sizeof(pr));
1168 strcpy(pr.Name, pool->hdr.name);
1169 if (!get_pool_dbr(ua, &pr)) {
1173 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
1175 id = db_update_pool_record(ua->jcr, ua->db, &pr);
1177 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
1178 id, db_strerror(ua->db));
1180 query = get_pool_memory(PM_MESSAGE);
1181 Mmsg(query, list_pool, pr.PoolId);
1182 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
1183 free_pool_memory(query);
1184 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
1189 static void do_storage_setdebug(UAContext *ua, STORE *store, int level, int trace_flag)
1193 ua->jcr->store = store;
1194 /* Try connecting for up to 15 seconds */
1195 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
1196 store->hdr.name, store->address, store->SDport);
1197 if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
1198 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1201 Dmsg0(120, _("Connected to storage daemon\n"));
1202 sd = ua->jcr->store_bsock;
1203 bnet_fsend(sd, "setdebug=%d trace=%d\n", level, trace_flag);
1204 if (bnet_recv(sd) >= 0) {
1205 bsendmsg(ua, "%s", sd->msg);
1207 bnet_sig(sd, BNET_TERMINATE);
1209 ua->jcr->store_bsock = NULL;
1213 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level, int trace_flag)
1217 /* Connect to File daemon */
1219 ua->jcr->client = client;
1220 /* Try to connect for 15 seconds */
1221 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1222 client->hdr.name, client->address, client->FDport);
1223 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1224 bsendmsg(ua, _("Failed to connect to Client.\n"));
1227 Dmsg0(120, "Connected to file daemon\n");
1228 fd = ua->jcr->file_bsock;
1229 bnet_fsend(fd, "setdebug=%d trace=%d\n", level, trace_flag);
1230 if (bnet_recv(fd) >= 0) {
1231 bsendmsg(ua, "%s", fd->msg);
1233 bnet_sig(fd, BNET_TERMINATE);
1235 ua->jcr->file_bsock = NULL;
1240 static void do_all_setdebug(UAContext *ua, int level, int trace_flag)
1242 STORE *store, **unique_store;
1243 CLIENT *client, **unique_client;
1247 debug_level = level;
1249 /* Count Storage items */
1252 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1254 unique_store = (STORE **) malloc(i * sizeof(STORE));
1255 /* Find Unique Storage address/port */
1256 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1258 unique_store[i++] = store;
1259 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1261 for (j=0; j<i; j++) {
1262 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1263 unique_store[j]->SDport == store->SDport) {
1269 unique_store[i++] = store;
1270 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1275 /* Call each unique Storage daemon */
1276 for (j=0; j<i; j++) {
1277 do_storage_setdebug(ua, unique_store[j], level, trace_flag);
1281 /* Count Client items */
1284 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1286 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1287 /* Find Unique Client address/port */
1288 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1290 unique_client[i++] = client;
1291 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1293 for (j=0; j<i; j++) {
1294 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1295 unique_client[j]->FDport == client->FDport) {
1301 unique_client[i++] = client;
1302 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1307 /* Call each unique File daemon */
1308 for (j=0; j<i; j++) {
1309 do_client_setdebug(ua, unique_client[j], level, trace_flag);
1311 free(unique_client);
1315 * setdebug level=nn all trace=1/0
1317 static int setdebug_cmd(UAContext *ua, const char *cmd)
1322 int trace_flag = -1;
1328 Dmsg1(120, "setdebug:%s:\n", cmd);
1331 i = find_arg_with_value(ua, _("level"));
1333 level = atoi(ua->argv[i]);
1336 if (!get_pint(ua, _("Enter new debug level: "))) {
1339 level = ua->pint32_val;
1342 /* Look for trace flag. -1 => not change */
1343 i = find_arg_with_value(ua, _("trace"));
1345 trace_flag = atoi(ua->argv[i]);
1346 if (trace_flag > 0) {
1351 /* General debug? */
1352 for (i=1; i<ua->argc; i++) {
1353 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1354 do_all_setdebug(ua, level, trace_flag);
1357 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1358 strcasecmp(ua->argk[i], _("director")) == 0) {
1359 debug_level = level;
1360 set_trace(trace_flag);
1363 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1364 strcasecmp(ua->argk[i], _("fd")) == 0) {
1367 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1369 do_client_setdebug(ua, client, level, trace_flag);
1373 client = select_client_resource(ua);
1375 do_client_setdebug(ua, client, level, trace_flag);
1380 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1381 strcasecmp(ua->argk[i], _("storage")) == 0 ||
1382 strcasecmp(ua->argk[i], _("sd")) == 0) {
1385 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1387 do_storage_setdebug(ua, store, level, trace_flag);
1391 store = get_storage_resource(ua, 0);
1393 do_storage_setdebug(ua, store, level, trace_flag);
1399 * We didn't find an appropriate keyword above, so
1402 start_prompt(ua, _("Available daemons are: \n"));
1403 add_prompt(ua, _("Director"));
1404 add_prompt(ua, _("Storage"));
1405 add_prompt(ua, _("Client"));
1406 add_prompt(ua, _("All"));
1407 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1408 case 0: /* Director */
1409 debug_level = level;
1410 set_trace(trace_flag);
1413 store = get_storage_resource(ua, 0);
1415 do_storage_setdebug(ua, store, level, trace_flag);
1419 client = select_client_resource(ua);
1421 do_client_setdebug(ua, client, level, trace_flag);
1425 do_all_setdebug(ua, level, trace_flag);
1434 * Turn debug tracing to file on/off
1436 static int trace_cmd(UAContext *ua, const char *cmd)
1440 if (ua->argc != 2) {
1441 if (!get_cmd(ua, _("Turn on or off? "))) {
1446 onoff = ua->argk[1];
1449 set_trace((strcasecmp(onoff, _("off")) == 0) ? false : true);
1454 static int var_cmd(UAContext *ua, const char *cmd)
1456 POOLMEM *val = get_pool_memory(PM_FNAME);
1462 for (var=ua->cmd; *var != ' '; ) { /* skip command */
1465 while (*var == ' ') { /* skip spaces */
1468 Dmsg1(100, "Var=%s:\n", var);
1469 variable_expansion(ua->jcr, var, &val);
1470 bsendmsg(ua, "%s\n", val);
1471 free_pool_memory(val);
1475 static int estimate_cmd(UAContext *ua, const char *cmd)
1478 CLIENT *client = NULL;
1479 FILESET *fileset = NULL;
1482 char since[MAXSTRING];
1485 jcr->JobLevel = L_FULL;
1486 for (int i=1; i<ua->argc; i++) {
1487 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1488 strcasecmp(ua->argk[i], _("fd")) == 0) {
1490 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1494 if (strcasecmp(ua->argk[i], _("job")) == 0) {
1496 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1500 if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1502 fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1506 if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1510 if (strcasecmp(ua->argk[i], _("level")) == 0) {
1511 if (!get_level_from_name(ua->jcr, ua->argv[i])) {
1512 bsendmsg(ua, _("Level %s not valid.\n"), ua->argv[i]);
1517 if (!job && !(client && fileset)) {
1518 if (!(job = select_job_resource(ua))) {
1523 job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1525 bsendmsg(ua, _("No job specified.\n"));
1530 client = job->client;
1533 fileset = job->fileset;
1535 jcr->client = client;
1536 jcr->fileset = fileset;
1538 ua->catalog = client->catalog;
1545 jcr->JobType = JT_BACKUP;
1546 init_jcr_job_record(jcr);
1548 if (!get_or_create_client_record(jcr)) {
1551 if (!get_or_create_fileset_record(jcr, &fsr)) {
1555 get_level_since_time(ua->jcr, since, sizeof(since));
1557 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1558 job->client->hdr.name, job->client->address, job->client->FDport);
1559 if (!connect_to_file_daemon(jcr, 1, 15, 0)) {
1560 bsendmsg(ua, _("Failed to connect to Client.\n"));
1564 if (!send_include_list(jcr)) {
1565 bsendmsg(ua, _("Error sending include list.\n"));
1569 if (!send_exclude_list(jcr)) {
1570 bsendmsg(ua, _("Error sending exclude list.\n"));
1574 if (!send_level_command(jcr)) {
1578 bnet_fsend(jcr->file_bsock, "estimate listing=%d\n", listing);
1579 while (bnet_recv(jcr->file_bsock) >= 0) {
1580 bsendmsg(ua, "%s", jcr->file_bsock->msg);
1584 if (jcr->file_bsock) {
1585 bnet_sig(jcr->file_bsock, BNET_TERMINATE);
1586 bnet_close(jcr->file_bsock);
1587 jcr->file_bsock = NULL;
1596 static int time_cmd(UAContext *ua, const char *cmd)
1599 time_t ttime = time(NULL);
1601 localtime_r(&ttime, &tm);
1602 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1603 bsendmsg(ua, "%s\n", sdt);
1608 * reload the conf file
1610 extern "C" void reload_config(int sig);
1612 static int reload_cmd(UAContext *ua, const char *cmd)
1619 * Delete Pool records (should purge Media with it).
1621 * delete pool=<pool-name>
1622 * delete volume pool=<pool-name> volume=<name>
1623 * delete job jobid=xxx
1625 static int delete_cmd(UAContext *ua, const char *cmd)
1627 static const char *keywords[] = {
1638 switch (find_arg_keyword(ua, keywords)) {
1648 while ((i=find_arg(ua, _("jobid"))) > 0) {
1650 *ua->argk[i] = 0; /* zap keyword already visited */
1658 "In general it is not a good idea to delete either a\n"
1659 "Pool or a Volume since they may contain data.\n\n"));
1661 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1672 bsendmsg(ua, _("Nothing done.\n"));
1680 * delete_job has been modified to parse JobID lists like the
1682 * delete job JobID=3,4,6,7-11,14
1684 * Thanks to Phil Stracchino for the above addition.
1687 static void delete_job(UAContext *ua)
1692 int i = find_arg_with_value(ua, _("jobid"));
1694 if (strchr(ua->argv[i], ',') != NULL || strchr(ua->argv[i], '-') != NULL) {
1695 s = bstrdup(ua->argv[i]);
1698 * We could use strtok() here. But we're not going to, because:
1699 * (a) strtok() is deprecated, having been replaced by strsep();
1700 * (b) strtok() is broken in significant ways.
1701 * we could use strsep() instead, but it's not universally available.
1702 * so we grow our own using strchr().
1704 sep = strchr(tok, ',');
1705 while (sep != NULL) {
1707 if (strchr(tok, '-')) {
1708 delete_job_id_range(ua, tok);
1710 JobId = str_to_int64(tok);
1711 do_job_delete(ua, JobId);
1714 sep = strchr(tok, ',');
1716 /* pick up the last token */
1717 if (strchr(tok, '-')) {
1718 delete_job_id_range(ua, tok);
1720 JobId = str_to_int64(tok);
1721 do_job_delete(ua, JobId);
1726 JobId = str_to_int64(ua->argv[i]);
1727 do_job_delete(ua, JobId);
1729 } else if (!get_pint(ua, _("Enter JobId to delete: "))) {
1732 JobId = ua->pint32_val;
1733 do_job_delete(ua, JobId);
1738 * we call delete_job_id_range to parse range tokens and iterate over ranges
1740 static void delete_job_id_range(UAContext *ua, char *tok)
1745 tok2 = strchr(tok, '-');
1748 j1 = str_to_int64(tok);
1749 j2 = str_to_int64(tok2);
1750 for (j=j1; j<=j2; j++) {
1751 do_job_delete(ua, j);
1756 * do_job_delete now performs the actual delete operation atomically
1757 * we always return 1 because C++ is pissy about void functions
1760 static void do_job_delete(UAContext *ua, JobId_t JobId)
1762 POOLMEM *query = get_pool_memory(PM_MESSAGE);
1764 Mmsg(query, "DELETE FROM Job WHERE JobId=%u", JobId);
1765 db_sql_query(ua->db, query, NULL, (void *)NULL);
1766 Mmsg(query, "DELETE FROM File WHERE JobId=%u", JobId);
1767 db_sql_query(ua->db, query, NULL, (void *)NULL);
1768 Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%u", JobId);
1769 db_sql_query(ua->db, query, NULL, (void *)NULL);
1770 free_pool_memory(query);
1771 bsendmsg(ua, _("Job %u and associated records deleted from the catalog.\n"), JobId);
1775 * Delete media records from database -- dangerous
1777 static int delete_volume(UAContext *ua)
1781 if (!select_media_dbr(ua, &mr)) {
1784 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1785 "and all Jobs saved on that volume from the Catalog\n"),
1788 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1791 if (ua->pint32_val) {
1792 db_delete_media_record(ua->jcr, ua->db, &mr);
1798 * Delete a pool record from the database -- dangerous
1800 static int delete_pool(UAContext *ua)
1804 memset(&pr, 0, sizeof(pr));
1806 if (!get_pool_dbr(ua, &pr)) {
1809 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1812 if (ua->pint32_val) {
1813 db_delete_pool_record(ua->jcr, ua->db, &pr);
1819 static void do_mount_cmd(UAContext *ua, const char *command)
1823 char dev_name[MAX_NAME_LENGTH];
1829 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1831 store = get_storage_resource(ua, 1);
1836 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1837 store->media_type, store->dev_name);
1839 ua->jcr->store = store;
1840 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1841 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1844 sd = ua->jcr->store_bsock;
1845 strcpy(dev_name, store->dev_name);
1846 bash_spaces(dev_name);
1847 bnet_fsend(sd, "%s %s", command, dev_name);
1848 while (bnet_recv(sd) >= 0) {
1849 bsendmsg(ua, "%s", sd->msg);
1851 bnet_sig(sd, BNET_TERMINATE);
1853 ua->jcr->store_bsock = NULL;
1857 * mount [storage | device] <name>
1859 static int mount_cmd(UAContext *ua, const char *cmd)
1861 do_mount_cmd(ua, "mount"); /* mount */
1867 * unmount [storage | device] <name>
1869 static int unmount_cmd(UAContext *ua, const char *cmd)
1871 do_mount_cmd(ua, "unmount"); /* unmount */
1877 * release [storage | device] <name>
1879 static int release_cmd(UAContext *ua, const char *cmd)
1881 do_mount_cmd(ua, "release"); /* release */
1888 * use catalog=<name>
1890 static int use_cmd(UAContext *ua, const char *cmd)
1892 CAT *oldcatalog, *catalog;
1895 close_db(ua); /* close any previously open db */
1896 oldcatalog = ua->catalog;
1898 if (!(catalog = get_catalog_resource(ua))) {
1899 ua->catalog = oldcatalog;
1901 ua->catalog = catalog;
1904 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1905 ua->catalog->hdr.name, ua->catalog->db_name);
1910 int quit_cmd(UAContext *ua, const char *cmd)
1917 * Wait until no job is running
1919 int wait_cmd(UAContext *ua, const char *cmd)
1922 bmicrosleep(0, 200000); /* let job actually start */
1923 for (bool running=true; running; ) {
1927 if (jcr->JobId != 0) {
1929 free_locked_jcr(jcr);
1932 free_locked_jcr(jcr);
1943 static int help_cmd(UAContext *ua, const char *cmd)
1947 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1948 for (i=0; i<comsize; i++) {
1949 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1951 bsendmsg(ua, _("\nWhen at a prompt, entering a period cancels the command.\n\n"));
1955 int qhelp_cmd(UAContext *ua, const char *cmd)
1959 for (i=0; i<comsize; i++) {
1960 bsendmsg(ua, _("%s %s\n"), _(commands[i].key), _(commands[i].help));
1965 static int version_cmd(UAContext *ua, const char *cmd)
1967 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1972 /* A bit brain damaged in that if the user has not done
1973 * a "use catalog xxx" command, we simply find the first
1974 * catalog resource and open it.
1976 int open_db(UAContext *ua)
1983 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1986 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1989 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1990 ua->catalog->hdr.name, ua->catalog->db_name);
1994 ua->jcr->catalog = ua->catalog;
1996 Dmsg0(150, "Open database\n");
1997 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1998 ua->catalog->db_password, ua->catalog->db_address,
1999 ua->catalog->db_port, ua->catalog->db_socket);
2000 if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
2001 bsendmsg(ua, _("Could not open database \"%s\".\n"),
2002 ua->catalog->db_name);
2004 bsendmsg(ua, "%s", db_strerror(ua->db));
2009 ua->jcr->db = ua->db;
2010 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
2014 void close_db(UAContext *ua)
2017 db_close_database(ua->jcr, ua->db);