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