5 Simple tracking of remote files in central git repository
6 with only shell, netcat, rsync and ssh on client
8 Start server, install on remote-host or upgrade with:
10 ./bak-git-server.pl /path/to/backup 192.168.42.42
11 [--install remote-host]
14 C<rsync> traffic is always transfered over ssh, but C<diff> or C<ch> can
15 still leak sensitive information if C<bak> shell client connects directly
18 Add following line to C<~/.ssh/config> under C<Host> for which you want encrypted
19 controll channel (or to pass through server ssh hops using C<ProxyCommand>)
21 RemoteForward 9001 192.168.42.42:9001
23 bak command, overview:
26 bak commit [/path [message]]
27 bak diff [host:][/path]
33 bak revert [host:]/path
37 bak find filename-pattern
39 bak - push all changed files to server
43 See L<http://blog.rot13.org/bak-git> for more information
58 'upgrade!' => \$upgrade,
59 'install=s' => \$install,
62 my ( $dir, $server_ip ) = @ARGV;
63 die "usage: $0 /backup/directory 127.0.0.1\n" unless $dir;
64 $server_ip ||= '127.0.0.1';
68 open(my $ssh_fd, '<', "$ENV{HOME}/.ssh/config");
72 next unless length($_) > 0;
75 if ( /^Host\s+(.+)/i ) {
77 } elsif ( /^\s+(\S+)\s+(.+)/ ) {
78 $ssh_tunnel->{$host}++ if lc($1) eq 'remoteforward' && $2 =~ m/9001/;
82 warn "can't parse [$_]";
87 my ( $hostname ) = @_;
88 my $path = '/tmp/bak';
89 my $server = $server_ip;
90 $server = '127.0.0.1' if $ssh_tunnel->{$hostname};
91 warn "# ssh_client $hostname $server";
92 open(my $fh, '>', $path);
93 print $fh "#!/bin/sh\n";
94 print $fh "echo \$USER/\$SUDO_USER $hostname `pwd` \$* | nc $server 9001\n";
101 while ( my($host,$pid) = each %$ssh_tunnel ) {
102 warn "$host kill TERM $pid";
103 eval { kill 15, $pid; } # TERM
108 $SIG{TERM} = &_kill_ssh;
111 system 'git init' unless -e '.git';
113 if ( $upgrade || $install ) {
115 my @hosts = grep { -d $_ } glob '*';
116 @hosts = ( $install ) if $install;
118 foreach my $hostname ( @hosts ) {
119 warn "install on $hostname\n";
120 system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
121 my $path = shell_client( $hostname );
122 system "scp $path root\@$hostname:/usr/local/bin/";
123 system "ssh root\@$hostname apt-get install -y netcat rsync";
126 my $ssh = $ENV{SSH} || 'ssh';
127 warn "# start $ssh tunnels...";
128 foreach my $host ( keys %$ssh_tunnel ) {
129 last; # FIXME disabled
132 if ( ! defined $pid ) {
136 warn "FIXME: waitpid $pid";
138 warn "EXEC $ssh $host";
139 exec "$ssh -N root\@$host";
142 $ssh_tunnel->{$host} = $pid;
146 warn "dir: $dir listen: $server_ip:9001\n";
148 my $server = IO::Socket::INET->new(
150 # LocalAddr => $server_ip,
158 warn "# rsync ",join(' ', @_), "\n";
163 my $hostname = shift;
164 system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
166 open(my $files, '>>', "/tmp/$hostname.list");
167 print $files "$_\n" foreach @_;
170 rsync split / /, "-avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/";
175 $path =~ s{/[^/]+$}{};
176 warn "# mkpath $path\n";
177 mkpath $path || die $!;
180 while (my $client = $server->accept()) {
181 my $line = <$client>;
184 my $peerhost = $client->peerhost;
185 if ( $peerhost !~ m/^(10\.13\.37\.|10\.60\.0\.)/ ) {
186 print $client "$peerhost not allowed\n";
190 warn "<<< $peerhost $line\n";
191 my ($user,$hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,6);
192 $hostname =~ s/\..+$//;
195 if ( $rel_path =~ s/^([^:]+):(.+)$/$2/ ) {
199 print $client "host $1 doesn't exist in backup\n";
203 my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
205 foreach my $command ( split /,/, $command ) { # XXX command loop
207 warn "$hostname [$command] $on_host:$path | $message\n";
209 my $args_message = $message;
211 $message ||= "$path [$command]";
212 $message = "$hostname: $message";
215 $dir =~ s{/[^/]+$}{};
217 my $backup_path = -e "$hostname/$path" ? "$hostname/$path" : $hostname;
220 my $args = join(' ',@_);
221 warn "# git $args\n";
222 my $out = `git $args`;
223 warn "$out\n# [", length($out), " bytes]\n" if defined $out;
228 pull_changes $hostname;
229 } elsif ( $command eq 'add' ) {
230 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
232 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
233 print $client git 'add', "$hostname/$path";
235 $args_message =~ s/^(.+)\b// || last;
239 } elsif ( $command eq 'commit' ) {
240 pull_changes $hostname;
241 $message =~ s/'/\\'/g;
243 print $client git( "commit -m '$message' --author '$user <$hostname>' $backup_path" );
244 } elsif ( $command =~ m{(diff|status|log|ch)} ) {
245 $command .= ' --stat' if $command eq 'log';
246 $command = 'log --patch-with-stat' if $command =~ m/^ch/;
247 pull_changes( $hostname ) if $command eq 'diff';
249 mkpath $_ foreach grep { ! -e $_ } ( "$hostname/$dir", "$on_host/$dir" );
250 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
251 rsync( '-avv', "root\@$on_host:$path", "$on_host/$path" );
252 open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
257 # commands without path will show host-wide status/changes
258 my $backup_path = $path ? "$hostname/$path" : "$hostname/";
259 # hostname must end with / to prevent error from git:
260 # ambiguous argument 'arh-hw': both revision and filename
261 # to support branches named as hosts
262 print $client git($command, $backup_path);
264 } elsif ( $command eq 'revert' ) {
266 rsync( '-avv', "$on_host/$path", "root\@$hostname:$path" );
268 print $client git "checkout -- $hostname/$path";
269 rsync( '-avv', "$hostname/$path", "root\@$hostname:$path" );
271 } elsif ( $command eq 'cat' ) {
272 my $file_path = ( $on_host ? $on_host : $hostname ) . "/$path";
273 if ( -r $file_path ) {
274 open(my $file, '<', $file_path) || warn "ERROR $file_path: $!";
280 print $client "ERROR: $file_path: $!\n";
282 } elsif ( $command eq 'ls' ) {
283 my $file_path = ( $on_host ? $on_host : $hostname ) . "/$path";
284 print $client `ls $file_path 2>&1`;
285 } elsif ( $command eq 'show' ) {
286 print $client `git show $rel_path`;
287 } elsif ( $command eq 'grep' ) {
288 print $client `git log -g --grep=$rel_path`;
289 } elsif ( $command eq 'find' ) {
290 print $client `find . -iname '*$rel_path*' | sed -e 's,^./,,' -e 's,/,:/,'`
291 } elsif ( $command eq 'link' ) {
293 mkbasedir "$on_host/$path";
294 rsync( '-avv', "root\@$on_host:$path", "$on_host/$path" );
295 mkbasedir "$hostname/$path";
296 link "$on_host/$path", "$hostname/$path";
297 rsync( '-avv', "$hostname/$path", "root\@$hostname:$path" );
299 print $client "ERROR: link requires host:/path\n";
302 print $client "ERROR: unknown command: $command\n";
305 } # XXX command, loop