| 1 | package App::SimpleBackuper; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Fcntl ':mode'; # For S_ISDIR & same | ||
| 6 | use App::SimpleBackuper::_format; | ||
| 7 | |||
| 8 | sub Restore { | ||
| 9 | my($options, $state) = @_; | ||
| 10 | |||
| 11 | my($backup) = grep {$_->{name} eq $options->{'backup-name'}} | ||
| 12 | map {$state->{db}->{backups}->unpack($_)} | ||
| 13 | @{ $state->{db}->{backups} } | ||
| 14 | ; | ||
| 15 | die qq|Backup $options->{'backup-name'} was not found in database| if ! $backup; | ||
| 16 | $state->{backup_id} = $backup->{id}; | ||
| 17 | |||
| 18 | my @path = split(/\//, $options->{path}, -1); | ||
| 19 | pop @path if @path and $path[-1] eq ''; | ||
| 20 | |||
| 21 | my $file = {id => 0}; | ||
| 22 | my @cur_path; | ||
| 23 | foreach my $path_node (@path) { | ||
| 24 | push @cur_path, $path_node; | ||
| 25 | $file = $state->{db}->{files}->find_by_parent_id_name($file->{id}, $path_node); | ||
| 26 | return {error => 'NOT_FOUND'} if ! $file; | ||
| 27 | } | ||
| 28 | |||
| 29 | $options->{destination} =~ s/\/$//g; | ||
| 30 | |||
| 31 | _proc_file($options, $state, $file, join('/', @cur_path) || '/', $options->{destination}); | ||
| 32 | |||
| 33 | print "Backup '$options->{'backup-name'}' was successful restored.\n" if ! $options->{quiet}; | ||
| 34 | |||
| 35 | return {}; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _restore_part { | ||
| 39 | my($options, $reg_file, $storage, $part, $number) = @_; | ||
| 40 | |||
| 41 | $reg_file->data_ref(\$storage->get(fmt_hex2base64($part->{hash}))->[0]); | ||
| 42 | print "fetched, " if $options->{verbose}; | ||
| 43 | $reg_file->decrypt($part->{aes_key}, $part->{aes_iv}); | ||
| 44 | print "decrypted, " if $options->{verbose}; | ||
| 45 | my $ratio = $reg_file->decompress(); | ||
| 46 | printf "decompressed (x%d)", $ratio if $options->{verbose}; | ||
| 47 | $reg_file->write($number); | ||
| 48 | print " and restored.\n" if $options->{verbose}; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _proc_file { | ||
| 52 | my($options, $state, $file, $backup_path, $fs_path) = @_; | ||
| 53 | |||
| 54 | print "$backup_path -> $fs_path\n" if $options->{verbose}; | ||
| 55 | |||
| 56 | |||
| 57 | my($version) = grep {$_->{backup_id_min} <= $state->{backup_id} and $_->{backup_id_max} >= $state->{backup_id}} | ||
| 58 | @{ $file->{versions} }; | ||
| 59 | if(! $version) { | ||
| 60 | print "\tnot exists in this backup.\n" if $options->{verbose}; | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | my @stat = lstat($fs_path); | ||
| 65 | my($fs_user, $fs_group); | ||
| 66 | if(@stat) { | ||
| 67 | $fs_user = getpwuid($stat[4]); | ||
| 68 | $fs_group = getpwuid($stat[5]); | ||
| 69 | } | ||
| 70 | |||
| 71 | if(S_ISDIR $version->{mode}) { | ||
| 72 | my $need2mkdir; | ||
| 73 | if(@stat) { | ||
| 74 | if(! S_ISDIR $stat[2]) { | ||
| 75 | print "\tin backup it's dir but on FS it's not.\n" if $options->{verbose}; | ||
| 76 | unlink $fs_path if $options->{write}; | ||
| 77 | $need2mkdir = 1; | ||
| 78 | } | ||
| 79 | } else { | ||
| 80 | $need2mkdir = 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | if($need2mkdir) { | ||
| 84 | mkdir($fs_path, $version->{mode}) or die "Can't mkdir $fs_path: $!" if $options->{write}; | ||
| 85 | $fs_user = scalar getpwuid $<; | ||
| 86 | $fs_group = scalar getgrgid $(; | ||
| 87 | $stat[2] = $version->{mode}; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | elsif(S_ISLNK $version->{mode}) { | ||
| 91 | my $need2link; | ||
| 92 | if(@stat) { | ||
| 93 | if(S_ISLNK $stat[2]) { | ||
| 94 | my $symlink_to = readlink($fs_path) // die "Can't read symlink $fs_path: $!"; | ||
| 95 | if($symlink_to ne $version->{symlink_to}) { | ||
| 96 | print "\tin backup this symlink refers to $version->{symlink_to} but on FS - to $symlink_to.\n" if $options->{verbose}; | ||
| 97 | unlink $fs_path if $options->{write}; | ||
| 98 | $need2link = 1; | ||
| 99 | } | ||
| 100 | } else { | ||
| 101 | print "\tin backup it's a symlink but on FS it's not.\n" if $options->{verbose}; | ||
| 102 | unlink $fs_path if $options->{write}; | ||
| 103 | $need2link = 1; | ||
| 104 | } | ||
| 105 | } else { | ||
| 106 | $need2link = 1; | ||
| 107 | } | ||
| 108 | |||
| 109 | if($need2link) { | ||
| 110 | if($options->{write}) { | ||
| 111 | symlink($version->{symlink_to}, $fs_path) or die "Can't make symlink $fs_path -> $version->{symlink_to}: $!"; | ||
| 112 | } | ||
| 113 | $fs_user = scalar getpwuid $<; | ||
| 114 | $fs_group = scalar getgrgid $(; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | elsif(S_ISREG $version->{mode}) { | ||
| 118 | my $need2rewrite_whole_file; | ||
| 119 | if(@stat) { | ||
| 120 | if(S_ISREG $stat[2]) { | ||
| 121 | my $reg_file = App::SimpleBackuper::RegularFile->new($fs_path, $options); | ||
| 122 | my $file_writer; | ||
| 123 | if($stat[7] != $version->{size}) { | ||
| 124 | print "\tin backup it's file with size ".fmt_weight($version->{size}).", but on FS - ".fmt_weight($version->{size}).".\n" if $options->{verbose}; | ||
| 125 | $reg_file->truncate($version->{size}) if $options->{write}; | ||
| 126 | } | ||
| 127 | for my $part_number (0 .. $#{ $version->{parts} }) { | ||
| 128 | $reg_file->read($part_number); | ||
| 129 | my $fs_hash = $reg_file->hash; | ||
| 130 | if($fs_hash eq $version->{parts}->[ $part_number ]->{hash}) { | ||
| 131 | print "\tpart #$part_number hash is ".fmt_hex2base64($fs_hash)." (OK)\n" if $options->{verbose}; | ||
| 132 | } | ||
| 133 | else { | ||
| 134 | print "\tpart#$part_number in backup has hash ".fmt_hex2base64($version->{parts}->[ $part_number ]->{hash}).", but on FS - ".fmt_hex2base64($fs_hash).": " if $options->{verbose}; | ||
| 135 | if($options->{write}) { | ||
| 136 | _restore_part($options, $reg_file, $state->{storage}, $version->{parts}->[ $part_number ], $part_number); | ||
| 137 | } else { | ||
| 138 | print "\twill be restored\n" if $options->{verbose}; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } else { | ||
| 143 | print "\tin backup it's a regular file, but on FS it's not.\n" if $options->{verbose}; | ||
| 144 | $need2rewrite_whole_file = 1; | ||
| 145 | unlink $fs_path if $options->{write}; | ||
| 146 | } | ||
| 147 | } else { | ||
| 148 | $need2rewrite_whole_file = 1; | ||
| 149 | } | ||
| 150 | |||
| 151 | if($need2rewrite_whole_file) { | ||
| 152 | if($options->{write}) { | ||
| 153 | my $reg_file = App::SimpleBackuper::RegularFile->new($fs_path, $options, $state); | ||
| 154 | if(@{ $version->{parts} }) { | ||
| 155 | for my $part_number (0 .. $#{ $version->{parts} }) { | ||
| 156 | print "\tpart #$part_number hash is ".fmt_hex2base64($version->{parts}->[ $part_number ]->{hash}).": " if $options->{verbose}; | ||
| 157 | _restore_part($options, $reg_file, $state->{storage}, $version->{parts}->[ $part_number ], $part_number); | ||
| 158 | } | ||
| 159 | } else { | ||
| 160 | $reg_file->set_write_mode(); | ||
| 161 | } | ||
| 162 | } else { | ||
| 163 | print "\tfile will be restored.\n" if $options->{verbose}; | ||
| 164 | } | ||
| 165 | $fs_user = scalar getpwuid $<; | ||
| 166 | $fs_group = scalar getgrgid $(; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | if((! @stat or $stat[2] != $version->{mode}) and ! S_ISLNK $version->{mode}) { | ||
| 172 | printf "\tin backup it has mode %o but on FS - %o.\n", $version->{mode}, $stat[2] // 0 if $options->{verbose}; | ||
| 173 | if($options->{write}) { | ||
| 174 | chmod($version->{mode}, $fs_path) or die sprintf("Can't chmod %s to %o: %s", $fs_path, $version->{mode}, $!); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | my($db_user) = map {$_->{name}} | ||
| 179 | grep {$_->{id} == $version->{uid}} | ||
| 180 | map { $state->{db}->{uids_gids}->unpack($_) } | ||
| 181 | @{ $state->{db}->{uids_gids} } | ||
| 182 | ; | ||
| 183 | my($db_group) = map {$_->{name}} | ||
| 184 | grep {$_->{id} == $version->{gid}} | ||
| 185 | map { $state->{db}->{uids_gids}->unpack($_) } | ||
| 186 | @{ $state->{db}->{uids_gids} } | ||
| 187 | ; | ||
| 188 | if(($fs_user ne $db_user or $fs_group ne $db_group) and ! S_ISLNK $version->{mode}) { | ||
| 189 | print "\tin backup it owned by $db_user:$db_group but on FS - by $fs_user:$fs_group.\n" if $options->{verbose}; | ||
| 190 | chown scalar(getpwnam $db_user), scalar getgrnam($db_group), $fs_path if $options->{write}; | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | if(S_ISDIR $version->{mode}) { | ||
| 195 | foreach my $subfile (sort {$a->{name} cmp $b->{name}} map {@$_} $state->{db}->{files}->find_all({parent_id => $file->{id}})) { | ||
| 196 | _proc_file($options, $state, $subfile, $backup_path.'/'.$subfile->{name}, $fs_path.'/'.$subfile->{name}); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | 1; |