summaryrefslogtreecommitdiffstats
path: root/lib/MDV/Snapshot/Restore.pm
blob: bc98886a7e16e974d0238ba6046d3ebf49ea2aad (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package MDV::Snapshot::Restore;

################################################################################
# Backup Configuration Tool                                                    # 
#                                                                              #
# Copyright (C) 2008 Mandriva                                                  #
#                                                                              #
# Thierry Vignaud <tvignaud at mandriva dot com>                               #
#                                                                              #
# This program is free software; you can redistribute it and/or modify         #
# it under the terms of the GNU General Public License Version 2 as            #
# published by the Free Software Foundation.                                   #
#                                                                              #
# This program is distributed in the hope that it will be useful,              #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with this program; if not, write to the Free Software                  #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.   #
################################################################################

use strict;
use warnings;
use lib qw(/usr/lib/libDrakX);
use c;
use locale;
use MDV::Snapshot::Common;
use common;
use interactive;
use POSIX;
use run_program;
use fsedit;
use fs::type;
use fs;
use any;

sub get_fstab() {
    my $all_hds = fsedit::get_hds();
    fs::get_raw_hds('', $all_hds);
    fs::get_info_from_fstab($all_hds);
    [ fs::get::fstab($all_hds) ]; # $fstab
}

sub get_handle {
    my ($part, $o_rw) = @_;
    my $mntpnt = $part->{mntpoint};
    
    # do not bother failed to mount with ro b/c already mounted:
    $mntpnt ? { dir => $mntpnt } : any::inspect($part, $::prefix, $o_rw);
}


sub find_disks_with_rsnapshot {
    my ($fstab) = @_;

    my @true_fs = 
      grep { isTrueLocalFS($_) && 
	     !member($_->{mntpoint},
                     '/home', grep { $_ ne '/' } fs::type::directories_needed_to_boot());
	 } @$fstab;

    log::l("looking for rsnapshot.conf on partitions " . join(' ', map { $_->{device} } @true_fs));

    my @found;
    foreach my $part (@true_fs) {
        my $handle = get_handle($part);
        next if !$handle;
        next if ! -e "$handle->{dir}/etc/rsnapshot.conf";

	if (my $f = common::release_file($handle->{dir})) {
	    my $h = common::parse_release_file($handle->{dir}, $f, $part);
	    $h->{name} = $h->{release};
	    push @found, $h;
	}
    }

    return @found;
}

sub get_snapshots() {
    my %backups = 
      map {
          my $backup = $_;
          my ($type, $_number) = m!$backup_directory/([^/]*)\.([0-9]*)!;
          my $raw_date = [ stat($backup) ]->[-4];
          my $date = POSIX::strftime($type eq 'hourly' ? "%c" : "%x", localtime($raw_date));
          c::set_tagged_utf8($date);
          $backup => { raw_date => $raw_date, date => $date, backup => $backup, type => $type };
      } glob_("$backup_directory/*");
    %backups;
}

sub configure {
    my ($in) = @_;
    my $backup;
    my %backups = get_snapshots();

    if (!%backups) {
        $in->ask_warn(N("Error"), N("No backup found!"));
        return;
    }

    my %i18n = (
        'hourly'  => N("Hourly"),
        'daily'   => N("Daily"),
        'weekly'  => N("Weekly"),
        'monthly' => N("Monthly"),
    );

    $in->ask_from_(
        { 
            title => N("Backup snapshots configuration"),
        },
        [
            { title => 1, label => N("Backup snapshots configuration") },
            { label => N("Please select the backup you want to restore.") },
            { val => \$backup, sort => 0, allow_empty_list => 1,
              format => sub { 
                  #-PO: eg: "Daily snapshot done on Sun Feb 4
                  N("%s snapshot done on %s", $i18n{$backups{$_[0]}{type}}, $backups{$_[0]}{date});
              },
              list => [ sort { $backups{$a}{raw_date} cmp $backups{$b}{raw_date} } keys %backups ],
          },
        ]) or return;

    my $_wait = $in->wait_message(N("Please wait"), N("Restoring backup in progress"));
    my $backup_dir = $backups{$backup}{backup};
    # FIXME: add a progress bar
    foreach (glob_("$backup_dir/localhost/*")) {
        my $dir = $_;
        s!^$backup_dir/localhost!!;
        log::l("would run system('rsync', '-vrlpt', $dir, $::prefix$_)\n") if $::testing;
        # FIXME: should we use --delete?
        run_program::run('rsync', '-vrlpt', $dir, "$::prefix$_") if !$::testing;
    }
    undef $_wait;
    $in->ask_from_no_check(
	{
	 title => N("Congratulations"),
	 messages => N("Congratulations, restoration is complete."),
	 ok => $::isStandalone || $::local_install ? N("Quit") : N("Reboot"),
	}, []);
}


sub mount_all {
    my ($in, $fstab) = @_;
    local $::testing = 0;

    my @errs;
    foreach my $part (sort { $a->{mntpoint} cmp $b->{mntpoint} }
        grep { $_->{mntpoint} && $_->{mntpoint} ne '/' && maybeFormatted($_) && $_->{device} ne 'none'
                 && !member($_->{fs_type}, 'swap', 'nfs', if_($::isInstall, 'ntfs'));
           }
        @$fstab) {

        eval { fs::mount::part($part) };
        push @errs, $@ if $@;
    }
    $in->ask_warn(N("Warning"), N("mount failed: ") . join("\n", '', @errs)) if @errs;
}

sub main {
    my ($in) = @_;

    my ($system, $fstab, @found);
  refresh:
    $fstab = get_fstab();
    @found = find_disks_with_rsnapshot($fstab);

    $in->ask_from_({ interactive_help_id => 'configureX_chooser',
		     title => N("Restoring Backup"), 
                     messages => join("\n",
                                      N("Please select the partition of the system to restore."),
                                      N("Do not forget to plug the USB disk that contains your backups."),
                                  )
                 },
		   [
                       { label => N("System to restore"),  },
                       {
                           val => \$system, list => \@found, allow_empty_list => 1,
                           format => sub { 
                               my $dev = $_[0];
                               N("\"%s\" (on %s)", $dev->{name}, $dev->{part}{device});
                           },
                       },
                       {
                           val => N("Refresh"), clicked => sub { goto refresh },
                       },
                   ]);

    my $handle = get_handle($system->{part}, 1);
    local $::prefix = $handle->{dir};

    # reinit paths according to temp mount point:
    MDV::Snapshot::Common::init();

    my $ch_fstab;
    if (ref($handle) =~ /before_leaving/) {
        $ch_fstab = [ fs::read_fstab($::prefix, '/etc/fstab') ];
        mount_all($in, $ch_fstab);
    }

    configure($in);

    # make sure everything is umounted:
    if (ref($handle) =~ /before_leaving/) {

        # reread for {real_mountpoint}:
        $ch_fstab = [ fs::read_fstab($::prefix, '/etc/fstab') ];

        eval { fs::mount::umount_all($ch_fstab) };
    }
}

1;