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