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 `hostname` `pwd` \$* | nc $server_ip 9001
62 system 'git init' unless -e '.git';
64 if ( $upgrade || $install ) {
65 open(my $fh, '>', '/tmp/bak');
66 print $fh $shell_client;
68 chmod 0755, '/tmp/bak';
70 my @hosts = grep { -d $_ } glob '*';
71 @hosts = ( $install ) if $install;
73 foreach my $hostname ( @hosts ) {
74 warn "install on $hostname\n";
75 system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
76 system "scp /tmp/bak root\@$hostname:/usr/local/bin/";
77 system "ssh root\@$hostname apt-get install -y rsync";
81 my $server = IO::Socket::INET->new(
83 LocalAddr => $server_ip,
90 warn "dir: $dir listen: $server_ip:9001\n"
95 warn "# rsync ",join(' ', @_), "\n";
100 my $hostname = shift;
101 system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
103 open(my $files, '>>', "/tmp/$hostname.list");
104 print $files "$_\n" foreach @_;
107 rsync( qw( -avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/ ) );
110 while (my $client = $server->accept()) {
111 my $line = <$client>;
114 my ($user,$hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,6);
115 $hostname =~ s/\..+$//;
118 if ( $rel_path =~ s/^([^:]+):(.+)$/$2/ ) {
122 print $client "host $1 doesn't exist in backup\n";
126 my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
128 warn "$hostname [$command] $on_host:$path | $message\n";
130 my $args_message = $message;
132 $message ||= "$path [$command]";
133 $message = "$hostname: $message";
136 $dir =~ s{/[^/]+$}{};
138 my $backup_path = -e "$hostname/$path" ? "$hostname/$path" : $hostname;
141 my $args = join(' ',@_);
142 warn "# git $args\n";
143 my $out = `git $args`;
144 warn "$out\n# [", length($out), " bytes]\n" if defined $out;
149 pull_changes $hostname;
150 } elsif ( $command eq 'add' ) {
151 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
153 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
154 print $client git 'add', "$hostname/$path";
156 $args_message =~ s/^(.+)\b// || last;
160 } elsif ( $command eq 'commit' ) {
161 pull_changes $hostname;
162 $message =~ s/'/\\'/g;
164 print $client git( "commit -m '$message' --author '$user <$hostname>' $backup_path" );
165 } elsif ( $command =~ m{(diff|status|log|ch)} ) {
166 $command .= ' --stat' if $command eq 'log';
167 $command = 'log --patch-with-stat' if $command =~ m/^ch/;
168 pull_changes( $hostname ) if $command eq 'diff';
170 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
171 rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
172 rsync( '-avv', "root\@$on_host:$path", "$on_host/$path" );
173 open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
178 # commands without path will show host-wide status/changes
179 my $backup_path = $path ? "$hostname/$path" : "$hostname/";
180 # hostname must end with / to prevent error from git:
181 # ambiguous argument 'arh-hw': both revision and filename
182 # to support branches named as hosts
183 print $client git($command, $backup_path);
185 } elsif ( $command eq 'revert' ) {
187 rsync( '-avv', "$on_host/$path", "root\@$hostname:$path" );
189 print $client git "checkout -- $hostname/$path";
190 rsync( '-avv', "$hostname/$path", "root\@$hostname:$path" );
192 } elsif ( $command eq 'cat' ) {
193 my $file_path = ( $on_host ? $on_host : $hostname ) . "/$path";
194 open(my $file, '<', $file_path) || warn "ERROR $file_path: $!";
200 print $client "Unknown command: $command\n";