From Mageia wiki
Jump to: navigation, search

For more informations about making a Mageia mirror, please read the mirror readme file.

You can find here an example script that could be used to mirror Mageia :

#! /usr/bin/perl
# Raphaël Gertz (rapsys) <rapsys@rapsys.eu>
# Copyright (C) 2010-2011 by Raphaël Gertz
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Strict mode
use strict;
use warnings;

# Global class
use Data::Dumper;
use File::Basename;
use File::Rsync;
use Sys::Syslog;

use POSIX qw(setsid);

# Files
my $basename = basename($0);
my $lockFile = '/tmp/.'.$basename.'.pid';

# Daemonize process
sub daemonize {
    chdir '/' or die $basename.': can\'t chdir to /: '.$!;
    umask 0;
    open STDIN, '/dev/null' or die $basename.': can\'t read /dev/null: '.$!;
    open STDOUT, '>/dev/null' or die $basename.': can\'t write to /dev/null: '.$!;
    open STDERR, '>/dev/null' or die $basename.': can\'t write to /dev/null: '.$!;
    defined(my $pid = fork) or die $basename.': can\'t fork: '.$!;
    exit if $pid;
    setsid or die $basename.': Can\'t start a new session: '.$!;
    return 1;
}

# Lock pid
sub plock {
        my $lockFile = shift;

        # Check process status
        if (-f $lockFile) {
                my $ctime = (stat($lockFile))[10];
                # Check if file exists since at least 24h
                if (time() - $ctime > 86400) {
                        open FD, $lockFile;
                        my $pid = <FD>;
                        close FD;
                        # Kill all rsync processes
                        `killall -9 rsync > /dev/null 2>&1`;
                        sleep 30;
                        # Avoid kill on critical pid
                        `kill -9 $pid > /dev/null 2>&1` if ($pid > 100);
                        unlink $lockFile;
                        print $basename.': '.$lockFile.' older than 24 hour, killing '.$pid.' and remove lock'."\n";
                } else {
                        exit 0;
                }
        }

        # create lock file 
        open FD, '>', $lockFile;
        print FD $$;
        close FD;
}

# Unlock pid
sub punlock {
        my $lockFile = shift;
        unlink $lockFile;
}

# Lock process
plock($lockFile);

# Daemonize
#daemonize();

# Open syslog
openlog($basename, 'nofatal,pid', 'user');

# Syslog
syslog('info', $basename.': rsync start');

# Rsync object
my $obj = File::Rsync->new({'archive' => 1, 'hard-links' => 1, 'delete-after' => 1, 'sparse' => 1, 'verbose' => 1, 'no-perms' => 1, 'chmod' => ['u=rwX,go=rX']});
if (defined $obj) {
        $obj->exec({src => 'rsync://distrib-coffee.ipsl.jussieu.fr/mageia', dest => '/var/ftp/mageia'}) or warn $basename.': Rsync failed'."\n";
}

# Syslog
syslog('info', $basename.': rsync finished');

# Close log
closelog();

# Unlock process
punlock($lockFile);

# Clean exit
exit 0;
/etc/cron.d/mirsync
# Sync mirror
*/5 * * * * ftp test -x /usr/local/bin/mirsync && /usr/local/bin/mirsync