Dackup

 view release on metacpan or  search on metacpan

lib/Dackup/Target/SSH.pm  view on Meta::CPAN

        . $ssh->{_host}
        . file( $self->prefix, $entry->key );
}

sub update {
    my ( $self, $source, $entry ) = @_;
    my $ssh                   = $self->ssh;
    my $source_type           = ref($source);
    my $destination_filename  = $self->filename($entry);
    my $destination_directory = $destination_filename->parent;
    my $directories           = $self->directories;

    if ( $source_type eq 'Dackup::Target::Filesystem' ) {
        my $source_filename = $source->filename($entry);

        unless ( $directories->{$destination_directory} ) {

            my $quoted_destination_directory
                = $ssh->shell_quote("$destination_directory");

            # warn "mkdir -p $quoted_destination_directory";
            $ssh->system("mkdir -p $quoted_destination_directory")
                || die "mkdir -p $quoted_destination_directory failed: "
                . $ssh->error;
            $directories->{$destination_directory} = 1;
        }

        #warn "$source_filename -> $destination_filename";

        my $scp_options = {};
        my $throttle    = $self->dackup->throttle;
        if ($throttle) {
            my $data_rate       = Number::DataRate->new;
            my $bits_per_second = $data_rate->to_bits_per_second($throttle);
            $scp_options->{bwlimit} = $bits_per_second / 1000;    # in Kbit/s
        }

        $ssh->scp_put( $scp_options, "$source_filename",
            "$destination_filename" )
            || die "scp failed: " . $ssh->error;
    } else {
        confess "Do not know how to update from $source_type";
    }
}

sub delete {
    my ( $self, $entry ) = @_;
    my $ssh      = $self->ssh;
    my $filename = $self->filename($entry);

    $ssh->system("rm -f $filename")
        || die "rm -f $filename failed: " . $ssh->error;
}

1;

__END__

=head1 NAME

Dackup::Target::SSH - Flexible file backup remote hosts via SSH

=head1 SYNOPSIS

  use Dackup;
  use Net::OpenSSH;

  my $ssh = Net::OpenSSH->new('acme:password@backuphost');
  $ssh->error
      and die "Couldn't establish SSH connection: " . $ssh->error;

  my $source = Dackup::Target::Filesystem->new(
      prefix => '/home/acme/important/' );

  my $destination = Dackup::Target::SSH->new(
      ssh    => $ssh,
      prefix => '/home/acme/important_backup/'
  );

  my $dackup = Dackup->new(
      source      => $source,
      destination => $destination,
      delete      => 0,
  );
  $dackup->backup;

=head1 DESCRIPTION

This is a Dackup target for a remote host via SSH.

=head1 AUTHOR

Leon Brocard <acme@astray.com>

=head1 COPYRIGHT

Copyright (C) 2009, Leon Brocard.

=head1 LICENSE

This module is free software; you can redistribute it or 
modify it under the same terms as Perl itself.



( run in 1.010 second using v1.01-cache-2.11-cpan-f56aa216473 )