From 0475702456819c9b1862b4a44d721a8455caebf5 Mon Sep 17 00:00:00 2001 From: hjp Date: Mon, 18 Jun 2007 19:42:08 +0000 Subject: [PATCH] Get targets from database. Store metadata of backed up files in database. Use -i option of ssh. --- backup | 10 +++---- lib/Simba/CA.pm | 72 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/backup b/backup index d4b6d16..51ae11c 100755 --- a/backup +++ b/backup @@ -7,10 +7,10 @@ $ENV{PATH} = "/usr/bin"; open(my $log, '>>', '/var/log/simba/ca.log'); $log->autoflush(1); -my $ca = Simba::CA->new({fh_log => $log}); -$ca->targets([ - { host => 'bernon.wsr.ac.at', dir => '/' } - ]); -$ca->basedir('/backup/simba'); + +my $ca = Simba::CA->new({ + dbi_file => $ENV{SIMBA_DB_CONN} || "$ENV{HOME}/.dbi/simba", + fh_log => $log, + }); $ca->run(); diff --git a/lib/Simba/CA.pm b/lib/Simba/CA.pm index 0f13b73..7b4c5ff 100644 --- a/lib/Simba/CA.pm +++ b/lib/Simba/CA.pm @@ -13,6 +13,7 @@ use List::Util qw(min); use IO::Handle; use File::stat; use Scalar::Util qw(tainted); +use DBI; Readonly my $BUFSIZE => 128 * 1024; @@ -23,9 +24,6 @@ sub new { bless $self, $class; $self->{basedir} = '/backup'; - $self->{targets} = [ - { host => 'localhost', dir => '/var/tmp', }, - ]; $self->{unknown_uid} = 65533; $self->{unknown_gid} = 65533; $self->{fh_log} = exists($opt->{fh_log}) ? $opt->{fh_log} : \*STDERR; @@ -50,6 +48,16 @@ sub new { } ); } + $self->{targets} = $self->{dbh}->selectall_arrayref("select * from filesets", { Slice => {} }); + if ($ENV{HOME} =~ m{([/\w]*)}) { + if (-f "$1/.ssh/id_rsa") { + if (my $st = stat("$1/.ssh/id_rsa")) { + if ($st->uid == $>) { + $self->{ssh_id_file} = "$1/.ssh/id_rsa"; + } + } + } + } return $self; } @@ -78,13 +86,18 @@ sub backup2disk { my ($list_pid, $list_cfd, $list_dfd); # connection to get list of files my ($file_pid, $file_cfd, $file_dfd); # connection to get content of files - $list_pid = open2($list_dfd, $list_cfd, "/usr/bin/ssh", "-l", "simba_da", $target->{host}, "da"); + $list_pid = open2($list_dfd, $list_cfd, + "/usr/bin/ssh", + "-l", "simba_da", + $self->{ssh_id_file} ? ("-i", $self->{ssh_id_file}) : (), + $target->{host}, "da"); $list_cfd->printflush("list $target->{dir}\n"); # XXX - encode! close($list_cfd); while (<$list_dfd>) { chomp; $self->log(10, "file: $_"); # split into fields + chomp; my $f = $self->parse($_); # if file is already present if ($f->{t} eq 'f') { @@ -95,7 +108,11 @@ sub backup2disk { # else request from da unless ($file_pid) { - $file_pid = open2($file_dfd, $file_cfd, "/usr/bin/ssh", "-l", "simba_da", $target->{host}, "da"); + $file_pid = open2($file_dfd, $file_cfd, + "/usr/bin/ssh", + "-l", "simba_da", + $self->{ssh_id_file} ? ("-i", $self->{ssh_id_file}) : (), + $target->{host}, "da"); } $file_cfd->printflush("get $target->{dir}/$f->{name}\n"); # XXX - encode! my $header = <$file_dfd>; # this should be the same as $_ - check? @@ -128,9 +145,11 @@ sub backup2disk { if ($trailer =~ /^fail /) { $self->log(5, $trailer); } elsif ($trailer =~ /^chk sha1 (\w+)/) { - if ($sha1->hexdigest ne $1) { + my $checksum = $sha1->hexdigest; + if ($checksum ne $1) { $self->log(5, "checksum error\n"); } + $f->{checksum} = $checksum; } else { $self->log(5, "unexpected trailer $trailer\n"); } @@ -157,6 +176,7 @@ sub backup2disk { $self->log(5, "ignored $_\n"); } # insert into DB. + $self->db_record_version($target, $f); } } @@ -335,5 +355,45 @@ sub log_level { return $self->{log_level}; } +sub db_record_version { + my ($self, $target, $f) = @_; + + my $db_f = $self->{dbh}->selectall_arrayref("select * from files where fileset=? and path=?", + { Slice => {} }, + $target->{id}, $f->{name}); + unless (@$db_f) { + $self->{dbh}->do("insert into files(fileset, path) values(?, ?)", + {}, + $target->{id}, $f->{name}); + $db_f = $self->{dbh}->selectall_arrayref("select * from files where fileset=? and path=?", + { Slice => {} }, + $target->{id}, $f->{name}); + + } + $self->{dbh}->do("insert into versions(file, + file_id, file_type, file_size, file_mtime, + file_owner, file_group, file_acl, + file_unix_bits, + file_rdev, + date, checksum, online, file_linktarget) + values(?, + ?, ?, ?, ?, + ?, ?, ?, + ?, + ?, + ?, ?, ?, ?)", + {}, + $db_f->[0]{id}, + $f->{id}, $f->{t}, $f->{s}, $f->{m}, + $f->{o}, $f->{g}, $f->{acl}, + join(',', map {$f->{$_} ? ($_) : ()} qw(setuid setgid sticky)), + $f->{rdev}, + time(), + $f->{checksum}, + 1, + $f->{lt} + ); +} + # vim: tw=0 expandtab 1;