strip hostname server side
[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 bak command overview:
19
20   bak add /path
21   bak commit [/path [message]]
22   bak diff [host:][/path]
23   bak status [/path]
24   bak log [/path]
25
26   bak ch[anges]
27   bak revert [host:]/path
28
29   bak - push all changed files to server
30
31 =cut
32
33 use warnings;
34 use strict;
35 use autodie;
36 use IO::Socket::INET;
37 use File::Path;
38 use Getopt::Long;
39
40 my $upgrade = 0;
41 my $install;
42
43 GetOptions(
44         'upgrade!'  => \$upgrade,
45         'install=s' => \$install,
46 ) || die "$!\n";
47
48 my ( $dir, $server_ip ) = @ARGV;
49 die "usage: $0 /backup/directory 127.0.0.1\n" unless $dir;
50 $server_ip ||= '127.0.0.1';
51
52 my $shell_client = <<__SHELL_CLIENT__;
53 #!/bin/sh
54 echo \$USER/\$SUDO_USER `hostname` `pwd` \$* | nc $server_ip 9001
55 __SHELL_CLIENT__
56
57 chdir $dir;
58 system 'git init' unless -e '.git';
59
60 if ( $upgrade || $install ) {
61         open(my $fh, '>', '/tmp/bak');
62         print $fh $shell_client;
63         close($fh);
64         chmod 0755, '/tmp/bak';
65
66         my @hosts = grep { -d $_ } glob '*';
67         @hosts = ( $install ) if $install;
68
69         foreach my $hostname ( @hosts ) {
70                 warn "install on $hostname\n";
71                 system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
72                 system "scp /tmp/bak root\@$hostname:/usr/local/bin/";
73                 system "ssh root\@$hostname apt-get install -y rsync";
74         }
75 }
76
77 my $server = IO::Socket::INET->new(
78         Proto     => 'tcp',
79         LocalAddr => $server_ip,
80         LocalPort => 9001,
81         Listen    => SOMAXCONN,
82         Reuse     => 1
83 ) || die $!;
84
85
86 warn "dir: $dir listen: $server_ip:9001\n"
87         , $shell_client
88 ;
89
90 sub pull_changes {
91         my $hostname = shift;
92         system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
93         if ( @_ ) {
94                 open(my $files, '>>', "/tmp/$hostname.list");
95                 print $files "$_\n" foreach @_;
96                 close($files);
97         }
98         system "rsync -avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/"
99 }
100
101 while (my $client = $server->accept()) {
102         my $line = <$client>;
103         chomp($line);
104         warn "<<< $line\n";
105         my ($user,$hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,6);
106         $hostname =~ s/\..+$//;
107
108         my $on_host = '';
109         if ( $rel_path =~ s/^([^:]+):(.+)$/$2/ ) {
110                 if ( -e $1 ) {
111                         $on_host = $1;
112                 } else {
113                         print $client "host $1 doesn't exist in backup\n";
114                         next;
115                 }
116         }
117         my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
118
119         warn "$hostname [$command] $on_host:$path | $message\n";
120
121         my $args_message = $message;
122
123         $message ||= "$path [$command]";
124         $message = "$hostname: $message";
125
126         my $dir = $path;
127         $dir =~ s{/[^/]+$}{};
128
129         my $backup_path = -e "$hostname/$path" ? "$hostname/$path" : $hostname;
130
131         sub git {
132                 my $args = join(' ',@_);
133                 warn "# git $args\n";
134                 my $out = `git $args`;
135                 warn "$out\n# [", length($out), " bytes]\n" if defined $out;
136                 return $out;
137         }
138
139         if ( ! $command ) {
140                 pull_changes $hostname;
141         } elsif ( $command eq 'add' ) {
142                 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
143                 while ( $path ) {
144                         system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path";
145                         print $client git 'add', "$hostname/$path";
146
147                         $args_message =~ s/^(.+)\b// || last;
148                         $path = $1;
149                         warn "? $path";
150                 }
151         } elsif ( $command eq 'commit' ) {
152                 pull_changes $hostname;
153                 $message =~ s/'/\\'/g;
154                 $user =~ s/\/$//;
155                 print $client git( "commit -m '$message' --author '$user <$hostname>' $backup_path" );
156         } elsif ( $command =~ m{(diff|status|log|ch)} ) {
157                 $command .= ' --stat' if $command eq 'log';
158                 $command = 'log --patch-with-stat' if $command =~ m/^ch/;
159                 pull_changes( $hostname ) if $command eq 'diff';
160                 if ( $on_host ) {
161                         system 'rsync', '-avv', "root\@$on_host:$path", "$on_host/$path";
162                         open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
163                         while(<$diff>) {
164                                 print $client $_;
165                         }
166                 } else {
167                         # commands without path will show host-wide status/changes
168                         my $backup_path = $rel_path ? $backup_path : "$hostname/";
169                         # hostname must end with / to prevent error from git:
170                         # ambiguous argument 'arh-hw': both revision and filename
171                         # to support branches named as hosts
172                         print $client git($command, $backup_path);
173                 }
174         } elsif ( $command eq 'revert' ) {
175                 if ( $on_host ) {
176                         system 'rsync', '-avv', "$on_host/$path", "root\@$hostname:$path";
177                 } else {
178                         print $client git "checkout -- $hostname/$path";
179                         system 'rsync', '-avv', "$hostname/$path", "root\@$hostname:$path";
180                 }
181         } else {
182                 print $client "Unknown command: $command\n";
183         }
184
185 }
186