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:19:08
Captured SHA85df3ad0f26ddf290b4ef90e9edba3a7fa9b0fc8733c91a4ec91b70e4d2040ea
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. +9 additions, -53 deletions, 174 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}) {
69 # Find the previous captured version of this file for the "before" pane.
70 # Prefer same-monitor scope, but fall back to file-name-only for legacy
71 # rows where file_monitor_list_id is NULL.
41 # Find the previous captured version of this file for the "before" pane
7242 my $q_fname = $dbh->quote($row->{file_name});
73 my $scope = defined $row->{file_monitor_list_id}
74 ? "file_monitor_list_id = $row->{file_monitor_list_id} AND "
75 : '';
7643 my $prev = $db->db_readwrite($dbh, qq~
7744 SELECT blob_sha FROM FILE_CHANGES
78 WHERE $scope
79 file_name = $q_fname
45 WHERE file_monitor_list_id = $row->{file_monitor_list_id}
46 AND file_name = $q_fname
8047 AND file_changes_id < $id
8148 AND blob_sha IS NOT NULL
8249 ORDER BY file_changes_id DESC LIMIT 1
8350 ~, __FILE__, __LINE__);
8451 $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>~;
52 $subtitle = "$row->{status} on $row->{server_name} at $row->{date_time}";
8753 $blob_sha = $row->{blob_sha};
8854 $prev_sha = $prev && $prev->{blob_sha} ? $prev->{blob_sha} : '';
8955 $meta = { %$row };
9056 }
9157}
9258elsif ($kind eq 'schema' && $id) {
9359 my $row = $db->db_readwrite($dbh, qq~
9460 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
61 sc.schema_blob_sha, sc.change_datetime, s.server_name, sc.server_id
9862 FROM SCHEMA_CHANGE sc LEFT JOIN SERVERS s ON s.server_id = sc.server_id
9963 WHERE sc.schema_change_id = $id LIMIT 1
10064 ~, __FILE__, __LINE__);
10165 if ($row && $row->{id}) {
10266 my $q_db = $dbh->quote($row->{database_name});
10367 my $q_t = $dbh->quote($row->{table_name});
10468 my $prev = $db->db_readwrite($dbh, qq~
10569 SELECT schema_blob_sha FROM SCHEMA_CHANGE
10670 WHERE server_id = $row->{server_id}
10771 AND database_name = $q_db AND table_name = $q_t
10872 AND schema_change_id < $id
10973 AND schema_blob_sha IS NOT NULL
11074 ORDER BY schema_change_id DESC LIMIT 1
11175 ~, __FILE__, __LINE__);
11276 $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>~;
77 $subtitle = "on $row->{server_name} at $row->{change_datetime}";
11578 $blob_sha = $row->{schema_blob_sha};
11679 $prev_sha = $prev && $prev->{schema_blob_sha} ? $prev->{schema_blob_sha} : '';
11780 $meta = { %$row };
11881 }
11982}
12083
12184# Fetch + decompress blobs
12285my $curr = $blob_sha ? _fetch_blob($db, $dbh, $blob_sha) : '';
12386my $prev = $prev_sha ? _fetch_blob($db, $dbh, $prev_sha) : '';
12487
12588$db->db_disconnect($dbh);
12689
12790# Compute a naive line-level diff
128my @lines = MODS::Diff::lines($prev // '', $curr // '');
91my @lines = _line_diff($prev // '', $curr // '');
12992my ($ct_add, $ct_del) = (0, 0);
13093foreach my $L (@lines) {
13194 $ct_add++ if $L->{op} eq 'add';
13295 $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;
13996}
14097
14198my $tvars = {
14299 title => $title || '(unknown)',
143100 subtitle => $subtitle,
144101 kind => $kind,
145102 id => $id,
146103 has_diff => scalar(@lines) ? 1 : 0,
147104 lines => \@lines,
148105 ct_add => $ct_add,
149106 ct_del => $ct_del,
150107 ct_context => scalar(@lines) - $ct_add - $ct_del,
151108 has_prev => $prev_sha ? 1 : 0,
152109 curr_sha => substr($blob_sha || '', 0, 12),
153110 prev_sha => substr($prev_sha || '', 0, 12),
154 restore_url => ($kind eq 'file' && $blob_sha) ? "/restore.cgi?id=$id" : '',
155111};
156112
157113my @body = $tpl->template('diff.html', $tvars);
158114MODS::PageWrapper->new->wrapper(
159115 page_title => "Diff -- $title",
160116 page_key => ($kind eq 'schema' ? 'schema_changes' : 'file_changes'),
161117 body_html => join('', @body),
162118 userinfo => { display_name => 'Operator' },
163119);
164120
165121#---------------------------------------------------------------------
166122sub _fetch_blob {
167123 my ($db, $dbh, $sha) = @_;
168124 my $sth = $dbh->prepare("SELECT content_gz FROM BLOB_STORE WHERE blob_sha = ?");
169125 return '' unless $sth && $sth->execute($sha);
170126 my $row = $sth->fetchrow_arrayref;
171127 $sth->finish;
172128 return '' unless $row && $row->[0];
173129 my $decompressed = eval { uncompress($row->[0]) };
174130 return $decompressed // '';
175131}
176132
177133#---------------------------------------------------------------------
178134# _line_diff -- naive line-level diff. Returns an array of hashrefs:
179135# { op => 'add' | 'del' | 'ctx', text => $line, old_n, new_n }
180136# Uses a simple LCS-based approach; adequate for small-to-medium files.
181137# For huge diffs shell out to /usr/bin/diff (future optimization).
182138#---------------------------------------------------------------------
183139sub _line_diff {
184140 my ($old, $new) = @_;
185141 my @old_lines = split /\r?\n/, ($old // '');
186142 my @new_lines = split /\r?\n/, ($new // '');
187143
188144 # LCS length table (dynamic programming) -- capped to prevent
189145 # runaway memory on huge inputs.
190146 my $O = scalar @old_lines;
191147 my $N = scalar @new_lines;
192148 if ($O > 5000 || $N > 5000) {
193149 # Fall back: just dump both sides linearly, marked as del/add.
194150 my @out;
195151 my $i = 0;
196152 push @out, { op => 'del', text => $_, old_n => ++$i, new_n => '' } for @old_lines;
197153 my $j = 0;
198154 push @out, { op => 'add', text => $_, old_n => '', new_n => ++$j } for @new_lines;
199155 return @out;
200156 }
201157
202158 my @lcs; for my $i (0..$O) { $lcs[$i][0] = 0; }
203159 for my $j (0..$N) { $lcs[0][$j] = 0; }
204160 for my $i (1..$O) {
205161 for my $j (1..$N) {
206162 $lcs[$i][$j] = $old_lines[$i-1] eq $new_lines[$j-1]
207163 ? $lcs[$i-1][$j-1] + 1
208164 : ($lcs[$i-1][$j] >= $lcs[$i][$j-1] ? $lcs[$i-1][$j] : $lcs[$i][$j-1]);
209165 }
210166 }
211167
212168 my @out;
213169 my ($i, $j) = ($O, $N);
214170 while ($i > 0 || $j > 0) {
215171 if ($i > 0 && $j > 0 && $old_lines[$i-1] eq $new_lines[$j-1]) {
216172 unshift @out, { op => 'ctx', text => $old_lines[$i-1], old_n => $i, new_n => $j };
217173 $i--; $j--;
218174 } elsif ($j > 0 && ($i == 0 || $lcs[$i][$j-1] >= $lcs[$i-1][$j])) {
219175 unshift @out, { op => 'add', text => $new_lines[$j-1], old_n => '', new_n => $j };
220176 $j--;
221177 } else {
222178 unshift @out, { op => 'del', text => $old_lines[$i-1], old_n => $i, new_n => '' };
223179 $i--;
224180 }
225181 }
226182 return @out;
227183}
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.