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"
96 system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
98 open(my $files, '>>', "/tmp/$hostname.list");
99 print $files "$_\n" foreach @_;
102 system "rsync -avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/"
105 while (my $client = $server->accept()) {
106 my $line = <$client>;
109 my ($user,$hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,6);
110 $hostname =~ s/\..+$//;
113 if ( $rel_path =~ s/^([^:]+):(.+)$/$2/ ) {
117 print $client "host $1 doesn't exist in backup\n";
121 my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
123 warn "$hostname [$command] $on_host:$path | $message\n";
125 my $args_message = $message;
127 $message ||= "$path [$command]";
128 $message = "$hostname: $message";
131 $dir =~ s{/[^/]+$}{};
133 my $backup_path = -e "$hostname/$path" ? "$hostname/$path" : $hostname;
136 my $args = join(' ',@_);
137 warn "# git $args\n";
138 my $out = `git $args`;
139 warn "$out\n# [", length($out), " bytes]\n" if defined $out;
144 pull_changes $hostname;
145 } elsif ( $command eq 'add' ) {
146 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
148 system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path";
149 print $client git 'add', "$hostname/$path";
151 $args_message =~ s/^(.+)\b// || last;
155 } elsif ( $command eq 'commit' ) {
156 pull_changes $hostname;
157 $message =~ s/'/\\'/g;
159 print $client git( "commit -m '$message' --author '$user <$hostname>' $backup_path" );
160 } elsif ( $command =~ m{(diff|status|log|ch)} ) {
161 $command .= ' --stat' if $command eq 'log';
162 $command = 'log --patch-with-stat' if $command =~ m/^ch/;
163 pull_changes( $hostname ) if $command eq 'diff';
165 system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path";
166 system 'rsync', '-avv', "root\@$on_host:$path", "$on_host/$path";
167 open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
172 # commands without path will show host-wide status/changes
173 my $backup_path = $path ? "$hostname/$path" : "$hostname/";
174 # hostname must end with / to prevent error from git:
175 # ambiguous argument 'arh-hw': both revision and filename
176 # to support branches named as hosts
177 print $client git($command, $backup_path);
179 } elsif ( $command eq 'revert' ) {
181 system 'rsync', '-avv', "$on_host/$path", "root\@$hostname:$path";
183 print $client git "checkout -- $hostname/$path";
184 system 'rsync', '-avv', "$hostname/$path", "root\@$hostname:$path";
186 } elsif ( $command eq 'cat' ) {
187 my $file_path = ( $on_host ? $on_host : $hostname ) . "/$path";
188 open(my $file, '<', $file_path) || warn "ERROR $file_path: $!";
194 print $client "Unknown command: $command\n";