Brackup

 view release on metacpan or  search on metacpan

lib/Brackup/Target/Filesystem.pm  view on Meta::CPAN

    }
    unless ($actual_size == $expected_size) {
        die "Chunk $path was written to disk wrong:  size is $actual_size, expecting $expected_size\n";
    }

    return 1;
}

sub delete_chunk {
    my ($self, $dig) = @_;
    my $path = $self->chunkpath($dig);
    unlink $path;
}


# returns a list of names of all chunks
sub chunks {
    my $self = shift;

    my @chunks = ();
    my $found_chunk = sub {
        m/\.chunk$/ or return;
        my $chunk_name = basename($_);
        $chunk_name =~ s/\.chunk$//;
        $chunk_name =~ s/\./:/g if $self->nocolons;
        push @chunks, $chunk_name;
    };
    File::Find::find({ wanted => $found_chunk, no_chdir => 1}, $self->{path});
    return @chunks;
}

sub store_backup_meta {
    my ($self, $name, $fh) = @_;

    my $dir = $self->metapath();
    unless (-d $dir) {
        mkdir $dir or die "Failed to mkdir $dir: $!\n";
    }

    my $out_filepath = "$dir/$name.brackup";
    open (my $out_fh, '>', $out_filepath)
      or die "Failed to open metafile '$out_filepath': $!\n";
    io_print_to_fh($fh, $out_fh);
    close $out_fh or die "Failed to close metafile '$out_filepath': $!\n";

    return 1;
}

sub backups {
    my ($self) = @_;

    my $dir = $self->metapath();
    return () unless -d $dir;

    opendir(my $dh, $dir) or
        die "Failed to open $dir: $!\n";

    my @ret = ();
    while (my $fn = readdir($dh)) {
        next unless $fn =~ s/\.brackup$//;
        my $stat = File::stat::stat("$dir/$fn.brackup");
        push @ret, Brackup::TargetBackupStatInfo->new($self, $fn,
                                                      time => $stat->mtime,
                                                      size => $stat->size);
    }
    closedir($dh);

    return @ret;
}

# downloads the given backup name to the current directory (with
# *.brackup extension) or to the specified location
sub get_backup {
    my ($self, $name, $output_file) = @_;
    my $file = $self->metapath("$name.brackup");

    die "File doesn't exist: $file" unless -e $file;

    $output_file ||= "$name.brackup";

    open(my $in,  $file) or die "Failed to open $file: $!\n";
    open(my $out, '>', $output_file) or die "Failed to open $output_file: $!\n";

    my $buf;
    my $rv;
    while ($rv = sysread($in, $buf, 128*1024)) {
        my $outv = syswrite($out, $buf);
        die "copy error" unless $outv == $rv;
    }
    die "copy error" unless defined $rv;

    return 1;
}

sub delete_backup {
    my $self = shift;
    my $name = shift;

    my $file = $self->metapath("$name.brackup");
    die "File doesn't exist: $file" unless -e $file;
    unlink $file;
    return 1;
}

1;


=head1 NAME

Brackup::Target::Filesystem - backup to a locally mounted filesystem

=head1 DESCRIPTION

Back up to an NFS or Samba server, another disk array (external storage), etc.

=head1 EXAMPLE

In your ~/.brackup.conf file:

  [TARGET:nfs_in_garage]
  type = Filesystem



( run in 2.554 seconds using v1.01-cache-2.11-cpan-13bb782fe5a )