0c1c336c2d6d832270f5079358abe9597a5a0e1a
[BackupPC.git] / lib / BackupPC / Xfer / Rsync.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::Xfer::Rsync package
4 #
5 # DESCRIPTION
6 #
7 #   This library defines a BackupPC::Xfer::Rsync class for managing
8 #   the rsync-based transport of backup data from the client.
9 #
10 # AUTHOR
11 #   Craig Barratt  <cbarratt@users.sourceforge.net>
12 #
13 # COPYRIGHT
14 #   Copyright (C) 2002-2007  Craig Barratt
15 #
16 #   This program is free software; you can redistribute it and/or modify
17 #   it under the terms of the GNU General Public License as published by
18 #   the Free Software Foundation; either version 2 of the License, or
19 #   (at your option) any later version.
20 #
21 #   This program is distributed in the hope that it will be useful,
22 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
23 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 #   GNU General Public License for more details.
25 #
26 #   You should have received a copy of the GNU General Public License
27 #   along with this program; if not, write to the Free Software
28 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 #
30 #========================================================================
31 #
32 # Version 3.2.0beta0, released 17 Jan 2009.
33 #
34 # See http://backuppc.sourceforge.net.
35 #
36 #========================================================================
37
38 package BackupPC::Xfer::Rsync;
39
40 use strict;
41 use BackupPC::View;
42 use BackupPC::Xfer::RsyncFileIO;
43 use Encode qw/from_to encode/;
44 use base qw(BackupPC::Xfer::Protocol);
45
46 use vars qw( $RsyncLibOK $RsyncLibErr );
47
48 BEGIN {
49     eval "use File::RsyncP;";
50     if ( $@ ) {
51         #
52         # Rsync module doesn't exist.
53         #
54         $RsyncLibOK = 0;
55         $RsyncLibErr = "File::RsyncP module doesn't exist";
56     } else {
57         #
58         # Note: also update configure.pl when this version number is changed!
59         #
60         if ( $File::RsyncP::VERSION < 0.68 ) {
61             $RsyncLibOK = 0;
62             $RsyncLibErr = "File::RsyncP module version"
63                          . " ($File::RsyncP::VERSION) too old: need >= 0.68";
64         } else {
65             $RsyncLibOK = 1;
66         }
67     }
68 };
69
70 sub new
71 {
72     my($class, $bpc, $args) = @_;
73
74     return if ( !$RsyncLibOK );
75     my $t = BackupPC::Xfer::Protocol->new($bpc, $args);
76     return bless($t, $class);
77 }
78
79 sub start
80 {
81     my($t) = @_;
82     my $bpc = $t->{bpc};
83     my $conf = $t->{conf};
84     my(@fileList, $rsyncClientCmd, $rsyncArgs, $logMsg,
85        $incrDate, $argList, $fioArgs);
86
87     #
88     # We add a slash to the share name we pass to rsync
89     #
90     ($t->{shareNameSlash} = "$t->{shareName}/") =~ s{//+$}{/};
91
92     if ( $t->{type} eq "restore" ) {
93         $rsyncClientCmd = $conf->{RsyncClientRestoreCmd};
94         $rsyncArgs = $conf->{RsyncRestoreArgs};
95         my $remoteDir = "$t->{shareName}/$t->{pathHdrDest}";
96         $remoteDir    =~ s{//+}{/}g;
97         from_to($remoteDir, "utf8", $conf->{ClientCharset})
98                                     if ( $conf->{ClientCharset} ne "" );
99         $argList = ['--server', @$rsyncArgs, '.', $remoteDir];
100         $fioArgs = {
101             client   => $t->{bkupSrcHost},
102             share    => $t->{bkupSrcShare},
103             viewNum  => $t->{bkupSrcNum},
104             fileList => $t->{fileList},
105         };
106         $logMsg = "restore started below directory $t->{shareName}"
107                 . " to host $t->{host}";
108     } else {
109         #
110         # Turn $conf->{BackupFilesOnly} and $conf->{BackupFilesExclude}
111         # into a hash of arrays of files, and $conf->{RsyncShareName}
112         # to an array
113         #
114         $bpc->backupFileConfFix($conf, "RsyncShareName");
115
116         if ( defined($conf->{BackupFilesOnly}{$t->{shareName}}) ) {
117             my(@inc, @exc, %incDone, %excDone);
118             foreach my $file ( @{$conf->{BackupFilesOnly}{$t->{shareName}}} ) {
119                 #
120                 # If the user wants to just include /home/craig, then
121                 # we need to do create include/exclude pairs at
122                 # each level:
123                 #     --include /home --exclude /*
124                 #     --include /home/craig --exclude /home/*
125                 #
126                 # It's more complex if the user wants to include multiple
127                 # deep paths.  For example, if they want /home/craig and
128                 # /var/log, then we need this mouthfull:
129                 #     --include /home --include /var --exclude /*
130                 #     --include /home/craig --exclude /home/*
131                 #     --include /var/log --exclude /var/*
132                 #
133                 # To make this easier we do all the includes first and all
134                 # of the excludes at the end (hopefully they commute).
135                 #
136                 $file =~ s{/$}{};
137                 $file = "/$file";
138                 $file =~ s{//+}{/}g;
139                 if ( $file eq "/" ) {
140                     #
141                     # This is a special case: if the user specifies
142                     # "/" then just include it and don't exclude "/*".
143                     #
144                     push(@inc, $file) if ( !$incDone{$file} );
145                     next;
146                 }
147                 my $f = "";
148                 while ( $file =~ m{^/([^/]*)(.*)} ) {
149                     my $elt = $1;
150                     $file = $2;
151                     if ( $file eq "/" ) {
152                         #
153                         # preserve a tailing slash
154                         #
155                         $file = "";
156                         $elt = "$elt/";
157                     }
158                     push(@exc, "$f/*") if ( !$excDone{"$f/*"} );
159                     $excDone{"$f/*"} = 1;
160                     $f = "$f/$elt";
161                     push(@inc, $f) if ( !$incDone{$f} );
162                     $incDone{$f} = 1;
163                 }
164             }
165             foreach my $file ( @inc ) {
166                 $file = encode($conf->{ClientCharset}, $file)
167                             if ( $conf->{ClientCharset} ne "" );
168                 push(@fileList, "--include=$file");
169             }
170             foreach my $file ( @exc ) {
171                 $file = encode($conf->{ClientCharset}, $file)
172                             if ( $conf->{ClientCharset} ne "" );
173                 push(@fileList, "--exclude=$file");
174             }
175         }
176         if ( defined($conf->{BackupFilesExclude}{$t->{shareName}}) ) {
177             foreach my $file ( @{$conf->{BackupFilesExclude}{$t->{shareName}}} )
178             {
179                 #
180                 # just append additional exclude lists onto the end
181                 #
182                 $file = encode($conf->{ClientCharset}, $file)
183                             if ( $conf->{ClientCharset} ne "" );
184                 push(@fileList, "--exclude=$file");
185             }
186         }
187         if ( $t->{type} eq "full" ) {
188             if ( $t->{partialNum} ) {
189                 $logMsg = "full backup started for directory $t->{shareName};"
190                         . " updating partial #$t->{partialNum}";
191             } else {
192                 $logMsg = "full backup started for directory $t->{shareName}";
193                 if ( $t->{incrBaseBkupNum} ne "" ) {
194                     $logMsg .= " (baseline backup #$t->{incrBaseBkupNum})";
195                 }
196             }
197         } else {
198             $incrDate = $bpc->timeStamp($t->{incrBaseTime}, 1);
199             $logMsg = "incr backup started back to $incrDate"
200                     . " (backup #$t->{incrBaseBkupNum}) for directory"
201                     . " $t->{shareName}";
202         }
203         
204         #
205         # A full dump is implemented with --ignore-times: this causes all
206         # files to be checksummed, even if the attributes are the same.
207         # That way all the file contents are checked, but you get all
208         # the efficiencies of rsync: only files deltas need to be
209         # transferred, even though it is a full dump.
210         #
211         $rsyncArgs = $conf->{RsyncArgs};
212         $rsyncArgs = [@$rsyncArgs, @fileList] if ( @fileList );
213         $rsyncArgs = [@$rsyncArgs, "--ignore-times"]
214                                     if ( $t->{type} eq "full" );
215         $rsyncClientCmd = $conf->{RsyncClientCmd};
216         my $shareNameSlash = $t->{shareNameSlash};
217         from_to($shareNameSlash, "utf8", $conf->{ClientCharset})
218                             if ( $conf->{ClientCharset} ne "" );
219         $argList = ['--server', '--sender', @$rsyncArgs,
220                               '.', $shareNameSlash];
221         eval {
222             $argList = File::RsyncP->excludeStrip($argList);
223         };
224         $fioArgs = {
225             client     => $t->{client},
226             share      => $t->{shareName},
227             viewNum    => $t->{incrBaseBkupNum},
228             partialNum => $t->{partialNum},
229         };
230     }
231
232     #
233     # Merge variables into $rsyncClientCmd
234     #
235     my $args = {
236         host      => $t->{host},
237         hostIP    => $t->{hostIP},
238         client    => $t->{client},
239         shareName => $t->{shareName},
240         shareNameSlash => $t->{shareNameSlash},
241         rsyncPath => $conf->{RsyncClientPath},
242         sshPath   => $conf->{SshPath},
243         argList   => $argList,
244     };
245     from_to($args->{shareName}, "utf8", $conf->{ClientCharset})
246                             if ( $conf->{ClientCharset} ne "" );
247     from_to($args->{shareNameSlash}, "utf8", $conf->{ClientCharset})
248                             if ( $conf->{ClientCharset} ne "" );
249     $rsyncClientCmd = $bpc->cmdVarSubstitute($rsyncClientCmd, $args);
250
251     #
252     # Create the Rsync object, and tell it to use our own File::RsyncP::FileIO
253     # module, which handles all the special BackupPC file storage
254     # (compression, mangling, hardlinks, special files, attributes etc).
255     #
256     $t->{rsyncClientCmd} = $rsyncClientCmd;
257     $t->{rs} = File::RsyncP->new({
258         logLevel     => $t->{logLevel} || $conf->{RsyncLogLevel},
259         rsyncCmd     => sub {
260                             $bpc->verbose(0);
261                             $bpc->cmdExecOrEval($rsyncClientCmd, $args);
262                         },
263         rsyncCmdType => "full",
264         rsyncArgs    => $rsyncArgs,
265         timeout      => $conf->{ClientTimeout},
266         doPartial    => defined($t->{partialNum}) ? 1 : undef,
267         logHandler   =>
268                 sub {
269                     my($str) = @_;
270                     $str .= "\n";
271                     $t->{XferLOG}->write(\$str);
272                     if ( $str =~ /^Remote\[1\]: read errors mapping "(.*)"/ ) {
273                         #
274                         # Files with read errors (eg: region locked files
275                         # on WinXX) are filled with 0 by rsync.  Remember
276                         # them and delete them later.
277                         #
278                         my $badFile = $1;
279                         $badFile =~ s/^\/+//;
280                         push(@{$t->{badFiles}}, {
281                                 share => $t->{shareName},
282                                 file  => $badFile
283                             });
284                     }
285                 },
286         pidHandler   => sub {
287                             $t->{pidHandler}(@_);
288                         },
289         completionPercent => sub {
290                             $t->{completionPercent}(@_);
291                         },
292         clientCharset => $conf->{ClientCharset},
293         fio          => BackupPC::Xfer::RsyncFileIO->new({
294                             xfer       => $t,
295                             bpc        => $t->{bpc},
296                             conf       => $t->{conf},
297                             backups    => $t->{backups},
298                             logLevel   => $t->{logLevel}
299                                               || $conf->{RsyncLogLevel},
300                             logHandler => sub {
301                                               my($str) = @_;
302                                               $str .= "\n";
303                                               $t->{XferLOG}->write(\$str);
304                                           },
305                             cacheCheckProb => $conf->{RsyncCsumCacheVerifyProb},
306                             clientCharset  => $conf->{ClientCharset},
307                             %$fioArgs,
308                       }),
309     });
310
311     delete($t->{_errStr});
312
313     return $logMsg;
314 }
315
316 sub run
317 {
318     my($t) = @_;
319     my $rs = $t->{rs};
320     my $conf = $t->{conf};
321     my($remoteSend, $remoteDir, $remoteDirDaemon);
322
323     alarm($conf->{ClientTimeout});
324     if ( $t->{type} eq "restore" ) {
325         $remoteSend       = 0;
326         ($remoteDir       = "$t->{shareName}/$t->{pathHdrDest}") =~ s{//+}{/}g;
327         ($remoteDirDaemon = "$t->{shareName}/$t->{pathHdrDest}") =~ s{//+}{/}g;
328         $remoteDirDaemon  = $t->{shareNameSlash}
329                                 if ( $t->{pathHdrDest} eq ""
330                                               || $t->{pathHdrDest} eq "/" );
331     } else {
332         $remoteSend      = 1;
333         $remoteDir       = $t->{shareNameSlash};
334         $remoteDirDaemon = ".";
335     }
336     from_to($remoteDir, "utf8", $conf->{ClientCharset})
337                                 if ( $conf->{ClientCharset} ne "" );
338     from_to($remoteDirDaemon, "utf8", $conf->{ClientCharset})
339                                 if ( $conf->{ClientCharset} ne "" );
340
341     if ( $t->{XferMethod} eq "rsync" ) {
342         #
343         # Run rsync command
344         #
345         my $str = "Running: "
346                 . $t->{bpc}->execCmd2ShellCmd(@{$t->{rsyncClientCmd}})
347                 . "\n";
348         from_to($str, $conf->{ClientCharset}, "utf8")
349                                 if ( $conf->{ClientCharset} ne "" );
350         $t->{XferLOG}->write(\$str);
351         $rs->remoteStart($remoteSend, $remoteDir);
352     } else {
353         #
354         # Connect to the rsync server
355         #
356         if ( defined(my $err = $rs->serverConnect($t->{hostIP},
357                                              $conf->{RsyncdClientPort})) ) {
358             $t->{hostError} = $err;
359             my $str = "Error connecting to rsync daemon at $t->{hostIP}"
360                     . ":$conf->{RsyncdClientPort}: $err\n";
361             $t->{XferLOG}->write(\$str);
362             return;
363         }
364         #
365         # Pass module name, and follow it with a slash if it already
366         # contains a slash; otherwise just keep the plain module name.
367         #
368         my $module = $t->{shareName};
369         $module = $t->{shareNameSlash} if ( $module =~ /\// );
370         from_to($module, "utf8", $conf->{ClientCharset})
371                                     if ( $conf->{ClientCharset} ne "" );
372         if ( defined(my $err = $rs->serverService($module,
373                                              $conf->{RsyncdUserName},
374                                              $conf->{RsyncdPasswd},
375                                              $conf->{RsyncdAuthRequired})) ) {
376             my $str = "Error connecting to module $module at $t->{hostIP}"
377                     . ":$conf->{RsyncdClientPort}: $err\n";
378             $t->{XferLOG}->write(\$str);
379             $t->{hostError} = $err;
380             return;
381         }
382         
383         #
384         # This is a hack.  To avoid wide chars we encode the arguments
385         # to utf8 byte streams, then to the client's local charset.
386         # The second conversion should really go in File::RsyncP, since
387         # it shouldn't be applied to in-line include/exclude arguments.
388         #
389         for ( my $i = 0 ; $i < @{$rs->{rsyncArgs}} ; $i++ ) {
390             $rs->{rsyncArgs}[$i] = encode('utf8', $rs->{rsyncArgs}[$i]);
391             from_to($rs->{rsyncArgs}[$i], 'utf8', $conf->{ClientCharset})
392                                     if ( $conf->{ClientCharset} ne "" );
393         }
394     
395         my $str = "RsyncArgsBefore: " . join(" ", @{$rs->{rsyncArgs}}) . "\n";
396         $t->{XferLOG}->write(\$str);
397
398         $rs->serverStart($remoteSend, $remoteDirDaemon);
399
400         my $str = "RsyncArgsAfter: " . join(" ", @{$rs->{rsyncArgs}}) . "\n";
401         $t->{XferLOG}->write(\$str);
402     }
403     my $shareNameSlash = $t->{shareNameSlash};
404     from_to($shareNameSlash, "utf8", $conf->{ClientCharset})
405                                 if ( $conf->{ClientCharset} ne "" );
406     my $error = $rs->go($shareNameSlash);
407     $rs->serverClose();
408
409     #
410     # TODO: generate sensible stats
411     # 
412     # $rs->{stats}{totalWritten}
413     # $rs->{stats}{totalSize}
414     #
415     my $stats = $rs->statsFinal;
416     if ( !defined($error) && defined($stats) ) {
417         $t->{xferOK} = 1;
418     } else {
419         $t->{xferOK} = 0;
420     }
421     $t->{xferErrCnt} = $stats->{remoteErrCnt}
422                      + $stats->{childStats}{errorCnt}
423                      + $stats->{parentStats}{errorCnt};
424     $t->{byteCnt}    = $stats->{childStats}{TotalFileSize}
425                      + $stats->{parentStats}{TotalFileSize};
426     $t->{fileCnt}    = $stats->{childStats}{TotalFileCnt}
427                      + $stats->{parentStats}{TotalFileCnt};
428     my $str = "Done: $t->{fileCnt} files, $t->{byteCnt} bytes\n";
429     $t->{XferLOG}->write(\$str);
430     #
431     # TODO: get error count, and call fio to get stats...
432     #
433     $t->{hostError} = $error if ( defined($error) );
434
435     if ( $t->{type} eq "restore" ) {
436         return (
437             $t->{fileCnt},
438             $t->{byteCnt},
439             0,
440             0
441         );
442     } else {
443         return (
444             0,
445             $stats->{childStats}{ExistFileCnt}
446                 + $stats->{parentStats}{ExistFileCnt},
447             $stats->{childStats}{ExistFileSize}
448                 + $stats->{parentStats}{ExistFileSize},
449             $stats->{childStats}{ExistFileCompSize}
450                 + $stats->{parentStats}{ExistFileCompSize},
451             $stats->{childStats}{TotalFileCnt}
452                 + $stats->{parentStats}{TotalFileCnt},
453             $stats->{childStats}{TotalFileSize}
454                 + $stats->{parentStats}{TotalFileSize},
455         );
456     }
457 }
458
459 sub abort
460 {
461     my($t, $reason) = @_;
462     my $rs = $t->{rs};
463
464     $rs->abort($reason);
465     return 1;
466 }
467
468 sub errStr
469 {
470     my($t) = @_;
471
472     return $RsyncLibErr if ( !defined($t) || ref($t) ne "HASH" );
473     return $t->{_errStr};
474 }
475
476 sub xferPid
477 {
478     my($t) = @_;
479
480     return ();
481 }
482
483 1;