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 record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
37 /* Global variables */
38 static DEVICE *in_dev = NULL;
39 static DEVICE *out_dev = NULL;
40 static JCR *in_jcr; /* input jcr */
41 static JCR *out_jcr; /* output jcr */
42 static BSR *bsr = NULL;
43 static char *wd = "/tmp";
44 static int verbose = 0;
45 static int list_records = 0;
46 static uint32_t records = 0;
47 static uint32_t jobs = 0;
48 static DEV_BLOCK *out_block;
50 #define CONFIG_FILE "bacula-sd.conf"
57 "\nVersion: " VERSION " (" BDATE ")\n\n"
58 "Usage: bcopy [-d debug_level] <input-archive> <output-archive>\n"
59 " -b bootstrap specify a bootstrap file\n"
60 " -c <file> specify configuration file\n"
61 " -dnn set debug level to nn\n"
63 " -w dir specify working directory (default /tmp)\n"
64 " -? print this message\n\n"));
68 int main (int argc, char *argv[])
72 my_name_is(argc, argv, "bscan");
75 while ((ch = getopt(argc, argv, "b:c:d:mn:p:rsu:vw:?")) != -1) {
78 bsr = parse_bsr(NULL, optarg);
81 case 'c': /* specify config file */
82 if (configfile != NULL) {
85 configfile = bstrdup(optarg);
88 case 'd': /* debug level */
89 debug_level = atoi(optarg);
112 Pmsg0(0, _("Wrong number of arguments: \n"));
116 working_directory = wd;
118 if (configfile == NULL) {
119 configfile = bstrdup(CONFIG_FILE);
122 parse_config(configfile);
124 /* Setup and acquire input device for reading */
125 in_jcr = setup_jcr("bcopy", argv[0], bsr);
126 in_dev = setup_to_access_device(in_jcr, 1); /* read device */
131 /* Setup output device for writing */
132 out_jcr = setup_jcr("bcopy", argv[1], bsr);
133 out_dev = setup_to_access_device(out_jcr, 0); /* no acquire */
137 /* For we must now acquire the device for writing */
138 out_block = new_block(out_dev);
139 lock_device(out_dev);
140 if (open_dev(out_dev, out_jcr->VolumeName, READ_WRITE) < 0) {
141 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), out_dev->errmsg);
142 unlock_device(out_dev);
143 free_block(out_block);
146 unlock_device(out_dev);
147 if (!(out_dev=acquire_device_for_append(out_jcr, out_dev, out_block))) {
148 free_block(out_block);
153 read_records(in_jcr, in_dev, record_cb, mount_next_read_volume);
154 if (!write_block_to_device(out_jcr, out_dev, out_block)) {
155 Pmsg0(000, _("Write of last block failed.\n"));
158 Pmsg2(000, _("%u Jobs copied. %u records copied.\n"), jobs, records);
160 free_block(out_block);
170 static void record_cb(JCR *in_jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
173 Pmsg5(000, _("Record: SessId=%u SessTim=%u FileIndex=%d Stream=%d len=%u\n"),
174 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex,
175 rec->Stream, rec->data_len);
178 * Check for Start or End of Session Record
181 if (rec->FileIndex < 0) {
184 dump_label_record(dev, rec, 1);
186 switch (rec->FileIndex) {
188 Pmsg0(000, "Volume is prelabeled. This volume cannot be copied.\n");
191 Pmsg0(000, "Volume label not copied.\n");
197 while (!write_record_to_block(out_block, rec)) {
198 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec->data_len,
200 if (!write_block_to_device(out_jcr, out_dev, out_block)) {
201 Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
202 dev_name(out_dev), strerror_dev(out_dev));
203 Jmsg(out_jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
204 strerror_dev(out_dev));
207 if (!write_block_to_device(out_jcr, out_dev, out_block)) {
208 Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
209 dev_name(out_dev), strerror_dev(out_dev));
210 Jmsg(out_jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
211 strerror_dev(out_dev));
215 Pmsg0(000, "EOM label not copied.\n");
217 case EOT_LABEL: /* end of all tapes */
218 Pmsg0(000, "EOT label not copied.\n");
227 while (!write_record_to_block(out_block, rec)) {
228 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec->data_len,
230 if (!write_block_to_device(out_jcr, out_dev, out_block)) {
231 Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
232 dev_name(out_dev), strerror_dev(out_dev));
233 Jmsg(out_jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
234 strerror_dev(out_dev));
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)
254 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
255 in_jcr->VolumeName, dev_name(dev));