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]
32 bak revert [host:]/path
36 bak - push all changed files to server
38 See L<http://blog.rot13.org/bak-git> for more information
53 'upgrade!' => \$upgrade,
54 'install=s' => \$install,
57 my ( $dir, $server_ip ) = @ARGV;
58 die "usage: $0 /backup/directory 127.0.0.1\n" unless $dir;
59 $server_ip ||= '127.0.0.1';
63 open(my $ssh_fd, '<', "$ENV{HOME}/.ssh/config");
67 next unless length($_) > 0;
70 if ( /^Host\s+(.+)/i ) {
72 } elsif ( /^\s+(\S+)\s+(.+)/ ) {
73 $ssh_tunnel->{$host}++ if lc($1) eq 'remoteforward' && $2 =~ m/9001/;
80 my ( $hostname ) = @_;
81 my $path = '/tmp/bak';
82 my $server = $server_ip;
83 $server = '127.0.0.1' if $ssh_tunnel->{$hostname};
84 warn "# ssh_client $hostname $server";
85 open(my $fh, '>', $path);
86 print $fh "#!/bin/sh\n";
87 print $fh "echo \$USER/\$SUDO_USER $hostname `pwd` \$* | nc $server 9001\n";
94 system 'git init' unless -e '.git';
96 if ( $upgrade || $install ) {
98 my @hosts = grep { -d $_ } glob '*';
99 @hosts = ( $install ) if $install;
101 foreach my $hostname ( @hosts ) {
102 warn "install on $hostname\n";
103 system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
104 my $path = shell_client( $hostname );
105 system "scp $path root\@$hostname:/usr/local/bin/";
106 system "ssh root\@$hostname apt-get install -y rsync";
109 warn "# start ssh tunnels...";
110 foreach my $host ( keys %$ssh_tunnel ) {
112 open( $ssh_tunnel->{$host}, '-|', "ssh -N root\@$host &" ) or die $!;
116 my $server = IO::Socket::INET->new(
118 LocalAddr => $server_ip,
125 warn "dir: $dir listen: $server_ip:9001\n";
128 warn "# rsync ",join(' ', @_), "\n";
133 my $hostname = shift;
134 system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
136 open(my $files, '>>', "/tmp/$hostname.list");
137 print $files "$_\n" foreach @_;
140 rsync split / /, "-avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/";
143 while (my $client = $server->accept()) {
144 my $line = <$client>;
147 my ($user,$hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,6);
148 $hostname =~ s/\..+$//;
151 if ( $rel_path =~ s/^([^:]+):(.+)$/$2/ ) {
155 print $client "host $1 doesn't exist in backup\n";
159 my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
161 warn "$hostname [$command] $on_host:$path | $message\n";
163 my $args_message = $message;
165 $message ||= "$path [$command]";
166 $message = "$hostname: $message";
169 $dir =~ s{/[^/]+$}{};
171 my $backup_path = -e "$hostname/$path" ? "$hostname/$path" : $hostname;
174 my $args = join(' ',@_);
175 warn "# git $args\n";
176 my $out = `git $args`;
177 warn "$out\n# [", length($out), " bytes]\n" if defined $out;
182 pull_changes $hostname;
183 } elsif ( $command eq 'add' ) {
184 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
186 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
187 print $client git 'add', "$hostname/$path";
189 $args_message =~ s/^(.+)\b// || last;
193 } elsif ( $command eq 'commit' ) {
194 pull_changes $hostname;
195 $message =~ s/'/\\'/g;
197 print $client git( "commit -m '$message' --author '$user <$hostname>' $backup_path" );
198 } elsif ( $command =~ m{(diff|status|log|ch)} ) {
199 $command .= ' --stat' if $command eq 'log';
200 $command = 'log --patch-with-stat' if $command =~ m/^ch/;
201 pull_changes( $hostname ) if $command eq 'diff';
203 mkpath $_ foreach grep { ! -e $_ } ( "$hostname/$dir", "$on_host/$dir" );
204 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
205 rsync( '-avv', "root\@$on_host:$path", "$on_host/$path" );
206 open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
211 # commands without path will show host-wide status/changes
212 my $backup_path = $path ? "$hostname/$path" : "$hostname/";
213 # hostname must end with / to prevent error from git:
214 # ambiguous argument 'arh-hw': both revision and filename
215 # to support branches named as hosts
216 print $client git($command, $backup_path);
218 } elsif ( $command eq 'revert' ) {
220 rsync( '-avv', "$on_host/$path", "root\@$hostname:$path" );
222 print $client git "checkout -- $hostname/$path";
223 rsync( '-avv', "$hostname/$path", "root\@$hostname:$path" );
225 } elsif ( $command eq 'cat' ) {
226 my $file_path = ( $on_host ? $on_host : $hostname ) . "/$path";
227 open(my $file, '<', $file_path) || warn "ERROR $file_path: $!";
232 } elsif ( $command eq 'ls' ) {
233 print $client `ls $backup_path`;
235 print $client "Unknown command: $command\n";