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
44 'upgrade!' => \$upgrade,
45 'install=s' => \$install,
48 my ( $dir, $server_ip ) = @ARGV;
49 die "usage: $0 /backup/directory 127.0.0.1\n" unless $dir;
50 $server_ip ||= '127.0.0.1';
52 my $shell_client = <<__SHELL_CLIENT__;
54 echo \$USER/\$SUDO_USER `hostname -s` `pwd` \$* | nc $server_ip 9001
58 system 'git init' unless -e '.git';
60 if ( $upgrade || $install ) {
61 open(my $fh, '>', '/tmp/bak');
62 print $fh $shell_client;
64 chmod 0755, '/tmp/bak';
66 my @hosts = grep { -d $_ } glob '*';
67 @hosts = ( $install ) if $install;
69 foreach my $hostname ( @hosts ) {
70 warn "install on $hostname\n";
71 system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
72 system "scp /tmp/bak root\@$hostname:/usr/local/bin/";
73 system "ssh root\@$hostname apt-get install -y rsync";
77 my $server = IO::Socket::INET->new(
79 LocalAddr => $server_ip,
86 warn "dir: $dir listen: $server_ip:9001\n"
92 system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
94 open(my $files, '>>', "/tmp/$hostname.list");
95 print $files "$_\n" foreach @_;
98 system "rsync -avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/"
101 while (my $client = $server->accept()) {
102 my $line = <$client>;
105 my ($user,$hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,6);
108 if ( $rel_path =~ s/^([^:]+):(.+)$/$2/ ) {
112 print $client "host $1 doesn't exist in backup\n";
116 my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
118 warn "$hostname [$command] $on_host:$path | $message\n";
120 my $args_message = $message;
122 $message ||= "$path [$command]";
123 $message = "$hostname: $message";
126 $dir =~ s{/[^/]+$}{};
128 my $backup_path = -e "$hostname/$path" ? "$hostname/$path" : $hostname;
131 my $args = join(' ',@_);
132 warn "# git $args\n";
133 my $out = `git $args`;
134 warn "$out\n# [", length($out), " bytes]\n" if defined $out;
139 pull_changes $hostname;
140 } elsif ( $command eq 'add' ) {
141 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
143 system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path";
144 print $client git 'add', "$hostname/$path";
146 $args_message =~ s/^(.+)\b// || last;
150 } elsif ( $command eq 'commit' ) {
151 pull_changes $hostname;
152 $message =~ s/'/\\'/g;
154 print $client git( "commit -m '$message' --author '$user <$hostname>' $backup_path" );
155 } elsif ( $command =~ m{(diff|status|log|ch)} ) {
156 $command .= ' --stat' if $command eq 'log';
157 $command = 'log --patch-with-stat' if $command =~ m/^ch/;
158 pull_changes( $hostname ) if $command eq 'diff';
160 system 'rsync', '-avv', "root\@$on_host:$path", "$on_host/$path";
161 open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
166 # commands without path will show host-wide status/changes
167 my $backup_path = $rel_path ? $backup_path : "$hostname/";
168 # hostname must end with / to prevent error from git:
169 # ambiguous argument 'arh-hw': both revision and filename
170 # to support branches named as hosts
171 print $client git($command, $backup_path);
173 } elsif ( $command eq 'revert' ) {
175 system 'rsync', '-avv', "$on_host/$path", "root\@$hostname:$path";
177 print $client git "checkout -- $hostname/$path";
178 system 'rsync', '-avv', "$hostname/$path", "root\@$hostname:$path";
181 print $client "Unknown command: $command\n";