From 5d6d95a304d7a4d5f6ca1bcb3a704f247478cb2f Mon Sep 17 00:00:00 2001 From: Jose V Beneyto Date: Wed, 31 Mar 2010 15:02:09 +0200 Subject: [PATCH] rsync: added missing files --- rsync/rsync.driver | 143 +++++++++++++++++++++++++++++++++++++++++++++ rsync/rsyncd | 23 ++++++++ rsync/rsyncd.conf | 15 +++++ 3 files changed, 181 insertions(+) create mode 100644 rsync/rsync.driver create mode 100644 rsync/rsyncd create mode 100644 rsync/rsyncd.conf diff --git a/rsync/rsync.driver b/rsync/rsync.driver new file mode 100644 index 0000000..31bb14a --- /dev/null +++ b/rsync/rsync.driver @@ -0,0 +1,143 @@ +#!/usr/bin/perl +# +# /etc/ports/drivers/rsync: rsync(1) driver script for ports(8) +# + +use warnings; +use strict; +use File::Basename; + +my $host = ''; +my $collection = ''; +my $destination = ''; +my %new_checkouts; +my %old_checkouts; + +sub error +{ + my $message = shift; + print "Error: $message ($!)\nUpdating failed\n"; + exit 1; +} + +sub warning +{ + my $message = shift; + print "Warning: $message ($!)\n"; +} + +if ($#ARGV < 0) +{ + print "Usage: $0 \n"; + exit 1; +} + +open(FILE, $ARGV[0]) or error("Couldn't open $ARGV[0]"); +while () +{ + chomp; + if (/^host=(.*)/) { $host = $1; } + elsif (/^collection=(.*)/) { $collection = $1; } + elsif (/^destination=(.*)/) { $destination = $1; } +} +close(FILE); + +if ($host eq '') { error("Host field not set in $ARGV[0]"); } +if ($collection eq '') { error("Collection field not set in $ARGV[0]"); } +if ($destination eq '') { error("Destination field not set in $ARGV[0]"); } + +if (-e "$destination/.checkouts") +{ + # read the old .checkouts file into memory + open(FILE, "$destination/.checkouts") or error("Couldn't read checkouts from $destination/.checkouts"); + while () + { + chomp; + $old_checkouts{$_} = 1; + } + close(FILE); +} + +print "Updating file list from " . $host . "::$collection\n"; + +# get the remote file list (new .checkouts) +open(PIPE, 'rsync -crz ' . $host . '::' . $collection . '|') or error("Couldn't open pipe to rsync"); +while () +{ + chomp; + + next if /^MOTD:/; # ignore MOTD lines + s/^(.{43})//; # ignore the first 43 characters (mode, date etc...) + next if /^.$/; # ignore the . directory + + $new_checkouts{$_} = 1; +} +close(PIPE); +error("Running rsync failed") unless $? == 0; + +print "Updating collection " . basename($destination) . "\n"; + +# now really run rsync +open(PIPE, 'rsync -crz --log-format "%o %n" ' . $host . "::$collection $destination|") or error("Couldn't open pipe to rsync"); +while () +{ + chomp; + + if (/^recv (.*)/) + { + if ($old_checkouts{$1}) + { + s/^recv/ Edit/; + } + else + { + s/^recv/ Checkout/; + } + } + + print $_ . "\n"; +} +close(PIPE); +error("Running rsync failed") unless $? == 0; + +# save new checkouts into .checkouts +open(FILE, ">$destination/.checkouts") or error("Couldn't save checkouts to $destination/.checkouts"); +foreach my $checkout (sort keys %new_checkouts) +{ + print FILE "$checkout\n"; +} +close(FILE); + +# use chroot as an additional safety measure when removing files +chroot($destination) or error("Couldn't chroot into $destination"); +chdir('/'); + +# iterate through old checkouts, remove obsolete files +foreach my $checkout (sort keys %old_checkouts) +{ + if (!$new_checkouts{$checkout}) + { + if (-f $checkout) + { + print " Delete $checkout\n"; + unlink($checkout) or warning("Couldn't delete $checkout"); + } + } +} + +# iterate through old checkouts, remove obsolete directories +foreach my $checkout (sort keys %old_checkouts) +{ + if (!$new_checkouts{$checkout}) + { + if (-d $checkout) + { + print " Delete $checkout\n"; + rmdir($checkout) or warning("Couldn't delete $checkout"); + } + } +} + +print "Finished successfully\n"; + +# End of file diff --git a/rsync/rsyncd b/rsync/rsyncd new file mode 100644 index 0000000..8507211 --- /dev/null +++ b/rsync/rsyncd @@ -0,0 +1,23 @@ +#!/bin/sh +# +# /etc/rc.d/rsyncd: start/stop rsyncd daemon +# + +case $1 in +start) + /usr/bin/rsync --daemon + ;; +stop) + kill `cat /var/run/rsyncd.pid` + ;; +restart) + $0 stop + sleep 2 + $0 start + ;; +*) + echo "usage: $0 [start|stop|restart]" + ;; +esac + +# End of file diff --git a/rsync/rsyncd.conf b/rsync/rsyncd.conf new file mode 100644 index 0000000..02bbdc6 --- /dev/null +++ b/rsync/rsyncd.conf @@ -0,0 +1,15 @@ +# +# /etc/rsyncd.conf +# + +pid file = /var/run/rsyncd.pid +log file = /var/log/rsyncd.log + +[test] + path = /tmp + comment = test share for rsyncd + hosts allow = 192.168.0.0/24 + read only = true + use chroot = true + +# End of file -- 2.26.2