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