Restore
Restore
Restore file to captured state
Every captured change carries its SHA-256-addressed content in BLOB_STORE. Restoring writes that content back to the original path — current file gets backed up to <path>.drift_restore_backup_<epoch> before overwrite.
What will change
Preview -- nothing has been written yet.
| Target file | /var/www/vhosts/3dshawn.com/site1/restore.cgi |
| Site | DriftSense self-monitor on local |
| Kind | local |
| Captured at | 2026-07-11 23:34:31 |
| Captured SHA | 1ea1f8830b275797740802e09411a83598e57d628198bc37ff99658177e255cc |
Current state on disk
Live check just now. If SHAs match, the restore is a no-op.
| File present | yes |
| Size | 16800 bytes |
| Current SHA | 2f8b971be093 |
| Same as captured? | no -- restore will change it |
What restore will change — live diff
Left column shows current on-disk content; right shows what restore will write. +7 additions, -51 deletions, 411 unchanged context lines.
| 1 | 1 | #!/usr/bin/perl |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # DriftSense -- /restore.cgi |
| 4 | 4 | # |
| 5 | 5 | # Restore a captured file to its original path. Two flavors: |
| 6 | 6 | # Local (server.kind = local) -> write blob to target path directly |
| 7 | 7 | # Agent (server.kind = ssh_agent) -> pipe blob content over SSH to target |
| 8 | 8 | # |
| 9 | 9 | # Safety: |
| 10 | 10 | # - GET /restore.cgi?id=N -> preview page (dry-run details) |
| 11 | 11 | # - POST /restore.cgi id=N confirm=1 -> perform the restore |
| 12 | 12 | # |
| 13 | 13 | # * Path must be inside a configured FILE_MONITOR_SETTINGS.scan_path |
| 14 | 14 | # for that server (prevents restoring blob to /etc/passwd from a |
| 15 | 15 | # tampered FILE_CHANGES row). |
| 16 | 16 | # * Current file content is backed up to |
| 17 | 17 | # $target.drift_restore_backup_$UNIX_TIMESTAMP before overwrite. |
| 18 | 18 | # * Every attempt (success, failure, dry_run) is logged to RESTORE_LOG. |
| 19 | 19 | #====================================================================== |
| 20 | 20 | use strict; |
| 21 | 21 | use warnings; |
| 22 | 22 | use CGI (); |
| 23 | 23 | use IPC::Open3; |
| 24 | 24 | use Symbol 'gensym'; |
| 25 | 25 | use Fcntl (); |
| 26 | 26 | use POSIX (); |
| 27 | 27 | use Compress::Zlib qw(uncompress); |
| 28 | 28 | use Digest::SHA qw(sha256_hex); |
| 29 | 29 | use MODS::Config; |
| 30 | 30 | use MODS::DBConnect; |
| 31 | 31 | use MODS::Template; |
| 32 | 32 | use MODS::PageWrapper; |
| 33 | 33 | use MODS::Crypto; |
| 34 | use MODS::Diff; | |
| 35 | 34 | |
| 36 | 35 | my $cgi = CGI->new; |
| 37 | 36 | my $db = MODS::DBConnect->new; |
| 38 | 37 | my $tpl = MODS::Template->new; |
| 39 | 38 | $|=1; |
| 40 | 39 | |
| 41 | 40 | my $dbh = $db->db_connect or die "DriftSense: DB connect failed\n"; |
| 42 | 41 | |
| 43 | 42 | my $method = $ENV{REQUEST_METHOD} || 'GET'; |
| 44 | 43 | my $id = int($cgi->param('id') || 0); |
| 45 | 44 | |
| 46 | 45 | # ---- Load the source change row ------------------------------------- |
| 47 | 46 | my $row = _load_change_row($dbh, $id); |
| 48 | 47 | unless ($row && $row->{id}) { |
| 49 | 48 | _render_error("Change #$id not found"); |
| 50 | 49 | } |
| 51 | 50 | |
| 52 | 51 | # ---- Path safety: target file must be inside a monitored scan_path -- |
| 53 | 52 | my $monitor = _load_monitor($dbh, $row->{file_monitor_list_id}); |
| 54 | 53 | my $path_ok = 0; |
| 55 | 54 | if ($monitor && $monitor->{scan_path}) { |
| 56 | 55 | my $sp = $monitor->{scan_path}; |
| 57 | 56 | $sp =~ s!/+$!!; |
| 58 | 57 | $path_ok = 1 if index($row->{file_name}, "$sp/") == 0 || $row->{file_name} eq $sp; |
| 59 | 58 | } |
| 60 | 59 | unless ($path_ok) { |
| 61 | 60 | _render_error("Target path <code>$row->{file_name}</code> is outside its monitor's configured scan_path; refusing to restore."); |
| 62 | 61 | } |
| 63 | 62 | |
| 64 | 63 | # ---- If POST, perform the restore ---------------------------------- |
| 65 | 64 | if ($method eq 'POST' && $cgi->param('confirm')) { |
| 66 | 65 | my $result = _do_restore($dbh, $row, $monitor); |
| 67 | 66 | _log_restore($dbh, $row, $result); |
| 68 | 67 | |
| 69 | 68 | # Render result page |
| 70 | 69 | my $tvars = { |
| 71 | 70 | id => $id, |
| 72 | 71 | target_file => $row->{file_name}, |
| 73 | 72 | server_name => $row->{server_name} // 'local', |
| 74 | 73 | server_kind => $row->{server_kind} // 'local', |
| 75 | 74 | source_sha => substr($row->{blob_sha} // '', 0, 12), |
| 76 | 75 | captured_h => $row->{date_time} // '', |
| 77 | 76 | captured_epoch => int($row->{ts_epoch} || 0), |
| 78 | 77 | result_status => $result->{status}, |
| 79 | 78 | result_ok => $result->{status} eq 'success' ? 1 : 0, |
| 80 | 79 | result_msg => $result->{message} // '', |
| 81 | 80 | backup_path => $result->{backup_path} // '', |
| 82 | 81 | did_restore => 1, |
| 83 | 82 | }; |
| 84 | 83 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; |
| 85 | 84 | my @body = $tpl->template('restore.html', $tvars); |
| 86 | 85 | MODS::PageWrapper->new->wrapper( |
| 87 | 86 | page_title => 'Restore', page_key => '', |
| 88 | 87 | body_html => join('', @body), userinfo => { display_name => 'Operator' }, |
| 89 | 88 | ); |
| 90 | 89 | $db->db_disconnect($dbh); |
| 91 | 90 | exit; |
| 92 | } | |
| 93 | ||
| 94 | # ---- Recent RESTORE_LOG for this file (Wave 5 F4) ------------------ | |
| 95 | my $q_file_esc = $dbh->quote($row->{file_name}); | |
| 96 | my @recent_restores_this = $db->db_readwrite_multiple($dbh, qq~ | |
| 97 | SELECT restore_id, status, LEFT(error_message, 80) AS err_short, | |
| 98 | LEFT(source_blob_sha, 12) AS sha_short, | |
| 99 | restored_by, | |
| 100 | UNIX_TIMESTAMP(restored_at) AS restored_epoch, | |
| 101 | DATE_FORMAT(restored_at, '%b %d %H:%i') AS restored_h | |
| 102 | FROM RESTORE_LOG | |
| 103 | WHERE target_file = $q_file_esc | |
| 104 | ORDER BY restore_id DESC LIMIT 5 | |
| 105 | ~, __FILE__, __LINE__); | |
| 106 | foreach my $rr (@recent_restores_this) { | |
| 107 | $rr->{status_pill} = $rr->{status} eq 'success' ? 'pill-ok' : | |
| 108 | $rr->{status} eq 'failed' ? 'pill-bad' : 'pill-mute'; | |
| 109 | $rr->{restored_epoch} //= 0; | |
| 110 | 91 | } |
| 111 | 92 | |
| 112 | 93 | # ---- GET: preview page --------------------------------------------- |
| 113 | my ($current_sha, $current_exists, $current_size, $current_content) = _peek_current($row, $monitor); | |
| 114 | ||
| 115 | # Live diff: current on-disk vs captured content | |
| 116 | my @diff_lines; | |
| 117 | my ($diff_add, $diff_del) = (0, 0); | |
| 118 | if ($current_exists && $current_sha && $row->{blob_sha} && $current_sha ne $row->{blob_sha}) { | |
| 119 | my $captured = _fetch_blob($dbh, $row->{blob_sha}) // ''; | |
| 120 | @diff_lines = MODS::Diff::html_escape_lines(MODS::Diff::lines($current_content // '', $captured)); | |
| 121 | for my $L (@diff_lines) { | |
| 122 | $diff_add++ if $L->{op} eq 'add'; | |
| 123 | $diff_del++ if $L->{op} eq 'del'; | |
| 124 | } | |
| 125 | } | |
| 94 | my ($current_sha, $current_exists, $current_size) = _peek_current($row, $monitor); | |
| 126 | 95 | my $tvars = { |
| 127 | 96 | id => $id, |
| 128 | 97 | target_file => $row->{file_name}, |
| 129 | 98 | server_name => $row->{server_name} // 'local', |
| 130 | 99 | server_kind => $row->{server_kind} // 'local', |
| 131 | 100 | scan_name => $monitor->{scan_name} // '', |
| 132 | 101 | source_sha => substr($row->{blob_sha} // '', 0, 12), |
| 133 | 102 | source_sha_full=> $row->{blob_sha} // '', |
| 134 | 103 | captured_h => $row->{date_time} // '', |
| 135 | 104 | captured_epoch => int($row->{ts_epoch} || 0), |
| 136 | 105 | current_sha => substr($current_sha // '', 0, 12), |
| 137 | 106 | current_exists => $current_exists, |
| 138 | 107 | current_size => defined($current_size) ? "$current_size bytes" : 'unknown', |
| 139 | 108 | same_content => ($current_sha && $current_sha eq ($row->{blob_sha} // '')) ? 1 : 0, |
| 140 | 109 | did_restore => 0, |
| 141 | 110 | diff_url => "/diff.cgi?kind=file&id=$id", |
| 142 | diff_lines => \@diff_lines, | |
| 143 | has_live_diff => scalar(@diff_lines) ? 1 : 0, | |
| 144 | diff_add => $diff_add, | |
| 145 | diff_del => $diff_del, | |
| 146 | diff_ctx => scalar(@diff_lines) - $diff_add - $diff_del, | |
| 147 | recent_restores_this => \@recent_restores_this, | |
| 148 | has_recent_restores => scalar(@recent_restores_this) ? 1 : 0, | |
| 149 | 111 | }; |
| 150 | 112 | |
| 151 | 113 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; |
| 152 | 114 | my @body = $tpl->template('restore.html', $tvars); |
| 153 | 115 | MODS::PageWrapper->new->wrapper( |
| 154 | 116 | page_title => 'Restore', page_key => '', |
| 155 | 117 | body_html => join('', @body), userinfo => { display_name => 'Operator' }, |
| 156 | 118 | ); |
| 157 | 119 | $db->db_disconnect($dbh); |
| 158 | 120 | exit; |
| 159 | 121 | |
| 160 | 122 | #====================================================================== |
| 161 | 123 | sub _load_change_row { |
| 162 | 124 | my ($dbh, $id) = @_; |
| 163 | 125 | return undef unless $id > 0; |
| 164 | 126 | return $db->db_readwrite($dbh, qq~ |
| 165 | 127 | SELECT fc.file_changes_id AS id, |
| 166 | 128 | fc.file_monitor_list_id, fc.server_id, |
| 167 | 129 | fc.file_name, fc.blob_sha, fc.status, fc.date_time, |
| 168 | 130 | UNIX_TIMESTAMP(fc.date_time) AS ts_epoch, |
| 169 | 131 | s.server_name, s.kind AS server_kind, |
| 170 | 132 | s.ssh_host, s.ssh_user, s.ssh_port, |
| 171 | 133 | s.ssh_key_path, s.ssh_options, |
| 172 | 134 | s.ssh_key_blob |
| 173 | 135 | FROM FILE_CHANGES fc |
| 174 | 136 | LEFT JOIN SERVERS s ON s.server_id = fc.server_id |
| 175 | 137 | WHERE fc.file_changes_id = $id |
| 176 | 138 | LIMIT 1 |
| 177 | 139 | ~, __FILE__, __LINE__); |
| 178 | 140 | } |
| 179 | 141 | |
| 180 | 142 | sub _load_monitor { |
| 181 | 143 | my ($dbh, $mid) = @_; |
| 182 | 144 | return undef unless $mid; |
| 183 | 145 | return $db->db_readwrite($dbh, qq~ |
| 184 | 146 | SELECT file_monitor_list_id, scan_path, scan_name, server_id |
| 185 | 147 | FROM FILE_MONITOR_SETTINGS |
| 186 | 148 | WHERE file_monitor_list_id = $mid |
| 187 | 149 | LIMIT 1 |
| 188 | 150 | ~, __FILE__, __LINE__); |
| 189 | 151 | } |
| 190 | 152 | |
| 191 | 153 | sub _fetch_blob { |
| 192 | 154 | my ($dbh, $sha) = @_; |
| 193 | 155 | return undef unless $sha; |
| 194 | 156 | my $sth = $dbh->prepare("SELECT content_gz FROM BLOB_STORE WHERE blob_sha = ?"); |
| 195 | 157 | return undef unless $sth && $sth->execute($sha); |
| 196 | 158 | my $r = $sth->fetchrow_arrayref; |
| 197 | 159 | $sth->finish; |
| 198 | 160 | return undef unless $r && $r->[0]; |
| 199 | 161 | return eval { uncompress($r->[0]) }; |
| 200 | 162 | } |
| 201 | 163 | |
| 202 | 164 | #--------------------------------------------------------------------- |
| 203 | 165 | # _peek_current($row, $monitor) -> ($sha, $exists, $size) |
| 204 | 166 | # For local: stat + hash the current on-disk content. |
| 205 | 167 | # For ssh_agent: SSH and sha256sum + wc -c. |
| 206 | 168 | #--------------------------------------------------------------------- |
| 207 | 169 | sub _peek_current { |
| 208 | 170 | my ($row, $monitor) = @_; |
| 209 | 171 | my $path = $row->{file_name}; |
| 210 | 172 | if (($row->{server_kind} // 'local') eq 'local') { |
| 211 | return (undef, 0, undef, undef) unless -e $path; | |
| 212 | return (undef, 1, -s $path, undef) unless -r $path; | |
| 213 | open(my $fh, '<:raw', $path) or return (undef, 1, -s $path, undef); | |
| 173 | return (undef, 0, undef) unless -e $path; | |
| 174 | return (undef, 1, -s $path) unless -r $path; | |
| 175 | open(my $fh, '<:raw', $path) or return (undef, 1, -s $path); | |
| 214 | 176 | local $/; my $c = <$fh>; close $fh; |
| 215 | return (sha256_hex($c), 1, length $c, $c); | |
| 177 | return (sha256_hex($c), 1, length $c); | |
| 216 | 178 | } |
| 217 | 179 | # SSH agent |
| 218 | 180 | my ($rc, $out, $err) = _ssh_run($row, "test -f " . _sh_quote($path) . " && sha256sum " . _sh_quote($path) . " && wc -c < " . _sh_quote($path)); |
| 219 | 181 | if ($rc != 0) { |
| 220 | return (undef, 0, undef, undef); | |
| 182 | return (undef, 0, undef); | |
| 221 | 183 | } |
| 222 | 184 | my @lines = split /\n/, $out; |
| 223 | 185 | my $sha; |
| 224 | 186 | my $size; |
| 225 | 187 | foreach my $l (@lines) { |
| 226 | 188 | if ($l =~ /^([0-9a-f]{64})\s/) { $sha = $1; } |
| 227 | 189 | elsif ($l =~ /^(\d+)$/) { $size = int($1); } |
| 228 | } | |
| 229 | # Fetch content over SSH for the live diff -- capped at 512 KB | |
| 230 | my $content; | |
| 231 | if ($sha && $size && $size < 512_000) { | |
| 232 | my ($rc2, $out2, $err2) = _ssh_run($row, "cat " . _sh_quote($path)); | |
| 233 | $content = $out2 if $rc2 == 0; | |
| 234 | 190 | } |
| 235 | return ($sha, 1, $size, $content); | |
| 191 | return ($sha, 1, $size); | |
| 236 | 192 | } |
| 237 | 193 | |
| 238 | 194 | #--------------------------------------------------------------------- |
| 239 | 195 | # _do_restore($dbh, $row, $monitor) -> { status, message, backup_path } |
| 240 | 196 | #--------------------------------------------------------------------- |
| 241 | 197 | sub _do_restore { |
| 242 | 198 | my ($dbh, $row, $monitor) = @_; |
| 243 | 199 | my $blob_sha = $row->{blob_sha}; |
| 244 | 200 | unless ($blob_sha) { |
| 245 | 201 | return { status => 'failed', message => 'source change has no blob_sha' }; |
| 246 | 202 | } |
| 247 | 203 | my $content = _fetch_blob($dbh, $blob_sha); |
| 248 | 204 | unless (defined $content) { |
| 249 | 205 | return { status => 'failed', message => "blob $blob_sha not in BLOB_STORE" }; |
| 250 | 206 | } |
| 251 | 207 | |
| 252 | 208 | my $stamp = time(); |
| 253 | 209 | my $backup = "$row->{file_name}.drift_restore_backup_$stamp"; |
| 254 | 210 | |
| 255 | 211 | if (($row->{server_kind} // 'local') eq 'local') { |
| 256 | 212 | # Local restore -- backup then write |
| 257 | 213 | if (-e $row->{file_name}) { |
| 258 | 214 | unless (rename($row->{file_name}, $backup)) { |
| 259 | 215 | # Fallback: cp to backup, then overwrite in place |
| 260 | 216 | open(my $src, '<:raw', $row->{file_name}) or return { |
| 261 | 217 | status => 'failed', |
| 262 | 218 | message => "cannot read current file: $!" |
| 263 | 219 | }; |
| 264 | 220 | open(my $bak, '>:raw', $backup) or return { |
| 265 | 221 | status => 'failed', |
| 266 | 222 | message => "cannot write backup: $!" |
| 267 | 223 | }; |
| 268 | 224 | local $/; print {$bak} <$src>; |
| 269 | 225 | close $src; close $bak; |
| 270 | 226 | } |
| 271 | 227 | } else { |
| 272 | 228 | $backup = ''; # nothing to back up |
| 273 | 229 | } |
| 274 | 230 | # Write new content |
| 275 | 231 | open(my $out, '>:raw', $row->{file_name}) or return { |
| 276 | 232 | status => 'failed', |
| 277 | 233 | message => "write failed: $!", |
| 278 | 234 | backup_path => $backup, |
| 279 | 235 | }; |
| 280 | 236 | print {$out} $content; |
| 281 | 237 | close $out; |
| 282 | 238 | # Verify SHA post-write |
| 283 | 239 | open(my $v, '<:raw', $row->{file_name}); |
| 284 | 240 | local $/; my $verify = <$v>; close $v; |
| 285 | 241 | my $vsha = sha256_hex($verify); |
| 286 | 242 | if ($vsha ne $blob_sha) { |
| 287 | 243 | return { |
| 288 | 244 | status => 'failed', |
| 289 | 245 | message => "post-write SHA mismatch: $vsha vs $blob_sha", |
| 290 | 246 | backup_path => $backup, |
| 291 | 247 | }; |
| 292 | 248 | } |
| 293 | 249 | return { |
| 294 | 250 | status => 'success', |
| 295 | 251 | message => "restored " . length($content) . " bytes", |
| 296 | 252 | backup_path => $backup, |
| 297 | 253 | }; |
| 298 | 254 | } |
| 299 | 255 | |
| 300 | 256 | # SSH agent restore |
| 301 | 257 | # Step 1: cp current to backup (best-effort) |
| 302 | 258 | my ($rc, $out, $err) = _ssh_run($row, |
| 303 | 259 | "test -f " . _sh_quote($row->{file_name}) . |
| 304 | 260 | " && cp -p " . _sh_quote($row->{file_name}) . " " . _sh_quote($backup) . |
| 305 | 261 | " ; echo done"); |
| 306 | 262 | if ($rc != 0) { |
| 307 | 263 | $backup = ''; # backup failed but we'll still try the restore |
| 308 | 264 | } |
| 309 | 265 | |
| 310 | 266 | # Step 2: write content via SSH stdin |
| 311 | 267 | my $ok = _ssh_write($row, $row->{file_name}, $content); |
| 312 | 268 | unless ($ok) { |
| 313 | 269 | return { |
| 314 | 270 | status => 'failed', |
| 315 | 271 | message => "ssh write failed", |
| 316 | 272 | backup_path => $backup, |
| 317 | 273 | }; |
| 318 | 274 | } |
| 319 | 275 | |
| 320 | 276 | # Step 3: Verify SHA remotely |
| 321 | 277 | ($rc, $out, $err) = _ssh_run($row, "sha256sum " . _sh_quote($row->{file_name})); |
| 322 | 278 | my $vsha = ($out =~ /^([0-9a-f]{64})\s/) ? $1 : ''; |
| 323 | 279 | if ($vsha ne $blob_sha) { |
| 324 | 280 | return { |
| 325 | 281 | status => 'failed', |
| 326 | 282 | message => "post-write remote SHA mismatch: $vsha vs $blob_sha", |
| 327 | 283 | backup_path => $backup, |
| 328 | 284 | }; |
| 329 | 285 | } |
| 330 | 286 | return { |
| 331 | 287 | status => 'success', |
| 332 | 288 | message => "remote restored " . length($content) . " bytes", |
| 333 | 289 | backup_path => $backup, |
| 334 | 290 | }; |
| 335 | 291 | } |
| 336 | 292 | |
| 337 | 293 | #--------------------------------------------------------------------- |
| 338 | 294 | sub _log_restore { |
| 339 | 295 | my ($dbh, $row, $result) = @_; |
| 340 | 296 | my $q_file = $dbh->quote($row->{file_name} // ''); |
| 341 | 297 | my $q_sha = $dbh->quote($row->{blob_sha} // ''); |
| 342 | 298 | my $q_bak = $dbh->quote($result->{backup_path} // ''); |
| 343 | 299 | my $q_stat = $dbh->quote($result->{status} // 'failed'); |
| 344 | 300 | my $q_err = $dbh->quote($result->{status} eq 'success' ? '' : ($result->{message} // '')); |
| 345 | 301 | my $sid = int($row->{server_id} || 0); |
| 346 | 302 | my $cid = int($row->{id} || 0); |
| 347 | 303 | $db->db_readwrite($dbh, qq~ |
| 348 | 304 | INSERT INTO RESTORE_LOG |
| 349 | 305 | (source_change_id, server_id, target_file, source_blob_sha, |
| 350 | 306 | backup_path, status, error_message) |
| 351 | 307 | VALUES |
| 352 | 308 | ($cid, $sid, $q_file, $q_sha, $q_bak, $q_stat, $q_err) |
| 353 | 309 | ~, __FILE__, __LINE__); |
| 354 | 310 | } |
| 355 | 311 | |
| 356 | 312 | #--------------------------------------------------------------------- |
| 357 | 313 | # SSH helpers -- resolve key (encrypted blob or on-disk), spawn ssh |
| 358 | 314 | #--------------------------------------------------------------------- |
| 359 | 315 | sub _ssh_argv { |
| 360 | 316 | my ($row) = @_; |
| 361 | 317 | my $user = $row->{ssh_user} || 'root'; |
| 362 | 318 | my $host = $row->{ssh_host}; |
| 363 | 319 | my $port = $row->{ssh_port} || 22; |
| 364 | 320 | my $key_path = $row->{ssh_key_path} || ''; |
| 365 | 321 | my $tmp_key; |
| 366 | 322 | |
| 367 | 323 | if ($row->{ssh_key_blob}) { |
| 368 | 324 | my $pem = eval { MODS::Crypto::decrypt($row->{ssh_key_blob}) }; |
| 369 | 325 | if ($pem) { |
| 370 | 326 | my $t = time(); |
| 371 | 327 | my $p = "/tmp/.ds_restore_key_${t}_$$_" . int(rand(1_000_000)); |
| 372 | 328 | if (sysopen(my $fh, $p, |
| 373 | 329 | Fcntl::O_WRONLY() | Fcntl::O_CREAT() | Fcntl::O_EXCL(), 0600)) { |
| 374 | 330 | print $fh $pem; close $fh; |
| 375 | 331 | $key_path = $p; |
| 376 | 332 | $tmp_key = $p; |
| 377 | 333 | } |
| 378 | 334 | } |
| 379 | 335 | } |
| 380 | 336 | |
| 381 | 337 | my @argv = ('/usr/bin/ssh', |
| 382 | 338 | '-o', 'BatchMode=yes', |
| 383 | 339 | '-o', 'StrictHostKeyChecking=no', |
| 384 | 340 | '-o', 'UserKnownHostsFile=/dev/null', |
| 385 | 341 | '-o', 'LogLevel=ERROR', |
| 386 | 342 | '-o', 'ConnectTimeout=8', |
| 387 | 343 | '-p', $port); |
| 388 | 344 | push @argv, '-i', $key_path if $key_path && -r $key_path; |
| 389 | 345 | if ($row->{ssh_options}) { |
| 390 | 346 | foreach my $o (split /\s+/, $row->{ssh_options}) { |
| 391 | 347 | push @argv, '-o', $o if length $o; |
| 392 | 348 | } |
| 393 | 349 | } |
| 394 | 350 | push @argv, "$user\@$host"; |
| 395 | 351 | return (\@argv, $tmp_key); |
| 396 | 352 | } |
| 397 | 353 | |
| 398 | 354 | sub _ssh_run { |
| 399 | 355 | my ($row, $cmd) = @_; |
| 400 | 356 | my ($base, $tmp_key) = _ssh_argv($row); |
| 401 | 357 | my @full = (@$base, $cmd); |
| 402 | 358 | my ($wtr, $rdr, $err_fh); |
| 403 | 359 | $err_fh = gensym; |
| 404 | 360 | my $pid = eval { open3($wtr, $rdr, $err_fh, @full) }; |
| 405 | 361 | if ($@ || !$pid) { |
| 406 | 362 | unlink $tmp_key if $tmp_key; |
| 407 | 363 | return (-1, '', $@); |
| 408 | 364 | } |
| 409 | 365 | close $wtr; |
| 410 | 366 | my $out = do { local $/; <$rdr> // '' }; |
| 411 | 367 | my $err = do { local $/; <$err_fh> // '' }; |
| 412 | 368 | close $rdr; close $err_fh; |
| 413 | 369 | waitpid $pid, 0; |
| 414 | 370 | my $rc = $? >> 8; |
| 415 | 371 | unlink $tmp_key if $tmp_key; |
| 416 | 372 | return ($rc, $out, $err); |
| 417 | 373 | } |
| 418 | 374 | |
| 419 | 375 | sub _ssh_write { |
| 420 | 376 | my ($row, $path, $content) = @_; |
| 421 | 377 | my ($base, $tmp_key) = _ssh_argv($row); |
| 422 | 378 | # `cat > $path` on the remote side, content on stdin |
| 423 | 379 | my @full = (@$base, "cat > " . _sh_quote($path)); |
| 424 | 380 | my ($wtr, $rdr, $err_fh); |
| 425 | 381 | $err_fh = gensym; |
| 426 | 382 | my $pid = eval { open3($wtr, $rdr, $err_fh, @full) }; |
| 427 | 383 | if ($@ || !$pid) { |
| 428 | 384 | unlink $tmp_key if $tmp_key; |
| 429 | 385 | return 0; |
| 430 | 386 | } |
| 431 | 387 | binmode $wtr; |
| 432 | 388 | print {$wtr} $content; |
| 433 | 389 | close $wtr; |
| 434 | 390 | my $out = do { local $/; <$rdr> // '' }; |
| 435 | 391 | my $err = do { local $/; <$err_fh> // '' }; |
| 436 | 392 | close $rdr; close $err_fh; |
| 437 | 393 | waitpid $pid, 0; |
| 438 | 394 | my $rc = $? >> 8; |
| 439 | 395 | unlink $tmp_key if $tmp_key; |
| 440 | 396 | return $rc == 0 ? 1 : 0; |
| 441 | 397 | } |
| 442 | 398 | |
| 443 | 399 | sub _sh_quote { |
| 444 | 400 | my $s = shift; $s //= ''; |
| 445 | 401 | $s =~ s/'/'\\''/g; |
| 446 | 402 | return "'$s'"; |
| 447 | 403 | } |
| 448 | 404 | |
| 449 | 405 | sub _render_error { |
| 450 | 406 | my ($msg) = @_; |
| 451 | 407 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; |
| 452 | 408 | my @body = $tpl->template('restore.html', { |
| 453 | 409 | error_msg => $msg, |
| 454 | 410 | has_error => 1, |
| 455 | 411 | did_restore => 0, |
| 456 | 412 | }); |
| 457 | 413 | MODS::PageWrapper->new->wrapper( |
| 458 | 414 | page_title => 'Restore', page_key => '', |
| 459 | 415 | body_html => join('', @body), userinfo => { display_name => 'Operator' }, |
| 460 | 416 | ); |
| 461 | 417 | exit; |
| 462 | 418 | } |
Confirm restore
Backs current content to <target>.drift_restore_backup_<epoch>, writes the captured content, verifies SHA post-write.