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/named_releases.cgi |
| Site | DriftSense self-monitor on local |
| Kind | local |
| Captured at | 2026-07-11 23:34:28 |
| Captured SHA | f9cb5afdbf95f9dc879e727de593e884ebdf87a29abe4e72c42970566a3b63e1 |
Current state on disk
Live check just now. If SHAs match, the restore is a no-op.
| File present | yes |
| Size | 8588 bytes |
| Current SHA | 3b5abff71074 |
| 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. +4 additions, -93 deletions, 103 unchanged context lines.
| 1 | 1 | #!/usr/bin/perl |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # DriftSense -- /named_releases.cgi |
| 4 | 4 | # |
| 5 | 5 | # List + create named releases. Each release is a bookmark that pins |
| 6 | 6 | # every blob referenced by FILE_CHANGES rows in a chosen scope. A |
| 7 | 7 | # release can be portfolio-wide (scope_monitor_id NULL) or scoped to |
| 8 | 8 | # a single site (scope_monitor_id set). Pinned blobs are exempt from |
| 9 | 9 | # auto-purge. |
| 10 | 10 | #====================================================================== |
| 11 | 11 | use strict; |
| 12 | 12 | use warnings; |
| 13 | 13 | use CGI (); |
| 14 | 14 | use MODS::Config; use MODS::DBConnect; use MODS::Template; use MODS::PageWrapper; |
| 15 | use MODS::Webhook; | |
| 16 | use JSON::PP (); | |
| 17 | 15 | my $cgi = CGI->new; my $db = MODS::DBConnect->new; my $tpl = MODS::Template->new; |
| 18 | 16 | $|=1; |
| 19 | 17 | |
| 20 | 18 | my $dbh = $db->db_connect or die "DB connect failed\n"; |
| 21 | 19 | |
| 22 | 20 | # ---- POST: create --------------------------------------------------- |
| 23 | 21 | if (($ENV{REQUEST_METHOD} || '') eq 'POST' && $cgi->param('save')) { |
| 24 | 22 | my $name = $cgi->param('name') || ''; |
| 25 | 23 | my $desc = $cgi->param('description') || ''; |
| 26 | 24 | my $by = $cgi->param('released_by') || 'Operator'; |
| 27 | 25 | my $scope_mid = int($cgi->param('scope_monitor_id') || 0); |
| 28 | 26 | my $scope_sid = int($cgi->param('scope_server_id') || 0); |
| 29 | my $dkind = $cgi->param('alert_delivery_kind') || 'none'; | |
| 30 | $dkind = 'none' unless $dkind =~ /^(none|generic_webhook|slack|discord)$/; | |
| 31 | my $durl = $cgi->param('alert_delivery_url') || ''; | |
| 32 | my $tier = $cgi->param('tier') || 'draft'; | |
| 33 | $tier = 'draft' unless $tier =~ /^(draft|working|stable)$/; | |
| 34 | my $vlabel = $cgi->param('version_label') || ''; | |
| 35 | 27 | |
| 36 | 28 | if (length $name) { |
| 37 | 29 | my $q_name = $dbh->quote($name); |
| 38 | 30 | my $q_desc = $dbh->quote($desc); |
| 39 | 31 | my $q_by = $dbh->quote($by); |
| 40 | my $q_dk = $dbh->quote($dkind); | |
| 41 | my $q_du = $dbh->quote($durl); | |
| 42 | my $q_tier = $dbh->quote($tier); | |
| 43 | my $q_vl = $dbh->quote($vlabel); | |
| 44 | 32 | my $mid_expr = $scope_mid ? $scope_mid : 'NULL'; |
| 45 | 33 | my $sid_expr = $scope_sid ? $scope_sid : 'NULL'; |
| 46 | 34 | $db->db_readwrite($dbh, qq~ |
| 47 | 35 | INSERT INTO NAMED_RELEASES |
| 48 | 36 | (name, description, released_at, released_by, is_locked, created_at, |
| 49 | scope_monitor_id, scope_server_id, | |
| 50 | alert_delivery_kind, alert_delivery_url, | |
| 51 | tier, version_label) | |
| 37 | scope_monitor_id, scope_server_id) | |
| 52 | 38 | VALUES |
| 53 | 39 | ($q_name, $q_desc, NOW(), $q_by, 1, NOW(), |
| 54 | $mid_expr, $sid_expr, | |
| 55 | $q_dk, $q_du, | |
| 56 | $q_tier, $q_vl) | |
| 40 | $mid_expr, $sid_expr) | |
| 57 | 41 | ~, __FILE__, __LINE__); |
| 58 | # Extra scopes for cross-scope releases (Wave 5 F5): multi-select | |
| 59 | # scope_monitor_ids[] additive | |
| 60 | my $new_rel = $db->db_readwrite($dbh, "SELECT LAST_INSERT_ID() AS id", __FILE__, __LINE__); | |
| 61 | my $rid = $new_rel && $new_rel->{id}; | |
| 62 | if ($rid) { | |
| 63 | foreach my $extra ($cgi->param('extra_monitor_ids')) { | |
| 64 | my $eid = int($extra || 0); | |
| 65 | next unless $eid && $eid != $scope_mid; | |
| 66 | $db->db_readwrite($dbh, qq~ | |
| 67 | INSERT INTO NAMED_RELEASE_SCOPES | |
| 68 | (named_release_id, file_monitor_list_id) | |
| 69 | VALUES ($rid, $eid) | |
| 70 | ~, __FILE__, __LINE__); | |
| 71 | } | |
| 72 | } | |
| 73 | } | |
| 74 | print "Status: 302 Found\nLocation: /named_releases.cgi\n\n"; exit; | |
| 75 | } | |
| 76 | ||
| 77 | # ---- AJAX: test alert delivery ------------------------------------ | |
| 78 | if ($cgi->param('op') && $cgi->param('op') eq 'test_alert') { | |
| 79 | my $rid = int($cgi->param('release_id') || 0); | |
| 80 | print "Content-Type: application/json\nCache-Control: no-cache\n\n"; | |
| 81 | my $r = $db->db_readwrite($dbh, qq~ | |
| 82 | SELECT name, alert_delivery_kind, alert_delivery_url | |
| 83 | FROM NAMED_RELEASES WHERE named_release_id = $rid LIMIT 1 | |
| 84 | ~, __FILE__, __LINE__); | |
| 85 | unless ($r && $r->{alert_delivery_url}) { | |
| 86 | print JSON::PP::encode_json({ ok => \0, message => 'no delivery URL configured' }); | |
| 87 | exit; | |
| 88 | 42 | } |
| 89 | my $summary = "TEST alert from DriftSense -- release '$r->{name}' -- ignore me"; | |
| 90 | my $body = ($r->{alert_delivery_kind} eq 'slack') ? { | |
| 91 | text => "*DriftSense TEST*: $summary", | |
| 92 | attachments => [{ color => '#f59e0b', text => 'This is a synthetic drift payload. No files actually drifted.' }], | |
| 93 | } : ($r->{alert_delivery_kind} eq 'discord') ? { | |
| 94 | content => "**DriftSense TEST**: $summary", | |
| 95 | } : { | |
| 96 | source => 'drift_sense', alert_type => 'test', | |
| 97 | release_name => $r->{name}, summary => $summary, | |
| 98 | }; | |
| 99 | my ($code, $rbody, $err) = MODS::Webhook::post_json( | |
| 100 | $r->{alert_delivery_url}, $body, | |
| 101 | timeout => 8, | |
| 102 | user_agent => 'DriftSense/1.0 (test-alert)', | |
| 103 | ); | |
| 104 | my $ok = (!$err && $code >= 200 && $code < 400) ? \1 : \0; | |
| 105 | print JSON::PP::encode_json({ | |
| 106 | ok => $ok, http_code => $code, message => $err ? $err : "HTTP $code", | |
| 107 | }); | |
| 108 | exit; | |
| 109 | } | |
| 110 | ||
| 111 | # ---- POST: promote / demote tier ----------------------------------- | |
| 112 | if (($ENV{REQUEST_METHOD} || '') eq 'POST' && $cgi->param('promote')) { | |
| 113 | my $rid = int($cgi->param('release_id') || 0); | |
| 114 | my $tier = $cgi->param('to_tier') || 'stable'; | |
| 115 | $tier = 'draft' unless $tier =~ /^(draft|working|stable)$/; | |
| 116 | my $q_tier = $dbh->quote($tier); | |
| 117 | $db->db_readwrite($dbh, | |
| 118 | "UPDATE NAMED_RELEASES SET tier = $q_tier WHERE named_release_id = $rid", | |
| 119 | __FILE__, __LINE__); | |
| 120 | 43 | print "Status: 302 Found\nLocation: /named_releases.cgi\n\n"; exit; |
| 121 | 44 | } |
| 122 | 45 | |
| 123 | 46 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; |
| 124 | 47 | |
| 125 | 48 | # ---- Existing releases + count how many blobs each pins ------------- |
| 126 | 49 | my @rows = $db->db_readwrite_multiple($dbh, q~ |
| 127 | SELECT nr.named_release_id AS id, nr.name, nr.description, nr.tier, nr.version_label, | |
| 50 | SELECT nr.named_release_id AS id, nr.name, nr.description, | |
| 128 | 51 | DATE_FORMAT(nr.released_at, '%b %d, %Y %H:%i') AS released_h, |
| 129 | 52 | UNIX_TIMESTAMP(nr.released_at) AS released_epoch, |
| 130 | 53 | nr.released_by, nr.is_locked, |
| 131 | 54 | nr.scope_monitor_id, nr.scope_server_id, |
| 132 | 55 | m.scan_name, s.server_name |
| 133 | 56 | FROM NAMED_RELEASES nr |
| 134 | 57 | LEFT JOIN FILE_MONITOR_SETTINGS m ON m.file_monitor_list_id = nr.scope_monitor_id |
| 135 | 58 | LEFT JOIN SERVERS s ON s.server_id = nr.scope_server_id |
| 136 | ORDER BY (nr.tier = 'stable') DESC, | |
| 137 | (nr.tier = 'working') DESC, | |
| 138 | nr.released_at DESC | |
| 59 | ORDER BY nr.released_at DESC | |
| 139 | 60 | LIMIT 100 |
| 140 | 61 | ~, __FILE__, __LINE__); |
| 141 | 62 | foreach my $r (@rows) { |
| 142 | 63 | $r->{lock_pill} = $r->{is_locked} ? 'pill-ok' : 'pill-mute'; |
| 143 | 64 | $r->{lock_lbl} = $r->{is_locked} ? 'PROTECTED' : 'UNLOCKED'; |
| 144 | 65 | $r->{released_epoch} //= 0; |
| 145 | $r->{tier} //= 'draft'; | |
| 146 | $r->{tier_pill} = $r->{tier} eq 'stable' ? 'pill-ok' | |
| 147 | : $r->{tier} eq 'working' ? 'pill-info' | |
| 148 | : 'pill-mute'; | |
| 149 | $r->{tier_lbl} = uc($r->{tier}); | |
| 150 | $r->{display_label} = $r->{version_label} || $r->{name}; | |
| 151 | $r->{is_stable} = $r->{tier} eq 'stable' ? 1 : 0; | |
| 152 | $r->{is_draft} = $r->{tier} eq 'draft' ? 1 : 0; | |
| 153 | $r->{is_working} = $r->{tier} eq 'working'? 1 : 0; | |
| 154 | $r->{restore_url} = "/restore_release.cgi?release_id=$r->{id}"; | |
| 155 | 66 | if ($r->{scope_monitor_id}) { |
| 156 | 67 | $r->{scope_pill} = 'pill-info'; |
| 157 | 68 | $r->{scope_lbl} = 'per-site'; |
| 158 | 69 | $r->{scope_h} = $r->{scan_name} || "monitor #$r->{scope_monitor_id}"; |
| 159 | 70 | } elsif ($r->{scope_server_id}) { |
| 160 | 71 | $r->{scope_pill} = 'pill-info'; |
| 161 | 72 | $r->{scope_lbl} = 'per-server'; |
| 162 | 73 | $r->{scope_h} = $r->{server_name} || "server #$r->{scope_server_id}"; |
| 163 | 74 | } else { |
| 164 | 75 | $r->{scope_pill} = 'pill-mute'; |
| 165 | 76 | $r->{scope_lbl} = 'portfolio'; |
| 166 | 77 | $r->{scope_h} = 'All monitored sites'; |
| 167 | 78 | } |
| 168 | 79 | } |
| 169 | 80 | |
| 170 | 81 | # ---- All monitors + servers for scope picker ------------------------ |
| 171 | 82 | my @monitors = $db->db_readwrite_multiple($dbh, q~ |
| 172 | 83 | SELECT m.file_monitor_list_id AS mid, m.scan_name, s.server_name |
| 173 | 84 | FROM FILE_MONITOR_SETTINGS m |
| 174 | 85 | LEFT JOIN SERVERS s ON s.server_id = m.server_id |
| 175 | 86 | WHERE m.status = 1 |
| 176 | 87 | ORDER BY s.server_id ASC, m.file_monitor_list_id ASC |
| 177 | 88 | ~, __FILE__, __LINE__); |
| 178 | 89 | foreach my $m (@monitors) { |
| 179 | 90 | $m->{label} = ($m->{scan_name} // '?') . ' (on ' . ($m->{server_name} // '?') . ')'; |
| 180 | 91 | } |
| 181 | 92 | |
| 182 | 93 | my $total = scalar @rows; |
| 183 | 94 | $db->db_disconnect($dbh); |
| 184 | 95 | |
| 185 | 96 | my $tvars = { |
| 186 | 97 | rows => \@rows, |
| 187 | 98 | has_rows => $total ? 1 : 0, |
| 188 | 99 | total => $total, |
| 189 | 100 | monitors => \@monitors, |
| 190 | 101 | has_monitors=> scalar(@monitors) ? 1 : 0, |
| 191 | 102 | }; |
| 192 | 103 | my @body = $tpl->template('named_releases.html', $tvars); |
| 193 | 104 | MODS::PageWrapper->new->wrapper( |
| 194 | 105 | page_title => 'Named releases', page_key => 'named_releases', |
| 195 | 106 | body_html => join('', @body), userinfo => { display_name => 'Operator' }, |
| 196 | 107 | ); |
Confirm restore
Backs current content to <target>.drift_restore_backup_<epoch>, writes the captured content, verifies SHA post-write.