simple listener which store files in git
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 17 Feb 2010 19:34:06 +0000 (20:34 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 17 Feb 2010 19:34:06 +0000 (20:34 +0100)
server.pl [new file with mode: 0755]

diff --git a/server.pl b/server.pl
new file mode 100755 (executable)
index 0000000..bf8e56d
--- /dev/null
+++ b/server.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use autodie;
+use IO::Socket::INET;
+use File::Path;
+
+my $dir = '../backup';
+
+chdir $dir;
+system 'git init' unless -e '.git';
+
+my $server = IO::Socket::INET->new(
+       Proto     => 'tcp',
+       LocalPort => 9001,
+       Listen    => SOMAXCONN,
+       Reuse     => 1
+);
+
+while (my $client = $server->accept()) {
+       my ($command,$path,$message) = split(/\s+/,<$client>,3);
+       my $ip = $client->peerhost;
+
+       warn "$ip [$command] $path | $message\n";
+
+       if ( ! -d $ip ) {
+               system 'ssh-copy-id', "root\@$ip";
+       }
+
+
+       my $dir = $path;
+       $dir =~ s{/[^/]+$}{};
+
+       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";
+               system 'git', 'add', "$ip/$path";
+       } else {
+               system 'rsync', "root\@$ip:$path", "$ip/$path";
+       }
+
+       $message ||= "$command $ip $path";
+       system 'git', 'commit', '-m', $message, "$ip/$path";
+}
+