pull_changes before diff
[bak-git.git] / bak-git-server.pl
1 #!/usr/bin/perl
2
3 =head1 bak-git
4
5 Simple tracking of remote files in central git repository
6 with only shell, netcat, rsync and ssh on client
7
8 Start server, install on remote-host or upgrade with:
9
10   ./bak-git-server.pl /path/to/backup 192.168.42.42
11         [--install remote-host]
12         [--upgrade]
13
14 You will want to add following to C<~/.ssh/config>
15
16   RemoteForward 9001 localhost:9001
17
18 =cut
19
20 use warnings;
21 use strict;
22 use autodie;
23 use IO::Socket::INET;
24 use File::Path;
25 use Getopt::Long;
26
27 my $upgrade = 0;
28 my $install;
29
30 GetOptions(
31         'upgrade!'  => \$upgrade,
32         'install=s' => \$install,
33 ) || die "$!\n";
34
35 my ( $dir, $server_ip ) = @ARGV;
36 die "usage: $0 /backup/directory\n" unless $dir;
37 $server_ip ||= '127.0.0.1';
38
39 my $shell_client = <<__SHELL_CLIENT__;
40 #!/bin/sh
41 echo `hostname -s` `pwd` \$* | nc $server_ip 9001
42 __SHELL_CLIENT__
43
44 chdir $dir;
45 system 'git init' unless -e '.git';
46
47 if ( $upgrade || $install ) {
48         open(my $fh, '>', '/tmp/bak');
49         print $fh $shell_client;
50         close($fh);
51         chmod 0755, '/tmp/bak';
52
53         my @hosts = grep { -d $_ } glob '*';
54         @hosts = ( $install ) if $install;
55
56         foreach my $hostname ( @hosts ) {
57                 warn "install on $hostname\n";
58                 system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
59                 system "scp /tmp/bak root\@$hostname:/usr/local/bin/";
60                 system "ssh root\@$hostname apt-get install rsync";
61         }
62 }
63
64 my $server = IO::Socket::INET->new(
65         Proto     => 'tcp',
66         LocalAddr => $server_ip,
67         LocalPort => 9001,
68         Listen    => SOMAXCONN,
69         Reuse     => 1
70 ) || die $!;
71
72
73 warn "dir: $dir listen: $server_ip:9001\n"
74         , $shell_client
75 ;
76
77 sub pull_changes {
78         my $hostname = shift;
79         system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
80         system "rsync -avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/"
81 }
82
83 while (my $client = $server->accept()) {
84         my ($hostname,$pwd,$command,$path,$message) = split(/\s+/,<$client>,5);
85
86         $message ||= '';
87         $path = "$pwd/$path" unless $path =~ m{^/};
88
89         warn "$hostname [$command] $path | $message\n";
90
91
92         my $dir = $path;
93         $dir =~ s{/[^/]+$}{};
94
95         mkpath "$hostname/$dir" unless -e "$hostname/$dir";
96
97         if ( ! $command ) {
98                 pull_changes $hostname;
99         } elsif ( $command eq 'add' ) {
100                 system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path";
101                 system 'git', 'add', "$hostname/$path";
102         } elsif ( $command eq 'commit' ) {
103                 system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path" if $path;
104                 $message ||= "$command $hostname $path";
105                 system 'git', 'commit', '-m', $message, "$hostname/$path";
106         } elsif ( $command =~ m{(diff|status|log)} ) {
107                 my $opt = '--summary' if $command eq 'log';
108                 pull_changes $hostname if $command eq 'diff';
109                 print $client `git $command $opt $hostname`;
110         } elsif ( $command eq 'revert' ) {
111                 print $client `git checkout -- $hostname/$path`;
112                 system 'rsync', '-avv', "$hostname/$path", "root\@$hostname:$path";
113         } else {
114                 print $client "Unknown command: $command\n";
115         }
116
117 }
118