execute commands on current path
[bak-git.git] / bak-git-server.pl
1 #!/usr/bin/perl
2
3 =head1 bak-git
4
5 Simple tracking of remote files in central git repository
6 with only shell, netcat, rsync and ssh on client
7
8 Start server, install on remote-host or upgrade with:
9
10   ./bak-git-server.pl /path/to/backup 192.168.42.42
11         [--install remote-host]
12         [--upgrade]
13
14 You will want to add following to C<~/.ssh/config>
15
16   RemoteForward 9001 localhost:9001
17
18 bak command overview:
19
20   bak add /path
21   bak commit [/path [message]]
22   bak diff [host:][/path]
23   bak status [/path]
24   bak log [/path]
25
26   bak ch[anges]
27   bak revert [host:]/path
28
29   bak - push all changed files to server
30
31 See L<http://blog.rot13.org/bak-git> for more information
32
33 =cut
34
35 use warnings;
36 use strict;
37 use autodie;
38 use IO::Socket::INET;
39 use File::Path;
40 use Getopt::Long;
41
42 my $upgrade = 0;
43 my $install;
44
45 GetOptions(
46         'upgrade!'  => \$upgrade,
47         'install=s' => \$install,
48 ) || die "$!\n";
49
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';
53
54 my $shell_client = <<__SHELL_CLIENT__;
55 #!/bin/sh
56 echo \$USER/\$SUDO_USER `hostname` `pwd` \$* | nc $server_ip 9001
57 __SHELL_CLIENT__
58
59 chdir $dir;
60 system 'git init' unless -e '.git';
61
62 if ( $upgrade || $install ) {
63         open(my $fh, '>', '/tmp/bak');
64         print $fh $shell_client;
65         close($fh);
66         chmod 0755, '/tmp/bak';
67
68         my @hosts = grep { -d $_ } glob '*';
69         @hosts = ( $install ) if $install;
70
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";
76         }
77 }
78
79 my $server = IO::Socket::INET->new(
80         Proto     => 'tcp',
81         LocalAddr => $server_ip,
82         LocalPort => 9001,
83         Listen    => SOMAXCONN,
84         Reuse     => 1
85 ) || die $!;
86
87
88 warn "dir: $dir listen: $server_ip:9001\n"
89         , $shell_client
90 ;
91
92 sub pull_changes {
93         my $hostname = shift;
94         system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
95         if ( @_ ) {
96                 open(my $files, '>>', "/tmp/$hostname.list");
97                 print $files "$_\n" foreach @_;
98                 close($files);
99         }
100         system "rsync -avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/"
101 }
102
103 while (my $client = $server->accept()) {
104         my $line = <$client>;
105         chomp($line);
106         warn "<<< $line\n";
107         my ($user,$hostname,$pwd,$command,$rel_path,$message) = split(/\s+/,$line,6);
108         $hostname =~ s/\..+$//;
109
110         my $on_host = '';
111         if ( $rel_path =~ s/^([^:]+):(.+)$/$2/ ) {
112                 if ( -e $1 ) {
113                         $on_host = $1;
114                 } else {
115                         print $client "host $1 doesn't exist in backup\n";
116                         next;
117                 }
118         }
119         my $path = $rel_path =~ m{^/} ? $rel_path : "$pwd/$rel_path";
120
121         warn "$hostname [$command] $on_host:$path | $message\n";
122
123         my $args_message = $message;
124
125         $message ||= "$path [$command]";
126         $message = "$hostname: $message";
127
128         my $dir = $path;
129         $dir =~ s{/[^/]+$}{};
130
131         my $backup_path = -e "$hostname/$path" ? "$hostname/$path" : $hostname;
132
133         sub git {
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;
138                 return $out;
139         }
140
141         if ( ! $command ) {
142                 pull_changes $hostname;
143         } elsif ( $command eq 'add' ) {
144                 mkpath "$hostname/$dir" unless -e "$hostname/$dir";
145                 while ( $path ) {
146                         system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path";
147                         print $client git 'add', "$hostname/$path";
148
149                         $args_message =~ s/^(.+)\b// || last;
150                         $path = $1;
151                         warn "? $path";
152                 }
153         } elsif ( $command eq 'commit' ) {
154                 pull_changes $hostname;
155                 $message =~ s/'/\\'/g;
156                 $user =~ s/\/$//;
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';
162                 if ( $on_host ) {
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");
166                         while(<$diff>) {
167                                 print $client $_;
168                         }
169                 } else {
170                         # commands without path will show host-wide status/changes
171                         my $backup_path = $path ? "$hostname/$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);
176                 }
177         } elsif ( $command eq 'revert' ) {
178                 if ( $on_host ) {
179                         system 'rsync', '-avv', "$on_host/$path", "root\@$hostname:$path";
180                 } else {
181                         print $client git "checkout -- $hostname/$path";
182                         system 'rsync', '-avv', "$hostname/$path", "root\@$hostname:$path";
183                 }
184         } else {
185                 print $client "Unknown command: $command\n";
186         }
187
188 }
189