simba/lib/Simba/CA.pm

62 lines
1.5 KiB
Perl

package Simba::CA;
use Encode;
use IPC::Open2;
sub new {
my ($class) = @_;
my $self = {};
bless $self, $class;
$self->{basedir} = '/home/hjp/backup';
$self->{targets} = [
{ host => 'bernon.wsr.ac.at', dir => '/', }
{ host => 'users.wsr.ac.at', dir => '/shares/user', }
];
return $self;
}
sub run {
my ($self) = @_;
# run sequentially for prototype. In production we probably
# want some concurrency
for my $target (@{$self->{targets}}) {
$self->backup2disk($target);
}
}
sub backup2disk {
my ($self, $target) = @_;
# get previous generation
$self->{last_backup} = "...";
$self->{this_backup} = "...";
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(my $list_cfd, my $list_dfd, "ssh", "-l", "simba_da", $host, "da");
$list_cfd->printflush("list $dir\n"); # XXX - encode!
while (<$list_dfd>) {
# split into fields
my $f = $self->parse($_);
# if file is already present
if ($f->{t} eq 'f') {
if($self->present($f)) {
link("$self->{last_backup}/$f->{name}", "$self->{this_backup}/$f->{name}") or die; # XXX
} else {
# else request from da
unless ($file_pid) {
$file_pid = open2(my $file_cfd, my $file_dfd, "ssh", "-l", "simba_da", $host, "da");
}
$file_cfd->printflush("get $dir\n"); # XXX - encode!
} else {
# create local copy (or insert into DB only?)
}
# insert into DB.
}
}