simple listener which store files in git
[bak-git.git] / server.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use autodie;
6 use IO::Socket::INET;
7 use File::Path;
8
9 my $dir = '../backup';
10
11 chdir $dir;
12 system 'git init' unless -e '.git';
13
14 my $server = IO::Socket::INET->new(
15         Proto     => 'tcp',
16         LocalPort => 9001,
17         Listen    => SOMAXCONN,
18         Reuse     => 1
19 );
20
21 while (my $client = $server->accept()) {
22         my ($command,$path,$message) = split(/\s+/,<$client>,3);
23         my $ip = $client->peerhost;
24
25         warn "$ip [$command] $path | $message\n";
26
27         if ( ! -d $ip ) {
28                 system 'ssh-copy-id', "root\@$ip";
29         }
30
31
32         my $dir = $path;
33         $dir =~ s{/[^/]+$}{};
34
35         mkpath "$ip/$dir" unless -e "$ip/$dir";
36
37         if ( $command eq 'add' ) {
38                 warn 'rsync', "root\@$ip:$path", "$ip/$path";
39                 system 'rsync', "root\@$ip:$path", "$ip/$path";
40                 system 'git', 'add', "$ip/$path";
41         } else {
42                 system 'rsync', "root\@$ip:$path", "$ip/$path";
43         }
44
45         $message ||= "$command $ip $path";
46         system 'git', 'commit', '-m', $message, "$ip/$path";
47 }
48