2 Bacula® - The Network Backup Solution
4 Copyright (C) 2006-2008 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * SD -- mac.c -- responsible for doing
30 * migration, archive, copy, and virtual backup jobs.
32 * Kern Sibbald, January MMVI
40 /* Import functions */
41 extern char Job_end[];
43 /* Forward referenced subroutines */
44 static bool record_cb(DCR *dcr, DEV_RECORD *rec);
48 * Read Data and send to File Daemon
49 * Returns: false on failure
55 BSOCK *dir = jcr->dir_bsock;
60 switch(jcr->JobType) {
71 Type = "Virtual Backup";
79 Dmsg0(20, "Start read data.\n");
81 if (!jcr->read_dcr || !jcr->dcr) {
82 Jmsg(jcr, M_FATAL, 0, _("Read and write devices not properly initialized.\n"));
85 Dmsg2(100, "read_dcr=%p write_dcr=%p\n", jcr->read_dcr, jcr->dcr);
88 create_restore_volume_list(jcr);
89 if (jcr->NumReadVolumes == 0) {
90 Jmsg(jcr, M_FATAL, 0, _("No Volume names found for %s.\n"), Type);
94 Dmsg3(200, "Found %d volumes names for %s. First=%s\n", jcr->NumReadVolumes,
95 jcr->VolList->VolumeName, Type);
97 /* Ready devices for reading and writing */
98 if (!acquire_device_for_read(jcr->read_dcr) ||
99 !acquire_device_for_append(jcr->dcr)) {
100 set_jcr_job_status(jcr, JS_ErrorTerminated);
104 Dmsg2(200, "===== After acquire pos %u:%u\n", jcr->dcr->dev->file, jcr->dcr->dev->block_num);
106 set_jcr_job_status(jcr, JS_Running);
107 dir_send_job_status(jcr);
109 begin_data_spool(jcr->dcr);
110 begin_attribute_spool(jcr);
112 jcr->dcr->VolFirstIndex = jcr->dcr->VolLastIndex = 0;
113 jcr->run_time = time(NULL);
114 set_start_vol_position(jcr->dcr);
116 ok = read_records(jcr->read_dcr, record_cb, mount_next_read_volume);
125 if (ok || dev->can_write()) {
126 /* Flush out final partial block of this session */
127 if (!write_block_to_device(jcr->dcr)) {
128 Jmsg2(jcr, M_FATAL, 0, _("Fatal append error on device %s: ERR=%s\n"),
129 dev->print_name(), dev->bstrerror());
130 Dmsg0(100, _("Set ok=FALSE after write_block_to_device.\n"));
133 Dmsg2(200, "Flush block to device pos %u:%u\n", dev->file, dev->block_num);
137 discard_data_spool(jcr->dcr);
139 /* Note: if commit is OK, the device will remain locked */
140 commit_data_spool(jcr->dcr);
143 if (ok && dev->is_dvd()) {
144 ok = dvd_close_job(jcr->dcr); /* do DVD cleanup if any */
146 /* Release the device -- and send final Vol info to DIR */
147 release_device(jcr->dcr);
149 if (!ok || job_canceled(jcr)) {
150 discard_attribute_spool(jcr);
152 commit_attribute_spool(jcr);
157 if (!release_device(jcr->read_dcr)) {
162 free_restore_volume_list(jcr);
164 dir_send_job_status(jcr); /* update director */
167 Dmsg0(30, "Done reading.\n");
168 jcr->end_time = time(NULL);
169 dequeue_messages(jcr); /* send any queued messages */
171 set_jcr_job_status(jcr, JS_Terminated);
173 generate_daemon_event(jcr, "JobEnd");
174 dir->fsend(Job_end, jcr->Job, jcr->JobStatus, jcr->JobFiles,
175 edit_uint64(jcr->JobBytes, ec1));
176 Dmsg4(200, Job_end, jcr->Job, jcr->JobStatus, jcr->JobFiles, ec1);
178 dir->signal(BNET_EOD); /* send EOD to Director daemon */
184 * Called here for each record from read_records()
185 * Returns: true if OK
188 static bool record_cb(DCR *dcr, DEV_RECORD *rec)
191 DEVICE *dev = jcr->dcr->dev;
192 char buf1[100], buf2[100];
195 /* If label and not for us, discard it */
196 if (rec->FileIndex < 0 && rec->match_stat <= 0) {
199 /* We want to write SOS_LABEL and EOS_LABEL discard all others */
200 switch (rec->FileIndex) {
205 return true; /* don't write vol labels */
208 * Modify record SessionId and SessionTime to correspond to
211 rec->VolSessionId = jcr->VolSessionId;
212 rec->VolSessionTime = jcr->VolSessionTime;
213 Dmsg5(200, "before write_rec JobId=%d FI=%s SessId=%d Strm=%s len=%d\n",
215 FI_to_ascii(buf1, rec->FileIndex), rec->VolSessionId,
216 stream_to_ascii(buf1, rec->Stream,rec->FileIndex), rec->data_len);
218 while (!write_record_to_block(jcr->dcr->block, rec)) {
219 Dmsg4(200, "!write_record_to_block blkpos=%u:%u len=%d rem=%d\n",
220 dev->file, dev->block_num, rec->data_len, rec->remainder);
221 if (!write_block_to_device(jcr->dcr)) {
222 Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
223 dev->print_name(), dev->bstrerror());
224 Jmsg2(jcr, M_FATAL, 0, _("Fatal append error on device %s: ERR=%s\n"),
225 dev->print_name(), dev->bstrerror());
228 Dmsg2(200, "===== Wrote block new pos %u:%u\n", dev->file, dev->block_num);
230 jcr->JobBytes += rec->data_len; /* increment bytes this job */
231 if (rec->FileIndex > 0) {
232 jcr->JobFiles = rec->FileIndex;
234 return true; /* don't send LABELs to Dir */
236 Dmsg5(500, "wrote_record JobId=%d FI=%s SessId=%d Strm=%s len=%d\n",
238 FI_to_ascii(buf1, rec->FileIndex), rec->VolSessionId,
239 stream_to_ascii(buf2, rec->Stream, rec->FileIndex), rec->data_len);
241 /* Send attributes and digest to Director for Catalog */
242 stream = rec->Stream;
243 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX ||
244 crypto_digest_stream_type(stream) != CRYPTO_DIGEST_NONE) {
245 if (!jcr->no_attributes) {
246 BSOCK *dir = jcr->dir_bsock;
247 if (are_attributes_spooled(jcr)) {
250 Dmsg0(850, "Send attributes to dir.\n");
251 if (!dir_update_file_attributes(jcr->dcr, rec)) {
252 dir->clear_spooling();
253 Jmsg(jcr, M_FATAL, 0, _("Error updating file attributes. ERR=%s\n"),
257 dir->clear_spooling();