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/MODS/PageWrapper.pm |
| Site | DriftSense self-monitor on local |
| Kind | local |
| Captured at | 2026-07-10 23:23:35 |
| Captured SHA | 3a22da864a4f7cf7c2551df76247c8d0c42f8134b49d90a88f1d1589923d3ba6 |
Current state on disk
Live check just now. If SHAs match, the restore is a no-op.
| File present | yes |
| Size | 8026 bytes |
| Current SHA | 65c126296f9c |
| 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, -125 deletions, 98 unchanged context lines.
| 1 | 1 | package MODS::PageWrapper; |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # DriftSense -- page wrapper. |
| 4 | 4 | # |
| 5 | 5 | # Loads TEMPLATES/page_wrapper.html, injects the CGI's rendered body |
| 6 | 6 | # into $page_body, sets branding + active-nav-item tvars, and hands |
| 7 | 7 | # the whole thing back through MODS::Template so its normal tag |
| 8 | 8 | # resolution (variables, [if:...], [url:...]) runs. |
| 9 | 9 | # |
| 10 | 10 | # Usage from a CGI: |
| 11 | 11 | # my $load = MODS::PageWrapper->new; |
| 12 | 12 | # $load->wrapper( |
| 13 | 13 | # page_title => 'Dashboard', |
| 14 | 14 | # page_key => 'dashboard', # sets $active_dashboard = 1 for the sidebar |
| 15 | 15 | # body_html => $rendered_body, |
| 16 | 16 | # userinfo => $u, |
| 17 | 17 | # ); |
| 18 | 18 | #====================================================================== |
| 19 | 19 | use strict; |
| 20 | 20 | use warnings; |
| 21 | 21 | use MODS::Config; |
| 22 | use MODS::DBConnect; | |
| 23 | 22 | use MODS::Template; |
| 24 | 23 | |
| 25 | 24 | my $cfg = MODS::Config->new; |
| 26 | 25 | |
| 27 | 26 | # Cache-bust CSS/JS by the newest mtime among the assets we ship. Recomputed |
| 28 | 27 | # per-process; requests inside the same worker share the value, but a new |
| 29 | 28 | # deploy touches files and the next worker start picks it up automatically. |
| 30 | 29 | my $ASSET_VERSION; |
| 31 | 30 | sub _asset_version { |
| 32 | 31 | return $ASSET_VERSION if defined $ASSET_VERSION; |
| 33 | 32 | my $base = '/var/www/vhosts/3dshawn.com/site1'; |
| 34 | 33 | my $max = 0; |
| 35 | 34 | for my $p ("$base/assets/styles/drift_sense.css", |
| 36 | "$base/assets/styles/help_tips.css", | |
| 37 | 35 | "$base/assets/js/tz_localize.js", |
| 38 | "$base/assets/js/ds_charts.js", | |
| 39 | "$base/assets/js/ds_help.js") { | |
| 36 | "$base/assets/js/ds_charts.js") { | |
| 40 | 37 | my @st = stat $p; |
| 41 | 38 | $max = $st[9] if @st && $st[9] > $max; |
| 42 | 39 | } |
| 43 | 40 | $ASSET_VERSION = $max || time; |
| 44 | 41 | return $ASSET_VERSION; |
| 45 | 42 | } |
| 46 | 43 | |
| 47 | 44 | sub new { return bless({}, shift); } |
| 48 | 45 | |
| 49 | 46 | sub wrapper { |
| 50 | 47 | my ($self, %args) = @_; |
| 51 | 48 | my $tpl = MODS::Template->new; |
| 52 | 49 | |
| 53 | 50 | my $userinfo = $args{userinfo} || {}; |
| 54 | 51 | my $initials = _initials($userinfo->{display_name} || $userinfo->{email} || 'User'); |
| 55 | ||
| 56 | # Load the "monitored sites" list for the sidebar. Cheap query -- | |
| 57 | # runs once per page render. | |
| 58 | my $sites = _sidebar_sites(); | |
| 59 | ||
| 60 | # Load "stable releases" for the sidebar (Wave 5). | |
| 61 | my $stables = _sidebar_stables(); | |
| 62 | ||
| 63 | my $active_mid = $args{active_mid} // 0; | |
| 64 | foreach my $s (@$sites) { | |
| 65 | $s->{is_active} = ($active_mid && $s->{mid} == $active_mid) ? 1 : 0; | |
| 66 | } | |
| 67 | 52 | |
| 68 | 53 | my $tvars = { |
| 69 | 54 | # Branding |
| 70 | 55 | brand_name => $cfg->settings('brand_name') || 'DriftSense', |
| 71 | 56 | brand_tagline => $cfg->settings('brand_tagline') || '', |
| 72 | ||
| 73 | # Sidebar: monitored sites + stable releases | |
| 74 | sites => $sites, | |
| 75 | has_sites => scalar(@$sites) ? 1 : 0, | |
| 76 | sites_ct => scalar(@$sites), | |
| 77 | stable_releases=> $stables, | |
| 78 | has_stables => scalar(@$stables) ? 1 : 0, | |
| 79 | stables_ct => scalar(@$stables), | |
| 80 | 57 | |
| 81 | 58 | # Asset cache-bust stamp -- max mtime of shipped CSS/JS |
| 82 | 59 | asset_version => _asset_version(), |
| 83 | 60 | |
| 84 | 61 | # Page identity |
| 85 | 62 | page_title => $args{page_title} || 'DriftSense', |
| 86 | 63 | page_body => $args{body_html} // (@{$args{body_lines} || []} ? join('', @{$args{body_lines}}) : ''), |
| 87 | 64 | |
| 88 | 65 | # User |
| 89 | 66 | user_name => $userinfo->{display_name} || $userinfo->{email} || 'Operator', |
| 90 | 67 | user_initials => $initials, |
| 91 | 68 | |
| 92 | 69 | # Sidebar active-item flags (page_key sets exactly one) |
| 93 | 70 | active_dashboard => 0, |
| 94 | 71 | active_file_changes => 0, |
| 95 | 72 | active_schema_changes => 0, |
| 96 | 73 | active_table_locks => 0, |
| 97 | 74 | active_named_releases => 0, |
| 98 | active_alerts => 0, | |
| 99 | active_export => 0, | |
| 100 | active_purge_log => 0, | |
| 101 | active_sites => 0, | |
| 102 | 75 | active_databases => 0, |
| 103 | 76 | active_file_monitors => 0, |
| 104 | 77 | active_servers => 0, |
| 105 | 78 | active_containers => 0, |
| 106 | 79 | active_settings => 0, |
| 107 | 80 | }; |
| 108 | 81 | my $key = $args{page_key} || ''; |
| 109 | 82 | $tvars->{"active_$key"} = 1 if exists $tvars->{"active_$key"}; |
| 110 | 83 | |
| 111 | 84 | my @html = $tpl->template('page_wrapper.html', $tvars); |
| 112 | 85 | print @html; |
| 113 | 86 | return; |
| 114 | } | |
| 115 | ||
| 116 | #--------------------------------------------------------------------- | |
| 117 | # Sidebar sites list (cached per-worker; refreshes when process restarts) | |
| 118 | #--------------------------------------------------------------------- | |
| 119 | my @_SIDEBAR_SITES_CACHE; | |
| 120 | my $_SIDEBAR_CACHE_AT = 0; | |
| 121 | ||
| 122 | sub _sidebar_sites { | |
| 123 | my $now = time; | |
| 124 | return \@_SIDEBAR_SITES_CACHE if $_SIDEBAR_CACHE_AT && ($now - $_SIDEBAR_CACHE_AT) < 30; | |
| 125 | ||
| 126 | my @out; | |
| 127 | my $db = MODS::DBConnect->new; | |
| 128 | my $dbh = eval { $db->db_connect } or return \@out; | |
| 129 | my @rows = $db->db_readwrite_multiple($dbh, q~ | |
| 130 | SELECT m.file_monitor_list_id AS mid, | |
| 131 | m.scan_name, | |
| 132 | m.scan_path, | |
| 133 | s.server_name, | |
| 134 | s.kind AS server_kind, | |
| 135 | (SELECT COUNT(*) FROM FILE_CHANGES fc | |
| 136 | WHERE fc.file_monitor_list_id = m.file_monitor_list_id | |
| 137 | AND fc.date_time >= DATE_SUB(NOW(), INTERVAL 24 HOUR)) AS changes_24h | |
| 138 | FROM FILE_MONITOR_SETTINGS m | |
| 139 | LEFT JOIN SERVERS s ON s.server_id = m.server_id | |
| 140 | WHERE m.status = 1 | |
| 141 | ORDER BY (s.kind='ssh_agent') DESC, s.server_id ASC, m.file_monitor_list_id ASC | |
| 142 | ~, __FILE__, __LINE__); | |
| 143 | $db->db_disconnect($dbh); | |
| 144 | ||
| 145 | foreach my $r (@rows) { | |
| 146 | my $label = $r->{scan_name} // "monitor #$r->{mid}"; | |
| 147 | # Shorten if too long | |
| 148 | $r->{label} = length($label) > 26 ? substr($label, 0, 24) . '..' : $label; | |
| 149 | $r->{initials} = _site_initials($label); | |
| 150 | $r->{changes_24h} //= 0; | |
| 151 | $r->{activity_dot} = $r->{changes_24h} > 50 ? 'hot' | |
| 152 | : $r->{changes_24h} > 5 ? 'warm' | |
| 153 | : $r->{changes_24h} > 0 ? 'live' : 'quiet'; | |
| 154 | $r->{site_url} = "/site.cgi?mid=$r->{mid}"; | |
| 155 | push @out, $r; | |
| 156 | } | |
| 157 | @_SIDEBAR_SITES_CACHE = @out; | |
| 158 | $_SIDEBAR_CACHE_AT = $now; | |
| 159 | return \@out; | |
| 160 | } | |
| 161 | ||
| 162 | my @_SIDEBAR_STABLES_CACHE; | |
| 163 | my $_STABLES_CACHE_AT = 0; | |
| 164 | sub _sidebar_stables { | |
| 165 | my $now = time; | |
| 166 | return \@_SIDEBAR_STABLES_CACHE if $_STABLES_CACHE_AT && ($now - $_STABLES_CACHE_AT) < 30; | |
| 167 | my @out; | |
| 168 | my $db = MODS::DBConnect->new; | |
| 169 | my $dbh = eval { $db->db_connect } or return \@out; | |
| 170 | # NOTE: `tier` column added in wave 5; guard with a probe so this | |
| 171 | # code works on pre-wave5 schemas gracefully. | |
| 172 | my $has_tier = $db->db_readwrite($dbh, q~ | |
| 173 | SELECT 1 AS ok FROM INFORMATION_SCHEMA.COLUMNS | |
| 174 | WHERE TABLE_SCHEMA = DATABASE() | |
| 175 | AND TABLE_NAME = 'NAMED_RELEASES' AND COLUMN_NAME = 'tier' | |
| 176 | ~, __FILE__, __LINE__); | |
| 177 | if ($has_tier && $has_tier->{ok}) { | |
| 178 | my @rows = $db->db_readwrite_multiple($dbh, q~ | |
| 179 | SELECT named_release_id AS id, name, version_label, tier | |
| 180 | FROM NAMED_RELEASES | |
| 181 | WHERE tier = 'stable' | |
| 182 | ORDER BY named_release_id DESC | |
| 183 | LIMIT 8 | |
| 184 | ~, __FILE__, __LINE__); | |
| 185 | foreach my $r (@rows) { | |
| 186 | $r->{label} = $r->{version_label} || $r->{name}; | |
| 187 | $r->{label} = length($r->{label}) > 26 ? substr($r->{label}, 0, 24) . '..' : $r->{label}; | |
| 188 | $r->{restore_url} = "/restore_release.cgi?release_id=$r->{id}"; | |
| 189 | push @out, $r; | |
| 190 | } | |
| 191 | } | |
| 192 | $db->db_disconnect($dbh); | |
| 193 | @_SIDEBAR_STABLES_CACHE = @out; | |
| 194 | $_STABLES_CACHE_AT = $now; | |
| 195 | return \@out; | |
| 196 | } | |
| 197 | ||
| 198 | sub _site_initials { | |
| 199 | my $s = shift; $s //= ''; | |
| 200 | # Strip parenthetical parts like "(webstls.com)" | |
| 201 | my $core = $s; | |
| 202 | $core =~ s/\s*\(.*$//; | |
| 203 | my @tok = grep { length } split /[\s_\-\.\/]+/, $core; | |
| 204 | my $i = ''; | |
| 205 | if (@tok >= 2) { | |
| 206 | $i = uc(substr($tok[0], 0, 1) . substr($tok[1], 0, 1)); | |
| 207 | } elsif (@tok) { | |
| 208 | $i = uc(substr($tok[0], 0, 2)); | |
| 209 | } | |
| 210 | return length($i) ? $i : '??'; | |
| 211 | 87 | } |
| 212 | 88 | |
| 213 | 89 | sub _initials { |
| 214 | 90 | my $s = shift; $s //= ''; |
| 215 | 91 | my @p = split /[\s\@\.]+/, $s, 3; |
| 216 | 92 | my $i = ''; |
| 217 | 93 | $i .= uc(substr($p[0], 0, 1)) if defined $p[0] && length $p[0]; |
| 218 | 94 | $i .= uc(substr($p[1], 0, 1)) if defined $p[1] && length $p[1]; |
| 219 | 95 | $i = uc(substr($s, 0, 1)) unless length $i; |
| 220 | 96 | return $i; |
| 221 | 97 | } |
| 222 | 98 | |
| 223 | 99 | 1; |
Confirm restore
Backs current content to <target>.drift_restore_backup_<epoch>, writes the captured content, verifies SHA post-write.