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
38 bak - push all changed files to server
42 See L<http://blog.rot13.org/bak-git> for more information
57 'upgrade!' => \$upgrade,
58 'install=s' => \$install,
61 my ( $dir, $server_ip ) = @ARGV;
62 die "usage: $0 /backup/directory 127.0.0.1\n" unless $dir;
63 $server_ip ||= '127.0.0.1';
67 open(my $ssh_fd, '<', "$ENV{HOME}/.ssh/config");
71 next unless length($_) > 0;
74 if ( /^Host\s+(.+)/i ) {
76 } elsif ( /^\s+(\S+)\s+(.+)/ ) {
77 $ssh_tunnel->{$host}++ if lc($1) eq 'remoteforward' && $2 =~ m/9001/;
84 my ( $hostname ) = @_;
85 my $path = '/tmp/bak';
86 my $server = $server_ip;
87 $server = '127.0.0.1' if $ssh_tunnel->{$hostname};
88 warn "# ssh_client $hostname $server";
89 open(my $fh, '>', $path);
90 print $fh "#!/bin/sh\n";
91 print $fh "echo \$USER/\$SUDO_USER $hostname `pwd` \$* | nc $server 9001\n";
98 while ( my($host,$pid) = each %$ssh_tunnel ) {
99 warn "$host kill TERM $pid";
100 kill 15, $pid; # TERM
105 $SIG{TERM} = &_kill_ssh;
108 system 'git init' unless -e '.git';
110 if ( $upgrade || $install ) {
112 my @hosts = grep { -d $_ } glob '*';
113 @hosts = ( $install ) if $install;
115 foreach my $hostname ( @hosts ) {
116 warn "install on $hostname\n";
117 system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
118 my $path = shell_client( $hostname );
119 system "scp $path root\@$hostname:/usr/local/bin/";
120 system "ssh root\@$hostname apt-get install -y netcat rsync";
123 my $ssh = $ENV{SSH} || 'ssh';
124 warn "# start $ssh tunnels...";
125 foreach my $host ( keys %$ssh_tunnel ) {
126 last; # FIXME disabled
129 if ( ! defined $pid ) {
133 warn "FIXME: waitpid $pid";
135 warn "EXEC $ssh $host";
136 exec "$ssh -N root\@$host";
139 $ssh_tunnel->{$host} = $pid;
143 warn "dir: $dir listen: $server_ip:9001\n";
145 my $server = IO::Socket::INET->new(
147 # LocalAddr => $server_ip,
155 warn "# rsync ",join(' ', @_), "\n";
160 my $hostname = shift;
161 system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
163 open(my $files, '>>', "/tmp/$hostname.list");
164 print $files "$_\n" foreach @_;
167 rsync split / /, "-avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/";
172 $path =~ s{/[^/]+$}{};
173 warn "# mkpath $path\n";
174 mkpath $path || die $!;
177 while (my $client = $server->accept()) {
178 my $line = <$client>;
181 my ($user,$hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,6);
182 $hostname =~ s/\..+$//;
185 if ( $rel_path =~ s/^([^:]+):(.+)$/$2/ ) {
189 print $client "host $1 doesn't exist in backup\n";
193 my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
195 foreach my $command ( split /,/, $command ) { # XXX command loop
197 warn "$hostname [$command] $on_host:$path | $message\n";
199 my $args_message = $message;
201 $message ||= "$path [$command]";
202 $message = "$hostname: $message";
205 $dir =~ s{/[^/]+$}{};
207 my $backup_path = -e "$hostname/$path" ? "$hostname/$path" : $hostname;
210 my $args = join(' ',@_);
211 warn "# git $args\n";
212 my $out = `git $args`;
213 warn "$out\n# [", length($out), " bytes]\n" if defined $out;
218 pull_changes $hostname;
219 } elsif ( $command eq 'add' ) {
220 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
222 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
223 print $client git 'add', "$hostname/$path";
225 $args_message =~ s/^(.+)\b// || last;
229 } elsif ( $command eq 'commit' ) {
230 pull_changes $hostname;
231 $message =~ s/'/\\'/g;
233 print $client git( "commit -m '$message' --author '$user <$hostname>' $backup_path" );
234 } elsif ( $command =~ m{(diff|status|log|ch)} ) {
235 $command .= ' --stat' if $command eq 'log';
236 $command = 'log --patch-with-stat' if $command =~ m/^ch/;
237 pull_changes( $hostname ) if $command eq 'diff';
239 mkpath $_ foreach grep { ! -e $_ } ( "$hostname/$dir", "$on_host/$dir" );
240 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
241 rsync( '-avv', "root\@$on_host:$path", "$on_host/$path" );
242 open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
247 # commands without path will show host-wide status/changes
248 my $backup_path = $path ? "$hostname/$path" : "$hostname/";
249 # hostname must end with / to prevent error from git:
250 # ambiguous argument 'arh-hw': both revision and filename
251 # to support branches named as hosts
252 print $client git($command, $backup_path);
254 } elsif ( $command eq 'revert' ) {
256 rsync( '-avv', "$on_host/$path", "root\@$hostname:$path" );
258 print $client git "checkout -- $hostname/$path";
259 rsync( '-avv', "$hostname/$path", "root\@$hostname:$path" );
261 } elsif ( $command eq 'cat' ) {
262 my $file_path = ( $on_host ? $on_host : $hostname ) . "/$path";
263 if ( -r $file_path ) {
264 open(my $file, '<', $file_path) || warn "ERROR $file_path: $!";
270 print $client "ERROR: $file_path: $!\n";
272 } elsif ( $command eq 'ls' ) {
273 print $client `ls $backup_path`;
274 } elsif ( $command eq 'show' ) {
275 print $client `git show $rel_path`;
276 } elsif ( $command eq 'grep' ) {
277 print $client `git log -g --grep=$rel_path`;
278 } elsif ( $command eq 'link' ) {
280 mkbasedir "$on_host/$path";
281 rsync( '-avv', "root\@$on_host:$path", "$on_host/$path" );
282 mkbasedir "$hostname/$path";
283 link "$on_host/$path", "$hostname/$path";
284 rsync( '-avv', "$hostname/$path", "root\@$hostname:$path" );
286 print $client "ERROR: link requires host:/path\n";
289 print $client "ERROR: unknown command: $command\n";
292 } # XXX command, loop