Skip to content
Snippets Groups Projects
Verified Commit 0dce1207 authored by Vladimír Štill's avatar Vladimír Štill
Browse files

backup: First version of restore script

parent 1ebad83e
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env -S perl -T
use warnings;
use strict;
use File::Spec::Functions ( "catfile", "catdir", "file_name_is_absolute", "splitdir" );
use Scalar::Util ( "tainted" );
$ENV{PATH} = "/usr/local/bin:/usr/bin:/bin";
my $thrs = 1;
if ( `nproc` =~ /^([0-9])$/ ) {
$thrs = int( $1 / 4 );
$thrs = 1 if $thrs <= 0;
$thrs = 8 if $thrs > 8;
}
sub usage {
my ( $stream ) = @_;
$stream = *STDERR unless defined $stream;
print $stream "backup-restore MACHINE DATE PATH\n"
}
if ( @ARGV < 3 ) {
usage(*STDOUT);
exit 1;
}
my $machine;
if ( (shift @ARGV) =~ m|^([^/ ]*)$| ) {
$machine = $1;
} else {
print STDERR "Machine must be a machine name only, not a path\n";
usage;
exit 2;
}
my $date;
if ( (shift @ARGV) =~ /([0-9]+-[0-9]+-[0-9]+)/ ) {
$date = $1;
} else {
print STDERR "Date must be in YEAR-MOONTH-DAY format\n";
usage;
exit 2;
}
my @paths = ();
for my $p ( @ARGV ) {
if ( $p =~ m|^(/.*)$| ) {
push( @paths, $1 );
} else {
print STDERR "Expected absolute path but got $p\n";
usage;
exit 2;
}
}
print "$machine $date @paths\n";
my $full;
my @weekly = ();
my @daily = ();
my $namematch = qr/^([0-9]+-[0-9]+-[0-9]+)[.](daily|weekly|full)[.](.*)$/;
opendir( my $dir, catdir("/backup-tar", $machine ) ) or die $!;
my @backups = sort( grep { /$namematch/ } readdir( $dir ) );
for my $backup ( @backups ) {
$backup =~ /$namematch/;
$backup = "$1.$2.$3";
my $path = catfile( "/backup-tar", $machine, $backup );
my $from = $1;
my $type = $2;
last if $from gt $date;
if ( $type eq "full" ) {
$full = $path;
@weekly = ();
} elsif ( $type eq "weekly" ) {
push( @weekly, $path );
@daily = ();
} else {
push( @daily, $path );
}
}
my @to_restore = ( $full, @weekly, @daily );
for my $backup ( @to_restore ) {
print $backup . ": " . tainted( $backup ) . "\n";
system( "tar", "--extract", "--auto-compress", "--incremental", "--xattrs",
"--acls", "-vv", "-T$thrs", "-f$backup", "--", @paths ) == 0
or die "extract of $backup failed $?";
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment