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
29 bak - push all changed files to server
31 See L<http://blog.rot13.org/bak-git> for more information
46 'upgrade!' => \$upgrade,
47 'install=s' => \$install,
50 my ( $dir, $server_ip ) = @ARGV;
51 die "usage: $0 /backup/directory 127.0.0.1\n" unless $dir;
52 $server_ip ||= '127.0.0.1';
54 my $shell_client = <<__SHELL_CLIENT__;
56 echo \$USER/\$SUDO_USER `hostname` `pwd` \$* | nc $server_ip 9001
60 system 'git init' unless -e '.git';
62 if ( $upgrade || $install ) {
63 open(my $fh, '>', '/tmp/bak');
64 print $fh $shell_client;
66 chmod 0755, '/tmp/bak';
68 my @hosts = grep { -d $_ } glob '*';
69 @hosts = ( $install ) if $install;
71 foreach my $hostname ( @hosts ) {
72 warn "install on $hostname\n";
73 system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
74 system "scp /tmp/bak root\@$hostname:/usr/local/bin/";
75 system "ssh root\@$hostname apt-get install -y rsync";
79 my $server = IO::Socket::INET->new(
81 LocalAddr => $server_ip,
88 warn "dir: $dir listen: $server_ip:9001\n"
94 system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
96 open(my $files, '>>', "/tmp/$hostname.list");
97 print $files "$_\n" foreach @_;
100 system "rsync -avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/"
103 while (my $client = $server->accept()) {
104 my $line = <$client>;
107 my ($user,$hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,6);
108 $hostname =~ s/\..+$//;
111 if ( $rel_path =~ s/^([^:]+):(.+)$/$2/ ) {
115 print $client "host $1 doesn't exist in backup\n";
119 my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
121 warn "$hostname [$command] $on_host:$path | $message\n";
123 my $args_message = $message;
125 $message ||= "$path [$command]";
126 $message = "$hostname: $message";
129 $dir =~ s{/[^/]+$}{};
131 my $backup_path = -e "$hostname/$path" ? "$hostname/$path" : $hostname;
134 my $args = join(' ',@_);
135 warn "# git $args\n";
136 my $out = `git $args`;
137 warn "$out\n# [", length($out), " bytes]\n" if defined $out;
142 pull_changes $hostname;
143 } elsif ( $command eq 'add' ) {
144 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
146 system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path";
147 print $client git 'add', "$hostname/$path";
149 $args_message =~ s/^(.+)\b// || last;
153 } elsif ( $command eq 'commit' ) {
154 pull_changes $hostname;
155 $message =~ s/'/\\'/g;
157 print $client git( "commit -m '$message' --author '$user <$hostname>' $backup_path" );
158 } elsif ( $command =~ m{(diff|status|log|ch)} ) {
159 $command .= ' --stat' if $command eq 'log';
160 $command = 'log --patch-with-stat' if $command =~ m/^ch/;
161 pull_changes( $hostname ) if $command eq 'diff';
163 system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path";
164 system 'rsync', '-avv', "root\@$on_host:$path", "$on_host/$path";
165 open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
170 # commands without path will show host-wide status/changes
171 my $backup_path = $rel_path ? $backup_path : "$hostname/";
172 # hostname must end with / to prevent error from git:
173 # ambiguous argument 'arh-hw': both revision and filename
174 # to support branches named as hosts
175 print $client git($command, $backup_path);
177 } elsif ( $command eq 'revert' ) {
179 system 'rsync', '-avv', "$on_host/$path", "root\@$hostname:$path";
181 print $client git "checkout -- $hostname/$path";
182 system 'rsync', '-avv', "$hostname/$path", "root\@$hostname:$path";
185 print $client "Unknown command: $command\n";