3 * Program to copy a Bacula from one volume to another.
5 * Kern E. Sibbald, October 2002
11 Copyright (C) 2002-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 /* Forward referenced functions */
34 static bool record_cb(DCR *dcr, 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 const char *wd = "/tmp";
44 static int list_records = 0;
45 static uint32_t records = 0;
46 static uint32_t jobs = 0;
47 static DEV_BLOCK *out_block;
49 #define CONFIG_FILE "bacula-sd.conf"
57 "Copyright (C) 2002-2004 Kern Sibbald and John Walker.\n"
58 "\nVersion: " VERSION " (" BDATE ")\n\n"
59 "Usage: bcopy [-d debug_level] <input-archive> <output-archive>\n"
60 " -b bootstrap specify a bootstrap file\n"
61 " -c <file> specify configuration file\n"
62 " -d <nn> set debug level to nn\n"
63 " -i specify input Volume names (separated by |)\n"
64 " -o specify output Volume names (separated by |)\n"
65 " -p proceed inspite of errors\n"
67 " -w <dir> specify working directory (default /tmp)\n"
68 " -? print this message\n\n"));
72 int main (int argc, char *argv[])
75 char *iVolumeName = NULL;
76 char *oVolumeName = NULL;
77 bool ignore_label_errors = false;
79 my_name_is(argc, argv, "bcopy");
82 while ((ch = getopt(argc, argv, "b:c:d:i:o:pvw:?")) != -1) {
85 bsr = parse_bsr(NULL, optarg);
88 case 'c': /* specify config file */
89 if (configfile != NULL) {
92 configfile = bstrdup(optarg);
95 case 'd': /* debug level */
96 debug_level = atoi(optarg);
101 case 'i': /* input Volume name */
102 iVolumeName = optarg;
105 case 'o': /* output Volume name */
106 oVolumeName = optarg;
110 ignore_label_errors = true;
132 Pmsg0(0, _("Wrong number of arguments: \n"));
136 working_directory = wd;
138 if (configfile == NULL) {
139 configfile = bstrdup(CONFIG_FILE);
142 parse_config(configfile);
144 /* Setup and acquire input device for reading */
145 in_jcr = setup_jcr("bcopy", argv[0], bsr, iVolumeName, 1); /* read device */
149 in_jcr->ignore_label_errors = ignore_label_errors;
150 in_dev = in_jcr->dcr->dev;
155 /* Setup output device for writing */
156 out_jcr = setup_jcr("bcopy", argv[1], bsr, oVolumeName, 0); /* no acquire */
160 out_dev = out_jcr->dcr->dev;
164 /* For we must now acquire the device for writing */
165 lock_device(out_dev);
166 if (open_dev(out_dev, out_jcr->dcr->VolumeName, OPEN_READ_WRITE) < 0) {
167 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), out_dev->errmsg);
168 unlock_device(out_dev);
171 unlock_device(out_dev);
172 if (!acquire_device_for_append(out_jcr)) {
176 out_block = out_jcr->dcr->block;
178 read_records(in_jcr->dcr, record_cb, mount_next_read_volume);
179 if (!write_block_to_device(out_jcr->dcr)) {
180 Pmsg0(000, _("Write of last block failed.\n"));
183 Pmsg2(000, _("%u Jobs copied. %u records copied.\n"), jobs, records);
195 * read_records() calls back here for each record it gets
197 static bool record_cb(DCR *in_dcr, DEV_RECORD *rec)
200 Pmsg5(000, _("Record: SessId=%u SessTim=%u FileIndex=%d Stream=%d len=%u\n"),
201 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex,
202 rec->Stream, rec->data_len);
205 * Check for Start or End of Session Record
208 if (rec->FileIndex < 0) {
211 dump_label_record(in_dcr->dev, rec, 1);
213 switch (rec->FileIndex) {
215 Pmsg0(000, "Volume is prelabeled. This volume cannot be copied.\n");
218 Pmsg0(000, "Volume label not copied.\n");
224 while (!write_record_to_block(out_block, rec)) {
225 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec->data_len,
227 if (!write_block_to_device(out_jcr->dcr)) {
228 Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
229 dev_name(out_dev), strerror_dev(out_dev));
230 Jmsg(out_jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
231 strerror_dev(out_dev));
234 if (!write_block_to_device(out_jcr->dcr)) {
235 Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
236 dev_name(out_dev), strerror_dev(out_dev));
237 Jmsg(out_jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
238 strerror_dev(out_dev));
242 Pmsg0(000, "EOM label not copied.\n");
244 case EOT_LABEL: /* end of all tapes */
245 Pmsg0(000, "EOT label not copied.\n");
254 while (!write_record_to_block(out_block, rec)) {
255 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec->data_len,
257 if (!write_block_to_device(out_jcr->dcr)) {
258 Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
259 dev_name(out_dev), strerror_dev(out_dev));
260 Jmsg(out_jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
261 strerror_dev(out_dev));
269 /* Dummies to replace askdir.c */
270 bool dir_get_volume_info(DCR *dcr, enum get_vol_info_rw writing) { return 1;}
271 bool dir_find_next_appendable_volume(DCR *dcr) { return 1;}
272 bool dir_update_volume_info(DCR *dcr, bool relabel) { return 1; }
273 bool dir_create_jobmedia_record(DCR *dcr) { return 1; }
274 bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr) { return 1; }
275 bool dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec) { return 1;}
276 bool dir_send_job_status(JCR *jcr) {return 1;}
279 bool dir_ask_sysop_to_mount_volume(DCR *dcr)
281 DEVICE *dev = dcr->dev;
282 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
283 dcr->VolumeName, dev_name(dev));