Line data Source code
1 : #include "../burp.h"
2 : #include "../action.h"
3 : #include "../alloc.h"
4 : #include "../asfd.h"
5 : #include "../async.h"
6 : #include "../conf.h"
7 : #include "../cmd.h"
8 : #include "../cntr.h"
9 : #include "../fsops.h"
10 : #include "../handy.h"
11 : #include "../iobuf.h"
12 : #include "../log.h"
13 : #include "../run_script.h"
14 : #include "auth.h"
15 : #include "backup_phase1.h"
16 : #include "backup_phase3.h"
17 : #include "compress.h"
18 : #include "delete.h"
19 : #include "sdirs.h"
20 : #include "protocol1/backup_phase2.h"
21 : #include "protocol1/backup_phase4.h"
22 : #include "protocol2/backup_phase2.h"
23 : #include "protocol2/backup_phase4.h"
24 : #include "backup.h"
25 : #include "rubble.h"
26 : #include "timer.h"
27 :
28 1 : static int open_log(struct asfd *asfd,
29 : struct sdirs *sdirs, struct conf **cconfs)
30 : {
31 1 : int ret=-1;
32 1 : char *logpath=NULL;
33 1 : const char *peer_version=get_string(cconfs[OPT_PEER_VERSION]);
34 :
35 1 : if(!(logpath=prepend_s(sdirs->rworking, "log"))) goto end;
36 1 : if(log_fzp_set(logpath, cconfs))
37 : {
38 0 : logp("could not open log file: %s\n", logpath);
39 : goto end;
40 : }
41 :
42 1 : logp("Client version: %s\n", peer_version?:"");
43 1 : logp("Protocol: %d\n", (int)get_protocol(cconfs));
44 1 : if(get_int(cconfs[OPT_CLIENT_IS_WINDOWS]))
45 0 : logp("Client is Windows\n");
46 :
47 : // Make sure a warning appears in the backup log.
48 : // The client will already have been sent a message with logw.
49 : // This time, prevent it sending a logw to the client by specifying
50 : // NULL for asfd and cntr.
51 1 : if(get_int(cconfs[OPT_VERSION_WARN]))
52 1 : version_warn(NULL, NULL, cconfs);
53 :
54 : ret=0;
55 : end:
56 1 : free_w(&logpath);
57 1 : return ret;
58 : }
59 :
60 1 : static int write_incexc(const char *realworking, const char *incexc)
61 : {
62 1 : int ret=-1;
63 1 : char *tmp=NULL;
64 1 : char *path=NULL;
65 1 : struct fzp *fzp=NULL;
66 :
67 1 : if(!incexc || !*incexc) return 0;
68 :
69 0 : if(!(path=prepend_s(realworking, "incexc"))
70 0 : || !(tmp=prepend(path, ".tmp"))
71 0 : || !(fzp=fzp_open(tmp, "wb")))
72 : goto end;
73 :
74 0 : fzp_printf(fzp, "%s", incexc);
75 0 : if(fzp_close(&fzp))
76 : {
77 0 : logp("error writing to %s in %s\n", tmp, __func__);
78 0 : goto end;
79 : }
80 0 : if(do_rename(tmp, path))
81 : goto end;
82 0 : ret=0;
83 : end:
84 0 : free_w(&path);
85 0 : free_w(&tmp);
86 0 : return ret;
87 : }
88 :
89 1 : static int backup_phase1_server(struct async *as,
90 : struct sdirs *sdirs, struct conf **cconfs)
91 : {
92 1 : int breaking=get_int(cconfs[OPT_BREAKPOINT]);
93 1 : if(breaking==1)
94 0 : return breakpoint(breaking, __func__);
95 1 : return backup_phase1_server_all(as, sdirs, cconfs);
96 : }
97 :
98 0 : static int backup_phase2_server(struct async *as, struct sdirs *sdirs,
99 : const char *incexc, int resume, struct conf **cconfs)
100 : {
101 0 : int breaking=get_int(cconfs[OPT_BREAKPOINT]);
102 0 : if(breaking==2)
103 0 : return breakpoint(breaking, __func__);
104 :
105 0 : switch(get_protocol(cconfs))
106 : {
107 : case PROTO_1:
108 0 : return backup_phase2_server_protocol1(as, sdirs,
109 : incexc, resume, cconfs);
110 : default:
111 0 : return backup_phase2_server_protocol2(as, sdirs,
112 : resume, cconfs);
113 : }
114 : }
115 :
116 0 : static int backup_phase3_server(struct sdirs *sdirs, struct conf **cconfs)
117 : {
118 0 : int breaking=get_int(cconfs[OPT_BREAKPOINT]);
119 0 : if(breaking==3)
120 0 : return breakpoint(breaking, __func__);
121 :
122 0 : return backup_phase3_server_all(sdirs, cconfs);
123 : }
124 :
125 0 : static int backup_phase4_server(struct sdirs *sdirs, struct conf **cconfs)
126 : {
127 0 : int breaking=get_int(cconfs[OPT_BREAKPOINT]);
128 0 : if(breaking==4)
129 0 : return breakpoint(breaking, __func__);
130 :
131 0 : log_fzp_set(NULL, cconfs);
132 : // Phase4 will open logfp again (in case it is resuming).
133 0 : switch(get_protocol(cconfs))
134 : {
135 : case PROTO_1:
136 0 : return backup_phase4_server_protocol1(sdirs, cconfs);
137 : default:
138 0 : return backup_phase4_server_protocol2(sdirs, cconfs);
139 : }
140 : }
141 :
142 1 : static void log_rshash(struct conf **confs)
143 : {
144 1 : if(get_protocol(confs)!=PROTO_1) return;
145 0 : logp("Using librsync hash %s\n",
146 : rshash_to_str(get_e_rshash(confs[OPT_RSHASH])));
147 : }
148 :
149 1 : static void set_cntr_bno(struct cntr *cntr, struct sdirs *sdirs)
150 : {
151 1 : char *cp=NULL;
152 : // Should be easier than this.
153 1 : if(!(cp=strrchr(sdirs->rworking, '/')))
154 : return;
155 2 : cntr->bno=atoi(cp+1);
156 : }
157 :
158 1 : static int do_backup_server(struct async *as, struct sdirs *sdirs,
159 : struct conf **cconfs, const char *incexc, int resume)
160 : {
161 1 : int ret=0;
162 1 : int do_phase2=1;
163 1 : struct asfd *asfd=as->asfd;
164 1 : enum protocol protocol=get_protocol(cconfs);
165 1 : struct cntr *cntr=get_cntr(cconfs);
166 :
167 1 : logp("in do_backup_server\n");
168 :
169 1 : log_rshash(cconfs);
170 :
171 1 : if(resume)
172 : {
173 0 : if(sdirs_get_real_working_from_symlink(sdirs)
174 0 : || sdirs_get_real_manifest(sdirs, protocol)
175 0 : || open_log(asfd, sdirs, cconfs))
176 : goto error;
177 :
178 0 : set_cntr_bno(cntr, sdirs);
179 : }
180 : else
181 : {
182 : // Not resuming - need to set everything up fresh.
183 1 : if(sdirs_create_real_working(sdirs,
184 1 : get_string(cconfs[OPT_TIMESTAMP_FORMAT]))
185 1 : || sdirs_get_real_manifest(sdirs, protocol)
186 1 : || open_log(asfd, sdirs, cconfs))
187 : goto error;
188 :
189 1 : set_cntr_bno(cntr, sdirs);
190 :
191 1 : if(write_incexc(sdirs->rworking, incexc))
192 : {
193 0 : logp("unable to write incexc\n");
194 0 : goto error;
195 : }
196 :
197 1 : if(backup_phase1_server(as, sdirs, cconfs))
198 : {
199 1 : logp("error in phase 1\n");
200 1 : goto error;
201 : }
202 : }
203 :
204 0 : if(resume)
205 : {
206 : struct stat statp;
207 0 : if(lstat(sdirs->phase1data, &statp)
208 0 : && !lstat(sdirs->changed, &statp)
209 0 : && !lstat(sdirs->unchanged, &statp))
210 : {
211 : // In this condition, it looks like there was an
212 : // interruption during phase3. Skip phase2.
213 0 : do_phase2=0;
214 : }
215 : }
216 :
217 0 : if(do_phase2)
218 : {
219 0 : if(backup_phase2_server(as, sdirs, incexc, resume, cconfs))
220 : {
221 0 : logp("error in backup phase 2\n");
222 0 : goto error;
223 : }
224 :
225 0 : asfd->write_str(asfd, CMD_GEN, "okbackupend");
226 : }
227 :
228 : // Close the connection with the client, the rest of the job we can do
229 : // by ourselves.
230 0 : logp("Backup ending - disconnect from client.\n");
231 0 : if(asfd_flush_asio(asfd))
232 : goto error;
233 0 : as->asfd_remove(as, asfd);
234 0 : asfd_close(asfd);
235 :
236 0 : if(backup_phase3_server(sdirs, cconfs))
237 : {
238 0 : logp("error in backup phase 3\n");
239 0 : goto error;
240 : }
241 :
242 0 : if(do_rename(sdirs->working, sdirs->finishing))
243 : goto error;
244 :
245 0 : if(backup_phase4_server(sdirs, cconfs))
246 : {
247 0 : logp("error in backup phase 4\n");
248 0 : goto error;
249 : }
250 :
251 0 : cntr_print(cntr, ACTION_BACKUP, asfd);
252 0 : cntr_stats_to_file(cntr, sdirs->rworking, ACTION_BACKUP);
253 :
254 0 : if(protocol==PROTO_2)
255 : {
256 : // Regenerate dindex before the symlink is renamed, so that the
257 : // champ chooser cleanup does not try to remove data files
258 : // whilst the dindex regeneration is happening.
259 0 : if(regenerate_client_dindex(sdirs))
260 : goto error;
261 : }
262 :
263 : // Move the symlink to indicate that we are now in the end phase. The
264 : // rename() race condition is automatically recoverable here.
265 0 : if(do_rename(sdirs->finishing, sdirs->current))
266 : goto error;
267 :
268 0 : logp("Backup completed.\n");
269 0 : log_fzp_set(NULL, cconfs);
270 0 : compress_filename(sdirs->rworking,
271 : "log", "log.gz", get_int(cconfs[OPT_COMPRESSION]));
272 :
273 0 : goto end;
274 : error:
275 : ret=-1;
276 : end:
277 :
278 1 : log_fzp_set(NULL, cconfs);
279 1 : return ret;
280 : }
281 :
282 1 : int run_backup(struct async *as, struct sdirs *sdirs, struct conf **cconfs,
283 : const char *incexc, int *timer_ret, int resume)
284 : {
285 : int ret;
286 1 : char okstr[32]="";
287 1 : struct asfd *asfd=as->asfd;
288 1 : struct iobuf *rbuf=asfd->rbuf;
289 1 : const char *cname=get_string(cconfs[OPT_CNAME]);
290 :
291 1 : if(get_string(cconfs[OPT_RESTORE_CLIENT]))
292 : {
293 : // This client is not the original client, so a backup might
294 : // cause all sorts of trouble.
295 0 : logp("Not allowing backup of %s\n", cname);
296 0 : return asfd->write_str(asfd, CMD_GEN, "Backup is not allowed");
297 : }
298 :
299 : // Set quality of service bits on backups.
300 1 : asfd->set_bulk_packets(asfd);
301 :
302 1 : if(!strncmp_w(rbuf->buf, "backupphase1timed"))
303 : {
304 0 : int checkonly=!strncmp_w(rbuf->buf, "backupphase1timedcheck");
305 0 : if(checkonly) logp("Client asked for a timer check only.\n");
306 :
307 0 : if((*timer_ret=run_timer(asfd, sdirs, cconfs))<0)
308 : {
309 0 : logp("Error running timer for %s\n", cname);
310 0 : return -1;
311 : }
312 0 : else if(*timer_ret)
313 : {
314 0 : if(!checkonly)
315 0 : logp("Not running backup of %s\n", cname);
316 0 : return asfd->write_str(asfd,
317 : CMD_GEN, "timer conditions not met");
318 : }
319 0 : if(checkonly)
320 : {
321 0 : logp("Client asked for a timer check only,\n");
322 0 : logp("so a backup is not happening right now.\n");
323 0 : return asfd->write_str(asfd,
324 : CMD_GEN, "timer conditions met");
325 : }
326 0 : logp("Running backup of %s\n", cname);
327 : }
328 1 : else if(!get_int(cconfs[OPT_CLIENT_CAN_FORCE_BACKUP]))
329 : {
330 0 : logp("Not allowing forced backup of %s\n", cname);
331 0 : return asfd->write_str(asfd,
332 : CMD_GEN, "Forced backup is not allowed");
333 : }
334 :
335 1 : snprintf(okstr, sizeof(okstr), "%s:%d",
336 : resume?"resume":"ok", get_int(cconfs[OPT_COMPRESSION]));
337 1 : if(asfd->write_str(asfd, CMD_GEN, okstr)) return -1;
338 :
339 1 : if((ret=do_backup_server(as, sdirs, cconfs, incexc, resume)))
340 : goto end;
341 :
342 0 : if((ret=delete_backups(sdirs, cname,
343 : get_strlist(cconfs[OPT_KEEP]),
344 0 : get_string(cconfs[OPT_MANUAL_DELETE]))))
345 : goto end;
346 : end:
347 : return ret;
348 : }
|