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