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