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 You will want to add following to C<~/.ssh/config>
16 RemoteForward 9001 localhost:9001
21 bak commit [/path [message]]
22 bak diff [host:][/path]
27 bak revert [host:]/path
31 bak - push all changed files to server
33 See L<http://blog.rot13.org/bak-git> for more information
48 'upgrade!' => \$upgrade,
49 'install=s' => \$install,
52 my ( $dir, $server_ip ) = @ARGV;
53 die "usage: $0 /backup/directory 127.0.0.1\n" unless $dir;
54 $server_ip ||= '127.0.0.1';
56 my $shell_client = <<__SHELL_CLIENT__;
58 #echo \$USER/\$SUDO_USER $install `pwd` \$* | nc 127.0.0.1 9001
59 echo \$USER/\$SUDO_USER `hostname` `pwd` \$* | nc $server_ip 9001
63 system 'git init' unless -e '.git';
65 if ( $upgrade || $install ) {
66 open(my $fh, '>', '/tmp/bak');
67 print $fh $shell_client;
69 chmod 0755, '/tmp/bak';
71 my @hosts = grep { -d $_ } glob '*';
72 @hosts = ( $install ) if $install;
74 foreach my $hostname ( @hosts ) {
75 warn "install on $hostname\n";
76 system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
77 system "scp /tmp/bak root\@$hostname:/usr/local/bin/";
78 system "ssh root\@$hostname apt-get install -y rsync";
82 my $server = IO::Socket::INET->new(
84 LocalAddr => $server_ip,
91 warn "dir: $dir listen: $server_ip:9001\n"
96 warn "# rsync ",join(' ', @_), "\n";
101 my $hostname = shift;
102 system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
104 open(my $files, '>>', "/tmp/$hostname.list");
105 print $files "$_\n" foreach @_;
108 rsync split / /, "-avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/";
111 while (my $client = $server->accept()) {
112 my $line = <$client>;
115 my ($user,$hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,6);
116 $hostname =~ s/\..+$//;
119 if ( $rel_path =~ s/^([^:]+):(.+)$/$2/ ) {
123 print $client "host $1 doesn't exist in backup\n";
127 my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
129 warn "$hostname [$command] $on_host:$path | $message\n";
131 my $args_message = $message;
133 $message ||= "$path [$command]";
134 $message = "$hostname: $message";
137 $dir =~ s{/[^/]+$}{};
139 my $backup_path = -e "$hostname/$path" ? "$hostname/$path" : $hostname;
142 my $args = join(' ',@_);
143 warn "# git $args\n";
144 my $out = `git $args`;
145 warn "$out\n# [", length($out), " bytes]\n" if defined $out;
150 pull_changes $hostname;
151 } elsif ( $command eq 'add' ) {
152 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
154 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
155 print $client git 'add', "$hostname/$path";
157 $args_message =~ s/^(.+)\b// || last;
161 } elsif ( $command eq 'commit' ) {
162 pull_changes $hostname;
163 $message =~ s/'/\\'/g;
165 print $client git( "commit -m '$message' --author '$user <$hostname>' $backup_path" );
166 } elsif ( $command =~ m{(diff|status|log|ch)} ) {
167 $command .= ' --stat' if $command eq 'log';
168 $command = 'log --patch-with-stat' if $command =~ m/^ch/;
169 pull_changes( $hostname ) if $command eq 'diff';
171 mkpath $_ foreach grep { ! -e $_ } ( "$hostname/$dir", "$on_host/$dir" );
172 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
173 rsync( '-avv', "root\@$on_host:$path", "$on_host/$path" );
174 open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
179 # commands without path will show host-wide status/changes
180 my $backup_path = $path ? "$hostname/$path" : "$hostname/";
181 # hostname must end with / to prevent error from git:
182 # ambiguous argument 'arh-hw': both revision and filename
183 # to support branches named as hosts
184 print $client git($command, $backup_path);
186 } elsif ( $command eq 'revert' ) {
188 rsync( '-avv', "$on_host/$path", "root\@$hostname:$path" );
190 print $client git "checkout -- $hostname/$path";
191 rsync( '-avv', "$hostname/$path", "root\@$hostname:$path" );
193 } elsif ( $command eq 'cat' ) {
194 my $file_path = ( $on_host ? $on_host : $hostname ) . "/$path";
195 open(my $file, '<', $file_path) || warn "ERROR $file_path: $!";
200 } elsif ( $command eq 'ls' ) {
201 print $client `ls $backup_path`;
203 print $client "Unknown command: $command\n";