]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/bcopy.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / stored / bcopy.c
1 /*
2  *
3  *  Program to copy a Bacula from one volume to another.
4  *
5  *   Kern E. Sibbald, October 2002
6  *
7  *
8  *   Version $Id$
9  */
10 /*
11    Copyright (C) 2001, 2002 Kern Sibbald and John Walker
12
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.
17
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.
22
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,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "stored.h"
32
33 /* Forward referenced functions */
34 static int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
35
36
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 list_records = 0;
45 static uint32_t records = 0;
46 static uint32_t jobs = 0;
47 static DEV_BLOCK *out_block;
48
49 #define CONFIG_FILE "bacula-sd.conf"
50 char *configfile;
51
52
53 static void usage()
54 {
55    fprintf(stderr, _(
56 "\nVersion: " VERSION " (" BDATE ")\n\n"
57 "Usage: bcopy [-d debug_level] <input-archive> <output-archive>\n"
58 "       -b bootstrap      specify a bootstrap file\n"
59 "       -c <file>         specify configuration file\n"
60 "       -d <nn>           set debug level to nn\n"
61 "       -v                verbose\n"
62 "       -i                specify input Volume names (separated by |)\n"
63 "       -o                specify output Volume names (separated by |)\n"
64 "       -w <dir>          specify working directory (default /tmp)\n"
65 "       -?                print this message\n\n"));
66    exit(1);
67 }
68
69 int main (int argc, char *argv[])
70 {
71    int ch;
72    char *iVolumeName = NULL;
73    char *oVolumeName = NULL;
74
75    my_name_is(argc, argv, "bscan");
76    init_msg(NULL, NULL);
77
78    while ((ch = getopt(argc, argv, "b:c:d:mn:p:rsu:vV:w:?")) != -1) {
79       switch (ch) {
80          case 'b':
81             bsr = parse_bsr(NULL, optarg);
82             break;
83
84          case 'c':                    /* specify config file */
85             if (configfile != NULL) {
86                free(configfile);
87             }
88             configfile = bstrdup(optarg);
89             break;
90
91          case 'd':                    /* debug level */
92             debug_level = atoi(optarg);
93             if (debug_level <= 0)
94                debug_level = 1; 
95             break;
96
97          case 'v':
98             verbose++;
99             break;
100
101          case 'i':                    /* input Volume name */
102             iVolumeName = optarg;
103             break;
104
105          case 'o':                    /* output Volume name */
106             oVolumeName = optarg;
107             break;
108
109
110          case 'w':
111             wd = optarg;
112             break;
113
114          case '?':
115          default:
116             usage();
117
118       }  
119    }
120    argc -= optind;
121    argv += optind;
122
123    if (argc != 2) {
124       Pmsg0(0, _("Wrong number of arguments: \n"));
125       usage();
126    }
127
128    working_directory = wd;
129
130    if (configfile == NULL) {
131       configfile = bstrdup(CONFIG_FILE);
132    }
133
134    parse_config(configfile);
135
136    /* Setup and acquire input device for reading */
137    in_jcr = setup_jcr("bcopy", argv[0], bsr, iVolumeName);
138    in_dev = setup_to_access_device(in_jcr, 1);   /* read device */
139    if (!in_dev) { 
140       exit(1);
141    }
142
143    /* Setup output device for writing */
144    out_jcr = setup_jcr("bcopy", argv[1], bsr, oVolumeName);
145    out_dev = setup_to_access_device(out_jcr, 0);   /* no acquire */  
146    if (!out_dev) { 
147       exit(1);      
148    }
149    /* For we must now acquire the device for writing */
150    out_block = new_block(out_dev);
151    lock_device(out_dev);
152    if (open_dev(out_dev, out_jcr->VolumeName, READ_WRITE) < 0) {
153       Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), out_dev->errmsg);
154       unlock_device(out_dev);
155       free_block(out_block);
156       exit(1);
157    }
158    unlock_device(out_dev);
159    if (!(out_dev=acquire_device_for_append(out_jcr, out_dev, out_block))) {
160       free_block(out_block);
161       free_jcr(in_jcr);
162       exit(1);
163    }
164
165    read_records(in_jcr, in_dev, record_cb, mount_next_read_volume);
166    if (!write_block_to_device(out_jcr, out_dev, out_block)) {
167       Pmsg0(000, _("Write of last block failed.\n"));
168    }
169
170    Pmsg2(000, _("%u Jobs copied. %u records copied.\n"), jobs, records);
171
172    free_block(out_block);
173    term_dev(in_dev);
174    term_dev(out_dev);
175    free_jcr(in_jcr);
176    free_jcr(out_jcr);
177    return 0;
178 }
179   
180
181
182 static int record_cb(JCR *in_jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
183 {
184    if (list_records) {
185       Pmsg5(000, _("Record: SessId=%u SessTim=%u FileIndex=%d Stream=%d len=%u\n"),
186             rec->VolSessionId, rec->VolSessionTime, rec->FileIndex, 
187             rec->Stream, rec->data_len);
188    }
189    /* 
190     * Check for Start or End of Session Record 
191     *
192     */
193    if (rec->FileIndex < 0) {
194
195       if (verbose > 1) {
196          dump_label_record(dev, rec, 1);
197       }
198       switch (rec->FileIndex) {
199       case PRE_LABEL:
200          Pmsg0(000, "Volume is prelabeled. This volume cannot be copied.\n");
201          return 1;
202       case VOL_LABEL:
203          Pmsg0(000, "Volume label not copied.\n");
204          return 1;
205       case SOS_LABEL:
206          jobs++;
207          break;
208       case EOS_LABEL:
209          while (!write_record_to_block(out_block, rec)) {
210             Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec->data_len,
211                        rec->remainder);
212             if (!write_block_to_device(out_jcr, out_dev, out_block)) {
213                Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
214                   dev_name(out_dev), strerror_dev(out_dev));
215                Jmsg(out_jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
216                      strerror_dev(out_dev));
217             }
218          }
219          if (!write_block_to_device(out_jcr, out_dev, out_block)) {
220             Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
221                dev_name(out_dev), strerror_dev(out_dev));
222             Jmsg(out_jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
223                   strerror_dev(out_dev));
224          }
225          break;
226       case EOM_LABEL:
227          Pmsg0(000, "EOM label not copied.\n");
228          return 1;
229       case EOT_LABEL:              /* end of all tapes */
230          Pmsg0(000, "EOT label not copied.\n");
231          return 1;
232       default:
233          break;
234       }
235    }
236
237    /*  Write record */
238    records++;
239    while (!write_record_to_block(out_block, rec)) {
240       Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec->data_len,
241                  rec->remainder);
242       if (!write_block_to_device(out_jcr, out_dev, out_block)) {
243          Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
244             dev_name(out_dev), strerror_dev(out_dev));
245          Jmsg(out_jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
246                strerror_dev(out_dev));
247          break;
248       }
249    }
250    return 1;
251 }
252
253
254 /* Dummies to replace askdir.c */
255 int     dir_get_volume_info(JCR *jcr, enum get_vol_info_rw  writing) { return 1;}
256 int     dir_find_next_appendable_volume(JCR *jcr) { return 1;}
257 int     dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
258 int     dir_create_jobmedia_record(JCR *jcr) { return 1; }
259 int     dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
260 int     dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
261 int     dir_send_job_status(JCR *jcr) {return 1;}
262
263
264 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
265 {
266    fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
267       in_jcr->VolumeName, dev_name(dev));
268    getchar();   
269    return 1;
270 }