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_("umount"), unmount_cmd, _("umount <storage-name> for old-time Unix guys")},
132 { N_("update"), update_cmd, _("update Volume or Pool")},
133 { N_("use"), use_cmd, _("use catalog xxx")},
134 { N_("var"), var_cmd, _("does variable expansion")},
135 { N_("version"), version_cmd, _("print Director version")},
136 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
138 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
141 * Execute a command from the UA
143 int do_a_command(UAContext *ua, const char *cmd)
151 Dmsg1(200, "Command: %s\n", ua->UA_sock->msg);
156 len = strlen(ua->argk[0]);
157 for (i=0; i<comsize; i++) { /* search for command */
158 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
159 if (!acl_access_ok(ua, Command_ACL, ua->argk[0], len)) {
162 stat = (*commands[i].func)(ua, cmd); /* go execute command */
168 bnet_fsend(ua->UA_sock, _("%s: is an illegal command.\n"), ua->argk[0]);
174 * This is a common routine used to stuff the Pool DB record defaults
175 * into the Media DB record just before creating a media (Volume)
178 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
180 mr->PoolId = pr->PoolId;
181 bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));
182 mr->Recycle = pr->Recycle;
183 mr->VolRetention = pr->VolRetention;
184 mr->VolUseDuration = pr->VolUseDuration;
185 mr->MaxVolJobs = pr->MaxVolJobs;
186 mr->MaxVolFiles = pr->MaxVolFiles;
187 mr->MaxVolBytes = pr->MaxVolBytes;
192 * Add Volumes to an existing Pool
194 static int add_cmd(UAContext *ua, const char *cmd)
198 int num, i, max, startnum;
200 char name[MAX_NAME_LENGTH];
202 int Slot = 0, InChanger = 0;
205 "You probably don't want to be using this command since it\n"
206 "creates database records without labeling the Volumes.\n"
207 "You probably want to use the \"label\" command.\n\n"));
213 memset(&pr, 0, sizeof(pr));
214 memset(&mr, 0, sizeof(mr));
216 if (!get_pool_dbr(ua, &pr)) {
220 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
221 pr.MaxVols, pr.PoolType);
223 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
224 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
226 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
229 pr.MaxVols = ua->pint32_val;
234 if ((store = get_storage_resource(ua, 0)) != NULL) {
235 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
236 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
240 if (pr.MaxVols == 0) {
243 max = pr.MaxVols - pr.NumVols;
247 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
248 if (!get_pint(ua, buf)) {
251 num = ua->pint32_val;
252 if (num < 0 || num > max) {
253 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
260 if (!get_cmd(ua, _("Enter Volume name: "))) {
264 if (!get_cmd(ua, _("Enter base volume name: "))) {
268 /* Don't allow | in Volume name because it is the volume separator character */
269 if (!is_volume_name_legal(ua, ua->cmd)) {
272 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
273 bsendmsg(ua, _("Volume name too long.\n"));
276 if (strlen(ua->cmd) == 0) {
277 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
281 bstrncpy(name, ua->cmd, sizeof(name));
283 strcat(name, "%04d");
286 if (!get_pint(ua, _("Enter the starting number: "))) {
289 startnum = ua->pint32_val;
291 bsendmsg(ua, _("Start number must be greater than zero.\n"));
301 if (store && store->autochanger) {
302 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
305 Slot = ua->pint32_val;
306 if (!get_yesno(ua, _("InChanger? yes/no: "))) {
309 InChanger = ua->pint32_val;
312 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
313 for (i=startnum; i < num+startnum; i++) {
314 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
316 mr.InChanger = InChanger;
317 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
318 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
319 bsendmsg(ua, "%s", db_strerror(ua->db));
323 first_id = mr.PoolId;
327 Dmsg0(200, "Update pool record.\n");
328 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
329 bsendmsg(ua, "%s", db_strerror(ua->db));
332 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
338 * Turn auto mount on/off
343 int automount_cmd(UAContext *ua, const char *cmd)
348 if (!get_cmd(ua, _("Turn on or off? "))) {
356 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
364 static int cancel_cmd(UAContext *ua, const char *cmd)
369 char JobName[MAX_NAME_LENGTH];
375 for (i=1; i<ua->argc; i++) {
376 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
381 JobId = str_to_int64(ua->argv[i]);
382 if (!(jcr=get_jcr_by_id(JobId))) {
383 bsendmsg(ua, _("JobId %d is not running.\n"), JobId);
387 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
391 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
392 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
398 /* If we still do not have a jcr,
399 * throw up a list and ask the user to select one.
402 /* Count Jobs running */
405 if (jcr->JobId == 0) { /* this is us */
406 free_locked_jcr(jcr);
409 free_locked_jcr(jcr);
415 bsendmsg(ua, _("No Jobs running.\n"));
418 start_prompt(ua, _("Select Job:\n"));
421 if (jcr->JobId == 0) { /* this is us */
422 free_locked_jcr(jcr);
425 add_prompt(ua, jcr->Job);
426 free_locked_jcr(jcr);
430 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
434 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
438 /* NOTE! This increments the ref_count */
439 jcr = get_jcr_by_full_name(JobName);
441 bsendmsg(ua, _("Job %s not found.\n"), JobName);
446 ret = cancel_job(ua, jcr);
453 * This is a common routine to create or update a
454 * Pool DB base record from a Pool Resource. We handle
455 * the setting of MaxVols and NumVols slightly differently
456 * depending on if we are creating the Pool or we are
457 * simply bringing it into agreement with the resource (updage).
459 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
461 strcpy(pr->PoolType, pool->pool_type);
462 if (op == POOL_OP_CREATE) {
463 pr->MaxVols = pool->max_volumes;
465 } else { /* update pool */
466 if (pr->MaxVols != pool->max_volumes) {
467 pr->MaxVols = pool->max_volumes;
469 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
470 pr->MaxVols = pr->NumVols;
473 pr->UseOnce = pool->use_volume_once;
474 pr->UseCatalog = pool->use_catalog;
475 pr->AcceptAnyVolume = pool->accept_any_volume;
476 pr->Recycle = pool->Recycle;
477 pr->VolRetention = pool->VolRetention;
478 pr->VolUseDuration = pool->VolUseDuration;
479 pr->MaxVolJobs = pool->MaxVolJobs;
480 pr->MaxVolFiles = pool->MaxVolFiles;
481 pr->MaxVolBytes = pool->MaxVolBytes;
482 pr->AutoPrune = pool->AutoPrune;
483 pr->Recycle = pool->Recycle;
484 if (pool->label_format) {
485 strcpy(pr->LabelFormat, pool->label_format);
487 strcpy(pr->LabelFormat, "*"); /* none */
493 * Create a pool record from a given Pool resource
494 * Also called from backup.c
495 * Returns: -1 on error
496 * 0 record already exists
500 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
504 memset(&pr, 0, sizeof(POOL_DBR));
506 strcpy(pr.Name, pool->hdr.name);
508 if (db_get_pool_record(jcr, db, &pr)) {
510 if (op == POOL_OP_UPDATE) { /* update request */
511 set_pooldbr_from_poolres(&pr, pool, op);
512 db_update_pool_record(jcr, db, &pr);
514 return 0; /* exists */
517 set_pooldbr_from_poolres(&pr, pool, op);
519 if (!db_create_pool_record(jcr, db, &pr)) {
520 return -1; /* error */
528 * Create a Pool Record in the database.
529 * It is always created from the Resource record.
531 static int create_cmd(UAContext *ua, const char *cmd)
539 pool = get_pool_resource(ua);
544 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
546 bsendmsg(ua, _("Error: Pool %s already exists.\n"
547 "Use update to change it.\n"), pool->hdr.name);
551 bsendmsg(ua, "%s", db_strerror(ua->db));
557 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
563 * Set a new address in a Client resource. We do this only
564 * if the Console name is the same as the Client name
565 * and the Console can access the client.
567 static int setip_cmd(UAContext *ua, const char *cmd)
571 if (!ua->cons || !acl_access_ok(ua, Client_ACL, ua->cons->hdr.name)) {
572 bsendmsg(ua, _("Illegal command from this console.\n"));
576 client = (CLIENT *)GetResWithName(R_CLIENT, ua->cons->hdr.name);
579 bsendmsg(ua, _("Client \"%s\" not found.\n"), ua->cons->hdr.name);
582 if (client->address) {
583 free(client->address);
585 /* MA Bug 6 remove ifdef */
586 sockaddr_to_ascii(&(ua->UA_sock->client_addr), buf, sizeof(buf));
587 client->address = bstrdup(buf);
588 bsendmsg(ua, _("Client \"%s\" address set to %s\n"),
589 client->hdr.name, client->address);
597 * Update a Pool Record in the database.
598 * It is always updated from the Resource record.
600 * update pool=<pool-name>
601 * updates pool from Pool resource
602 * update media pool=<pool-name> volume=<volume-name>
603 * changes pool info for volume
604 * update slots [scan=...]
605 * updates autochanger slots
607 static int update_cmd(UAContext *ua, const char *cmd)
609 static const char *kw[] = {
611 N_("volume"), /* 1 */
620 switch (find_arg_keyword(ua, kw)) {
635 start_prompt(ua, _("Update choice:\n"));
636 add_prompt(ua, _("Volume parameters"));
637 add_prompt(ua, _("Pool from resource"));
638 add_prompt(ua, _("Slots from autochanger"));
639 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
655 static void update_volstatus(UAContext *ua, const char *val, MEDIA_DBR *mr)
657 POOLMEM *query = get_pool_memory(PM_MESSAGE);
671 for (i=0; kw[i]; i++) {
672 if (strcasecmp(val, kw[i]) == 0) {
678 bsendmsg(ua, _("Invalid VolStatus specified: %s\n"), val);
680 bstrncpy(mr->VolStatus, kw[i], sizeof(mr->VolStatus));
681 Mmsg(query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
682 mr->VolStatus, mr->MediaId);
683 if (!db_sql_query(ua->db, query, NULL, NULL)) {
684 bsendmsg(ua, "%s", db_strerror(ua->db));
686 bsendmsg(ua, _("New Volume status is: %s\n"), mr->VolStatus);
689 free_pool_memory(query);
692 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
696 if (!duration_to_utime(val, &mr->VolRetention)) {
697 bsendmsg(ua, _("Invalid retention period specified: %s\n"), val);
700 query = get_pool_memory(PM_MESSAGE);
701 Mmsg(query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
702 edit_uint64(mr->VolRetention, ed1), mr->MediaId);
703 if (!db_sql_query(ua->db, query, NULL, NULL)) {
704 bsendmsg(ua, "%s", db_strerror(ua->db));
706 bsendmsg(ua, _("New retention period is: %s\n"),
707 edit_utime(mr->VolRetention, ed1, sizeof(ed1)));
709 free_pool_memory(query);
712 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
717 if (!duration_to_utime(val, &mr->VolUseDuration)) {
718 bsendmsg(ua, _("Invalid use duration specified: %s\n"), val);
721 query = get_pool_memory(PM_MESSAGE);
722 Mmsg(query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
723 edit_uint64(mr->VolUseDuration, ed1), mr->MediaId);
724 if (!db_sql_query(ua->db, query, NULL, NULL)) {
725 bsendmsg(ua, "%s", db_strerror(ua->db));
727 bsendmsg(ua, _("New use duration is: %s\n"),
728 edit_utime(mr->VolUseDuration, ed1, sizeof(ed1)));
730 free_pool_memory(query);
733 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
735 POOLMEM *query = get_pool_memory(PM_MESSAGE);
736 Mmsg(query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%u",
738 if (!db_sql_query(ua->db, query, NULL, NULL)) {
739 bsendmsg(ua, "%s", db_strerror(ua->db));
741 bsendmsg(ua, _("New max jobs is: %s\n"), val);
743 free_pool_memory(query);
746 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
748 POOLMEM *query = get_pool_memory(PM_MESSAGE);
749 Mmsg(query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%u",
751 if (!db_sql_query(ua->db, query, NULL, NULL)) {
752 bsendmsg(ua, "%s", db_strerror(ua->db));
754 bsendmsg(ua, _("New max files is: %s\n"), val);
756 free_pool_memory(query);
759 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
765 if (!size_to_uint64(val, strlen(val), &maxbytes)) {
766 bsendmsg(ua, _("Invalid max. bytes specification: %s\n"), val);
769 query = get_pool_memory(PM_MESSAGE);
770 Mmsg(query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
771 edit_uint64(maxbytes, ed1), mr->MediaId);
772 if (!db_sql_query(ua->db, query, NULL, NULL)) {
773 bsendmsg(ua, "%s", db_strerror(ua->db));
775 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
777 free_pool_memory(query);
780 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
784 if (strcasecmp(val, _("yes")) == 0) {
786 } else if (strcasecmp(val, _("no")) == 0) {
789 bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
792 query = get_pool_memory(PM_MESSAGE);
793 Mmsg(query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
794 recycle, mr->MediaId);
795 if (!db_sql_query(ua->db, query, NULL, NULL)) {
796 bsendmsg(ua, "%s", db_strerror(ua->db));
798 bsendmsg(ua, _("New Recycle flag is: %s\n"),
799 mr->Recycle==1?_("yes"):_("no"));
801 free_pool_memory(query);
804 /* Modify the Pool in which this Volume is located */
805 static void update_vol_pool(UAContext *ua, char *val, MEDIA_DBR *mr, POOL_DBR *opr)
810 memset(&pr, 0, sizeof(pr));
811 bstrncpy(pr.Name, val, sizeof(pr.Name));
812 if (!get_pool_dbr(ua, &pr)) {
815 mr->PoolId = pr.PoolId; /* set new PoolId */
818 query = get_pool_memory(PM_MESSAGE);
820 Mmsg(query, "UPDATE Media SET PoolId=%d WHERE MediaId=%u",
821 mr->PoolId, mr->MediaId);
822 if (!db_sql_query(ua->db, query, NULL, NULL)) {
823 bsendmsg(ua, "%s", db_strerror(ua->db));
825 bsendmsg(ua, _("New Pool is: %s\n"), pr.Name);
827 if (!db_update_pool_record(ua->jcr, ua->db, opr)) {
828 bsendmsg(ua, "%s", db_strerror(ua->db));
831 if (!db_update_pool_record(ua->jcr, ua->db, &pr)) {
832 bsendmsg(ua, "%s", db_strerror(ua->db));
834 db_make_inchanger_unique(ua->jcr, ua->db, mr);
837 free_pool_memory(query);
841 * Refresh the Volume information from the Pool record
843 static void update_volfrompool(UAContext *ua, MEDIA_DBR *mr)
847 memset(&pr, 0, sizeof(pr));
848 pr.PoolId = mr->PoolId;
849 if (!db_get_pool_record(ua->jcr, ua->db, &pr) ||
850 !acl_access_ok(ua, Pool_ACL, pr.Name)) {
853 set_pool_dbr_defaults_in_media_dbr(mr, &pr);
854 if (!db_update_media_defaults(ua->jcr, ua->db, mr)) {
855 bsendmsg(ua, _("Error updating Volume record: ERR=%s"), db_strerror(ua->db));
857 bsendmsg(ua, _("Volume defaults updated from \"%s\" Pool record.\n"),
863 * Refresh the Volume information from the Pool record
866 static void update_all_vols_from_pool(UAContext *ua)
871 memset(&pr, 0, sizeof(pr));
872 memset(&mr, 0, sizeof(mr));
873 if (!get_pool_dbr(ua, &pr)) {
876 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
877 mr.PoolId = pr.PoolId;
878 if (!db_update_media_defaults(ua->jcr, ua->db, &mr)) {
879 bsendmsg(ua, _("Error updating Volume records: ERR=%s"), db_strerror(ua->db));
881 bsendmsg(ua, _("All Volume defaults updated from Pool record.\n"));
887 * Update a media record -- allows you to change the
888 * Volume status. E.g. if you want Bacula to stop
889 * writing on the volume, set it to anything other
892 static int update_volume(UAContext *ua)
900 N_("VolStatus"), /* 0 */
901 N_("VolRetention"), /* 1 */
902 N_("VolUse"), /* 2 */
903 N_("MaxVolJobs"), /* 3 */
904 N_("MaxVolFiles"), /* 4 */
905 N_("MaxVolBytes"), /* 5 */
906 N_("Recycle"), /* 6 */
908 N_("FromPool"), /* 8 */
909 N_("AllFromPool"), /* 9 */
912 for (int i=0; kw[i]; i++) {
915 if ((j=find_arg_with_value(ua, kw[i])) > 0) {
916 if (i != 9 && !select_media_dbr(ua, &mr)) {
921 update_volstatus(ua, ua->argv[j], &mr);
924 update_volretention(ua, ua->argv[j], &mr);
927 update_voluseduration(ua, ua->argv[j], &mr);
930 update_volmaxjobs(ua, ua->argv[j], &mr);
933 update_volmaxfiles(ua, ua->argv[j], &mr);
936 update_volmaxbytes(ua, ua->argv[j], &mr);
939 update_volrecycle(ua, ua->argv[j], &mr);
942 memset(&pr, 0, sizeof(POOL_DBR));
943 pr.PoolId = mr.PoolId;
944 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
945 bsendmsg(ua, "%s", db_strerror(ua->db));
948 update_vol_pool(ua, ua->argv[j], &mr, &pr);
951 update_volfrompool(ua, &mr);
954 update_all_vols_from_pool(ua);
962 if (!select_media_dbr(ua, &mr)) {
965 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
966 start_prompt(ua, _("Parameters to modify:\n"));
967 add_prompt(ua, _("Volume Status"));
968 add_prompt(ua, _("Volume Retention Period"));
969 add_prompt(ua, _("Volume Use Duration"));
970 add_prompt(ua, _("Maximum Volume Jobs"));
971 add_prompt(ua, _("Maximum Volume Files"));
972 add_prompt(ua, _("Maximum Volume Bytes"));
973 add_prompt(ua, _("Recycle Flag"));
974 add_prompt(ua, _("Slot"));
975 add_prompt(ua, _("InChanger Flag"));
976 add_prompt(ua, _("Volume Files"));
977 add_prompt(ua, _("Pool"));
978 add_prompt(ua, _("Volume from Pool"));
979 add_prompt(ua, _("All Volumes from Pool"));
980 add_prompt(ua, _("Done"));
981 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
982 case 0: /* Volume Status */
983 /* Modify Volume Status */
984 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
985 start_prompt(ua, _("Possible Values are:\n"));
986 add_prompt(ua, "Append"); /* Better not translate these as */
987 add_prompt(ua, "Archive"); /* They are known in the database code */
988 add_prompt(ua, "Disabled");
989 add_prompt(ua, "Full");
990 add_prompt(ua, "Used");
991 add_prompt(ua, "Cleaning");
992 if (strcmp(mr.VolStatus, "Purged") == 0) {
993 add_prompt(ua, "Recycle");
995 add_prompt(ua, "Read-Only");
996 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
999 update_volstatus(ua, ua->cmd, &mr);
1001 case 1: /* Retention */
1002 bsendmsg(ua, _("Current retention period is: %s\n"),
1003 edit_utime(mr.VolRetention, ed1, sizeof(ed1)));
1004 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
1007 update_volretention(ua, ua->cmd, &mr);
1010 case 2: /* Use Duration */
1011 bsendmsg(ua, _("Current use duration is: %s\n"),
1012 edit_utime(mr.VolUseDuration, ed1, sizeof(ed1)));
1013 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
1016 update_voluseduration(ua, ua->cmd, &mr);
1019 case 3: /* Max Jobs */
1020 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
1021 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
1024 update_volmaxjobs(ua, ua->cmd, &mr);
1027 case 4: /* Max Files */
1028 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
1029 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
1032 update_volmaxfiles(ua, ua->cmd, &mr);
1035 case 5: /* Max Bytes */
1036 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
1037 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
1040 update_volmaxbytes(ua, ua->cmd, &mr);
1044 case 6: /* Recycle */
1045 bsendmsg(ua, _("Current recycle flag is: %s\n"),
1046 mr.Recycle==1?_("yes"):_("no"));
1047 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
1050 update_volrecycle(ua, ua->cmd, &mr);
1056 memset(&pr, 0, sizeof(POOL_DBR));
1057 pr.PoolId = mr.PoolId;
1058 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1059 bsendmsg(ua, "%s", db_strerror(ua->db));
1062 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
1063 if (!get_pint(ua, _("Enter new Slot: "))) {
1066 Slot = ua->pint32_val;
1067 if (pr.MaxVols > 0 && Slot > (int)pr.MaxVols) {
1068 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
1074 * Make sure to use db_update... rather than doing this directly,
1075 * so that any Slot is handled correctly.
1077 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1078 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1080 bsendmsg(ua, _("New Slot is: %d\n"), mr.Slot);
1084 case 8: /* InChanger */
1085 bsendmsg(ua, _("Current InChanger flag is: %d\n"), mr.InChanger);
1086 if (!get_yesno(ua, _("Set InChanger flag? yes/no: "))) {
1089 mr.InChanger = ua->pint32_val;
1091 * Make sure to use db_update... rather than doing this directly,
1092 * so that any Slot is handled correctly.
1094 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1095 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1097 bsendmsg(ua, _("New InChanger flag is: %d\n"), mr.InChanger);
1102 case 9: /* Volume Files */
1104 bsendmsg(ua, _("Warning changing Volume Files can result\n"
1105 "in loss of data on your Volume\n\n"));
1106 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
1107 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
1110 VolFiles = ua->pint32_val;
1111 if (VolFiles != (int)(mr.VolFiles + 1)) {
1112 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
1113 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
1117 query = get_pool_memory(PM_MESSAGE);
1118 Mmsg(query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
1119 VolFiles, mr.MediaId);
1120 if (!db_sql_query(ua->db, query, NULL, NULL)) {
1121 bsendmsg(ua, "%s", db_strerror(ua->db));
1123 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
1125 free_pool_memory(query);
1128 case 10: /* Volume's Pool */
1129 memset(&pr, 0, sizeof(POOL_DBR));
1130 pr.PoolId = mr.PoolId;
1131 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1132 bsendmsg(ua, "%s", db_strerror(ua->db));
1135 bsendmsg(ua, _("Current Pool is: %s\n"), pr.Name);
1136 if (!get_cmd(ua, _("Enter new Pool name: "))) {
1139 update_vol_pool(ua, ua->cmd, &mr, &pr);
1143 update_volfrompool(ua, &mr);
1146 update_all_vols_from_pool(ua);
1148 default: /* Done or error */
1149 bsendmsg(ua, "Selection done.\n");
1157 * Update pool record -- pull info from current POOL resource
1159 static int update_pool(UAContext *ua)
1166 pool = get_pool_resource(ua);
1171 memset(&pr, 0, sizeof(pr));
1172 strcpy(pr.Name, pool->hdr.name);
1173 if (!get_pool_dbr(ua, &pr)) {
1177 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
1179 id = db_update_pool_record(ua->jcr, ua->db, &pr);
1181 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
1182 id, db_strerror(ua->db));
1184 query = get_pool_memory(PM_MESSAGE);
1185 Mmsg(query, list_pool, pr.PoolId);
1186 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
1187 free_pool_memory(query);
1188 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
1193 static void do_storage_setdebug(UAContext *ua, STORE *store, int level, int trace_flag)
1199 /* Try connecting for up to 15 seconds */
1200 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
1201 store->hdr.name, store->address, store->SDport);
1202 if (!connect_to_storage_daemon(jcr, 1, 15, 0)) {
1203 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1206 Dmsg0(120, _("Connected to storage daemon\n"));
1207 sd = jcr->store_bsock;
1208 bnet_fsend(sd, "setdebug=%d trace=%d\n", level, trace_flag);
1209 if (bnet_recv(sd) >= 0) {
1210 bsendmsg(ua, "%s", sd->msg);
1212 bnet_sig(sd, BNET_TERMINATE);
1214 jcr->store_bsock = NULL;
1218 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level, int trace_flag)
1222 /* Connect to File daemon */
1224 ua->jcr->client = client;
1225 /* Try to connect for 15 seconds */
1226 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1227 client->hdr.name, client->address, client->FDport);
1228 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1229 bsendmsg(ua, _("Failed to connect to Client.\n"));
1232 Dmsg0(120, "Connected to file daemon\n");
1233 fd = ua->jcr->file_bsock;
1234 bnet_fsend(fd, "setdebug=%d trace=%d\n", level, trace_flag);
1235 if (bnet_recv(fd) >= 0) {
1236 bsendmsg(ua, "%s", fd->msg);
1238 bnet_sig(fd, BNET_TERMINATE);
1240 ua->jcr->file_bsock = NULL;
1245 static void do_all_setdebug(UAContext *ua, int level, int trace_flag)
1247 STORE *store, **unique_store;
1248 CLIENT *client, **unique_client;
1252 debug_level = level;
1254 /* Count Storage items */
1257 for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1259 unique_store = (STORE **) malloc(i * sizeof(STORE));
1260 /* Find Unique Storage address/port */
1261 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1263 unique_store[i++] = store;
1264 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1266 for (j=0; j<i; j++) {
1267 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1268 unique_store[j]->SDport == store->SDport) {
1274 unique_store[i++] = store;
1275 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1280 /* Call each unique Storage daemon */
1281 for (j=0; j<i; j++) {
1282 do_storage_setdebug(ua, unique_store[j], level, trace_flag);
1286 /* Count Client items */
1289 for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1291 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1292 /* Find Unique Client address/port */
1293 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1295 unique_client[i++] = client;
1296 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1298 for (j=0; j<i; j++) {
1299 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1300 unique_client[j]->FDport == client->FDport) {
1306 unique_client[i++] = client;
1307 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1312 /* Call each unique File daemon */
1313 for (j=0; j<i; j++) {
1314 do_client_setdebug(ua, unique_client[j], level, trace_flag);
1316 free(unique_client);
1320 * setdebug level=nn all trace=1/0
1322 static int setdebug_cmd(UAContext *ua, const char *cmd)
1327 int trace_flag = -1;
1333 Dmsg1(120, "setdebug:%s:\n", cmd);
1336 i = find_arg_with_value(ua, _("level"));
1338 level = atoi(ua->argv[i]);
1341 if (!get_pint(ua, _("Enter new debug level: "))) {
1344 level = ua->pint32_val;
1347 /* Look for trace flag. -1 => not change */
1348 i = find_arg_with_value(ua, _("trace"));
1350 trace_flag = atoi(ua->argv[i]);
1351 if (trace_flag > 0) {
1356 /* General debug? */
1357 for (i=1; i<ua->argc; i++) {
1358 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1359 do_all_setdebug(ua, level, trace_flag);
1362 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1363 strcasecmp(ua->argk[i], _("director")) == 0) {
1364 debug_level = level;
1365 set_trace(trace_flag);
1368 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1369 strcasecmp(ua->argk[i], _("fd")) == 0) {
1372 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1374 do_client_setdebug(ua, client, level, trace_flag);
1378 client = select_client_resource(ua);
1380 do_client_setdebug(ua, client, level, trace_flag);
1385 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1386 strcasecmp(ua->argk[i], _("storage")) == 0 ||
1387 strcasecmp(ua->argk[i], _("sd")) == 0) {
1390 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1392 do_storage_setdebug(ua, store, level, trace_flag);
1396 store = get_storage_resource(ua, 0);
1398 do_storage_setdebug(ua, store, level, trace_flag);
1404 * We didn't find an appropriate keyword above, so
1407 start_prompt(ua, _("Available daemons are: \n"));
1408 add_prompt(ua, _("Director"));
1409 add_prompt(ua, _("Storage"));
1410 add_prompt(ua, _("Client"));
1411 add_prompt(ua, _("All"));
1412 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1413 case 0: /* Director */
1414 debug_level = level;
1415 set_trace(trace_flag);
1418 store = get_storage_resource(ua, 0);
1420 do_storage_setdebug(ua, store, level, trace_flag);
1424 client = select_client_resource(ua);
1426 do_client_setdebug(ua, client, level, trace_flag);
1430 do_all_setdebug(ua, level, trace_flag);
1439 * Turn debug tracing to file on/off
1441 static int trace_cmd(UAContext *ua, const char *cmd)
1445 if (ua->argc != 2) {
1446 if (!get_cmd(ua, _("Turn on or off? "))) {
1451 onoff = ua->argk[1];
1454 set_trace((strcasecmp(onoff, _("off")) == 0) ? false : true);
1459 static int var_cmd(UAContext *ua, const char *cmd)
1461 POOLMEM *val = get_pool_memory(PM_FNAME);
1467 for (var=ua->cmd; *var != ' '; ) { /* skip command */
1470 while (*var == ' ') { /* skip spaces */
1473 Dmsg1(100, "Var=%s:\n", var);
1474 variable_expansion(ua->jcr, var, &val);
1475 bsendmsg(ua, "%s\n", val);
1476 free_pool_memory(val);
1480 static int estimate_cmd(UAContext *ua, const char *cmd)
1483 CLIENT *client = NULL;
1484 FILESET *fileset = NULL;
1487 char since[MAXSTRING];
1490 jcr->JobLevel = L_FULL;
1491 for (int i=1; i<ua->argc; i++) {
1492 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1493 strcasecmp(ua->argk[i], _("fd")) == 0) {
1495 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1499 if (strcasecmp(ua->argk[i], _("job")) == 0) {
1501 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1505 if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1507 fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1511 if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1515 if (strcasecmp(ua->argk[i], _("level")) == 0) {
1516 if (!get_level_from_name(ua->jcr, ua->argv[i])) {
1517 bsendmsg(ua, _("Level %s not valid.\n"), ua->argv[i]);
1522 if (!job && !(client && fileset)) {
1523 if (!(job = select_job_resource(ua))) {
1528 job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1530 bsendmsg(ua, _("No job specified.\n"));
1535 client = job->client;
1538 fileset = job->fileset;
1540 jcr->client = client;
1541 jcr->fileset = fileset;
1543 ua->catalog = client->catalog;
1550 jcr->JobType = JT_BACKUP;
1551 init_jcr_job_record(jcr);
1553 if (!get_or_create_client_record(jcr)) {
1556 if (!get_or_create_fileset_record(jcr, &fsr)) {
1560 get_level_since_time(ua->jcr, since, sizeof(since));
1562 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1563 job->client->hdr.name, job->client->address, job->client->FDport);
1564 if (!connect_to_file_daemon(jcr, 1, 15, 0)) {
1565 bsendmsg(ua, _("Failed to connect to Client.\n"));
1569 if (!send_include_list(jcr)) {
1570 bsendmsg(ua, _("Error sending include list.\n"));
1574 if (!send_exclude_list(jcr)) {
1575 bsendmsg(ua, _("Error sending exclude list.\n"));
1579 if (!send_level_command(jcr)) {
1583 bnet_fsend(jcr->file_bsock, "estimate listing=%d\n", listing);
1584 while (bnet_recv(jcr->file_bsock) >= 0) {
1585 bsendmsg(ua, "%s", jcr->file_bsock->msg);
1589 if (jcr->file_bsock) {
1590 bnet_sig(jcr->file_bsock, BNET_TERMINATE);
1591 bnet_close(jcr->file_bsock);
1592 jcr->file_bsock = NULL;
1601 static int time_cmd(UAContext *ua, const char *cmd)
1604 time_t ttime = time(NULL);
1606 localtime_r(&ttime, &tm);
1607 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1608 bsendmsg(ua, "%s\n", sdt);
1613 * reload the conf file
1615 extern "C" void reload_config(int sig);
1617 static int reload_cmd(UAContext *ua, const char *cmd)
1624 * Delete Pool records (should purge Media with it).
1626 * delete pool=<pool-name>
1627 * delete volume pool=<pool-name> volume=<name>
1628 * delete job jobid=xxx
1630 static int delete_cmd(UAContext *ua, const char *cmd)
1632 static const char *keywords[] = {
1643 switch (find_arg_keyword(ua, keywords)) {
1653 while ((i=find_arg(ua, _("jobid"))) > 0) {
1655 *ua->argk[i] = 0; /* zap keyword already visited */
1663 "In general it is not a good idea to delete either a\n"
1664 "Pool or a Volume since they may contain data.\n\n"));
1666 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1677 bsendmsg(ua, _("Nothing done.\n"));
1685 * delete_job has been modified to parse JobID lists like the
1687 * delete job JobID=3,4,6,7-11,14
1689 * Thanks to Phil Stracchino for the above addition.
1692 static void delete_job(UAContext *ua)
1697 int i = find_arg_with_value(ua, _("jobid"));
1699 if (strchr(ua->argv[i], ',') != NULL || strchr(ua->argv[i], '-') != NULL) {
1700 s = bstrdup(ua->argv[i]);
1703 * We could use strtok() here. But we're not going to, because:
1704 * (a) strtok() is deprecated, having been replaced by strsep();
1705 * (b) strtok() is broken in significant ways.
1706 * we could use strsep() instead, but it's not universally available.
1707 * so we grow our own using strchr().
1709 sep = strchr(tok, ',');
1710 while (sep != NULL) {
1712 if (strchr(tok, '-')) {
1713 delete_job_id_range(ua, tok);
1715 JobId = str_to_int64(tok);
1716 do_job_delete(ua, JobId);
1719 sep = strchr(tok, ',');
1721 /* pick up the last token */
1722 if (strchr(tok, '-')) {
1723 delete_job_id_range(ua, tok);
1725 JobId = str_to_int64(tok);
1726 do_job_delete(ua, JobId);
1731 JobId = str_to_int64(ua->argv[i]);
1732 do_job_delete(ua, JobId);
1734 } else if (!get_pint(ua, _("Enter JobId to delete: "))) {
1737 JobId = ua->pint32_val;
1738 do_job_delete(ua, JobId);
1743 * we call delete_job_id_range to parse range tokens and iterate over ranges
1745 static void delete_job_id_range(UAContext *ua, char *tok)
1750 tok2 = strchr(tok, '-');
1753 j1 = str_to_int64(tok);
1754 j2 = str_to_int64(tok2);
1755 for (j=j1; j<=j2; j++) {
1756 do_job_delete(ua, j);
1761 * do_job_delete now performs the actual delete operation atomically
1762 * we always return 1 because C++ is pissy about void functions
1765 static void do_job_delete(UAContext *ua, JobId_t JobId)
1767 POOLMEM *query = get_pool_memory(PM_MESSAGE);
1769 Mmsg(query, "DELETE FROM Job WHERE JobId=%u", JobId);
1770 db_sql_query(ua->db, query, NULL, (void *)NULL);
1771 Mmsg(query, "DELETE FROM File WHERE JobId=%u", JobId);
1772 db_sql_query(ua->db, query, NULL, (void *)NULL);
1773 Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%u", JobId);
1774 db_sql_query(ua->db, query, NULL, (void *)NULL);
1775 free_pool_memory(query);
1776 bsendmsg(ua, _("Job %u and associated records deleted from the catalog.\n"), JobId);
1780 * Delete media records from database -- dangerous
1782 static int delete_volume(UAContext *ua)
1786 if (!select_media_dbr(ua, &mr)) {
1789 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1790 "and all Jobs saved on that volume from the Catalog\n"),
1793 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1796 if (ua->pint32_val) {
1797 db_delete_media_record(ua->jcr, ua->db, &mr);
1803 * Delete a pool record from the database -- dangerous
1805 static int delete_pool(UAContext *ua)
1809 memset(&pr, 0, sizeof(pr));
1811 if (!get_pool_dbr(ua, &pr)) {
1814 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1817 if (ua->pint32_val) {
1818 db_delete_pool_record(ua->jcr, ua->db, &pr);
1824 static void do_mount_cmd(UAContext *ua, const char *command)
1829 char dev_name[MAX_NAME_LENGTH];
1834 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1836 store = get_storage_resource(ua, 1);
1841 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1842 store->media_type, store->dev_name);
1845 if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
1846 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1849 sd = jcr->store_bsock;
1850 bstrncpy(dev_name, store->dev_name, sizeof(dev_name));
1851 bash_spaces(dev_name);
1852 bnet_fsend(sd, "%s %s", command, dev_name);
1853 while (bnet_recv(sd) >= 0) {
1854 bsendmsg(ua, "%s", sd->msg);
1856 bnet_sig(sd, BNET_TERMINATE);
1858 jcr->store_bsock = NULL;
1862 * mount [storage | device] <name>
1864 static int mount_cmd(UAContext *ua, const char *cmd)
1866 do_mount_cmd(ua, "mount"); /* mount */
1872 * unmount [storage | device] <name>
1874 static int unmount_cmd(UAContext *ua, const char *cmd)
1876 do_mount_cmd(ua, "unmount"); /* unmount */
1882 * release [storage | device] <name>
1884 static int release_cmd(UAContext *ua, const char *cmd)
1886 do_mount_cmd(ua, "release"); /* release */
1893 * use catalog=<name>
1895 static int use_cmd(UAContext *ua, const char *cmd)
1897 CAT *oldcatalog, *catalog;
1900 close_db(ua); /* close any previously open db */
1901 oldcatalog = ua->catalog;
1903 if (!(catalog = get_catalog_resource(ua))) {
1904 ua->catalog = oldcatalog;
1906 ua->catalog = catalog;
1909 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1910 ua->catalog->hdr.name, ua->catalog->db_name);
1915 int quit_cmd(UAContext *ua, const char *cmd)
1922 * Wait until no job is running
1924 int wait_cmd(UAContext *ua, const char *cmd)
1927 bmicrosleep(0, 200000); /* let job actually start */
1928 for (bool running=true; running; ) {
1932 if (jcr->JobId != 0) {
1934 free_locked_jcr(jcr);
1937 free_locked_jcr(jcr);
1948 static int help_cmd(UAContext *ua, const char *cmd)
1952 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1953 for (i=0; i<comsize; i++) {
1954 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1956 bsendmsg(ua, _("\nWhen at a prompt, entering a period cancels the command.\n\n"));
1960 int qhelp_cmd(UAContext *ua, const char *cmd)
1964 for (i=0; i<comsize; i++) {
1965 bsendmsg(ua, _("%s %s\n"), _(commands[i].key), _(commands[i].help));
1970 static int version_cmd(UAContext *ua, const char *cmd)
1972 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1977 /* A bit brain damaged in that if the user has not done
1978 * a "use catalog xxx" command, we simply find the first
1979 * catalog resource and open it.
1981 int open_db(UAContext *ua)
1988 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1991 bsendmsg(ua, _("Could not find a Catalog resource\n"));
1994 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1995 ua->catalog->hdr.name, ua->catalog->db_name);
1999 ua->jcr->catalog = ua->catalog;
2001 Dmsg0(150, "Open database\n");
2002 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
2003 ua->catalog->db_password, ua->catalog->db_address,
2004 ua->catalog->db_port, ua->catalog->db_socket,
2005 ua->catalog->mult_db_connections);
2006 if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
2007 bsendmsg(ua, _("Could not open database \"%s\".\n"),
2008 ua->catalog->db_name);
2010 bsendmsg(ua, "%s", db_strerror(ua->db));
2015 ua->jcr->db = ua->db;
2016 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
2020 void close_db(UAContext *ua)
2023 db_close_database(ua->jcr, ua->db);