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
37 bak - push all changed files to server
39 See L<http://blog.rot13.org/bak-git> for more information
54 'upgrade!' => \$upgrade,
55 'install=s' => \$install,
58 my ( $dir, $server_ip ) = @ARGV;
59 die "usage: $0 /backup/directory 127.0.0.1\n" unless $dir;
60 $server_ip ||= '127.0.0.1';
64 open(my $ssh_fd, '<', "$ENV{HOME}/.ssh/config");
68 next unless length($_) > 0;
71 if ( /^Host\s+(.+)/i ) {
73 } elsif ( /^\s+(\S+)\s+(.+)/ ) {
74 $ssh_tunnel->{$host}++ if lc($1) eq 'remoteforward' && $2 =~ m/9001/;
81 my ( $hostname ) = @_;
82 my $path = '/tmp/bak';
83 my $server = $server_ip;
84 $server = '127.0.0.1' if $ssh_tunnel->{$hostname};
85 warn "# ssh_client $hostname $server";
86 open(my $fh, '>', $path);
87 print $fh "#!/bin/sh\n";
88 print $fh "echo \$USER/\$SUDO_USER $hostname `pwd` \$* | nc $server 9001\n";
95 system 'git init' unless -e '.git';
97 if ( $upgrade || $install ) {
99 my @hosts = grep { -d $_ } glob '*';
100 @hosts = ( $install ) if $install;
102 foreach my $hostname ( @hosts ) {
103 warn "install on $hostname\n";
104 system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
105 my $path = shell_client( $hostname );
106 system "scp $path root\@$hostname:/usr/local/bin/";
107 system "ssh root\@$hostname apt-get install -y rsync";
110 warn "# start ssh tunnels...";
111 foreach my $host ( keys %$ssh_tunnel ) {
113 open( $ssh_tunnel->{$host}, '-|', "ssh -N root\@$host &" ) or die $!;
117 my $server = IO::Socket::INET->new(
119 LocalAddr => $server_ip,
126 warn "dir: $dir listen: $server_ip:9001\n";
129 warn "# rsync ",join(' ', @_), "\n";
134 my $hostname = shift;
135 system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
137 open(my $files, '>>', "/tmp/$hostname.list");
138 print $files "$_\n" foreach @_;
141 rsync split / /, "-avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/";
144 while (my $client = $server->accept()) {
145 my $line = <$client>;
148 my ($user,$hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,6);
149 $hostname =~ s/\..+$//;
152 if ( $rel_path =~ s/^([^:]+):(.+)$/$2/ ) {
156 print $client "host $1 doesn't exist in backup\n";
160 my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
162 warn "$hostname [$command] $on_host:$path | $message\n";
164 my $args_message = $message;
166 $message ||= "$path [$command]";
167 $message = "$hostname: $message";
170 $dir =~ s{/[^/]+$}{};
172 my $backup_path = -e "$hostname/$path" ? "$hostname/$path" : $hostname;
175 my $args = join(' ',@_);
176 warn "# git $args\n";
177 my $out = `git $args`;
178 warn "$out\n# [", length($out), " bytes]\n" if defined $out;
183 pull_changes $hostname;
184 } elsif ( $command eq 'add' ) {
185 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
187 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
188 print $client git 'add', "$hostname/$path";
190 $args_message =~ s/^(.+)\b// || last;
194 } elsif ( $command eq 'commit' ) {
195 pull_changes $hostname;
196 $message =~ s/'/\\'/g;
198 print $client git( "commit -m '$message' --author '$user <$hostname>' $backup_path" );
199 } elsif ( $command =~ m{(diff|status|log|ch)} ) {
200 $command .= ' --stat' if $command eq 'log';
201 $command = 'log --patch-with-stat' if $command =~ m/^ch/;
202 pull_changes( $hostname ) if $command eq 'diff';
204 mkpath $_ foreach grep { ! -e $_ } ( "$hostname/$dir", "$on_host/$dir" );
205 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
206 rsync( '-avv', "root\@$on_host:$path", "$on_host/$path" );
207 open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
212 # commands without path will show host-wide status/changes
213 my $backup_path = $path ? "$hostname/$path" : "$hostname/";
214 # hostname must end with / to prevent error from git:
215 # ambiguous argument 'arh-hw': both revision and filename
216 # to support branches named as hosts
217 print $client git($command, $backup_path);
219 } elsif ( $command eq 'revert' ) {
221 rsync( '-avv', "$on_host/$path", "root\@$hostname:$path" );
223 print $client git "checkout -- $hostname/$path";
224 rsync( '-avv', "$hostname/$path", "root\@$hostname:$path" );
226 } elsif ( $command eq 'cat' ) {
227 my $file_path = ( $on_host ? $on_host : $hostname ) . "/$path";
228 open(my $file, '<', $file_path) || warn "ERROR $file_path: $!";
233 } elsif ( $command eq 'ls' ) {
234 print $client `ls $backup_path`;
235 } elsif ( $command eq 'show' ) {
236 print $client `git show`;
238 print $client "Unknown command: $command\n";