Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
pbackup/bin/limit_notify.pl
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
257 lines (198 sloc)
6.16 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/perl -w | |
# @afk: ssh {backupserver} /project/admin/pbackup/bin/limit_notify.pl {prj_name} | |
# | |
# root:afk:/project/admin/pbackup/# ssh null /project/admin/pbackup/bin/limit_notify.pl prj_chungmap | |
# | |
use strict; | |
use Data::Dumper; | |
use Getopt::Long; | |
use Mail::Mailer; | |
$|=1; | |
my %opt = (); | |
our $HOST=''; | |
GetOptions (\%opt, 'send!','to=s'); | |
my $backupserver = $ARGV[0]; | |
my $job = $ARGV[1]; | |
die "usage: $0 backupserver job\n" unless defined($job); | |
my $project = $job; | |
$project =~ s{^prj_}{/project/}; | |
$project =~ s{^home_}{/home/}; | |
$project =~ s{^cfdl_}{/confidential/}; | |
my $excludesdat = '/project/admin/pbackup/etc/excludes.dat'; | |
open I,'<',$excludesdat or die "cant read $excludesdat:$!"; | |
while(<I>) { | |
m{^\s*$project\s} and die "$project already excluded. see $excludesdat line $.\n"; | |
} | |
close I; | |
my %SERVER; | |
my $serverdat = '/project/admin/pbackup/etc/server.dat'; | |
open I,'<',$serverdat or die "cant read $serverdat:$!"; | |
while(<I>) { | |
/^\s*\#/ and next; | |
m{^\s*(\S+)\s+(\S+)} and $SERVER{$1} = $2; | |
} | |
close I; | |
my @history = map {s{transferred in\s}{/ };$_} `ssh $backupserver /usr/local/package/bin/pbackup job $job`; | |
print Dumper(\@history); | |
my $ll = $history[@history-1]; | |
{ | |
chomp($ll); | |
print "last line: $ll\n"; | |
my ($size) = $ll =~ m/(\d+\.\d+\s.byte) \(/; | |
defined($size) or die "### ERR: no size parsed:\n\"$ll\"\n"; | |
print "# size: $size\n"; | |
my ($path) = $ll =~ m/\)\s+\S+:(.*)$/; | |
defined($path) or die "### ERR: no path parsed:\n\"$ll\"\n"; | |
print "# path: $path\n"; | |
my ($host) = $path =~ m(/amd/(\w+)); | |
defined($host) or die "### ERR: no host parsed:\n\"$ll\"\n"; | |
print "# host: $host\n"; | |
print "# backup server: $backupserver\n"; | |
my $dir = "$path/."; | |
if ($host ne host()) { | |
# $dir = "/host/$host$dir"; | |
$dir = "$project/."; | |
} | |
print "# ($project) stat($dir) ..."; | |
my $gid=`ssh $host stat -c '%g' $dir`; | |
# my @s = stat($dir); | |
# print "\n"; | |
# | |
# @s or die "### ERR: cannot stat('$dir')"; | |
# print Dumper(\@s); | |
# | |
# my $gid = $s[5]; | |
my $recipients = defined($opt{to}) ? $opt{to} : resolve_group($gid,1); | |
my %VAR = ( | |
'#PROJECT#' => $project, | |
'#SIZE#' => $size, | |
'#HOST#' => $host, | |
'#DATE#' => date_tag(), | |
'#HISTORY#' => join('',@history), | |
); | |
print Dumper(\%VAR); | |
# my $text = ''; | |
# while(<DATA>) { | |
# my $l = $_; | |
# for my $v ( keys %VAR ) { | |
# $l =~ s/($v)/$VAR{$1}/eg; | |
# } | |
# $text .= $l; | |
# } | |
my $text = join( "", map { for my $v ( keys %VAR ) { s/($v)/$VAR{$v}/eg;};$_ } (<DATA>) ); | |
my @rec = split(',',$recipients); | |
if ($opt{send}) { | |
send_mail( | |
'helpdesk@molgen.mpg.de', | |
[@rec], | |
"WARNING: $project daily backup disabled. Size exceeded 1024 GB.", | |
$text, | |
'helpdesk@molgen.mpg.de', | |
); | |
} | |
else { | |
print "### will send:\n$text\n"; | |
print "### will send to: ".join(',',@rec)."\n"; | |
print "### change recipients with --to foo\@bar.com\n"; | |
print "### finally really send mail with --send\n"; | |
} | |
my $recipients_nd = $recipients; $recipients_nd =~ s/.molgen\.mpg\.de//g; | |
print "### disable backup of $project manually:\n"; | |
printf "# echo '%-20s # ".date_tag().": $size exceeded, mail to: $recipients_nd' >>$excludesdat\n",$project; | |
print "# cd /project/admin/pbackup && make amd_scan\n"; | |
} | |
sub send_mail { | |
my $from = shift; | |
my $to = shift; | |
my $subject = shift; | |
my $text = shift; | |
my $bcc = shift; | |
my $mailer=new Mail::Mailer 'smtp',Server=>'harry.molgen.mpg.de'; | |
$mailer->open | |
( | |
{ | |
From => $from, | |
To => $to, | |
Subject => $subject, | |
(defined $bcc ? (Bcc => $bcc) : () ), | |
} | |
); | |
print $mailer $text; | |
print $mailer "\n"; | |
$mailer->close; | |
print "# mail sent to ".join(', ',@{$to}).": $subject\n"; | |
} | |
sub host { | |
if ($HOST eq '') { | |
$HOST = `uname -n`; | |
chomp($HOST); | |
$HOST =~ s/.molgen.mpg.de$//; | |
} | |
return $HOST; | |
} | |
sub date_tag { | |
my $t = shift; | |
if (!defined($t)) { | |
$t = time; | |
} | |
@_ = localtime($t);$_[5]+=1900;$_[4]++; | |
return sprintf "%d.%d.%04d",$_[3],$_[4],$_[5]; | |
} | |
sub Send_mail { | |
my $from = shift; | |
my $to = shift; | |
my $subject = shift; | |
my $text = shift; | |
print "$text\n"; | |
} | |
sub resolve_group { | |
my $gid = shift; | |
my $fqdn = shift; | |
print "# getgrgid($gid)\n"; | |
my @members = getgrnam(getgrgid($gid)); | |
print "# getgrnam() = ". Dumper(\@members); | |
my $r = undef; | |
if (@members) { | |
$gid = $members[3]; | |
$gid = join( ' ' , grep {defined(getpwnam($_)) } split(/ /,$gid)); | |
if ($gid ne '') { | |
if ($fqdn) { | |
$gid = join(' ',map {$_ . '@molgen.mpg.de' } split( / /,$gid )); | |
} | |
$gid =~ s/ /,/g; | |
$r = $gid; | |
} | |
else { | |
die("ERROR: $gid doesn't resolve to members"); | |
} | |
} else { | |
die("ERROR: $gid doesn't resolve to a group"); | |
} | |
return $r; | |
} | |
__DATA__ | |
Dear #PROJECT# member, | |
#PROJECT# is currently using up to #SIZE# of data storage on #HOST#. | |
Backup of this huge amount of data might take longer than 24 hrs | |
blocking our backup server to save even smallest file changes on | |
other projects. | |
To prevent the risk of data loss on other data folders we have | |
DISABLED daily file backup on #PROJECT# since #DATE# (d.m.yyyy) | |
If you delete or modify files by accident we will not be able to recover | |
older versions of these files from backup. If we experience a server hardware | |
failure, your data inside #PROJECT# might be lost forever. | |
To regain backup functionality please clean up #PROJECT# and reduce the | |
allocated amount of data to approx. 800 GB. | |
All backup data of #PROJECT# so far will *expire* in approx. 3 months | |
You have to explicitly notify us about this: backup can only be re-enabled manually! | |
If you have files/folders you would like to get archived on 'tape' feel free | |
to contact us. We will be happy to take care of your long term archive requests. | |
Please consider 'archive' before deleting. Please do not archive on external media | |
like USB hard disks or even DVDs (drop one and you'll find out why). | |
You have to explicitly notify us when disk usage drops below 800 GB. | |
Backup can only be re-enabled manually! | |
Kind regards, | |
IT Helpdesk | |
( this mail has been generated automatically ) | |
full backup history: | |
#HISTORY# |