rsync management process
[cloudstore.git] / rsync-piper.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use autodie;
6 use POSIX;
7 use File::Slurp;
8 use IO::Select;
9 use Time::HiRes;
10 use Data::Dump qw(dump);
11
12 my $dir = '/srv/cloudstore/var';
13 my $log_fifo = "$dir/rsyncd.log";
14 our $pid_file = "$dir/rsyncd.pid";
15 my $cfg_file   = "$dir/rsyncd.conf";
16 my $users    = "users";
17
18 mkdir $dir if ! -e $dir;
19
20 unlink $log_fifo if -e $log_fifo;
21 mkfifo $log_fifo, 0700;
22
23 sub kill_rsync {
24         return unless -e $pid_file;
25         my $pid = read_file $pid_file;
26         warn "kill_rsync $pid_file = $pid";
27         kill 1, $pid && unlink $pid_file; warn "killed old rsync server using pid $pid";
28 }
29
30 my $rsync_config = qq{
31
32 #uid = nobody
33 #gid = nogroup
34 #use chroot = yes
35 use chroot = no
36
37 #max connections = 4
38 lock file = $dir/rsyncd.lock
39
40 #syslog facility = local5
41 log file  = $log_fifo
42
43 transfer logging = yes
44 log format = TRANSFER %o %u %h %a %m %l %f
45 max verbosity = 5
46
47 pid file  = $pid_file
48
49 # don't check secrets file permission (uid)
50 strict modes = no
51
52 pre-xfer exec = /srv/cloudstore/pre-xfer.sh
53 post-xfer exec = /srv/cloudstore/post-xfer.sh
54
55 [dpavlin]
56         path = /srv/cloudstore/users/dpavlin/blob
57         auth users = dpavlin
58         secrets file = /srv/cloudstore/secrets/dpavlin
59         read only = false
60
61 };
62
63 write_file $cfg_file, $rsync_config;
64 warn "created $cfg_file ", -s $cfg_file, " bytes\n";
65
66 kill_rsync;
67 sub DESTROY { kill_rsync; };
68
69 my $exec = "rsync --daemon --config $cfg_file --no-detach --port=6501";
70
71 #die "could not fork\n" unless defined(my $pid = fork);
72 #unless ($pid) {
73 #       warn "start server with $exec\n";
74 #       exec $exec || die $!;
75 #}
76
77 warn "START $exec\n";
78 open(my $rsync_fd, '-|', $exec);
79
80 while ( ! -e $pid_file ) {
81         sleep 1;
82 }
83
84
85 while(1) {
86         warn "# reading log output from $log_fifo\n";
87         open(my $log, '<', $log_fifo);
88         while( my $line = <$log> ) {
89                 print "LINE: $line";
90         }
91         close($log);
92         sleep 1;
93 }
94