Annotation of embedaddon/rsync/rsyncdb-mountinfo, revision 1.1

1.1     ! misho       1: #!/usr/bin/perl
        !             2: 
        !             3: # This script outputs data for rsyncdb --mounts.  It must output a complete
        !             4: # list of the mounts for the current host in a strict format -- 2 fields
        !             5: # with a Tab between:  $MOUNT_UNIQ\t$PATH
        !             6: #
        !             7: # The list of mounts MUST NOT contain any entry that has the same devnum
        !             8: # (st_dev) as any other entry in the list (as checked via its PATH).
        !             9: #
        !            10: # MOUNT_UNIQ is a unique string that identifies the mount on this host.
        !            11: # This cannot be the devnum (st_dev) because that can vary depending on the
        !            12: # mount order or be reused for different mounts if they are not mounted at
        !            13: # the same time.  Ideally this would be its UUID value, if that is available
        !            14: # on this OS.  This script looks in /dev/disk/by-uuid for the current UUID
        !            15: # mappings).  If the UUID is not found, the fallback default is the string
        !            16: # "Mount of $devname", which should be adequate for situations that don't
        !            17: # use removable media (though you may need to take steps to weed-out removable
        !            18: # mounts).
        !            19: #
        !            20: # You can override the MOUNT_UNIQ value by putting a .rsyncdb_mount_uniq
        !            21: # file in the root directory of any mount, at which point it is up to you
        !            22: # to make sure that the value stays unique (note that all sequences of
        !            23: # whitespace are transformed into a single space, and leading/trailing
        !            24: # whitespace is removed).
        !            25: #
        !            26: # MOUNT_UNIQ may never contain a Tab but it would be legal for PATH to have
        !            27: # a Tab (just really weird).  Neither may have a CR or LF in it.
        !            28: #
        !            29: # The maximum size for MOUNT_UNIQ is 256 characters.
        !            30: #
        !            31: # If this script doesn't meet your needs, feel free to edit/replace it and
        !            32: # choose some other method of finding a unique value for each mount.  If you
        !            33: # come up with a good idiom that might be useful to others, please share it
        !            34: # with the rsync mailing list.
        !            35: 
        !            36: use strict;
        !            37: use warnings;
        !            38: use Cwd 'abs_path';
        !            39: 
        !            40: my @MOUNT_FILES = qw( /proc/mounts /etc/mtab );
        !            41: my $VALID_DEVICE_REGEX = qr{^/dev|^rootfs$};
        !            42: my $UUID_DIR = '/dev/disk/by-uuid';
        !            43: my $OVERRIDE_FILE = '.rsyncdb_mount_uniq';
        !            44: 
        !            45: my (%hash, %uuid);
        !            46: 
        !            47: if (-d $UUID_DIR) {
        !            48:     foreach my $uuid (glob "$UUID_DIR/*") {
        !            49:        my $lnk = readlink($uuid);
        !            50:        if ($lnk !~ m{^/}) {
        !            51:            $lnk = abs_path("$UUID_DIR/$lnk");
        !            52:        }
        !            53:        $uuid =~ s{.*/}{};
        !            54:        $uuid{$lnk} = $uuid;
        !            55:     }
        !            56: }
        !            57: 
        !            58: foreach my $mount_file (@MOUNT_FILES) {
        !            59:     if (open MOUNTS, $mount_file) {
        !            60:        while (<MOUNTS>) {
        !            61:            my ($devname, $path) = (split)[0,1];
        !            62:            next unless $devname =~ /$VALID_DEVICE_REGEX/;
        !            63: 
        !            64:            my ($devno) = (stat($path))[0];
        !            65:            next unless defined $devno; # Skip if mount is invalid.
        !            66:            next if $hash{$devno}++; # SKip if we've seen this devno earlier.
        !            67: 
        !            68:            my $mount_uniq = $uuid{$devname} ? $uuid{$devname} : "Mount of $devname";
        !            69:            if (open UNIQ, '<', "$path/$OVERRIDE_FILE") {
        !            70:                $mount_uniq = <UNIQ>;
        !            71:                close UNIQ;
        !            72:                $mount_uniq =~ s/\s+/ /g; # This ensures no tab, CR, nor LF.
        !            73:                $mount_uniq =~ s/^ | $//g; # .. and no leading or trailing whitespace.
        !            74:            }
        !            75:            print $mount_uniq, "\t", $path, "\n";
        !            76:        }
        !            77:        close MOUNTS;
        !            78:        exit;
        !            79:     }
        !            80: }
        !            81: 
        !            82: die "Failed to to open any mount files: @MOUNT_FILES\n";

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>