3 * Bacula Director -- User Agent Commands
5 * Kern Sibbald, September MM
11 Copyright (C) 2000-2005 Kern Sibbald
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 retentioncmd(UAContext *ua, const char *cmd);
56 extern int prunecmd(UAContext *ua, const char *cmd);
57 extern int purgecmd(UAContext *ua, const char *cmd);
58 extern int restore_cmd(UAContext *ua, const char *cmd);
59 extern int label_cmd(UAContext *ua, const char *cmd);
60 extern int relabel_cmd(UAContext *ua, const char *cmd);
61 extern int update_slots(UAContext *ua); /* ua_label.c */
63 /* Forward referenced functions */
64 static int add_cmd(UAContext *ua, const char *cmd);
65 static int create_cmd(UAContext *ua, const char *cmd);
66 static int cancel_cmd(UAContext *ua, const char *cmd);
67 static int setdebug_cmd(UAContext *ua, const char *cmd);
68 static int trace_cmd(UAContext *ua, const char *cmd);
69 static int var_cmd(UAContext *ua, const char *cmd);
70 static int estimate_cmd(UAContext *ua, const char *cmd);
71 static int help_cmd(UAContext *ua, const char *cmd);
72 static int delete_cmd(UAContext *ua, const char *cmd);
73 static int use_cmd(UAContext *ua, const char *cmd);
74 static int unmount_cmd(UAContext *ua, const char *cmd);
75 static int version_cmd(UAContext *ua, const char *cmd);
76 static int automount_cmd(UAContext *ua, const char *cmd);
77 static int time_cmd(UAContext *ua, const char *cmd);
78 static int reload_cmd(UAContext *ua, const char *cmd);
79 static int update_volume(UAContext *ua);
80 static int update_pool(UAContext *ua);
81 static int delete_volume(UAContext *ua);
82 static int delete_pool(UAContext *ua);
83 static void delete_job(UAContext *ua);
84 static int mount_cmd(UAContext *ua, const char *cmd);
85 static int release_cmd(UAContext *ua, const char *cmd);
86 static int update_cmd(UAContext *ua, const char *cmd);
87 static int wait_cmd(UAContext *ua, const char *cmd);
88 static int setip_cmd(UAContext *ua, const char *cmd);
89 static int python_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=pool-name> | 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_("python"), python_cmd, _("python control commands")},
117 { N_("quit"), quit_cmd, _("quit")},
118 { N_("query"), querycmd, _("query catalog")},
119 { N_("restore"), restore_cmd, _("restore files")},
120 { N_("relabel"), relabel_cmd, _("relabel a tape")},
121 { N_("release"), release_cmd, _("release <storage-name>")},
122 { N_("reload"), reload_cmd, _("reload conf file")},
123 { N_("run"), run_cmd, _("run <job-name>")},
124 { N_("status"), status_cmd, _("status [storage | client]=<name>")},
125 { N_("setdebug"), setdebug_cmd, _("sets debug level")},
126 { N_("setip"), setip_cmd, _("sets new client address -- if authorized")},
127 { N_("show"), show_cmd, _("show (resource records) [jobs | pools | ... | all]")},
128 { N_("sqlquery"), sqlquerycmd, _("use SQL to query catalog")},
129 { N_("time"), time_cmd, _("print current time")},
130 { N_("trace"), trace_cmd, _("turn on/off trace to file")},
131 { N_("unmount"), unmount_cmd, _("unmount <storage-name>")},
132 { N_("umount"), unmount_cmd, _("umount <storage-name> for old-time Unix guys")},
133 { N_("update"), update_cmd, _("update Volume or Pool")},
134 { N_("use"), use_cmd, _("use catalog xxx")},
135 { N_("var"), var_cmd, _("does variable expansion")},
136 { N_("version"), version_cmd, _("print Director version")},
137 { N_("wait"), wait_cmd, _("wait until no jobs are running")},
139 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
142 * Execute a command from the UA
144 int do_a_command(UAContext *ua, const char *cmd)
152 Dmsg1(900, "Command: %s\n", ua->UA_sock->msg);
157 len = strlen(ua->argk[0]);
158 for (i=0; i<comsize; i++) { /* search for command */
159 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
160 if (!acl_access_ok(ua, Command_ACL, ua->argk[0], len)) {
163 stat = (*commands[i].func)(ua, cmd); /* go execute command */
169 bnet_fsend(ua->UA_sock, _("%s: is an illegal command.\n"), ua->argk[0]);
175 * This is a common routine used to stuff the Pool DB record defaults
176 * into the Media DB record just before creating a media (Volume)
179 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
181 mr->PoolId = pr->PoolId;
182 bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));
183 mr->Recycle = pr->Recycle;
184 mr->VolRetention = pr->VolRetention;
185 mr->VolUseDuration = pr->VolUseDuration;
186 mr->MaxVolJobs = pr->MaxVolJobs;
187 mr->MaxVolFiles = pr->MaxVolFiles;
188 mr->MaxVolBytes = pr->MaxVolBytes;
189 mr->LabelType = pr->LabelType;
194 * Add Volumes to an existing Pool
196 static int add_cmd(UAContext *ua, const char *cmd)
200 int num, i, max, startnum;
202 char name[MAX_NAME_LENGTH];
204 int Slot = 0, InChanger = 0;
207 "You probably don't want to be using this command since it\n"
208 "creates database records without labeling the Volumes.\n"
209 "You probably want to use the \"label\" command.\n\n"));
215 memset(&pr, 0, sizeof(pr));
216 memset(&mr, 0, sizeof(mr));
218 if (!get_pool_dbr(ua, &pr)) {
222 Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
223 pr.MaxVols, pr.PoolType);
225 while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
226 bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
228 if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
231 pr.MaxVols = ua->pint32_val;
236 if ((store = get_storage_resource(ua, 0)) != NULL) {
237 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
238 } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
242 if (pr.MaxVols == 0) {
245 max = pr.MaxVols - pr.NumVols;
249 bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
250 if (!get_pint(ua, buf)) {
253 num = ua->pint32_val;
254 if (num < 0 || num > max) {
255 bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
262 if (!get_cmd(ua, _("Enter Volume name: "))) {
266 if (!get_cmd(ua, _("Enter base volume name: "))) {
270 /* Don't allow | in Volume name because it is the volume separator character */
271 if (!is_volume_name_legal(ua, ua->cmd)) {
274 if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
275 bsendmsg(ua, _("Volume name too long.\n"));
278 if (strlen(ua->cmd) == 0) {
279 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
283 bstrncpy(name, ua->cmd, sizeof(name));
285 bstrncat(name, "%04d", sizeof(name));
288 if (!get_pint(ua, _("Enter the starting number: "))) {
291 startnum = ua->pint32_val;
293 bsendmsg(ua, _("Start number must be greater than zero.\n"));
303 if (store && store->autochanger) {
304 if (!get_pint(ua, _("Enter slot (0 for none): "))) {
307 Slot = ua->pint32_val;
308 if (!get_yesno(ua, _("InChanger? yes/no: "))) {
311 InChanger = ua->pint32_val;
314 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
315 for (i=startnum; i < num+startnum; i++) {
316 bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
318 mr.InChanger = InChanger;
319 mr.StorageId = store->StorageId;
320 Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
321 if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
322 bsendmsg(ua, "%s", db_strerror(ua->db));
326 first_id = mr.PoolId;
330 Dmsg0(200, "Update pool record.\n");
331 if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
332 bsendmsg(ua, "%s", db_strerror(ua->db));
335 bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
341 * Turn auto mount on/off
346 int automount_cmd(UAContext *ua, const char *cmd)
351 if (!get_cmd(ua, _("Turn on or off? "))) {
359 ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
367 static int cancel_cmd(UAContext *ua, const char *cmd)
372 char JobName[MAX_NAME_LENGTH];
378 for (i=1; i<ua->argc; i++) {
379 if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
384 JobId = str_to_int64(ua->argv[i]);
385 if (!(jcr=get_jcr_by_id(JobId))) {
386 bsendmsg(ua, _("JobId %d is not running.\n"), JobId);
390 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
394 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
395 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
401 /* If we still do not have a jcr,
402 * throw up a list and ask the user to select one.
406 /* Count Jobs running */
409 if (jcr->JobId == 0) { /* this is us */
410 free_locked_jcr(jcr);
413 free_locked_jcr(jcr);
419 bsendmsg(ua, _("No Jobs running.\n"));
422 start_prompt(ua, _("Select Job:\n"));
425 if (jcr->JobId == 0) { /* this is us */
426 free_locked_jcr(jcr);
429 bsnprintf(buf, sizeof(buf), "JobId=%d Job=%s", jcr->JobId, jcr->Job);
431 free_locked_jcr(jcr);
435 if (do_prompt(ua, _("Job"), _("Choose Job to cancel"), buf, sizeof(buf)) < 0) {
439 if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
443 /* NOTE! This increments the ref_count */
444 sscanf(buf, "JobId=%d Job=%127s", &njobs, JobName);
445 jcr = get_jcr_by_full_name(JobName);
447 bsendmsg(ua, _("Job %s not found.\n"), JobName);
452 ret = cancel_job(ua, jcr);
459 * This is a common routine to create or update a
460 * Pool DB base record from a Pool Resource. We handle
461 * the setting of MaxVols and NumVols slightly differently
462 * depending on if we are creating the Pool or we are
463 * simply bringing it into agreement with the resource (updage).
465 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
467 bstrncpy(pr->PoolType, pool->pool_type, sizeof(pr->PoolType));
468 if (op == POOL_OP_CREATE) {
469 pr->MaxVols = pool->max_volumes;
471 } else { /* update pool */
472 if (pr->MaxVols != pool->max_volumes) {
473 pr->MaxVols = pool->max_volumes;
475 if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
476 pr->MaxVols = pr->NumVols;
479 pr->LabelType = pool->LabelType;
480 pr->UseOnce = pool->use_volume_once;
481 pr->UseCatalog = pool->use_catalog;
482 pr->AcceptAnyVolume = pool->accept_any_volume;
483 pr->Recycle = pool->Recycle;
484 pr->VolRetention = pool->VolRetention;
485 pr->VolUseDuration = pool->VolUseDuration;
486 pr->MaxVolJobs = pool->MaxVolJobs;
487 pr->MaxVolFiles = pool->MaxVolFiles;
488 pr->MaxVolBytes = pool->MaxVolBytes;
489 pr->AutoPrune = pool->AutoPrune;
490 pr->Recycle = pool->Recycle;
491 if (pool->label_format) {
492 bstrncpy(pr->LabelFormat, pool->label_format, sizeof(pr->LabelFormat));
494 bstrncpy(pr->LabelFormat, "*", sizeof(pr->LabelFormat)); /* none */
500 * Create a pool record from a given Pool resource
501 * Also called from backup.c
502 * Returns: -1 on error
503 * 0 record already exists
507 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
511 memset(&pr, 0, sizeof(POOL_DBR));
513 bstrncpy(pr.Name, pool->hdr.name, sizeof(pr.Name));
515 if (db_get_pool_record(jcr, db, &pr)) {
517 if (op == POOL_OP_UPDATE) { /* update request */
518 set_pooldbr_from_poolres(&pr, pool, op);
519 db_update_pool_record(jcr, db, &pr);
521 return 0; /* exists */
524 set_pooldbr_from_poolres(&pr, pool, op);
526 if (!db_create_pool_record(jcr, db, &pr)) {
527 return -1; /* error */
535 * Create a Pool Record in the database.
536 * It is always created from the Resource record.
538 static int create_cmd(UAContext *ua, const char *cmd)
546 pool = get_pool_resource(ua);
551 switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
553 bsendmsg(ua, _("Error: Pool %s already exists.\n"
554 "Use update to change it.\n"), pool->hdr.name);
558 bsendmsg(ua, "%s", db_strerror(ua->db));
564 bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
569 extern DIRRES *director;
572 * Python control command
573 * python restart (restarts interpreter)
575 static int python_cmd(UAContext *ua, const char *cmd)
577 if (strcasecmp(ua->argk[1], _("restart")) == 0) {
578 term_python_interpreter();
579 init_python_interpreter(director->hdr.name, director->scripts_directory ?
580 director->scripts_directory : ".");
581 bsendmsg(ua, _("Python interpreter restarted.\n"));
583 bsendmsg(ua, _("Nothing done.\n"));
590 * Set a new address in a Client resource. We do this only
591 * if the Console name is the same as the Client name
592 * and the Console can access the client.
594 static int setip_cmd(UAContext *ua, const char *cmd)
598 if (!ua->cons || !acl_access_ok(ua, Client_ACL, ua->cons->hdr.name)) {
599 bsendmsg(ua, _("Illegal command from this console.\n"));
603 client = (CLIENT *)GetResWithName(R_CLIENT, ua->cons->hdr.name);
606 bsendmsg(ua, _("Client \"%s\" not found.\n"), ua->cons->hdr.name);
609 if (client->address) {
610 free(client->address);
612 /* MA Bug 6 remove ifdef */
613 sockaddr_to_ascii(&(ua->UA_sock->client_addr), buf, sizeof(buf));
614 client->address = bstrdup(buf);
615 bsendmsg(ua, _("Client \"%s\" address set to %s\n"),
616 client->hdr.name, client->address);
624 * Update a Pool Record in the database.
625 * It is always updated from the Resource record.
627 * update pool=<pool-name>
628 * updates pool from Pool resource
629 * update media pool=<pool-name> volume=<volume-name>
630 * changes pool info for volume
631 * update slots [scan=...]
632 * updates autochanger slots
634 static int update_cmd(UAContext *ua, const char *cmd)
636 static const char *kw[] = {
638 N_("volume"), /* 1 */
647 switch (find_arg_keyword(ua, kw)) {
662 start_prompt(ua, _("Update choice:\n"));
663 add_prompt(ua, _("Volume parameters"));
664 add_prompt(ua, _("Pool from resource"));
665 add_prompt(ua, _("Slots from autochanger"));
666 switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
682 static void update_volstatus(UAContext *ua, const char *val, MEDIA_DBR *mr)
684 POOLMEM *query = get_pool_memory(PM_MESSAGE);
698 for (i=0; kw[i]; i++) {
699 if (strcasecmp(val, kw[i]) == 0) {
705 bsendmsg(ua, _("Invalid VolStatus specified: %s\n"), val);
708 bstrncpy(mr->VolStatus, kw[i], sizeof(mr->VolStatus));
709 Mmsg(query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%s",
710 mr->VolStatus, edit_int64(mr->MediaId,ed1));
711 if (!db_sql_query(ua->db, query, NULL, NULL)) {
712 bsendmsg(ua, "%s", db_strerror(ua->db));
714 bsendmsg(ua, _("New Volume status is: %s\n"), mr->VolStatus);
717 free_pool_memory(query);
720 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
722 char ed1[150], ed2[50];
724 if (!duration_to_utime(val, &mr->VolRetention)) {
725 bsendmsg(ua, _("Invalid retention period specified: %s\n"), val);
728 query = get_pool_memory(PM_MESSAGE);
729 Mmsg(query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%s",
730 edit_uint64(mr->VolRetention, ed1), edit_int64(mr->MediaId,ed2));
731 if (!db_sql_query(ua->db, query, NULL, NULL)) {
732 bsendmsg(ua, "%s", db_strerror(ua->db));
734 bsendmsg(ua, _("New retention period is: %s\n"),
735 edit_utime(mr->VolRetention, ed1, sizeof(ed1)));
737 free_pool_memory(query);
740 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
742 char ed1[150], ed2[50];
745 if (!duration_to_utime(val, &mr->VolUseDuration)) {
746 bsendmsg(ua, _("Invalid use duration specified: %s\n"), val);
749 query = get_pool_memory(PM_MESSAGE);
750 Mmsg(query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%s",
751 edit_uint64(mr->VolUseDuration, ed1), edit_int64(mr->MediaId,ed2));
752 if (!db_sql_query(ua->db, query, NULL, NULL)) {
753 bsendmsg(ua, "%s", db_strerror(ua->db));
755 bsendmsg(ua, _("New use duration is: %s\n"),
756 edit_utime(mr->VolUseDuration, ed1, sizeof(ed1)));
758 free_pool_memory(query);
761 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
763 POOLMEM *query = get_pool_memory(PM_MESSAGE);
765 Mmsg(query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%s",
766 val, edit_int64(mr->MediaId,ed1));
767 if (!db_sql_query(ua->db, query, NULL, NULL)) {
768 bsendmsg(ua, "%s", db_strerror(ua->db));
770 bsendmsg(ua, _("New max jobs is: %s\n"), val);
772 free_pool_memory(query);
775 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
777 POOLMEM *query = get_pool_memory(PM_MESSAGE);
779 Mmsg(query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%s",
780 val, edit_int64(mr->MediaId, ed1));
781 if (!db_sql_query(ua->db, query, NULL, NULL)) {
782 bsendmsg(ua, "%s", db_strerror(ua->db));
784 bsendmsg(ua, _("New max files is: %s\n"), val);
786 free_pool_memory(query);
789 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
792 char ed1[50], ed2[50];
795 if (!size_to_uint64(val, strlen(val), &maxbytes)) {
796 bsendmsg(ua, _("Invalid max. bytes specification: %s\n"), val);
799 query = get_pool_memory(PM_MESSAGE);
800 Mmsg(query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%s",
801 edit_uint64(maxbytes, ed1), edit_int64(mr->MediaId, ed2));
802 if (!db_sql_query(ua->db, query, NULL, NULL)) {
803 bsendmsg(ua, "%s", db_strerror(ua->db));
805 bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
807 free_pool_memory(query);
810 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
815 if (strcasecmp(val, _("yes")) == 0) {
817 } else if (strcasecmp(val, _("no")) == 0) {
820 bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
823 query = get_pool_memory(PM_MESSAGE);
824 Mmsg(query, "UPDATE Media SET Recycle=%d WHERE MediaId=%s",
825 recycle, edit_int64(mr->MediaId, ed1));
826 if (!db_sql_query(ua->db, query, NULL, NULL)) {
827 bsendmsg(ua, "%s", db_strerror(ua->db));
829 bsendmsg(ua, _("New Recycle flag is: %s\n"),
830 mr->Recycle==1?_("yes"):_("no"));
832 free_pool_memory(query);
835 /* Modify the Pool in which this Volume is located */
836 static void update_vol_pool(UAContext *ua, char *val, MEDIA_DBR *mr, POOL_DBR *opr)
842 memset(&pr, 0, sizeof(pr));
843 bstrncpy(pr.Name, val, sizeof(pr.Name));
844 if (!get_pool_dbr(ua, &pr)) {
847 mr->PoolId = pr.PoolId; /* set new PoolId */
850 query = get_pool_memory(PM_MESSAGE);
852 Mmsg(query, "UPDATE Media SET PoolId=%d WHERE MediaId=%s",
853 mr->PoolId, edit_int64(mr->MediaId, ed1));
854 if (!db_sql_query(ua->db, query, NULL, NULL)) {
855 bsendmsg(ua, "%s", db_strerror(ua->db));
857 bsendmsg(ua, _("New Pool is: %s\n"), pr.Name);
859 if (!db_update_pool_record(ua->jcr, ua->db, opr)) {
860 bsendmsg(ua, "%s", db_strerror(ua->db));
863 if (!db_update_pool_record(ua->jcr, ua->db, &pr)) {
864 bsendmsg(ua, "%s", db_strerror(ua->db));
868 free_pool_memory(query);
872 * Refresh the Volume information from the Pool record
874 static void update_volfrompool(UAContext *ua, MEDIA_DBR *mr)
878 memset(&pr, 0, sizeof(pr));
879 pr.PoolId = mr->PoolId;
880 if (!db_get_pool_record(ua->jcr, ua->db, &pr) ||
881 !acl_access_ok(ua, Pool_ACL, pr.Name)) {
884 set_pool_dbr_defaults_in_media_dbr(mr, &pr);
885 if (!db_update_media_defaults(ua->jcr, ua->db, mr)) {
886 bsendmsg(ua, _("Error updating Volume record: ERR=%s"), db_strerror(ua->db));
888 bsendmsg(ua, _("Volume defaults updated from \"%s\" Pool record.\n"),
894 * Refresh the Volume information from the Pool record
897 static void update_all_vols_from_pool(UAContext *ua)
902 memset(&pr, 0, sizeof(pr));
903 memset(&mr, 0, sizeof(mr));
904 if (!get_pool_dbr(ua, &pr)) {
907 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
908 mr.PoolId = pr.PoolId;
909 if (!db_update_media_defaults(ua->jcr, ua->db, &mr)) {
910 bsendmsg(ua, _("Error updating Volume records: ERR=%s"), db_strerror(ua->db));
912 bsendmsg(ua, _("All Volume defaults updated from Pool record.\n"));
918 * Update a media record -- allows you to change the
919 * Volume status. E.g. if you want Bacula to stop
920 * writing on the volume, set it to anything other
923 static int update_volume(UAContext *ua)
931 N_("VolStatus"), /* 0 */
932 N_("VolRetention"), /* 1 */
933 N_("VolUse"), /* 2 */
934 N_("MaxVolJobs"), /* 3 */
935 N_("MaxVolFiles"), /* 4 */
936 N_("MaxVolBytes"), /* 5 */
937 N_("Recycle"), /* 6 */
939 N_("FromPool"), /* 8 */
940 N_("AllFromPool"), /* 9 */
943 for (int i=0; kw[i]; i++) {
946 if ((j=find_arg_with_value(ua, kw[i])) > 0) {
947 if (i != 9 && !select_media_dbr(ua, &mr)) {
952 update_volstatus(ua, ua->argv[j], &mr);
955 update_volretention(ua, ua->argv[j], &mr);
958 update_voluseduration(ua, ua->argv[j], &mr);
961 update_volmaxjobs(ua, ua->argv[j], &mr);
964 update_volmaxfiles(ua, ua->argv[j], &mr);
967 update_volmaxbytes(ua, ua->argv[j], &mr);
970 update_volrecycle(ua, ua->argv[j], &mr);
973 memset(&pr, 0, sizeof(POOL_DBR));
974 pr.PoolId = mr.PoolId;
975 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
976 bsendmsg(ua, "%s", db_strerror(ua->db));
979 update_vol_pool(ua, ua->argv[j], &mr, &pr);
982 update_volfrompool(ua, &mr);
985 update_all_vols_from_pool(ua);
993 if (!select_media_dbr(ua, &mr)) {
996 bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
997 start_prompt(ua, _("Parameters to modify:\n"));
998 add_prompt(ua, _("Volume Status"));
999 add_prompt(ua, _("Volume Retention Period"));
1000 add_prompt(ua, _("Volume Use Duration"));
1001 add_prompt(ua, _("Maximum Volume Jobs"));
1002 add_prompt(ua, _("Maximum Volume Files"));
1003 add_prompt(ua, _("Maximum Volume Bytes"));
1004 add_prompt(ua, _("Recycle Flag"));
1005 add_prompt(ua, _("Slot"));
1006 add_prompt(ua, _("InChanger Flag"));
1007 add_prompt(ua, _("Volume Files"));
1008 add_prompt(ua, _("Pool"));
1009 add_prompt(ua, _("Volume from Pool"));
1010 add_prompt(ua, _("All Volumes from Pool"));
1011 add_prompt(ua, _("Done"));
1012 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
1013 case 0: /* Volume Status */
1014 /* Modify Volume Status */
1015 bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
1016 start_prompt(ua, _("Possible Values are:\n"));
1017 add_prompt(ua, "Append"); /* Better not translate these as */
1018 add_prompt(ua, "Archive"); /* They are known in the database code */
1019 add_prompt(ua, "Disabled");
1020 add_prompt(ua, "Full");
1021 add_prompt(ua, "Used");
1022 add_prompt(ua, "Cleaning");
1023 if (strcmp(mr.VolStatus, "Purged") == 0) {
1024 add_prompt(ua, "Recycle");
1026 add_prompt(ua, "Read-Only");
1027 if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
1030 update_volstatus(ua, ua->cmd, &mr);
1032 case 1: /* Retention */
1033 bsendmsg(ua, _("Current retention period is: %s\n"),
1034 edit_utime(mr.VolRetention, ed1, sizeof(ed1)));
1035 if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
1038 update_volretention(ua, ua->cmd, &mr);
1041 case 2: /* Use Duration */
1042 bsendmsg(ua, _("Current use duration is: %s\n"),
1043 edit_utime(mr.VolUseDuration, ed1, sizeof(ed1)));
1044 if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
1047 update_voluseduration(ua, ua->cmd, &mr);
1050 case 3: /* Max Jobs */
1051 bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
1052 if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
1055 update_volmaxjobs(ua, ua->cmd, &mr);
1058 case 4: /* Max Files */
1059 bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
1060 if (!get_pint(ua, _("Enter new Maximum Files: "))) {
1063 update_volmaxfiles(ua, ua->cmd, &mr);
1066 case 5: /* Max Bytes */
1067 bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
1068 if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
1071 update_volmaxbytes(ua, ua->cmd, &mr);
1075 case 6: /* Recycle */
1076 bsendmsg(ua, _("Current recycle flag is: %s\n"),
1077 mr.Recycle==1?_("yes"):_("no"));
1078 if (!get_yesno(ua, _("Enter new Recycle status: "))) {
1081 update_volrecycle(ua, ua->cmd, &mr);
1087 memset(&pr, 0, sizeof(POOL_DBR));
1088 pr.PoolId = mr.PoolId;
1089 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1090 bsendmsg(ua, "%s", db_strerror(ua->db));
1093 bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
1094 if (!get_pint(ua, _("Enter new Slot: "))) {
1097 Slot = ua->pint32_val;
1098 if (pr.MaxVols > 0 && Slot > (int)pr.MaxVols) {
1099 bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
1105 * Make sure to use db_update... rather than doing this directly,
1106 * so that any Slot is handled correctly.
1108 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1109 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1111 bsendmsg(ua, _("New Slot is: %d\n"), mr.Slot);
1115 case 8: /* InChanger */
1116 bsendmsg(ua, _("Current InChanger flag is: %d\n"), mr.InChanger);
1117 if (!get_yesno(ua, _("Set InChanger flag? yes/no: "))) {
1120 mr.InChanger = ua->pint32_val;
1122 * Make sure to use db_update... rather than doing this directly,
1123 * so that any Slot is handled correctly.
1125 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1126 bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1128 bsendmsg(ua, _("New InChanger flag is: %d\n"), mr.InChanger);
1133 case 9: /* Volume Files */
1135 bsendmsg(ua, _("Warning changing Volume Files can result\n"
1136 "in loss of data on your Volume\n\n"));
1137 bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
1138 if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
1141 VolFiles = ua->pint32_val;
1142 if (VolFiles != (int)(mr.VolFiles + 1)) {
1143 bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
1144 if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
1148 query = get_pool_memory(PM_MESSAGE);
1149 Mmsg(query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%s",
1150 VolFiles, edit_int64(mr.MediaId, ed1));
1151 if (!db_sql_query(ua->db, query, NULL, NULL)) {
1152 bsendmsg(ua, "%s", db_strerror(ua->db));
1154 bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
1156 free_pool_memory(query);
1159 case 10: /* Volume's Pool */
1160 memset(&pr, 0, sizeof(POOL_DBR));
1161 pr.PoolId = mr.PoolId;
1162 if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1163 bsendmsg(ua, "%s", db_strerror(ua->db));
1166 bsendmsg(ua, _("Current Pool is: %s\n"), pr.Name);
1167 if (!get_cmd(ua, _("Enter new Pool name: "))) {
1170 update_vol_pool(ua, ua->cmd, &mr, &pr);
1174 update_volfrompool(ua, &mr);
1177 update_all_vols_from_pool(ua);
1179 default: /* Done or error */
1180 bsendmsg(ua, "Selection done.\n");
1188 * Update pool record -- pull info from current POOL resource
1190 static int update_pool(UAContext *ua)
1197 pool = get_pool_resource(ua);
1202 memset(&pr, 0, sizeof(pr));
1203 bstrncpy(pr.Name, pool->hdr.name, sizeof(pr.Name));
1204 if (!get_pool_dbr(ua, &pr)) {
1208 set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
1210 id = db_update_pool_record(ua->jcr, ua->db, &pr);
1212 bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
1213 id, db_strerror(ua->db));
1215 query = get_pool_memory(PM_MESSAGE);
1216 Mmsg(query, list_pool, pr.PoolId);
1217 db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
1218 free_pool_memory(query);
1219 bsendmsg(ua, _("Pool DB record updated from resource.\n"));
1224 static void do_storage_setdebug(UAContext *ua, STORE *store, int level, int trace_flag)
1229 set_storage(jcr, store);
1230 /* Try connecting for up to 15 seconds */
1231 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
1232 store->hdr.name, store->address, store->SDport);
1233 if (!connect_to_storage_daemon(jcr, 1, 15, 0)) {
1234 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1237 Dmsg0(120, _("Connected to storage daemon\n"));
1238 sd = jcr->store_bsock;
1239 bnet_fsend(sd, "setdebug=%d trace=%d\n", level, trace_flag);
1240 if (bnet_recv(sd) >= 0) {
1241 bsendmsg(ua, "%s", sd->msg);
1243 bnet_sig(sd, BNET_TERMINATE);
1245 jcr->store_bsock = NULL;
1249 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level, int trace_flag)
1253 /* Connect to File daemon */
1255 ua->jcr->client = client;
1256 /* Try to connect for 15 seconds */
1257 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1258 client->hdr.name, client->address, client->FDport);
1259 if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1260 bsendmsg(ua, _("Failed to connect to Client.\n"));
1263 Dmsg0(120, "Connected to file daemon\n");
1264 fd = ua->jcr->file_bsock;
1265 bnet_fsend(fd, "setdebug=%d trace=%d\n", level, trace_flag);
1266 if (bnet_recv(fd) >= 0) {
1267 bsendmsg(ua, "%s", fd->msg);
1269 bnet_sig(fd, BNET_TERMINATE);
1271 ua->jcr->file_bsock = NULL;
1276 static void do_all_setdebug(UAContext *ua, int level, int trace_flag)
1278 STORE *store, **unique_store;
1279 CLIENT *client, **unique_client;
1283 debug_level = level;
1285 /* Count Storage items */
1289 foreach_res(store, R_STORAGE) {
1292 unique_store = (STORE **) malloc(i * sizeof(STORE));
1293 /* Find Unique Storage address/port */
1294 store = (STORE *)GetNextRes(R_STORAGE, NULL);
1296 unique_store[i++] = store;
1297 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1299 for (j=0; j<i; j++) {
1300 if (strcmp(unique_store[j]->address, store->address) == 0 &&
1301 unique_store[j]->SDport == store->SDport) {
1307 unique_store[i++] = store;
1308 Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1313 /* Call each unique Storage daemon */
1314 for (j=0; j<i; j++) {
1315 do_storage_setdebug(ua, unique_store[j], level, trace_flag);
1319 /* Count Client items */
1323 foreach_res(client, R_CLIENT) {
1326 unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1327 /* Find Unique Client address/port */
1328 client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1330 unique_client[i++] = client;
1331 while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1333 for (j=0; j<i; j++) {
1334 if (strcmp(unique_client[j]->address, client->address) == 0 &&
1335 unique_client[j]->FDport == client->FDport) {
1341 unique_client[i++] = client;
1342 Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1347 /* Call each unique File daemon */
1348 for (j=0; j<i; j++) {
1349 do_client_setdebug(ua, unique_client[j], level, trace_flag);
1351 free(unique_client);
1355 * setdebug level=nn all trace=1/0
1357 static int setdebug_cmd(UAContext *ua, const char *cmd)
1362 int trace_flag = -1;
1368 Dmsg1(120, "setdebug:%s:\n", cmd);
1371 i = find_arg_with_value(ua, _("level"));
1373 level = atoi(ua->argv[i]);
1376 if (!get_pint(ua, _("Enter new debug level: "))) {
1379 level = ua->pint32_val;
1382 /* Look for trace flag. -1 => not change */
1383 i = find_arg_with_value(ua, _("trace"));
1385 trace_flag = atoi(ua->argv[i]);
1386 if (trace_flag > 0) {
1391 /* General debug? */
1392 for (i=1; i<ua->argc; i++) {
1393 if (strcasecmp(ua->argk[i], _("all")) == 0) {
1394 do_all_setdebug(ua, level, trace_flag);
1397 if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1398 strcasecmp(ua->argk[i], _("director")) == 0) {
1399 debug_level = level;
1400 set_trace(trace_flag);
1403 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1404 strcasecmp(ua->argk[i], _("fd")) == 0) {
1407 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1409 do_client_setdebug(ua, client, level, trace_flag);
1413 client = select_client_resource(ua);
1415 do_client_setdebug(ua, client, level, trace_flag);
1420 if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1421 strcasecmp(ua->argk[i], _("storage")) == 0 ||
1422 strcasecmp(ua->argk[i], _("sd")) == 0) {
1425 store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1427 do_storage_setdebug(ua, store, level, trace_flag);
1431 store = get_storage_resource(ua, 0);
1433 do_storage_setdebug(ua, store, level, trace_flag);
1439 * We didn't find an appropriate keyword above, so
1442 start_prompt(ua, _("Available daemons are: \n"));
1443 add_prompt(ua, _("Director"));
1444 add_prompt(ua, _("Storage"));
1445 add_prompt(ua, _("Client"));
1446 add_prompt(ua, _("All"));
1447 switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1448 case 0: /* Director */
1449 debug_level = level;
1450 set_trace(trace_flag);
1453 store = get_storage_resource(ua, 0);
1455 do_storage_setdebug(ua, store, level, trace_flag);
1459 client = select_client_resource(ua);
1461 do_client_setdebug(ua, client, level, trace_flag);
1465 do_all_setdebug(ua, level, trace_flag);
1474 * Turn debug tracing to file on/off
1476 static int trace_cmd(UAContext *ua, const char *cmd)
1480 if (ua->argc != 2) {
1481 if (!get_cmd(ua, _("Turn on or off? "))) {
1486 onoff = ua->argk[1];
1489 set_trace((strcasecmp(onoff, _("off")) == 0) ? false : true);
1494 static int var_cmd(UAContext *ua, const char *cmd)
1496 POOLMEM *val = get_pool_memory(PM_FNAME);
1502 for (var=ua->cmd; *var != ' '; ) { /* skip command */
1505 while (*var == ' ') { /* skip spaces */
1508 Dmsg1(100, "Var=%s:\n", var);
1509 variable_expansion(ua->jcr, var, &val);
1510 bsendmsg(ua, "%s\n", val);
1511 free_pool_memory(val);
1515 static int estimate_cmd(UAContext *ua, const char *cmd)
1518 CLIENT *client = NULL;
1519 FILESET *fileset = NULL;
1522 char since[MAXSTRING];
1525 jcr->JobLevel = L_FULL;
1526 for (int i=1; i<ua->argc; i++) {
1527 if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1528 strcasecmp(ua->argk[i], _("fd")) == 0) {
1530 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1534 if (strcasecmp(ua->argk[i], _("job")) == 0) {
1536 job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1540 if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1542 fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1546 if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1550 if (strcasecmp(ua->argk[i], _("level")) == 0) {
1551 if (!get_level_from_name(ua->jcr, ua->argv[i])) {
1552 bsendmsg(ua, _("Level %s not valid.\n"), ua->argv[i]);
1557 if (!job && !(client && fileset)) {
1558 if (!(job = select_job_resource(ua))) {
1563 job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1565 bsendmsg(ua, _("No job specified.\n"));
1570 client = job->client;
1573 fileset = job->fileset;
1575 jcr->client = client;
1576 jcr->fileset = fileset;
1578 ua->catalog = client->catalog;
1585 jcr->JobType = JT_BACKUP;
1586 init_jcr_job_record(jcr);
1588 if (!get_or_create_client_record(jcr)) {
1591 if (!get_or_create_fileset_record(jcr, &fsr)) {
1595 get_level_since_time(ua->jcr, since, sizeof(since));
1597 bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1598 job->client->hdr.name, job->client->address, job->client->FDport);
1599 if (!connect_to_file_daemon(jcr, 1, 15, 0)) {
1600 bsendmsg(ua, _("Failed to connect to Client.\n"));
1604 if (!send_include_list(jcr)) {
1605 bsendmsg(ua, _("Error sending include list.\n"));
1609 if (!send_exclude_list(jcr)) {
1610 bsendmsg(ua, _("Error sending exclude list.\n"));
1614 if (!send_level_command(jcr)) {
1618 bnet_fsend(jcr->file_bsock, "estimate listing=%d\n", listing);
1619 while (bnet_recv(jcr->file_bsock) >= 0) {
1620 bsendmsg(ua, "%s", jcr->file_bsock->msg);
1624 if (jcr->file_bsock) {
1625 bnet_sig(jcr->file_bsock, BNET_TERMINATE);
1626 bnet_close(jcr->file_bsock);
1627 jcr->file_bsock = NULL;
1636 static int time_cmd(UAContext *ua, const char *cmd)
1639 time_t ttime = time(NULL);
1641 localtime_r(&ttime, &tm);
1642 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1643 bsendmsg(ua, "%s\n", sdt);
1648 * reload the conf file
1650 extern "C" void reload_config(int sig);
1652 static int reload_cmd(UAContext *ua, const char *cmd)
1659 * Delete Pool records (should purge Media with it).
1661 * delete pool=<pool-name>
1662 * delete volume pool=<pool-name> volume=<name>
1663 * delete job jobid=xxx
1665 static int delete_cmd(UAContext *ua, const char *cmd)
1667 static const char *keywords[] = {
1678 switch (find_arg_keyword(ua, keywords)) {
1688 while ((i=find_arg(ua, _("jobid"))) > 0) {
1690 *ua->argk[i] = 0; /* zap keyword already visited */
1698 "In general it is not a good idea to delete either a\n"
1699 "Pool or a Volume since they may contain data.\n\n"));
1701 switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1712 bsendmsg(ua, _("Nothing done.\n"));
1720 * delete_job has been modified to parse JobID lists like the
1722 * delete job JobID=3,4,6,7-11,14
1724 * Thanks to Phil Stracchino for the above addition.
1727 static void delete_job(UAContext *ua)
1732 int i = find_arg_with_value(ua, _("jobid"));
1734 if (strchr(ua->argv[i], ',') != NULL || strchr(ua->argv[i], '-') != NULL) {
1735 s = bstrdup(ua->argv[i]);
1738 * We could use strtok() here. But we're not going to, because:
1739 * (a) strtok() is deprecated, having been replaced by strsep();
1740 * (b) strtok() is broken in significant ways.
1741 * we could use strsep() instead, but it's not universally available.
1742 * so we grow our own using strchr().
1744 sep = strchr(tok, ',');
1745 while (sep != NULL) {
1747 if (strchr(tok, '-')) {
1748 delete_job_id_range(ua, tok);
1750 JobId = str_to_int64(tok);
1751 do_job_delete(ua, JobId);
1754 sep = strchr(tok, ',');
1756 /* pick up the last token */
1757 if (strchr(tok, '-')) {
1758 delete_job_id_range(ua, tok);
1760 JobId = str_to_int64(tok);
1761 do_job_delete(ua, JobId);
1766 JobId = str_to_int64(ua->argv[i]);
1767 do_job_delete(ua, JobId);
1769 } else if (!get_pint(ua, _("Enter JobId to delete: "))) {
1772 JobId = ua->int64_val;
1773 do_job_delete(ua, JobId);
1778 * we call delete_job_id_range to parse range tokens and iterate over ranges
1780 static void delete_job_id_range(UAContext *ua, char *tok)
1785 tok2 = strchr(tok, '-');
1788 j1 = str_to_int64(tok);
1789 j2 = str_to_int64(tok2);
1790 for (j=j1; j<=j2; j++) {
1791 do_job_delete(ua, j);
1796 * do_job_delete now performs the actual delete operation atomically
1797 * we always return 1 because C++ is pissy about void functions
1800 static void do_job_delete(UAContext *ua, JobId_t JobId)
1802 POOLMEM *query = get_pool_memory(PM_MESSAGE);
1805 Mmsg(query, "DELETE FROM Job WHERE JobId=%s", edit_int64(JobId, ed1));
1806 db_sql_query(ua->db, query, NULL, (void *)NULL);
1807 Mmsg(query, "DELETE FROM File WHERE JobId=%s", edit_int64(JobId, ed1));
1808 db_sql_query(ua->db, query, NULL, (void *)NULL);
1809 Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%s", edit_int64(JobId, ed1));
1810 db_sql_query(ua->db, query, NULL, (void *)NULL);
1811 free_pool_memory(query);
1812 bsendmsg(ua, _("Job %s and associated records deleted from the catalog.\n"), edit_int64(JobId, ed1));
1816 * Delete media records from database -- dangerous
1818 static int delete_volume(UAContext *ua)
1822 if (!select_media_dbr(ua, &mr)) {
1825 bsendmsg(ua, _("\nThis command will delete volume %s\n"
1826 "and all Jobs saved on that volume from the Catalog\n"),
1829 if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1832 if (ua->pint32_val) {
1833 db_delete_media_record(ua->jcr, ua->db, &mr);
1839 * Delete a pool record from the database -- dangerous
1841 static int delete_pool(UAContext *ua)
1845 memset(&pr, 0, sizeof(pr));
1847 if (!get_pool_dbr(ua, &pr)) {
1850 if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1853 if (ua->pint32_val) {
1854 db_delete_pool_record(ua->jcr, ua->db, &pr);
1860 static void do_mount_cmd(UAContext *ua, const char *command)
1865 char dev_name[MAX_NAME_LENGTH];
1870 Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1872 store = get_storage_resource(ua, 1);
1877 Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1878 store->media_type, store->dev_name());
1880 set_storage(jcr, store);
1881 if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
1882 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1885 sd = jcr->store_bsock;
1886 bstrncpy(dev_name, store->dev_name(), sizeof(dev_name));
1887 bash_spaces(dev_name);
1888 bnet_fsend(sd, "%s %s", command, dev_name);
1889 while (bnet_recv(sd) >= 0) {
1890 bsendmsg(ua, "%s", sd->msg);
1892 bnet_sig(sd, BNET_TERMINATE);
1894 jcr->store_bsock = NULL;
1898 * mount [storage | device] <name>
1900 static int mount_cmd(UAContext *ua, const char *cmd)
1902 do_mount_cmd(ua, "mount"); /* mount */
1908 * unmount [storage | device] <name>
1910 static int unmount_cmd(UAContext *ua, const char *cmd)
1912 do_mount_cmd(ua, "unmount"); /* unmount */
1918 * release [storage | device] <name>
1920 static int release_cmd(UAContext *ua, const char *cmd)
1922 do_mount_cmd(ua, "release"); /* release */
1929 * use catalog=<name>
1931 static int use_cmd(UAContext *ua, const char *cmd)
1933 CAT *oldcatalog, *catalog;
1936 close_db(ua); /* close any previously open db */
1937 oldcatalog = ua->catalog;
1939 if (!(catalog = get_catalog_resource(ua))) {
1940 ua->catalog = oldcatalog;
1942 ua->catalog = catalog;
1945 bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1946 ua->catalog->hdr.name, ua->catalog->db_name);
1951 int quit_cmd(UAContext *ua, const char *cmd)
1958 * Wait until no job is running
1960 int wait_cmd(UAContext *ua, const char *cmd)
1963 bmicrosleep(0, 200000); /* let job actually start */
1964 for (bool running=true; running; ) {
1968 if (jcr->JobId != 0) {
1970 free_locked_jcr(jcr);
1973 free_locked_jcr(jcr);
1984 static int help_cmd(UAContext *ua, const char *cmd)
1988 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
1989 for (i=0; i<comsize; i++) {
1990 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1992 bsendmsg(ua, _("\nWhen at a prompt, entering a period cancels the command.\n\n"));
1996 int qhelp_cmd(UAContext *ua, const char *cmd)
2000 for (i=0; i<comsize; i++) {
2001 bsendmsg(ua, _("%s %s\n"), _(commands[i].key), _(commands[i].help));
2006 static int version_cmd(UAContext *ua, const char *cmd)
2008 bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
2013 /* A bit brain damaged in that if the user has not done
2014 * a "use catalog xxx" command, we simply find the first
2015 * catalog resource and open it.
2017 int open_db(UAContext *ua)
2024 ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
2027 bsendmsg(ua, _("Could not find a Catalog resource\n"));
2030 bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
2031 ua->catalog->hdr.name, ua->catalog->db_name);
2035 ua->jcr->catalog = ua->catalog;
2037 Dmsg0(150, "Open database\n");
2038 ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
2039 ua->catalog->db_password, ua->catalog->db_address,
2040 ua->catalog->db_port, ua->catalog->db_socket,
2041 ua->catalog->mult_db_connections);
2042 if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
2043 bsendmsg(ua, _("Could not open database \"%s\".\n"),
2044 ua->catalog->db_name);
2046 bsendmsg(ua, "%s", db_strerror(ua->db));
2051 ua->jcr->db = ua->db;
2052 Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
2056 void close_db(UAContext *ua)
2059 db_close_database(ua->jcr, ua->db);