better output, fixed install
[bak-git.git] / server.pl
index b845463..b6194e2 100755 (executable)
--- a/server.pl
+++ b/server.pl
@@ -7,7 +7,15 @@ only depenency) to ad-hoc remote server
 
 Install on client with:
 
-  echo install | nc 10.60.0.92 9001 > bak ; chmod 755 bak
+  echo install | nc 127.0.0.1 9001 > bak ; chmod 755 bak
+
+Start server with:
+
+  ./server.pl /path/to/backup 127.0.0.1
+
+You will want to add following to C<~/.ssh/config>
+
+  RemoteForward 9001 localhost:9001
 
 =cut
 
@@ -17,29 +25,46 @@ use autodie;
 use IO::Socket::INET;
 use File::Path;
 
-my $dir = shift @ARGV || die "usage: $0 /backup/directory\n";
+my ( $dir, $server_ip ) = @ARGV;
+die "usage: $0 /backup/directory\n" unless $dir;
+$server_ip ||= '127.0.0.1';
+
+my $shell_client = <<__SHELL_CLIENT__;
+#!/bin/sh
+echo `pwd` \$* | nc $server_ip 9001
+__SHELL_CLIENT__
 
 chdir $dir;
 system 'git init' unless -e '.git';
 
 my $server = IO::Socket::INET->new(
        Proto     => 'tcp',
+       LocalAddr => $server_ip,
        LocalPort => 9001,
        Listen    => SOMAXCONN,
        Reuse     => 1
 ) || die $!;
 
+
+warn "dir: $dir listen: $server_ip:9001\n"
+       , "remote-host> echo install | nc $server_ip 9001 > bak ; chmod 755 bak\n"
+       , $shell_client
+;
+
 while (my $client = $server->accept()) {
-       my ($command,$path,$message) = split(/\s+/,<$client>,3);
+       my ($pwd,$command,$path,$message) = split(/\s+/,<$client>,4);
        my $ip = $client->peerhost;
 
-       $message ||= '';
-
-       if ( $command eq 'install' ) {
-               print $client '#!/bin/sh',$/,'echo $* | nc 10.60.0.92 9001',$/;
+       if ( $pwd eq 'install' ) {
+               warn "install on $ip\n";
+               print $client $shell_client;
+               close($client);
                next;
        }
 
+       $message ||= '';
+       $path = "$pwd/$path" unless $path =~ m{^/};
+
        warn "$ip [$command] $path | $message\n";
 
        if ( ! -d $ip ) {
@@ -52,14 +77,20 @@ while (my $client = $server->accept()) {
 
        mkpath "$ip/$dir" unless -e "$ip/$dir";
 
-       if ( $command eq 'add' ) {
-               warn 'rsync', "root\@$ip:$path", "$ip/$path";
-               system 'rsync', "root\@$ip:$path", "$ip/$path";
+       if ( ! $command ) {
+               system "find $ip -type f | sed 's,$ip,,' > /tmp/$ip.list";
+               system "rsync -avv --files-from /tmp/$ip.list root\@$ip:/ $ip/"
+       } elsif ( $command eq 'add' ) {
+               system 'rsync', '-avv', "root\@$ip:$path", "$ip/$path";
                system 'git', 'add', "$ip/$path";
-       } else {
-               system 'rsync', "root\@$ip:$path", "$ip/$path";
+       } elsif ( $command eq 'commit' ) {
+               system 'rsync', '-avv', "root\@$ip:$path", "$ip/$path";
                $message ||= "$command $ip $path";
                system 'git', 'commit', '-m', $message, "$ip/$path";
+       } elsif ( $command =~ m{(diff|status|log)} ) {
+               print $client `git $command $ip`;
+       } else {
+               print $client "Unknown command: $command\n";
        }
 
 }