Restore

O Operator
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/diff.cgi
SiteDriftSense self-monitor on local
Kindlocal
Captured at2026-07-10 22:22:26
Captured SHAba2f6f72e4fb44ac8f234a8520c5eac473f4c11eb70b0a77135e2da969820615
Current state on disk
Live check just now. If SHAs match, the restore is a no-op.
File presentyes
Size9204 bytes
Current SHA902f256a1a7c
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. +6 additions, -45 deletions, 182 unchanged context lines.
11#!/usr/bin/perl
22#======================================================================
33# DriftSense -- /diff.cgi
44#
55# Renders a two-pane diff for a single change captured in FILE_CHANGES
66# or SCHEMA_CHANGE. Pulls both blobs from BLOB_STORE, decompresses,
77# runs a naive LCS-style line diff, marks added / removed / context,
88# renders as syntax-highlight-ish HTML with monospace typography.
99#
1010# URL:
1111# /diff.cgi?kind=file&id=N -- FILE_CHANGES row
1212# /diff.cgi?kind=schema&id=N -- SCHEMA_CHANGE row
1313#======================================================================
1414use strict;
1515use warnings;
1616use CGI ();
1717use Compress::Zlib qw(uncompress);
1818use MODS::Config; use MODS::DBConnect; use MODS::Template; use MODS::PageWrapper;
19use MODS::Diff;
20use MODS::CompileCheck;
21use JSON::PP ();
2219
2320my $cgi = CGI->new;
2421my $db = MODS::DBConnect->new;
2522my $tpl = MODS::Template->new;
26$|=1;
23$|=1; print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n";
2724
2825my $kind = $cgi->param('kind') || '';
2926my $id = int($cgi->param('id') || 0);
30my $op = $cgi->param('op') || '';
31
32# ---- AJAX: compile check ------------------------------------------
33if ($op eq 'compile' && $id) {
34 my $dbh2 = $db->db_connect;
35 my $r = $db->db_readwrite($dbh2, qq~
36 SELECT fc.file_name, fc.blob_sha, bs.content_gz
37 FROM FILE_CHANGES fc
38 LEFT JOIN BLOB_STORE bs ON bs.blob_sha = fc.blob_sha
39 WHERE fc.file_changes_id = $id LIMIT 1
40 ~, __FILE__, __LINE__);
41 $db->db_disconnect($dbh2);
42 print "Content-Type: application/json\nCache-Control: no-cache\n\n";
43 unless ($r && $r->{content_gz}) {
44 print JSON::PP::encode_json({ status => 'unchecked', message => 'no blob' });
45 exit;
46 }
47 my $content = eval { Compress::Zlib::uncompress($r->{content_gz}) };
48 my $result = MODS::CompileCheck::check($content, $r->{file_name});
49 print JSON::PP::encode_json($result);
50 exit;
51}
5227
53# HTML branch from here on
54print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n";
5528my $dbh = $db->db_connect or die "DB connect failed\n";
5629
5730my ($title, $subtitle, $blob_sha, $prev_sha, $meta) = ('', '', '', '', {});
5831
5932if ($kind eq 'file' && $id) {
6033 my $row = $db->db_readwrite($dbh, qq~
6134 SELECT fc.file_changes_id AS id, fc.file_name, fc.status, fc.blob_sha,
62 fc.date_time, UNIX_TIMESTAMP(fc.date_time) AS ts_epoch,
63 fc.uid, fc.gid, fc.permissions, fc.is_ts_only,
35 fc.date_time, fc.uid, fc.gid, fc.permissions, fc.is_ts_only,
6436 fc.file_monitor_list_id, s.server_name
6537 FROM FILE_CHANGES fc LEFT JOIN SERVERS s ON s.server_id = fc.server_id
6638 WHERE fc.file_changes_id = $id LIMIT 1
6739 ~, __FILE__, __LINE__);
6840 if ($row && $row->{id}) {
6941 # Find the previous captured version of this file for the "before" pane.
7042 # Prefer same-monitor scope, but fall back to file-name-only for legacy
7143 # rows where file_monitor_list_id is NULL.
7244 my $q_fname = $dbh->quote($row->{file_name});
7345 my $scope = defined $row->{file_monitor_list_id}
7446 ? "file_monitor_list_id = $row->{file_monitor_list_id} AND "
7547 : '';
7648 my $prev = $db->db_readwrite($dbh, qq~
7749 SELECT blob_sha FROM FILE_CHANGES
7850 WHERE $scope
7951 file_name = $q_fname
8052 AND file_changes_id < $id
8153 AND blob_sha IS NOT NULL
8254 ORDER BY file_changes_id DESC LIMIT 1
8355 ~, __FILE__, __LINE__);
8456 $title = $row->{file_name};
85 my $ep = int($row->{ts_epoch} || 0);
86 $subtitle = qq~$row->{status} on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{date_time}</span>~;
57 $subtitle = "$row->{status} on $row->{server_name} at $row->{date_time}";
8758 $blob_sha = $row->{blob_sha};
8859 $prev_sha = $prev && $prev->{blob_sha} ? $prev->{blob_sha} : '';
8960 $meta = { %$row };
9061 }
9162}
9263elsif ($kind eq 'schema' && $id) {
9364 my $row = $db->db_readwrite($dbh, qq~
9465 SELECT sc.schema_change_id AS id, sc.database_name, sc.table_name, sc.changes,
95 sc.schema_blob_sha, sc.change_datetime,
96 UNIX_TIMESTAMP(sc.change_datetime) AS ts_epoch,
97 s.server_name, sc.server_id
66 sc.schema_blob_sha, sc.change_datetime, s.server_name, sc.server_id
9867 FROM SCHEMA_CHANGE sc LEFT JOIN SERVERS s ON s.server_id = sc.server_id
9968 WHERE sc.schema_change_id = $id LIMIT 1
10069 ~, __FILE__, __LINE__);
10170 if ($row && $row->{id}) {
10271 my $q_db = $dbh->quote($row->{database_name});
10372 my $q_t = $dbh->quote($row->{table_name});
10473 my $prev = $db->db_readwrite($dbh, qq~
10574 SELECT schema_blob_sha FROM SCHEMA_CHANGE
10675 WHERE server_id = $row->{server_id}
10776 AND database_name = $q_db AND table_name = $q_t
10877 AND schema_change_id < $id
10978 AND schema_blob_sha IS NOT NULL
11079 ORDER BY schema_change_id DESC LIMIT 1
11180 ~, __FILE__, __LINE__);
11281 $title = "$row->{database_name}.$row->{table_name}";
113 my $ep = int($row->{ts_epoch} || 0);
114 $subtitle = qq~on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{change_datetime}</span>~;
82 $subtitle = "on $row->{server_name} at $row->{change_datetime}";
11583 $blob_sha = $row->{schema_blob_sha};
11684 $prev_sha = $prev && $prev->{schema_blob_sha} ? $prev->{schema_blob_sha} : '';
11785 $meta = { %$row };
11886 }
11987}
12088
12189# Fetch + decompress blobs
12290my $curr = $blob_sha ? _fetch_blob($db, $dbh, $blob_sha) : '';
12391my $prev = $prev_sha ? _fetch_blob($db, $dbh, $prev_sha) : '';
12492
12593$db->db_disconnect($dbh);
12694
12795# Compute a naive line-level diff
128my @lines = MODS::Diff::lines($prev // '', $curr // '');
96my @lines = _line_diff($prev // '', $curr // '');
12997my ($ct_add, $ct_del) = (0, 0);
13098foreach my $L (@lines) {
13199 $ct_add++ if $L->{op} eq 'add';
132100 $ct_del++ if $L->{op} eq 'del';
133 # HTML-escape diff'd content -- files under monitor may themselves be
134 # HTML/JS/CSS, and dumping raw content into an on-screen table renders
135 # the tags live. Escape it so it displays as source.
136 my $t = $L->{text}; $t = '' unless defined $t;
137 $t =~ s/&/&amp;/g; $t =~ s/</&lt;/g; $t =~ s/>/&gt;/g;
138 $L->{text} = $t;
139101}
140102
141103my $tvars = {
142104 title => $title || '(unknown)',
143105 subtitle => $subtitle,
144106 kind => $kind,
145107 id => $id,
146108 has_diff => scalar(@lines) ? 1 : 0,
147109 lines => \@lines,
148110 ct_add => $ct_add,
149111 ct_del => $ct_del,
150112 ct_context => scalar(@lines) - $ct_add - $ct_del,
151113 has_prev => $prev_sha ? 1 : 0,
152114 curr_sha => substr($blob_sha || '', 0, 12),
153115 prev_sha => substr($prev_sha || '', 0, 12),
154 restore_url => ($kind eq 'file' && $blob_sha) ? "/restore.cgi?id=$id" : '',
155116};
156117
157118my @body = $tpl->template('diff.html', $tvars);
158119MODS::PageWrapper->new->wrapper(
159120 page_title => "Diff -- $title",
160121 page_key => ($kind eq 'schema' ? 'schema_changes' : 'file_changes'),
161122 body_html => join('', @body),
162123 userinfo => { display_name => 'Operator' },
163124);
164125
165126#---------------------------------------------------------------------
166127sub _fetch_blob {
167128 my ($db, $dbh, $sha) = @_;
168129 my $sth = $dbh->prepare("SELECT content_gz FROM BLOB_STORE WHERE blob_sha = ?");
169130 return '' unless $sth && $sth->execute($sha);
170131 my $row = $sth->fetchrow_arrayref;
171132 $sth->finish;
172133 return '' unless $row && $row->[0];
173134 my $decompressed = eval { uncompress($row->[0]) };
174135 return $decompressed // '';
175136}
176137
177138#---------------------------------------------------------------------
178139# _line_diff -- naive line-level diff. Returns an array of hashrefs:
179140# { op => 'add' | 'del' | 'ctx', text => $line, old_n, new_n }
180141# Uses a simple LCS-based approach; adequate for small-to-medium files.
181142# For huge diffs shell out to /usr/bin/diff (future optimization).
182143#---------------------------------------------------------------------
183144sub _line_diff {
184145 my ($old, $new) = @_;
185146 my @old_lines = split /\r?\n/, ($old // '');
186147 my @new_lines = split /\r?\n/, ($new // '');
187148
188149 # LCS length table (dynamic programming) -- capped to prevent
189150 # runaway memory on huge inputs.
190151 my $O = scalar @old_lines;
191152 my $N = scalar @new_lines;
192153 if ($O > 5000 || $N > 5000) {
193154 # Fall back: just dump both sides linearly, marked as del/add.
194155 my @out;
195156 my $i = 0;
196157 push @out, { op => 'del', text => $_, old_n => ++$i, new_n => '' } for @old_lines;
197158 my $j = 0;
198159 push @out, { op => 'add', text => $_, old_n => '', new_n => ++$j } for @new_lines;
199160 return @out;
200161 }
201162
202163 my @lcs; for my $i (0..$O) { $lcs[$i][0] = 0; }
203164 for my $j (0..$N) { $lcs[0][$j] = 0; }
204165 for my $i (1..$O) {
205166 for my $j (1..$N) {
206167 $lcs[$i][$j] = $old_lines[$i-1] eq $new_lines[$j-1]
207168 ? $lcs[$i-1][$j-1] + 1
208169 : ($lcs[$i-1][$j] >= $lcs[$i][$j-1] ? $lcs[$i-1][$j] : $lcs[$i][$j-1]);
209170 }
210171 }
211172
212173 my @out;
213174 my ($i, $j) = ($O, $N);
214175 while ($i > 0 || $j > 0) {
215176 if ($i > 0 && $j > 0 && $old_lines[$i-1] eq $new_lines[$j-1]) {
216177 unshift @out, { op => 'ctx', text => $old_lines[$i-1], old_n => $i, new_n => $j };
217178 $i--; $j--;
218179 } elsif ($j > 0 && ($i == 0 || $lcs[$i][$j-1] >= $lcs[$i-1][$j])) {
219180 unshift @out, { op => 'add', text => $new_lines[$j-1], old_n => '', new_n => $j };
220181 $j--;
221182 } else {
222183 unshift @out, { op => 'del', text => $old_lines[$i-1], old_n => $i, new_n => '' };
223184 $i--;
224185 }
225186 }
226187 return @out;
227188}
Confirm restore
Backs current content to <target>.drift_restore_backup_<epoch>, writes the captured content, verifies SHA post-write.
Cancel Logged to RESTORE_LOG regardless of outcome.