3 * Program to copy a Bacula from one volume to another.
5 * Kern E. Sibbald, October 2002
11 Copyright (C) 2001, 2002 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 /* Forward referenced functions */
34 static void do_scan(void);
35 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
38 /* Global variables */
39 static DEVICE *dev = NULL;
41 static JCR *bjcr; /* jcr for bscan */
42 static BSR *bsr = NULL;
43 static char *wd = "/tmp";
44 static int verbose = 0;
46 #define CONFIG_FILE "bacula-sd.conf"
53 "\nVersion: " VERSION " (" DATE ")\n\n"
54 "Usage: bcopy [-d debug_level] <input-archive> <output-archive>\n"
55 " -b bootstrap specify a bootstrap file\n"
56 " -c <file> specify configuration file\n"
57 " -dnn set debug level to nn\n"
59 " -w dir specify working directory (default /tmp)\n"
60 " -? print this message\n\n"));
64 int main (int argc, char *argv[])
68 my_name_is(argc, argv, "bscan");
71 fprintf(stderr, "\n\nPlease don't use this program.\n\
72 It is currently under development and does not work.\n\n");
74 while ((ch = getopt(argc, argv, "b:c:d:mn:p:rsu:vw:?")) != -1) {
77 bsr = parse_bsr(NULL, optarg);
80 case 'c': /* specify config file */
81 if (configfile != NULL) {
84 configfile = bstrdup(optarg);
87 case 'd': /* debug level */
88 debug_level = atoi(optarg);
111 Pmsg0(0, _("Wrong number of arguments: \n"));
115 working_directory = wd;
117 if (configfile == NULL) {
118 configfile = bstrdup(CONFIG_FILE);
121 parse_config(configfile);
123 bjcr = setup_jcr("bcopy", argv[0], bsr);
124 dev = setup_to_access_device(bjcr, 0); /* read device */
136 static void do_copy()
138 detach_jcr_from_device(dev, bjcr);
140 read_records(bjcr, dev, record_cb, mount_next_read_volume);
141 release_device(bjcr, dev);
146 static void record_cb(JCR *bjcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
152 Pmsg5(000, _("Record: SessId=%u SessTim=%u FileIndex=%d Stream=%d len=%u\n"),
153 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex,
154 rec->Stream, rec->data_len);
157 * Check for Start or End of Session Record
160 if (rec->FileIndex < 0) {
163 dump_label_record(dev, rec, 1);
165 switch (rec->FileIndex) {
167 Pmsg0(000, "Volume is prelabeled. This volume cannot be copied.\n");
171 Pmsg1(000, "VOL_LABEL: OK for Volume: %s\n", mr.VolumeName);
179 case EOT_LABEL: /* end of all tapes */
193 * Free the Job Control Record if no one is still using it.
194 * Called from main free_jcr() routine in src/lib/jcr.c so
195 * that we can do our Director specific cleanup of the jcr.
197 static void dird_free_jcr(JCR *jcr)
199 Dmsg0(200, "Start dird free_jcr\n");
201 if (jcr->file_bsock) {
202 Dmsg0(200, "Close File bsock\n");
203 bnet_close(jcr->file_bsock);
205 if (jcr->store_bsock) {
206 Dmsg0(200, "Close Store bsock\n");
207 bnet_close(jcr->store_bsock);
209 if (jcr->RestoreBootstrap) {
210 free(jcr->RestoreBootstrap);
212 Dmsg0(200, "End dird free_jcr\n");
218 * Create a JCR as if we are really starting the job
220 static JCR *create_jcr(JOB_DBR *jr, DEV_RECORD *rec, uint32_t JobId)
224 * Transfer as much as possible to the Job JCR. Most important is
225 * the JobId and the ClientId.
227 jobjcr = new_jcr(sizeof(JCR), dird_free_jcr);
228 jobjcr->JobType = jr->Type;
229 jobjcr->JobLevel = jr->Level;
230 jobjcr->JobStatus = jr->JobStatus;
231 strcpy(jobjcr->Job, jr->Job);
232 jobjcr->JobId = JobId; /* this is JobId on tape */
233 jobjcr->sched_time = jr->SchedTime;
234 jobjcr->start_time = jr->StartTime;
235 jobjcr->VolSessionId = rec->VolSessionId;
236 jobjcr->VolSessionTime = rec->VolSessionTime;
237 jobjcr->ClientId = jr->ClientId;
238 attach_jcr_to_device(dev, jobjcr);
242 /* Dummies to replace askdir.c */
243 int dir_get_volume_info(JCR *jcr, int writing) { return 1;}
244 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
245 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
246 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
247 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
248 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
249 int dir_send_job_status(JCR *jcr) {return 1;}
252 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
255 * We are at the end of reading a tape. Now, we simulate handling
256 * the end of writing a tape by wiffling through the attached
257 * jcrs creating jobmedia records.
259 Dmsg1(100, "Walk attached jcrs. Volume=%s\n", dev->VolCatInfo.VolCatName);
260 for (JCR *mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
262 Pmsg1(000, "create JobMedia for Job %s\n", mjcr->Job);
264 if (dev->state & ST_TAPE) {
265 mjcr->EndBlock = dev->block_num;
266 mjcr->EndFile = dev->file;
268 mjcr->EndBlock = (uint32_t)dev->file_addr;
269 mjcr->StartBlock = (uint32_t)(dev->file_addr >> 32);
271 if (!create_jobmedia_record(db, mjcr)) {
272 Pmsg2(000, _("Could not create JobMedia record for Volume=%s Job=%s\n"),
273 dev->VolCatInfo.VolCatName, mjcr->Job);
277 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
278 jcr->VolumeName, dev_name(dev));