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
26 bak commit [/path [message]]
27 bak diff [host:][/path]
33 bak revert [host:]/path
38 bak - push all changed files to server
40 See L<http://blog.rot13.org/bak-git> for more information
55 'upgrade!' => \$upgrade,
56 'install=s' => \$install,
59 my ( $dir, $server_ip ) = @ARGV;
60 die "usage: $0 /backup/directory 127.0.0.1\n" unless $dir;
61 $server_ip ||= '127.0.0.1';
65 open(my $ssh_fd, '<', "$ENV{HOME}/.ssh/config");
69 next unless length($_) > 0;
72 if ( /^Host\s+(.+)/i ) {
74 } elsif ( /^\s+(\S+)\s+(.+)/ ) {
75 $ssh_tunnel->{$host}++ if lc($1) eq 'remoteforward' && $2 =~ m/9001/;
82 my ( $hostname ) = @_;
83 my $path = '/tmp/bak';
84 my $server = $server_ip;
85 $server = '127.0.0.1' if $ssh_tunnel->{$hostname};
86 warn "# ssh_client $hostname $server";
87 open(my $fh, '>', $path);
88 print $fh "#!/bin/sh\n";
89 print $fh "echo \$USER/\$SUDO_USER $hostname `pwd` \$* | nc $server 9001\n";
96 while ( my($host,$pid) = each %$ssh_tunnel ) {
97 warn "$host kill TERM $pid";
103 $SIG{TERM} = &_kill_ssh;
106 system 'git init' unless -e '.git';
108 if ( $upgrade || $install ) {
110 my @hosts = grep { -d $_ } glob '*';
111 @hosts = ( $install ) if $install;
113 foreach my $hostname ( @hosts ) {
114 warn "install on $hostname\n";
115 system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
116 my $path = shell_client( $hostname );
117 system "scp $path root\@$hostname:/usr/local/bin/";
118 system "ssh root\@$hostname apt-get install -y netcat rsync";
121 my $ssh = $ENV{SSH} || 'ssh';
122 warn "# start $ssh tunnels...";
123 foreach my $host ( keys %$ssh_tunnel ) {
124 last; # FIXME disabled
127 if ( ! defined $pid ) {
131 warn "FIXME: waitpid $pid";
133 warn "EXEC $ssh $host";
134 exec "$ssh -N root\@$host";
137 $ssh_tunnel->{$host} = $pid;
141 warn "dir: $dir listen: $server_ip:9001\n";
143 my $server = IO::Socket::INET->new(
145 # LocalAddr => $server_ip,
153 warn "# rsync ",join(' ', @_), "\n";
158 my $hostname = shift;
159 system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
161 open(my $files, '>>', "/tmp/$hostname.list");
162 print $files "$_\n" foreach @_;
165 rsync split / /, "-avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/";
170 $path =~ s{/[^/]+$}{};
171 warn "# mkpath $path\n";
172 mkpath $path || die $!;
175 while (my $client = $server->accept()) {
176 my $line = <$client>;
179 my ($user,$hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,6);
180 $hostname =~ s/\..+$//;
183 if ( $rel_path =~ s/^([^:]+):(.+)$/$2/ ) {
187 print $client "host $1 doesn't exist in backup\n";
191 my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
193 warn "$hostname [$command] $on_host:$path | $message\n";
195 my $args_message = $message;
197 $message ||= "$path [$command]";
198 $message = "$hostname: $message";
201 $dir =~ s{/[^/]+$}{};
203 my $backup_path = -e "$hostname/$path" ? "$hostname/$path" : $hostname;
206 my $args = join(' ',@_);
207 warn "# git $args\n";
208 my $out = `git $args`;
209 warn "$out\n# [", length($out), " bytes]\n" if defined $out;
214 pull_changes $hostname;
215 } elsif ( $command eq 'add' ) {
216 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
218 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
219 print $client git 'add', "$hostname/$path";
221 $args_message =~ s/^(.+)\b// || last;
225 } elsif ( $command eq 'commit' ) {
226 pull_changes $hostname;
227 $message =~ s/'/\\'/g;
229 print $client git( "commit -m '$message' --author '$user <$hostname>' $backup_path" );
230 } elsif ( $command =~ m{(diff|status|log|ch)} ) {
231 $command .= ' --stat' if $command eq 'log';
232 $command = 'log --patch-with-stat' if $command =~ m/^ch/;
233 pull_changes( $hostname ) if $command eq 'diff';
235 mkpath $_ foreach grep { ! -e $_ } ( "$hostname/$dir", "$on_host/$dir" );
236 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
237 rsync( '-avv', "root\@$on_host:$path", "$on_host/$path" );
238 open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
243 # commands without path will show host-wide status/changes
244 my $backup_path = $path ? "$hostname/$path" : "$hostname/";
245 # hostname must end with / to prevent error from git:
246 # ambiguous argument 'arh-hw': both revision and filename
247 # to support branches named as hosts
248 print $client git($command, $backup_path);
250 } elsif ( $command eq 'revert' ) {
252 rsync( '-avv', "$on_host/$path", "root\@$hostname:$path" );
254 print $client git "checkout -- $hostname/$path";
255 rsync( '-avv', "$hostname/$path", "root\@$hostname:$path" );
257 } elsif ( $command eq 'cat' ) {
258 my $file_path = ( $on_host ? $on_host : $hostname ) . "/$path";
259 if ( -r $file_path ) {
260 open(my $file, '<', $file_path) || warn "ERROR $file_path: $!";
266 print $client "ERROR: $file_path: $!\n";
268 } elsif ( $command eq 'ls' ) {
269 print $client `ls $backup_path`;
270 } elsif ( $command eq 'show' ) {
271 print $client `git show $rel_path`;
272 } elsif ( $command eq 'grep' ) {
273 print $client `git log -g --grep=$rel_path`;
274 } elsif ( $command eq 'link' ) {
276 mkbasedir "$on_host/$path";
277 rsync( '-avv', "root\@$on_host:$path", "$on_host/$path" );
278 mkbasedir "$hostname/$path";
279 link "$on_host/$path", "$hostname/$path";
280 rsync( '-avv', "$hostname/$path", "root\@$hostname:$path" );
282 print $client "ERROR: link requires host:/path\n";
285 print $client "ERROR: unknown command: $command\n";