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
31 'upgrade!' => \$upgrade,
32 'install=s' => \$install,
35 my ( $dir, $server_ip ) = @ARGV;
36 die "usage: $0 /backup/directory\n" unless $dir;
37 $server_ip ||= '127.0.0.1';
39 my $shell_client = <<__SHELL_CLIENT__;
41 echo `hostname -s` `pwd` \$* | nc $server_ip 9001
45 system 'git init' unless -e '.git';
47 if ( $upgrade || $install ) {
48 open(my $fh, '>', '/tmp/bak');
49 print $fh $shell_client;
51 chmod 0755, '/tmp/bak';
53 my @hosts = grep { -d $_ } glob '*';
54 @hosts = ( $install ) if $install;
56 foreach my $hostname ( @hosts ) {
57 warn "install on $hostname\n";
58 system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
59 system "scp /tmp/bak root\@$hostname:/usr/local/bin/";
60 system "ssh root\@$hostname apt-get install -y rsync";
64 my $server = IO::Socket::INET->new(
66 LocalAddr => $server_ip,
73 warn "dir: $dir listen: $server_ip:9001\n"
79 system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
80 system "rsync -avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/"
83 while (my $client = $server->accept()) {
86 my ($hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,5);
88 my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
91 warn "$hostname [$command] $path | $message\n";
92 $message ||= "$hostname [$command] $path";
98 my $args = join(' ',@_);
100 my $out = `git $args`;
101 warn "$out\n# [", length($out), " bytes]\n" if defined $out;
106 pull_changes $hostname;
107 } elsif ( $command eq 'add' ) {
108 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
109 system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path";
110 print $client git 'add', "$hostname/$path";
111 } elsif ( $command eq 'commit' ) {
112 pull_changes $hostname;
113 print $client git( 'commit', '-m', $message,
114 ( -e "$hostname/$path" ? "$hostname/$path" : $hostname )
116 } elsif ( $command =~ m{(diff|status|log)} ) {
117 $command .= ' --summary' if $command eq 'log';
118 pull_changes $hostname if $command eq 'diff';
119 print $client git($command,$hostname);
120 } elsif ( $command eq 'revert' ) {
121 print $client git "checkout -- $hostname/$path";
122 system 'rsync', '-avv', "$hostname/$path", "root\@$hostname:$path";
124 print $client "Unknown command: $command\n";