#!/usr/bin/perl #---------------------------------------------------- # Author: Florian "Bluewind" Pritz # # Licensed under WTFPL v2 # #---------------------------------------------------- # Simplify rsnapshot restores #---------------------------------------------------- use warnings; use strict; use File::Basename; use Cwd qw(abs_path 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), " \n"; print "restore the target(s) from backup\n"; exit 0; } my $startdir = cwd(); for my $filename (@ARGV) { chdir $startdir; my @filelist; my $file = 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; chdir "/"; next unless ($id =~ /^\d+$/ && exists ($filelist[$id])); 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"; } # vim: set noet: