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/;
85 my ( $hostname ) = @_;
86 my $path = '/tmp/bak';
87 my $server = $server_ip;
88 $server = '127.0.0.1' if $ssh_tunnel->{$hostname};
89 warn "# ssh_client $hostname $server";
90 open(my $fh, '>', $path);
91 print $fh "#!/bin/sh\n";
92 print $fh "echo \$USER/\$SUDO_USER $hostname `pwd` \$* | nc $server 9001\n";
99 while ( my($host,$pid) = each %$ssh_tunnel ) {
100 warn "$host kill TERM $pid";
101 eval { kill 15, $pid; } # TERM
106 $SIG{TERM} = &_kill_ssh;
109 system 'git init' unless -e '.git';
111 if ( $upgrade || $install ) {
113 my @hosts = grep { -d $_ } glob '*';
114 @hosts = ( $install ) if $install;
116 foreach my $hostname ( @hosts ) {
117 warn "install on $hostname\n";
118 system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
119 my $path = shell_client( $hostname );
120 system "scp $path root\@$hostname:/usr/local/bin/";
121 system "ssh root\@$hostname apt-get install -y netcat rsync";
124 my $ssh = $ENV{SSH} || 'ssh';
125 warn "# start $ssh tunnels...";
126 foreach my $host ( keys %$ssh_tunnel ) {
127 last; # FIXME disabled
130 if ( ! defined $pid ) {
134 warn "FIXME: waitpid $pid";
136 warn "EXEC $ssh $host";
137 exec "$ssh -N root\@$host";
140 $ssh_tunnel->{$host} = $pid;
144 warn "dir: $dir listen: $server_ip:9001\n";
146 my $server = IO::Socket::INET->new(
148 # LocalAddr => $server_ip,
156 warn "# rsync ",join(' ', @_), "\n";
161 my $hostname = shift;
162 system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
164 open(my $files, '>>', "/tmp/$hostname.list");
165 print $files "$_\n" foreach @_;
168 rsync split / /, "-avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/";
173 $path =~ s{/[^/]+$}{};
174 warn "# mkpath $path\n";
175 mkpath $path || die $!;
178 while (my $client = $server->accept()) {
179 my $line = <$client>;
182 my ($user,$hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,6);
183 $hostname =~ s/\..+$//;
186 if ( $rel_path =~ s/^([^:]+):(.+)$/$2/ ) {
190 print $client "host $1 doesn't exist in backup\n";
194 my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
196 foreach my $command ( split /,/, $command ) { # XXX command loop
198 warn "$hostname [$command] $on_host:$path | $message\n";
200 my $args_message = $message;
202 $message ||= "$path [$command]";
203 $message = "$hostname: $message";
206 $dir =~ s{/[^/]+$}{};
208 my $backup_path = -e "$hostname/$path" ? "$hostname/$path" : $hostname;
211 my $args = join(' ',@_);
212 warn "# git $args\n";
213 my $out = `git $args`;
214 warn "$out\n# [", length($out), " bytes]\n" if defined $out;
219 pull_changes $hostname;
220 } elsif ( $command eq 'add' ) {
221 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
223 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
224 print $client git 'add', "$hostname/$path";
226 $args_message =~ s/^(.+)\b// || last;
230 } elsif ( $command eq 'commit' ) {
231 pull_changes $hostname;
232 $message =~ s/'/\\'/g;
234 print $client git( "commit -m '$message' --author '$user <$hostname>' $backup_path" );
235 } elsif ( $command =~ m{(diff|status|log|ch)} ) {
236 $command .= ' --stat' if $command eq 'log';
237 $command = 'log --patch-with-stat' if $command =~ m/^ch/;
238 pull_changes( $hostname ) if $command eq 'diff';
240 mkpath $_ foreach grep { ! -e $_ } ( "$hostname/$dir", "$on_host/$dir" );
241 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
242 rsync( '-avv', "root\@$on_host:$path", "$on_host/$path" );
243 open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
248 # commands without path will show host-wide status/changes
249 my $backup_path = $path ? "$hostname/$path" : "$hostname/";
250 # hostname must end with / to prevent error from git:
251 # ambiguous argument 'arh-hw': both revision and filename
252 # to support branches named as hosts
253 print $client git($command, $backup_path);
255 } elsif ( $command eq 'revert' ) {
257 rsync( '-avv', "$on_host/$path", "root\@$hostname:$path" );
259 print $client git "checkout -- $hostname/$path";
260 rsync( '-avv', "$hostname/$path", "root\@$hostname:$path" );
262 } elsif ( $command eq 'cat' ) {
263 my $file_path = ( $on_host ? $on_host : $hostname ) . "/$path";
264 if ( -r $file_path ) {
265 open(my $file, '<', $file_path) || warn "ERROR $file_path: $!";
271 print $client "ERROR: $file_path: $!\n";
273 } elsif ( $command eq 'ls' ) {
274 my $file_path = ( $on_host ? $on_host : $hostname ) . "/$path";
275 print $client `ls $file_path 2>&1`;
276 } elsif ( $command eq 'show' ) {
277 print $client `git show $rel_path`;
278 } elsif ( $command eq 'grep' ) {
279 print $client `git log -g --grep=$rel_path`;
280 } elsif ( $command eq 'find' ) {
281 print $client `find . -iname '*$rel_path*' | sed -e 's,^./,,' -e 's,/,:/,'`
282 } elsif ( $command eq 'link' ) {
284 mkbasedir "$on_host/$path";
285 rsync( '-avv', "root\@$on_host:$path", "$on_host/$path" );
286 mkbasedir "$hostname/$path";
287 link "$on_host/$path", "$hostname/$path";
288 rsync( '-avv', "$hostname/$path", "root\@$hostname:$path" );
290 print $client "ERROR: link requires host:/path\n";
293 print $client "ERROR: unknown command: $command\n";
296 } # XXX command, loop