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