summaryrefslogtreecommitdiffstats
path: root/restore.pl
blob: 5e7214cfb7f0588ad2ae6462238bb13f0de3235e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/perl
#----------------------------------------------------
# Version: 0.1.0
# Author: Florian "Bluewind" Pritz <flo@xssn.at>
#
# 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 = <STDIN>;
  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";
}