#!/usr/bin/perl #---------------------------------------------------- # Version: 0.1.0 # Author: Florian "Bluewind" Pritz # # Licensed under WTFPL v2 # #---------------------------------------------------- # Simplify rsnapshot restores #---------------------------------------------------- use warnings; use strict; use File::Basename; use Cwd; use DateTime; use File::Copy::Recursive qw(rcopy); use File::Find; use File::Path qw(remove_tree); my $backuppath = "/mnt/backup"; my %mapping = ( "^/srv" => "/home/srv" ); if (@ARGV == 0) { print "usage: ", basename($0), " files(s)...\n"; exit 0; } for my $filename (@ARGV) { my @filelist; my $file = Cwd::abs_path($filename); my $lastmodtime = 0; for my $key (keys %mapping) { $file =~ s/$key/$mapping{$key}/; } for (glob("$backuppath/*")) { my $backupfile = "$_$file"; my $modtime = 0; # skip everything that doesn't seem to be a backup next unless -d and -r and -x; next unless -e $backupfile; if (-d $backupfile) { find(sub { my $current_mtime = (stat($File::Find::name))[9] or return; $modtime = $current_mtime if ($current_mtime > $modtime); }, $backupfile); } else { $modtime = (stat($backupfile))[9]; } push @filelist, {path => $backupfile, time => $modtime}; } @filelist = sort { $a->{time} cmp $b->{time} } @filelist; my $i = 0; for my $backupfile (@filelist) { if ($backupfile->{time} != $lastmodtime) { my $dt = DateTime->from_epoch(epoch => $backupfile->{time}); print "\e[0;33m$i: \e[1;33m", $dt->strftime("%F %H:%M:%S"),"\e[0m ", $backupfile->{path}, "\n"; $lastmodtime = $backupfile->{time}; } $i++; } print "\e[0;34mEnter ID to restore (Enter to skip): \e[0m"; my $id = ; chomp $id; next unless ($id =~ /^\d+$/); remove_tree $file if -d $file; # need mtime preservation #rcopy $filelist[$id]->{path}, $file; system "cp -a \"$filelist[$id]->{path}\" \"$file\""; print "\e[0;32mrestored $file\e[0m\n"; }