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-11 23:34:35
Captured SHA4de41045473797aeb542fea18ad6b4dae555252baa3462a0c024f58a5101da0d
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. +2 additions, -29 deletions, 198 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,
6235 fc.date_time, UNIX_TIMESTAMP(fc.date_time) AS ts_epoch,
6336 fc.uid, fc.gid, fc.permissions, fc.is_ts_only,
6437 fc.file_monitor_list_id, s.server_name
6538 FROM FILE_CHANGES fc LEFT JOIN SERVERS s ON s.server_id = fc.server_id
6639 WHERE fc.file_changes_id = $id LIMIT 1
6740 ~, __FILE__, __LINE__);
6841 if ($row && $row->{id}) {
6942 # Find the previous captured version of this file for the "before" pane.
7043 # Prefer same-monitor scope, but fall back to file-name-only for legacy
7144 # rows where file_monitor_list_id is NULL.
7245 my $q_fname = $dbh->quote($row->{file_name});
7346 my $scope = defined $row->{file_monitor_list_id}
7447 ? "file_monitor_list_id = $row->{file_monitor_list_id} AND "
7548 : '';
7649 my $prev = $db->db_readwrite($dbh, qq~
7750 SELECT blob_sha FROM FILE_CHANGES
7851 WHERE $scope
7952 file_name = $q_fname
8053 AND file_changes_id < $id
8154 AND blob_sha IS NOT NULL
8255 ORDER BY file_changes_id DESC LIMIT 1
8356 ~, __FILE__, __LINE__);
8457 $title = $row->{file_name};
8558 my $ep = int($row->{ts_epoch} || 0);
8659 $subtitle = qq~$row->{status} on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{date_time}</span>~;
8760 $blob_sha = $row->{blob_sha};
8861 $prev_sha = $prev && $prev->{blob_sha} ? $prev->{blob_sha} : '';
8962 $meta = { %$row };
9063 }
9164}
9265elsif ($kind eq 'schema' && $id) {
9366 my $row = $db->db_readwrite($dbh, qq~
9467 SELECT sc.schema_change_id AS id, sc.database_name, sc.table_name, sc.changes,
9568 sc.schema_blob_sha, sc.change_datetime,
9669 UNIX_TIMESTAMP(sc.change_datetime) AS ts_epoch,
9770 s.server_name, sc.server_id
9871 FROM SCHEMA_CHANGE sc LEFT JOIN SERVERS s ON s.server_id = sc.server_id
9972 WHERE sc.schema_change_id = $id LIMIT 1
10073 ~, __FILE__, __LINE__);
10174 if ($row && $row->{id}) {
10275 my $q_db = $dbh->quote($row->{database_name});
10376 my $q_t = $dbh->quote($row->{table_name});
10477 my $prev = $db->db_readwrite($dbh, qq~
10578 SELECT schema_blob_sha FROM SCHEMA_CHANGE
10679 WHERE server_id = $row->{server_id}
10780 AND database_name = $q_db AND table_name = $q_t
10881 AND schema_change_id < $id
10982 AND schema_blob_sha IS NOT NULL
11083 ORDER BY schema_change_id DESC LIMIT 1
11184 ~, __FILE__, __LINE__);
11285 $title = "$row->{database_name}.$row->{table_name}";
11386 my $ep = int($row->{ts_epoch} || 0);
11487 $subtitle = qq~on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{change_datetime}</span>~;
11588 $blob_sha = $row->{schema_blob_sha};
11689 $prev_sha = $prev && $prev->{schema_blob_sha} ? $prev->{schema_blob_sha} : '';
11790 $meta = { %$row };
11891 }
11992}
12093
12194# Fetch + decompress blobs
12295my $curr = $blob_sha ? _fetch_blob($db, $dbh, $blob_sha) : '';
12396my $prev = $prev_sha ? _fetch_blob($db, $dbh, $prev_sha) : '';
12497
12598$db->db_disconnect($dbh);
12699
127100# Compute a naive line-level diff
128my @lines = MODS::Diff::lines($prev // '', $curr // '');
101my @lines = _line_diff($prev // '', $curr // '');
129102my ($ct_add, $ct_del) = (0, 0);
130103foreach my $L (@lines) {
131104 $ct_add++ if $L->{op} eq 'add';
132105 $ct_del++ if $L->{op} eq 'del';
133106 # HTML-escape diff'd content -- files under monitor may themselves be
134107 # HTML/JS/CSS, and dumping raw content into an on-screen table renders
135108 # the tags live. Escape it so it displays as source.
136109 my $t = $L->{text}; $t = '' unless defined $t;
137110 $t =~ s/&/&amp;/g; $t =~ s/</&lt;/g; $t =~ s/>/&gt;/g;
138111 $L->{text} = $t;
139112}
140113
141114my $tvars = {
142115 title => $title || '(unknown)',
143116 subtitle => $subtitle,
144117 kind => $kind,
145118 id => $id,
146119 has_diff => scalar(@lines) ? 1 : 0,
147120 lines => \@lines,
148121 ct_add => $ct_add,
149122 ct_del => $ct_del,
150123 ct_context => scalar(@lines) - $ct_add - $ct_del,
151124 has_prev => $prev_sha ? 1 : 0,
152125 curr_sha => substr($blob_sha || '', 0, 12),
153126 prev_sha => substr($prev_sha || '', 0, 12),
154127 restore_url => ($kind eq 'file' && $blob_sha) ? "/restore.cgi?id=$id" : '',
155128};
156129
157130my @body = $tpl->template('diff.html', $tvars);
158131MODS::PageWrapper->new->wrapper(
159132 page_title => "Diff -- $title",
160133 page_key => ($kind eq 'schema' ? 'schema_changes' : 'file_changes'),
161134 body_html => join('', @body),
162135 userinfo => { display_name => 'Operator' },
163136);
164137
165138#---------------------------------------------------------------------
166139sub _fetch_blob {
167140 my ($db, $dbh, $sha) = @_;
168141 my $sth = $dbh->prepare("SELECT content_gz FROM BLOB_STORE WHERE blob_sha = ?");
169142 return '' unless $sth && $sth->execute($sha);
170143 my $row = $sth->fetchrow_arrayref;
171144 $sth->finish;
172145 return '' unless $row && $row->[0];
173146 my $decompressed = eval { uncompress($row->[0]) };
174147 return $decompressed // '';
175148}
176149
177150#---------------------------------------------------------------------
178151# _line_diff -- naive line-level diff. Returns an array of hashrefs:
179152# { op => 'add' | 'del' | 'ctx', text => $line, old_n, new_n }
180153# Uses a simple LCS-based approach; adequate for small-to-medium files.
181154# For huge diffs shell out to /usr/bin/diff (future optimization).
182155#---------------------------------------------------------------------
183156sub _line_diff {
184157 my ($old, $new) = @_;
185158 my @old_lines = split /\r?\n/, ($old // '');
186159 my @new_lines = split /\r?\n/, ($new // '');
187160
188161 # LCS length table (dynamic programming) -- capped to prevent
189162 # runaway memory on huge inputs.
190163 my $O = scalar @old_lines;
191164 my $N = scalar @new_lines;
192165 if ($O > 5000 || $N > 5000) {
193166 # Fall back: just dump both sides linearly, marked as del/add.
194167 my @out;
195168 my $i = 0;
196169 push @out, { op => 'del', text => $_, old_n => ++$i, new_n => '' } for @old_lines;
197170 my $j = 0;
198171 push @out, { op => 'add', text => $_, old_n => '', new_n => ++$j } for @new_lines;
199172 return @out;
200173 }
201174
202175 my @lcs; for my $i (0..$O) { $lcs[$i][0] = 0; }
203176 for my $j (0..$N) { $lcs[0][$j] = 0; }
204177 for my $i (1..$O) {
205178 for my $j (1..$N) {
206179 $lcs[$i][$j] = $old_lines[$i-1] eq $new_lines[$j-1]
207180 ? $lcs[$i-1][$j-1] + 1
208181 : ($lcs[$i-1][$j] >= $lcs[$i][$j-1] ? $lcs[$i-1][$j] : $lcs[$i][$j-1]);
209182 }
210183 }
211184
212185 my @out;
213186 my ($i, $j) = ($O, $N);
214187 while ($i > 0 || $j > 0) {
215188 if ($i > 0 && $j > 0 && $old_lines[$i-1] eq $new_lines[$j-1]) {
216189 unshift @out, { op => 'ctx', text => $old_lines[$i-1], old_n => $i, new_n => $j };
217190 $i--; $j--;
218191 } elsif ($j > 0 && ($i == 0 || $lcs[$i][$j-1] >= $lcs[$i-1][$j])) {
219192 unshift @out, { op => 'add', text => $new_lines[$j-1], old_n => '', new_n => $j };
220193 $j--;
221194 } else {
222195 unshift @out, { op => 'del', text => $old_lines[$i-1], old_n => $i, new_n => '' };
223196 $i--;
224197 }
225198 }
226199 return @out;
227200}
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.