何かするスクリプト

なにかするスクリプトのひな形

XXXX.NNNNNNみたいなファイルの処理で使う。

#!/usr/bin/perl
#
# Usage: 
#
# Remark: Don't use "RESET MASTER"
use warnings;
use strict;
use Sys::Syslog;

my $hist = '/usr/local/mysql/data/hist.dat'; # Save the file number that was last processed.
my $basedir = '/usr/local/mysql/data/';
my $binlog_prefix = "binlog";

##----------------------------
## Global variables
##----------------------------
my $MIN;
my $MAX;
my %BINLOG;
##----------------------------
## Functions
##----------------------------

# usage: get_start_num(hist_file)
# return: a value which is described in hist_file.
sub get_start_num {
    my @args = @_;
    my $fh;
    my $num = 0;
    if (open($fh, '<', $args[0])) {
	while (my $line = <$fh>) {
	    chop($line);
	    $num = $line;
	}
	close $fh;
    }
    else {
	open($fh, '>', $args[0]) or die;
	print $fh "$num\n";
	close $fh;
    }
    return $num;
}

# usage: get_max_num()
# return: 
sub get_max_num {
    my @args = @_;
    my $max = 0;

    opendir  my $dh, $args[0] or die "$args[0]:$!";
    while (my $file = readdir $dh) {
        next if $file =~ /^\.{1,2}$/; # skip '.', '..'                                              
        if ($file =~ /^$binlog_prefix\.\d+/) {
            my $tmp;
            my $key = $file;
            $key =~ s/^$binlog_prefix\.//g;
            $BINLOG{$key} = $file;
            $tmp = $key + 0; # string to number                                                     
            $max = $tmp if ($max < $tmp);
        }
    }
    closedir $dh;

    return $max;
}

# usage: update_hist(num, hist_file)
# 
sub update_hist {
    my $MAX = $_[0];
    my $hist = $_[1];
    my $fh;
    open($fh, '>', $hist) or die;
    print $fh $MAX . "\n";
    close($fh);
}

sub  message {
    openlog('test', 'pid', 'local0');
    syslog('info', $_[0]);
    closelog();
}

##----------------------------
## Main process
##----------------------------
&message("backup started.");

$MIN = &get_start_num($hist);
$MAX = &get_max_num($basedir); 

###
## FLUSH LOGS
###

##
##
foreach my $num (sort keys %BINLOG) {
    my $file = $BINLOG{$num};
    $num = $num + 0; # string to integer
    if ($MIN < $num && $num <= $MAX) {
       #
       # do something  ; error -> exit(-1)
       # 
       my @command = ('ls', $basedir."/".$file);
       my $ret = system @command;
       if ($ret != 0) {
            &message("Error: backup stopped.");
            exit(-1);
        }
        &update_hist($num, $hist);
    }
}

##
##
&message("backup completed.");

exit(0);

なにかするスクリプト

#!/bin/bash
#
# usage: XXX hostname days
#
MYSQL=/usr/local/mysql/bin/mysql
USER=root
CONF=/root/.my.cnf
HOST=localhost
COMMAND=""
DAYS=3

if [ $# -ne 2 ]; then
    exit -1
else
    HOST=$1;
    if [ $2 -gt $DAYS ]; then
        DAYS=$2
    fi
    COMMAND="PURGE MASTER LOGS BEFORE timestamp(date_sub(current_date, interval '$DAYS' day));"
fi

$MYSQL --defaults-extra-file=$CONF -h $HOST -u $USER -e "$COMMAND"                                   

if [ $? -ne 0 ]; then
    exit -1
fi

exit 0
スクリプトの準備
$ cat /usr/local/bin/logpurge.pl
#!/usr/bin/perl
#
# usage: XXX basedir log_prefix ndays
#
#
use strict;
use warnings;
use Sys::Syslog;

##--------------------------
## Global Variables
##--------------------------
my @NDate;
my $min_date;

my $basedir = '/usr/local/mysql/data/';
my $log_prefix = "secure";
my $ndays = 3;
##--------------------------
## Functions
##--------------------------
sub get_date {    
    my ($time) = $_[0];
    my @date;
    my ($sec,$min,$hour,$mday,$month,$year,$wday,$stime) 
	= localtime($time+0);
    $date[0] = $year + 1900;
    $date[1] = $month + 1;
    $date[2] = $mday;
    
    return @date;
}

sub ndays_ago {
    my $ndays = $_[0];
    $ndays += 0; # string to number
    return &get_date(time - ($ndays * 24 * 3600));
}

sub  message {
    openlog(‘logpurge’, 'pid', 'local0');
    syslog('info', $_[0]);
    closelog();
}

##--------------------------
## Main Process
##--------------------------
&message("purge log(". $log_prefix . ") started");

if ($#ARGV != 2) {
    &message("Error: purge log(". $log_prefix . ")");
    exit(-1);
}

$basedir = $ARGV[0];
$log_prefix = $ARGV[1];
$ndays = $ARGV[2] if ($ndays < $ARGV[2]);

@NDate = &ndays_ago($ndays);

$min_date = $NDate[0] . $NDate[1] . $NDate[2];
$min_date+=0; # string to number


opendir my $dh, $basedir or die "$basedir:$!";
while (my $file = readdir $dh) {
    next if $file =~ /^\.{1,2}$/; # skip '.', '..'
    if ($file =~ /^$log_prefix\.\d+\_\d+\.log/) {
	my $date = $file;
	$date =~ s/$log_prefix\.//g;
	$date =~ s/_\d+\.log//g;
	$date+=0; # string to number

	if ($date < $min_date) {
	    #
	    # do something
	    #
            my @command = ('/bin/rm', $basedir . "/" . $file);
            my $ret = system @command;
            if ($ret != 0) {
		&message("Error: purge log(". $log_prefix . ") " . $file . " remove f\
ailed.");
            }
#           printf ("\tfile = %s  (%d)\n", $file, $date);                             

            &message("purge log(". $log_prefix . ") " . $file . " removed.");
	}
    }
}
closedir $dh;

&message("purge log(". $log_prefix . ") completed");

exit(0);
$ chmod +x /usr/local/bin/purgelog.pl
crontab
$cat /usr/local/bin/purgelog
#!/bin/bash

PURGELOG=/usr/local/bin/purgelog.pl
BASEDIR=/var/log/fluentd
NDAYS=4

# host2
$PURGELOG $BASEDIR/host2 secure  10  2>&1 > /dev/null

## for other hosts……
##
##

exit 0
$ chmod +x /usr/local/bin/purgelog
$ whoami
root
$ crontab -e
18 21 * * *  /usr/local/bin/purgelog 2>&1 > /dev/null