Skip to content

Instantly share code, notes, and snippets.

@anazawa
Created October 7, 2013 17:53
Show Gist options
  • Save anazawa/6872144 to your computer and use it in GitHub Desktop.
Save anazawa/6872144 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
use Time::Seconds;
my $filesystem = shift || '/dev/ada0p2';
my %alias = ( '/dev/ada0p2' => 'root' );
my $backupdir = '/backup';
my %dumpdates;
if ( open my $fh, '<', '/etc/dumpdates' ) {
while ( my $line = <$fh> ) {
chomp $line;
my ( $fs, $level, $date ) = split ' ', $line, 3;
$date = localtime->strptime( $date, '%a %b %e %T %Y' );
$dumpdates{$fs} = { level => $level, date => $date };
}
close $fh;
}
my $now = localtime;
my $dumpdate = $dumpdates{$filesystem} || {};
if ( !exists $dumpdate->{date} or $now - $dumpdate->{date} > ONE_DAY ) {
my $level = exists $dumpdate->{level} ? $dumpdate->{level} + 1 : 0;
$level = 1 if $level > 7;
my $file = "$backupdir/";
$file .= $alias{$filesystem} || ( split '/', $filesystem )[-1];
$file .= '-' . $now->ymd(q{});
$file .= $level == 1 ? '-D' : '-I' if $level;
$file .= '.dump';
die "File already exists: $file" if -e $file;
# TODO: -L is missing
my @options = (
"-$level", # dump level
'-u', # update /etc/dumpdates
'-a', # auto-size
'-b' => 64, # blocksize
'-C' => 32, # cachesize
'-f' => $file, #
);
system '/sbin/dump', @options, $filesystem;
}
__END__
=head1 NAME
dump.pl - Multilevel incremental backup using dump(8)
=head1 SYNOPSIS
./dump.pl filesystem
=head1 DESCRIPTION
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment