re-use existing rsync on restart
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 15 Jul 2011 19:59:43 +0000 (19:59 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 15 Jul 2011 19:59:43 +0000 (19:59 +0000)
rsync-piper.pl

index fc4c560..59ad0ae 100755 (executable)
@@ -8,28 +8,20 @@ use File::Slurp;
 use IO::Select;
 use Time::HiRes;
 use Data::Dump qw(dump);
+use English;
 
 my $dir = '/srv/cloudstore/var';
 my $log_fifo = "$dir/rsyncd.log";
-our $pid_file = "$dir/rsyncd.pid";
+my $pid_file = "$dir/rsyncd.pid";
 my $cfg_file   = "$dir/rsyncd.conf";
 my $users    = "users";
 
 mkdir $dir if ! -e $dir;
 
-unlink $log_fifo if -e $log_fifo;
-mkfifo $log_fifo, 0700;
-
-sub kill_rsync {
-       return unless -e $pid_file;
-       my $pid = read_file $pid_file;
-       warn "kill_rsync $pid_file = $pid";
-       kill 2, $pid && warn "kill INT $pid old rsync server";
-       kill 0, $pid && unlink $pid_file && warn "removed $pid_file";
-}
+mkfifo $log_fifo, 0700 unless -p $log_fifo;
 
 my $transfer_log = {
-       ip => '%o',
+       ip => '%a',
        user => '%u',
        host => '%h',
        perms => '%B',
@@ -58,7 +50,7 @@ lock file = $dir/rsyncd.lock
 log file  = $log_fifo
 
 transfer logging = yes
-log format = TRANSFER } . join('|',values %$transfer_log) . qq{
+log format = transfer-log:} . join('|',values %$transfer_log) . qq{
 max verbosity = 5
 
 pid file  = $pid_file
@@ -80,22 +72,30 @@ post-xfer exec = /srv/cloudstore/post-xfer.sh
 write_file $cfg_file, $rsync_config;
 warn "created $cfg_file ", -s $cfg_file, " bytes\n";
 
-kill_rsync;
-sub DESTROY { kill_rsync; };
-
-my $exec = "rsync --daemon --config $cfg_file --no-detach --port=6501";
+if ( -e $pid_file ) {
+       my $pid = read_file $pid_file;
+       chomp($pid);
+       if ( kill 0, $pid ) {
+               warn "found rsync pid $pid";
+       } else {
+               unlink $pid_file;
+       }
+}
 
-#die "could not fork\n" unless defined(my $pid = fork);
-#unless ($pid) {
-#      warn "start server with $exec\n";
-#      exec $exec || die $!;
-#}
+if ( ! -e $pid_file ) {
+       my $exec = "rsync --daemon --config $cfg_file --no-detach --port=6501";
+       warn "START $exec\n";
 
-warn "START $exec\n";
-open(my $rsync_fd, '-|', $exec);
+       die "could not fork\n" unless defined(my $pid = fork);
+       unless ($pid) {
+               warn "start server with $exec\n";
+               exec $exec || die $!;
+       }
 
-while ( ! -e $pid_file ) {
-       sleep 1;
+       warn "wait for pid file";
+       while ( ! -e $pid_file ) {
+               sleep 1;
+       }
 }
 
 
@@ -105,9 +105,11 @@ while(1) {
        while( my $line = <$log> ) {
                print "LINE: $line";
                if ( $line =~ /transfer-log:(.+\|.+)/ ) {
-                       my (%data);
-                       %data{ keys %$transfer_log } = split(/\|/,$1); # FIXME validate?
-                       print dump(\%data);
+                       my %data;
+                       my @k = keys %$transfer_log;
+                       my @v = split(/\|/,$1);
+                       @data{@k} = @v ; # FIXME validate?
+                       print "transfer-log:",dump(\%data),$/;
                }
        }
        close($log);