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-12 00:48:51
Captured SHA6bddacd3e8ab941883f744224c3b731968dcf4ef527ef35e3279718c8f29d3fc
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. +1 additions, -3 deletions, 224 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;
1919use MODS::Diff;
2020use MODS::CompileCheck;
2121use JSON::PP ();
2222
2323my $cgi = CGI->new;
2424my $db = MODS::DBConnect->new;
2525my $tpl = MODS::Template->new;
26$|=1;
26$|=1; print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n";
2727
2828my $kind = $cgi->param('kind') || '';
2929my $id = int($cgi->param('id') || 0);
3030my $op = $cgi->param('op') || '';
3131
3232# ---- AJAX: compile check ------------------------------------------
3333if ($op eq 'compile' && $id) {
3434 my $dbh2 = $db->db_connect;
3535 my $r = $db->db_readwrite($dbh2, qq~
3636 SELECT fc.file_name, fc.blob_sha, bs.content_gz
3737 FROM FILE_CHANGES fc
3838 LEFT JOIN BLOB_STORE bs ON bs.blob_sha = fc.blob_sha
3939 WHERE fc.file_changes_id = $id LIMIT 1
4040 ~, __FILE__, __LINE__);
4141 $db->db_disconnect($dbh2);
4242 print "Content-Type: application/json\nCache-Control: no-cache\n\n";
4343 unless ($r && $r->{content_gz}) {
4444 print JSON::PP::encode_json({ status => 'unchecked', message => 'no blob' });
4545 exit;
4646 }
4747 my $content = eval { Compress::Zlib::uncompress($r->{content_gz}) };
4848 my $result = MODS::CompileCheck::check($content, $r->{file_name});
4949 print JSON::PP::encode_json($result);
5050 exit;
5151}
5252
53# HTML branch from here on
54print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n";
5553my $dbh = $db->db_connect or die "DB connect failed\n";
5654
5755my ($title, $subtitle, $blob_sha, $prev_sha, $meta) = ('', '', '', '', {});
5856
5957if ($kind eq 'file' && $id) {
6058 my $row = $db->db_readwrite($dbh, qq~
6159 SELECT fc.file_changes_id AS id, fc.file_name, fc.status, fc.blob_sha,
6260 fc.date_time, UNIX_TIMESTAMP(fc.date_time) AS ts_epoch,
6361 fc.uid, fc.gid, fc.permissions, fc.is_ts_only,
6462 fc.file_monitor_list_id, s.server_name
6563 FROM FILE_CHANGES fc LEFT JOIN SERVERS s ON s.server_id = fc.server_id
6664 WHERE fc.file_changes_id = $id LIMIT 1
6765 ~, __FILE__, __LINE__);
6866 if ($row && $row->{id}) {
6967 # Find the previous captured version of this file for the "before" pane.
7068 # Prefer same-monitor scope, but fall back to file-name-only for legacy
7169 # rows where file_monitor_list_id is NULL.
7270 my $q_fname = $dbh->quote($row->{file_name});
7371 my $scope = defined $row->{file_monitor_list_id}
7472 ? "file_monitor_list_id = $row->{file_monitor_list_id} AND "
7573 : '';
7674 my $prev = $db->db_readwrite($dbh, qq~
7775 SELECT blob_sha FROM FILE_CHANGES
7876 WHERE $scope
7977 file_name = $q_fname
8078 AND file_changes_id < $id
8179 AND blob_sha IS NOT NULL
8280 ORDER BY file_changes_id DESC LIMIT 1
8381 ~, __FILE__, __LINE__);
8482 $title = $row->{file_name};
8583 my $ep = int($row->{ts_epoch} || 0);
8684 $subtitle = qq~$row->{status} on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{date_time}</span>~;
8785 $blob_sha = $row->{blob_sha};
8886 $prev_sha = $prev && $prev->{blob_sha} ? $prev->{blob_sha} : '';
8987 $meta = { %$row };
9088 }
9189}
9290elsif ($kind eq 'schema' && $id) {
9391 my $row = $db->db_readwrite($dbh, qq~
9492 SELECT sc.schema_change_id AS id, sc.database_name, sc.table_name, sc.changes,
9593 sc.schema_blob_sha, sc.change_datetime,
9694 UNIX_TIMESTAMP(sc.change_datetime) AS ts_epoch,
9795 s.server_name, sc.server_id
9896 FROM SCHEMA_CHANGE sc LEFT JOIN SERVERS s ON s.server_id = sc.server_id
9997 WHERE sc.schema_change_id = $id LIMIT 1
10098 ~, __FILE__, __LINE__);
10199 if ($row && $row->{id}) {
102100 my $q_db = $dbh->quote($row->{database_name});
103101 my $q_t = $dbh->quote($row->{table_name});
104102 my $prev = $db->db_readwrite($dbh, qq~
105103 SELECT schema_blob_sha FROM SCHEMA_CHANGE
106104 WHERE server_id = $row->{server_id}
107105 AND database_name = $q_db AND table_name = $q_t
108106 AND schema_change_id < $id
109107 AND schema_blob_sha IS NOT NULL
110108 ORDER BY schema_change_id DESC LIMIT 1
111109 ~, __FILE__, __LINE__);
112110 $title = "$row->{database_name}.$row->{table_name}";
113111 my $ep = int($row->{ts_epoch} || 0);
114112 $subtitle = qq~on $row->{server_name} at <span class="ts" data-ts="$ep" data-fmt="datetime">$row->{change_datetime}</span>~;
115113 $blob_sha = $row->{schema_blob_sha};
116114 $prev_sha = $prev && $prev->{schema_blob_sha} ? $prev->{schema_blob_sha} : '';
117115 $meta = { %$row };
118116 }
119117}
120118
121119# Fetch + decompress blobs
122120my $curr = $blob_sha ? _fetch_blob($db, $dbh, $blob_sha) : '';
123121my $prev = $prev_sha ? _fetch_blob($db, $dbh, $prev_sha) : '';
124122
125123$db->db_disconnect($dbh);
126124
127125# Compute a naive line-level diff
128126my @lines = MODS::Diff::lines($prev // '', $curr // '');
129127my ($ct_add, $ct_del) = (0, 0);
130128foreach my $L (@lines) {
131129 $ct_add++ if $L->{op} eq 'add';
132130 $ct_del++ if $L->{op} eq 'del';
133131 # HTML-escape diff'd content -- files under monitor may themselves be
134132 # HTML/JS/CSS, and dumping raw content into an on-screen table renders
135133 # the tags live. Escape it so it displays as source.
136134 my $t = $L->{text}; $t = '' unless defined $t;
137135 $t =~ s/&/&amp;/g; $t =~ s/</&lt;/g; $t =~ s/>/&gt;/g;
138136 $L->{text} = $t;
139137}
140138
141139my $tvars = {
142140 title => $title || '(unknown)',
143141 subtitle => $subtitle,
144142 kind => $kind,
145143 id => $id,
146144 has_diff => scalar(@lines) ? 1 : 0,
147145 lines => \@lines,
148146 ct_add => $ct_add,
149147 ct_del => $ct_del,
150148 ct_context => scalar(@lines) - $ct_add - $ct_del,
151149 has_prev => $prev_sha ? 1 : 0,
152150 curr_sha => substr($blob_sha || '', 0, 12),
153151 prev_sha => substr($prev_sha || '', 0, 12),
154152 restore_url => ($kind eq 'file' && $blob_sha) ? "/restore.cgi?id=$id" : '',
155153};
156154
157155my @body = $tpl->template('diff.html', $tvars);
158156MODS::PageWrapper->new->wrapper(
159157 page_title => "Diff -- $title",
160158 page_key => ($kind eq 'schema' ? 'schema_changes' : 'file_changes'),
161159 body_html => join('', @body),
162160 userinfo => { display_name => 'Operator' },
163161);
164162
165163#---------------------------------------------------------------------
166164sub _fetch_blob {
167165 my ($db, $dbh, $sha) = @_;
168166 my $sth = $dbh->prepare("SELECT content_gz FROM BLOB_STORE WHERE blob_sha = ?");
169167 return '' unless $sth && $sth->execute($sha);
170168 my $row = $sth->fetchrow_arrayref;
171169 $sth->finish;
172170 return '' unless $row && $row->[0];
173171 my $decompressed = eval { uncompress($row->[0]) };
174172 return $decompressed // '';
175173}
176174
177175#---------------------------------------------------------------------
178176# _line_diff -- naive line-level diff. Returns an array of hashrefs:
179177# { op => 'add' | 'del' | 'ctx', text => $line, old_n, new_n }
180178# Uses a simple LCS-based approach; adequate for small-to-medium files.
181179# For huge diffs shell out to /usr/bin/diff (future optimization).
182180#---------------------------------------------------------------------
183181sub _line_diff {
184182 my ($old, $new) = @_;
185183 my @old_lines = split /\r?\n/, ($old // '');
186184 my @new_lines = split /\r?\n/, ($new // '');
187185
188186 # LCS length table (dynamic programming) -- capped to prevent
189187 # runaway memory on huge inputs.
190188 my $O = scalar @old_lines;
191189 my $N = scalar @new_lines;
192190 if ($O > 5000 || $N > 5000) {
193191 # Fall back: just dump both sides linearly, marked as del/add.
194192 my @out;
195193 my $i = 0;
196194 push @out, { op => 'del', text => $_, old_n => ++$i, new_n => '' } for @old_lines;
197195 my $j = 0;
198196 push @out, { op => 'add', text => $_, old_n => '', new_n => ++$j } for @new_lines;
199197 return @out;
200198 }
201199
202200 my @lcs; for my $i (0..$O) { $lcs[$i][0] = 0; }
203201 for my $j (0..$N) { $lcs[0][$j] = 0; }
204202 for my $i (1..$O) {
205203 for my $j (1..$N) {
206204 $lcs[$i][$j] = $old_lines[$i-1] eq $new_lines[$j-1]
207205 ? $lcs[$i-1][$j-1] + 1
208206 : ($lcs[$i-1][$j] >= $lcs[$i][$j-1] ? $lcs[$i-1][$j] : $lcs[$i][$j-1]);
209207 }
210208 }
211209
212210 my @out;
213211 my ($i, $j) = ($O, $N);
214212 while ($i > 0 || $j > 0) {
215213 if ($i > 0 && $j > 0 && $old_lines[$i-1] eq $new_lines[$j-1]) {
216214 unshift @out, { op => 'ctx', text => $old_lines[$i-1], old_n => $i, new_n => $j };
217215 $i--; $j--;
218216 } elsif ($j > 0 && ($i == 0 || $lcs[$i][$j-1] >= $lcs[$i-1][$j])) {
219217 unshift @out, { op => 'add', text => $new_lines[$j-1], old_n => '', new_n => $j };
220218 $j--;
221219 } else {
222220 unshift @out, { op => 'del', text => $old_lines[$i-1], old_n => $i, new_n => '' };
223221 $i--;
224222 }
225223 }
226224 return @out;
227225}
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.