--- a/bin/ftbackup Mon May 02 16:40:11 2011 +0200
+++ b/bin/ftbackup Tue May 03 13:05:37 2011 +0200
@@ -15,17 +15,27 @@
use English qw(-no_match_vars);
use if $ENV{DEBUG} => qw(Smart::Comments);
use File::Temp;
+use Log::Log4perl qw(:easy);
$ENV{LC_ALL} = "C";
my $ME = basename $0;
+
my $VERSION = "1.0";
+my $LOG_LEVEL = $ALL;
my @CONFIGS = ("/etc/$ME.conf", "$ENV{HOME}/.$ME.conf", "$ME.conf");
+Log::Log4perl->easy_init({
+ level => $ALL,
+ file => -t STDERR ? "STDERR" : ">/var/log/ftbackup/log",
+ layout => "%-6p{1} - %d (%5r) | %m%n"
+ });
+
my $HOSTNAME = hostname;
my $NOW = time();
+
my $opt_level = undef;
my $opt_today = strftime("%F", localtime $NOW);
my @opt_debug = ();
@@ -53,7 +63,8 @@
our @AT_EXIT;
END { $_->() foreach @AT_EXIT }
-$SIG{INT} = sub { warn "Got signal INT\n"; exit 1 };
+$SIG{INT} = sub { INFO "Got signal INT\n"; exit 1 };
+$SIG{__DIE__} = sub { LOGDIE @_ };
my %CONFIG = (
FTP_DIR => "backup/<LABEL>/<HOSTNAME>",
@@ -63,14 +74,11 @@
KEEP => 2,
);
-END {
- say STDERR "*** $ME STOP: " . localtime if not -t STDERR;
-}
+ALWAYS "START";
+END { ALWAYS "STOP" }
MAIN: {
- say STDERR "*** $ME START: " . localtime if not -t STDERR;
-
Getopt::Long::Configure("bundling");
GetOptions(
"l|level=i" => \$opt_level,
@@ -113,24 +121,11 @@
if system("command -v lvm >/dev/null");
push @errors, "Command `fsck' not found. ($ENV{PATH})"
if system("command -v fsck >/dev/null");
- die "$ME: pre-flight check failed:\n\t", join("\n\t" => @errors), "\n"
+ LOGDIE "$ME: pre-flight check failed:\n\t", join("\n\t" => @errors), "\n"
if @errors;
my $ftp;
- if (not "output" ~~ \@opt_debug) {
- $ftp = new FTP(
- $cf{FTP_HOST},
- Passive => $cf{FTP_PASSIVE},
- Debug => "ftp" ~~ \@opt_debug,
- ) or die $@;
- $ftp->login or die $ftp->message;
- $ftp->home($ftp->try(pwd => ()));
- $ftp->try(binary => ());
- $ftp->try(mkpath => $cf{FTP_DIR});
- $ftp->try(cwd => $cf{FTP_DIR});
- }
-
# get_history the situation - we rely on $opt_dumpdates
@devs = get_history(@devs);
@devs = calculate_level($cf{FULL_CYCLE}, @devs);
@@ -173,6 +168,20 @@
exit;
}
+ if (not "output" ~~ \@opt_debug) {
+ $ftp = new FTP(
+ $cf{FTP_HOST},
+ Passive => $cf{FTP_PASSIVE},
+ Debug => "ftp" ~~ \@opt_debug,
+ ) or LOGDIE $@;
+ $ftp->login or LOGDIE $ftp->message;
+ $ftp->home($ftp->try(pwd => ()));
+ $ftp->try(binary => ());
+ $ftp->try(mkpath => $cf{FTP_DIR});
+ $ftp->try(cwd => $cf{FTP_DIR});
+ }
+
+
# and now we can start doing something with our filesystems
DEVICE: foreach my $dev (@devs) {
my $dir = $dev->{mountpoint};
@@ -202,11 +211,11 @@
if ($dev->{level} > 0) {
if (!@last) {
$dev->{level} = 0;
- warn "adjusted backup level to 0, last full backup missing\n";
+ WARN "adjusted backup level to 0, last full backup missing\n";
}
elsif (($NOW - $last[0]) > ($cf{FULL_CYCLE} * 86_400)) {
$dev->{level} = 0;
- warn sprintf
+ WARN sprintf
"adjusted backup level to 0, last full backup is %.1f days old\n",
($NOW - $last[0]) / 86_400;
}
@@ -229,7 +238,7 @@
verbose "Creating snapshot $snap\n";
system($_ =
"lvcreate -s -L 1G -n $snap $dev->{lvm}{path} >/dev/null");
- die "failed system command: $_\n" if $?;
+ LOGDIE "failed system command: $_\n" if $?;
$dev->{cleanup} = sub {
system "lvdisplay $snap &>/dev/null"
@@ -244,7 +253,7 @@
system($_ =
"fsck -f @{[$opt_verbose ? '-C0' : '']} -y $device");
last if not $?;
- warn "fsck on $device (using: $_) failed"
+ INFO "fsck on $device (using: $_) failed"
. ($retries > 1 ? ", retrying…\n" : "") . "\n";
}
@@ -313,7 +322,7 @@
}
else {
print while <$dump>;
- warn "STOPPED after the first dump\n";
+ WARN "STOPPED after the first dump to STDOUT\n";
exit;
}
$dev->{cleanup}->() if $dev->{cleanup};
@@ -348,7 +357,7 @@
# executable exclude list
# <inum><space><filename><NULL>
local $/ = "\0";
- open(my $ex, "-|", "$excludelist") or die "Can't open $excludelist: $!\n";
+ open(my $ex, "-|", "$excludelist") or LOGDIE "Can't open $excludelist: $!\n";
while (<$ex>) {
chomp;
my ($i, $f) = split " ", $_;
@@ -357,7 +366,7 @@
}
}
else {
- open(my $ex, "<", $excludelist) or die "Can't open $excludelist: $!\n";
+ open(my $ex, "<", $excludelist) or LOGDIE "Can't open $excludelist: $!\n";
while (<$ex>) { chomp; @files{(glob)} = () }
@inodes{ map { (stat)[1] } keys %files} = ();
@@ -426,17 +435,17 @@
{
my $p = (stat)[2] & 07777;
my $u = (stat _)[4];
- die
+ LOGDIE
"$ME: $_ has wrong permissions: found @{[sprintf '%04o', $p]}, need 0600\n"
if $p != 0600;
- die
+ LOGDIE
"$ME: owner of $_ ($u) is not the EUID ($EUID) of this process\n"
if (stat _)[4] != $EUID;
# FIXME: should check the containing directories too!
};
- open(my $f, $_) or die "Can't open $_: $!\n";
+ open(my $f, $_) or LOGDIE "Can't open $_: $!\n";
my %h = map { split /\s*=\s*/, $_, 2 } grep { !/^\s*#/ and /=/ } <$f>;
map { chomp } values %h;
%r = (%r, %h);
@@ -455,11 +464,13 @@
use strict;
use warnings;
use base qw(Net::FTP);
+ use Log::Log4perl qw(:easy);
my %data;
sub new {
my $class = shift;
+ WARN("ABER HALLO");
return bless Net::FTP->new(@_) => $class;
}
@@ -467,7 +478,7 @@
my $self = shift;
my $func = shift;
$self->$func(@_)
- or die "FTP $func failed: " . $self->message . "\n";
+ or LOGDIE "FTP $func failed: " . $self->message . "\n";
}
sub mkpath {
@@ -498,7 +509,7 @@
sub update_devnames($$$) {
my ($file, $from, $to) = @_;
- open(my $f, "+>>" => $file) or die "Can't open $file: $!\n";
+ open(my $f, "+>>" => $file) or LOGDIE "Can't open $file: $!\n";
seek($f, 0, 0);
my $_ = join "", <$f>;
s/^$from\s/$to /mg;
@@ -535,7 +546,7 @@
seek($dd, 0, 0);
while (<$dd>) {
my ($dev, $level, $date) = /^(\S+)\s+(\d+)\s+(.{30})/
- or die "Can't parse $opt_dumpdates: `$_'\n";
+ or LOGDIE "Can't parse $opt_dumpdates: `$_'\n";
my $rdev = real_device($dev);
my $devno = devno($rdev);
@@ -578,9 +589,9 @@
sub get_estimate($$) {
my ($dev, $level) = @_;
- print STDERR "% estimating $dev->{rdev} at level $level: ";
+ INFO "estimating $dev->{rdev} at level $level";
chomp(my $_ = `dump -S -$level -E $dev->{exclude}{inodes} $dev->{rdev}`);
- print STDERR human_number($_) . "Byte\n";
+ INFO "level $level will use ~ " . human_number($_) . "B";
return $_;
}
@@ -609,7 +620,7 @@
}
}
}
- warn "% $dev->{dev} will use level $dev->{level}\n";
+ INFO "$dev->{dev} will use level $dev->{level}\n";
}
return @devs;
@@ -617,7 +628,7 @@
sub slurp($) {
my $f = shift;
- open(my $fh, "<", $f) or die "Can't open $f: $!\n";
+ open(my $fh, "<", $f) or LOGDIE "Can't open $f: $!\n";
return <$fh>;
}