Back up now from the admin page while the server runs
Browse files at this commit102b9afe1dd7b0861b27482a2440762986d9d776Parent: 7a822e7- feat
- back up now from the admin page while the server runs
7 changed files +206 -12
- README.md +4 -0
- crates/in-web/src/backup.rs +58 -2
- crates/in-web/src/boot.rs +3 -1
- crates/in-web/src/i18n/admin.rs +45 -2
- crates/in-web/src/pages/admin.rs +90 -5
- crates/in-web/src/state.rs +3 -0
- docs/architecture.md +3 -2
README.mdmodified+4 -0
| @@ -99,6 +99,10 @@ directory, which you add to `ReadWritePaths`. | |||
| 99 | With `[backup]` set, `in serve` writes a backup set every `interval_hours`, keeps the newest `keep` sets, and | 99 | With `[backup]` set, `in serve` writes a backup set every `interval_hours`, keeps the newest `keep` sets, and |
| 100 | pings `healthcheck_url` after each good run (a failed ping is logged and nothing else). | 100 | pings `healthcheck_url` after each good run (a failed ping is logged and nothing else). |
| 101 | 101 | ||
| 102 | +While the server runs, the *Backups* section of `/admin` has a *Back up now* button that starts a run at once. It is | ||
| 103 | +for administrators only and needs the form's CSRF token. At most one run is pending or running: a press made meanwhile | ||
| 104 | +starts nothing. The press is written to the audit log (`backup.requested`). Without `[backup]` the button is refused. | ||
| 105 | + | ||
| 102 | A set is a directory `set-<time>` in `dir`, holding: | 106 | A set is a directory `set-<time>` in `dir`, holding: |
| 103 | 107 | ||
| 104 | - `in.db`: a consistent snapshot of the database (`VACUUM INTO`, checkpointed to one file). | 108 | - `in.db`: a consistent snapshot of the database (`VACUUM INTO`, checkpointed to one file). |
crates/in-web/src/backup.rsmodified+58 -2
| @@ -1,22 +1,58 @@ | |||
| 1 | //! The scheduled backup worker: writes a backup set (snapshot, blob manifest and its MAC, see `in-core` `backup.rs`) | 1 | //! The scheduled backup worker: writes a backup set (snapshot, blob manifest and its MAC, see `in-core` `backup.rs`) |
| 2 | //! every `interval_hours`, keeps the newest `keep` sets, and pings the dead-man URL after each good run. | 2 | //! every `interval_hours`, keeps the newest `keep` sets, and pings the dead-man URL after each good run. |
| 3 | //! | 3 | //! |
| 4 | +//! The admin page can ask for a run now through [`BackupTrigger`]. At most one run is pending or running: a request | ||
| 5 | +//! made meanwhile starts nothing, and a scheduled run that is already going counts as running too. | ||
| 6 | +//! | ||
| 4 | //! A failed run is logged and the next one is tried at the next interval; a failed ping is logged and nothing | 7 | //! A failed run is logged and the next one is tried at the next interval; a failed ping is logged and nothing |
| 5 | //! else. The ping URL carries a secret, so it is never logged. The worker is not started without a `[backup]` | 8 | //! else. The ping URL carries a secret, so it is never logged. The worker is not started without a `[backup]` |
| 6 | //! table. | 9 | //! table. |
| 7 | 10 | ||
| 11 | +use std::sync::atomic::{AtomicBool, Ordering}; | ||
| 8 | use std::time::Duration; | 12 | use std::time::Duration; |
| 9 | 13 | ||
| 10 | use in_core::backup; | 14 | use in_core::backup; |
| 11 | use in_core::config::Backup; | 15 | use in_core::config::Backup; |
| 12 | use in_core::time::now_stamp; | 16 | use in_core::time::now_stamp; |
| 13 | -use tokio::sync::watch; | 17 | +use tokio::sync::{Notify, watch}; |
| 14 | 18 | ||
| 15 | use crate::state::AppState; | 19 | use crate::state::AppState; |
| 16 | 20 | ||
| 17 | /// How long the dead-man ping may take. | 21 | /// How long the dead-man ping may take. |
| 18 | pub const PING_TIMEOUT: Duration = Duration::from_secs(10); | 22 | pub const PING_TIMEOUT: Duration = Duration::from_secs(10); |
| 19 | 23 | ||
| 24 | +/// Asks the backup worker for a run now. At most one run is pending or running: a request made while one is | ||
| 25 | +/// pending or running starts nothing. | ||
| 26 | +#[derive(Default)] | ||
| 27 | +pub struct BackupTrigger { | ||
| 28 | + /// True from a request, or from the start of a run, until that run ends. | ||
| 29 | + busy: AtomicBool, | ||
| 30 | + /// Wakes the worker out of its wait when a request arrives. | ||
| 31 | + wake: Notify, | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +impl BackupTrigger { | ||
| 35 | + /// Asks for a run now. Returns true when this request starts one, false when one is already pending or | ||
| 36 | + /// running. | ||
| 37 | + pub fn request(&self) -> bool { | ||
| 38 | + if self.busy.swap(true, Ordering::AcqRel) { | ||
| 39 | + return false; | ||
| 40 | + } | ||
| 41 | + self.wake.notify_one(); | ||
| 42 | + true | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + /// Marks a run as running, so requests made meanwhile start nothing. | ||
| 46 | + fn begin(&self) { | ||
| 47 | + self.busy.store(true, Ordering::Release); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + /// Marks the run as ended. A request made after this starts a new run. | ||
| 51 | + fn finish(&self) { | ||
| 52 | + self.busy.store(false, Ordering::Release); | ||
| 53 | + } | ||
| 54 | +} | ||
| 55 | + | ||
| 20 | /// Runs one backup now and returns the new set's directory. | 56 | /// Runs one backup now and returns the new set's directory. |
| 21 | async fn run_once(state: &AppState, config: &Backup) -> in_core::error::Result<std::path::PathBuf> { | 57 | async fn run_once(state: &AppState, config: &Backup) -> in_core::error::Result<std::path::PathBuf> { |
| 22 | // The stamp names the set; `:` and `.` are not wanted in file names. | 58 | // The stamp names the set; `:` and `.` are not wanted in file names. |
| @@ -52,13 +88,15 @@ async fn ping(url: &str) { | |||
| 52 | } | 88 | } |
| 53 | } | 89 | } |
| 54 | 90 | ||
| 55 | -/// Runs backups every interval until the stop flag is set. The first run happens at start-up. | 91 | +/// Runs backups until the stop flag is set: one at start-up, then one per interval, and one at once whenever |
| 92 | +/// the admin page asks for it. | ||
| 56 | pub async fn run(state: AppState, mut stop: watch::Receiver<bool>) { | 93 | pub async fn run(state: AppState, mut stop: watch::Receiver<bool>) { |
| 57 | let Some(config) = state.config.backup.clone() else { | 94 | let Some(config) = state.config.backup.clone() else { |
| 58 | return; | 95 | return; |
| 59 | }; | 96 | }; |
| 60 | let every = Duration::from_secs(u64::from(config.interval_hours) * 60 * 60); | 97 | let every = Duration::from_secs(u64::from(config.interval_hours) * 60 * 60); |
| 61 | loop { | 98 | loop { |
| 99 | + state.backup.begin(); | ||
| 62 | match run_once(&state, &config).await { | 100 | match run_once(&state, &config).await { |
| 63 | Ok(_) => { | 101 | Ok(_) => { |
| 64 | if let Some(url) = &config.healthcheck_url { | 102 | if let Some(url) = &config.healthcheck_url { |
| @@ -67,9 +105,27 @@ pub async fn run(state: AppState, mut stop: watch::Receiver<bool>) { | |||
| 67 | } | 105 | } |
| 68 | Err(error) => tracing::error!(error = %error, "backup: the run failed"), | 106 | Err(error) => tracing::error!(error = %error, "backup: the run failed"), |
| 69 | } | 107 | } |
| 108 | + state.backup.finish(); | ||
| 70 | tokio::select! { | 109 | tokio::select! { |
| 71 | _ = stop.wait_for(|stopped| *stopped) => return, | 110 | _ = stop.wait_for(|stopped| *stopped) => return, |
| 72 | () = tokio::time::sleep(every) => {} | 111 | () = tokio::time::sleep(every) => {} |
| 112 | + () = state.backup.wake.notified() => {} | ||
| 73 | } | 113 | } |
| 74 | } | 114 | } |
| 75 | } | 115 | } |
| 116 | + | ||
| 117 | +#[cfg(test)] | ||
| 118 | +mod tests { | ||
| 119 | + use super::BackupTrigger; | ||
| 120 | + | ||
| 121 | + #[test] | ||
| 122 | + fn a_second_request_while_one_is_pending_starts_nothing() { | ||
| 123 | + let trigger = BackupTrigger::default(); | ||
| 124 | + assert!(trigger.request()); | ||
| 125 | + assert!(!trigger.request()); | ||
| 126 | + trigger.begin(); | ||
| 127 | + assert!(!trigger.request()); | ||
| 128 | + trigger.finish(); | ||
| 129 | + assert!(trigger.request()); | ||
| 130 | + } | ||
| 131 | +} | ||
crates/in-web/src/boot.rsmodified+3 -1
| @@ -23,7 +23,7 @@ use tokio::sync::watch; | |||
| 23 | use topcoat::cookie::RouterBuilderCookieExt; | 23 | use topcoat::cookie::RouterBuilderCookieExt; |
| 24 | use topcoat::router::{BodyLimit, Router, RouterBuilderDiscoverExt, RouterService, TrustedProxies}; | 24 | use topcoat::router::{BodyLimit, Router, RouterBuilderDiscoverExt, RouterService, TrustedProxies}; |
| 25 | 25 | ||
| 26 | -use crate::backup; | 26 | +use crate::backup::{self, BackupTrigger}; |
| 27 | use crate::directory; | 27 | use crate::directory; |
| 28 | use crate::family; | 28 | use crate::family; |
| 29 | use crate::family::FamilyCache; | 29 | use crate::family::FamilyCache; |
| @@ -179,6 +179,7 @@ async fn open_state(config: Config) -> Result<AppState> { | |||
| 179 | blobs, | 179 | blobs, |
| 180 | key: Arc::new(key), | 180 | key: Arc::new(key), |
| 181 | live: Arc::new(LiveHub::default()), | 181 | live: Arc::new(LiveHub::default()), |
| 182 | + backup: Arc::new(BackupTrigger::default()), | ||
| 182 | oidc, | 183 | oidc, |
| 183 | throttles: Arc::new(ShareThrottles::default()), | 184 | throttles: Arc::new(ShareThrottles::default()), |
| 184 | family: Arc::new(FamilyCache::default()), | 185 | family: Arc::new(FamilyCache::default()), |
| @@ -312,6 +313,7 @@ mod tests { | |||
| 312 | blobs, | 313 | blobs, |
| 313 | key: Arc::new(key), | 314 | key: Arc::new(key), |
| 314 | live: Arc::new(LiveHub::default()), | 315 | live: Arc::new(LiveHub::default()), |
| 316 | + backup: Arc::new(BackupTrigger::default()), | ||
| 315 | oidc: None, | 317 | oidc: None, |
| 316 | throttles: Arc::new(ShareThrottles::default()), | 318 | throttles: Arc::new(ShareThrottles::default()), |
| 317 | family: Arc::new(FamilyCache::default()), | 319 | family: Arc::new(FamilyCache::default()), |
crates/in-web/src/i18n/admin.rsmodified+45 -2
| @@ -136,6 +136,18 @@ pub enum AK { | |||
| 136 | RefusalRange, | 136 | RefusalRange, |
| 137 | /// Refusal of anything else. | 137 | /// Refusal of anything else. |
| 138 | RefusalGeneric, | 138 | RefusalGeneric, |
| 139 | + /// Section: backups. | ||
| 140 | + Backups, | ||
| 141 | + /// Label: the backup directory. | ||
| 142 | + BackupDir, | ||
| 143 | + /// Button: start a backup now. | ||
| 144 | + BackupNow, | ||
| 145 | + /// Shown when backups are off in the configuration. | ||
| 146 | + BackupOff, | ||
| 147 | + /// Notice after a backup was asked for. | ||
| 148 | + NoticeBackup, | ||
| 149 | + /// Refusal: backups are off, so there is nothing to start. | ||
| 150 | + RefusalBackupOff, | ||
| 139 | } | 151 | } |
| 140 | 152 | ||
| 141 | /// The text of `key` in `lang`. | 153 | /// The text of `key` in `lang`. |
| @@ -273,14 +285,44 @@ pub fn a(lang: Lang, key: AK) -> &'static str { | |||
| 273 | (AK::RefusalRange, Lang::Tr) => "Bu değer izin verilen aralığın dışında.", | 285 | (AK::RefusalRange, Lang::Tr) => "Bu değer izin verilen aralığın dışında.", |
| 274 | (AK::RefusalGeneric, Lang::En) => "Something went wrong. Try again.", | 286 | (AK::RefusalGeneric, Lang::En) => "Something went wrong. Try again.", |
| 275 | (AK::RefusalGeneric, Lang::Tr) => "Bir sorun oluştu. Yeniden deneyin.", | 287 | (AK::RefusalGeneric, Lang::Tr) => "Bir sorun oluştu. Yeniden deneyin.", |
| 288 | + (AK::Backups, Lang::En) => "Backups", | ||
| 289 | + (AK::Backups, Lang::Tr) => "Yedeklemeler", | ||
| 290 | + (AK::BackupDir, Lang::En) => "Directory", | ||
| 291 | + (AK::BackupDir, Lang::Tr) => "Dizin", | ||
| 292 | + (AK::BackupNow, Lang::En) => "Back up now", | ||
| 293 | + (AK::BackupNow, Lang::Tr) => "Şimdi yedekle", | ||
| 294 | + (AK::BackupOff, Lang::En) => { | ||
| 295 | + "Backups are off. Set [backup] dir in the configuration file to turn them on." | ||
| 296 | + } | ||
| 297 | + (AK::BackupOff, Lang::Tr) => { | ||
| 298 | + "Yedeklemeler kapalı. Açmak için yapılandırma dosyasında [backup] dir ayarlayın." | ||
| 299 | + } | ||
| 300 | + (AK::NoticeBackup, Lang::En) => "Backup started. It runs in the background.", | ||
| 301 | + (AK::NoticeBackup, Lang::Tr) => "Yedekleme başlatıldı. Arka planda çalışır.", | ||
| 302 | + (AK::RefusalBackupOff, Lang::En) => { | ||
| 303 | + "Backups are off, so there is nothing to start. Set [backup] dir first." | ||
| 304 | + } | ||
| 305 | + (AK::RefusalBackupOff, Lang::Tr) => { | ||
| 306 | + "Yedeklemeler kapalı, başlatılacak bir şey yok. Önce [backup] dir ayarlayın." | ||
| 307 | + } | ||
| 308 | + } | ||
| 309 | +} | ||
| 310 | + | ||
| 311 | +/// The schedule of the backups: how often a run happens and how many sets are kept. | ||
| 312 | +pub fn backup_schedule(lang: Lang, hours: u32, keep: u32) -> String { | ||
| 313 | + match lang { | ||
| 314 | + Lang::En => format!("every {hours} hours, keeping the newest {keep} sets"), | ||
| 315 | + Lang::Tr => format!("{hours} saatte bir, en yeni {keep} küme tutularak"), | ||
| 276 | } | 316 | } |
| 277 | } | 317 | } |
| 278 | 318 | ||
| 279 | -/// The sentence for a refusal word. Range refusals (any word ending `_range`) get their own text; anything | 319 | +/// The sentence for a refusal word. Range refusals (any word ending `_range`) get their own text; backups |
| 280 | -/// else gets the generic one. | 320 | +/// that are off get theirs; anything else gets the generic one. |
| 281 | pub fn refusal(lang: Lang, word: &str) -> &'static str { | 321 | pub fn refusal(lang: Lang, word: &str) -> &'static str { |
| 282 | if word.ends_with("_range") || word == "quota_negative" { | 322 | if word.ends_with("_range") || word == "quota_negative" { |
| 283 | a(lang, AK::RefusalRange) | 323 | a(lang, AK::RefusalRange) |
| 324 | + } else if word == "backup_off" { | ||
| 325 | + a(lang, AK::RefusalBackupOff) | ||
| 284 | } else { | 326 | } else { |
| 285 | a(lang, AK::RefusalGeneric) | 327 | a(lang, AK::RefusalGeneric) |
| 286 | } | 328 | } |
| @@ -292,6 +334,7 @@ pub fn notice(lang: Lang, word: &str) -> Option<&'static str> { | |||
| 292 | "quota_saved" => Some(a(lang, AK::NoticeQuota)), | 334 | "quota_saved" => Some(a(lang, AK::NoticeQuota)), |
| 293 | "service_quota_saved" => Some(a(lang, AK::NoticeServiceQuota)), | 335 | "service_quota_saved" => Some(a(lang, AK::NoticeServiceQuota)), |
| 294 | "defaults_saved" => Some(a(lang, AK::NoticeDefaults)), | 336 | "defaults_saved" => Some(a(lang, AK::NoticeDefaults)), |
| 337 | + "backup_started" => Some(a(lang, AK::NoticeBackup)), | ||
| 295 | _ => None, | 338 | _ => None, |
| 296 | } | 339 | } |
| 297 | } | 340 | } |
crates/in-web/src/pages/admin.rsmodified+90 -5
| @@ -2,13 +2,14 @@ | |||
| 2 | //! claim, stored as `members.is_admin`) sees it. Anyone else gets `404`, and so does a write from them. | 2 | //! claim, stored as `members.is_admin`) sees it. Anyone else gets `404`, and so does a write from them. |
| 3 | //! | 3 | //! |
| 4 | //! The page shows: health (the database and the blob store answer), the storage backend and its free space | 4 | //! The page shows: health (the database and the blob store answer), the storage backend and its free space |
| 5 | -//! or reachability, the base URL, every person with their quota, every service with its usage and quota, | 5 | +//! or reachability, the backup schedule with a button that asks for a backup now, the base URL, every person |
| 6 | -//! the defaults with the layer each value comes from (UI, file or built-in default), and the audit log with | 6 | +//! with their quota, every service with its usage and quota, the defaults with the layer each value comes from |
| 7 | -//! keyset paging and filters. | 7 | +//! (UI, file or built-in default), and the audit log with keyset paging and filters. |
| 8 | //! | 8 | //! |
| 9 | //! Every write is checked (administrator, then CSRF, both before any side effect), validated, and then | 9 | //! Every write is checked (administrator, then CSRF, both before any side effect), validated, and then |
| 10 | //! audited after it commits. The audit writer never fails a request. The defaults form is all-or-nothing: if | 10 | //! audited after it commits. The audit writer never fails a request. The defaults form is all-or-nothing: if |
| 11 | -//! one value is out of range, nothing is written. | 11 | +//! one value is out of range, nothing is written. A backup request only wakes the backup worker: the run itself |
| 12 | +//! happens in the background, and a request made while one is pending or running starts nothing. | ||
| 12 | 13 | ||
| 13 | use std::time::Duration; | 14 | use std::time::Duration; |
| 14 | 15 | ||
| @@ -36,7 +37,7 @@ use crate::audit::{self, Entry}; | |||
| 36 | use crate::auth::{Viewer, require_admin, require_admin_csrf}; | 37 | use crate::auth::{Viewer, require_admin, require_admin_csrf}; |
| 37 | use crate::health::{probe_blobs, probe_database}; | 38 | use crate::health::{probe_blobs, probe_database}; |
| 38 | use crate::i18n::Lang; | 39 | use crate::i18n::Lang; |
| 39 | -use crate::i18n::admin::{AK, a, notice, refusal}; | 40 | +use crate::i18n::admin::{AK, a, backup_schedule, notice, refusal}; |
| 40 | use crate::live::{Topic, stream_changes}; | 41 | use crate::live::{Topic, stream_changes}; |
| 41 | use crate::oidc::percent_encode; | 42 | use crate::oidc::percent_encode; |
| 42 | use crate::pages::drive::{viewer_lang, with_param}; | 43 | use crate::pages::drive::{viewer_lang, with_param}; |
| @@ -140,6 +141,26 @@ pub struct Probes { | |||
| 140 | pub blobs: bool, | 141 | pub blobs: bool, |
| 141 | } | 142 | } |
| 142 | 143 | ||
| 144 | +/// The form of the backup button: only the CSRF token of the session. | ||
| 145 | +#[derive(Debug, Default, Deserialize)] | ||
| 146 | +#[serde(default)] | ||
| 147 | +pub struct BackupForm { | ||
| 148 | + /// The CSRF token of the session. | ||
| 149 | + pub csrf: String, | ||
| 150 | +} | ||
| 151 | + | ||
| 152 | +/// What the backups section shows: where the sets go and how often a run happens. `None` when the | ||
| 153 | +/// configuration has no `[backup]` table. | ||
| 154 | +#[derive(Debug, Clone)] | ||
| 155 | +pub struct BackupStatus { | ||
| 156 | + /// The directory that receives the sets. | ||
| 157 | + pub dir: String, | ||
| 158 | + /// Hours between scheduled runs. | ||
| 159 | + pub interval_hours: u32, | ||
| 160 | + /// Sets kept. | ||
| 161 | + pub keep: u32, | ||
| 162 | +} | ||
| 163 | + | ||
| 143 | /// Whether a field's form unit is MiB. | 164 | /// Whether a field's form unit is MiB. |
| 144 | fn is_mib(key: &str) -> bool { | 165 | fn is_mib(key: &str) -> bool { |
| 145 | key == DEFAULT_QUOTA_KEY || key == DEFAULT_SERVICE_QUOTA_KEY || key == MAX_UPLOAD_KEY | 166 | key == DEFAULT_QUOTA_KEY || key == DEFAULT_SERVICE_QUOTA_KEY || key == MAX_UPLOAD_KEY |
| @@ -389,6 +410,37 @@ async fn storage_section(lang: Lang, status: &StorageStatus) -> Result<impl View | |||
| 389 | }) | 410 | }) |
| 390 | } | 411 | } |
| 391 | 412 | ||
| 413 | +/// The backups section: the directory and schedule, and the button that asks for a run now. Without | ||
| 414 | +/// `[backup]` it says that backups are off. | ||
| 415 | +#[component] | ||
| 416 | +async fn backup_section( | ||
| 417 | + lang: Lang, | ||
| 418 | + status: Option<&BackupStatus>, | ||
| 419 | + csrf: &str, | ||
| 420 | +) -> Result<impl View> { | ||
| 421 | + let schedule = status.map_or_else(String::new, |s| { | ||
| 422 | + backup_schedule(lang, s.interval_hours, s.keep) | ||
| 423 | + }); | ||
| 424 | + let dir = status.map_or_else(String::new, |s| s.dir.clone()); | ||
| 425 | + Ok(view! { | ||
| 426 | + <section class="admin-section"> | ||
| 427 | + <h2>(a(lang, AK::Backups))</h2> | ||
| 428 | + if status.is_some() { | ||
| 429 | + <ul> | ||
| 430 | + <li>(a(lang, AK::BackupDir)) ": " <code>(dir.as_str())</code></li> | ||
| 431 | + <li>(schedule.as_str())</li> | ||
| 432 | + </ul> | ||
| 433 | + <form method="post" action="/admin/backup" class="inline-form"> | ||
| 434 | + <input type="hidden" name="csrf" value=(csrf)> | ||
| 435 | + <button class="button" type="submit">(a(lang, AK::BackupNow))</button> | ||
| 436 | + </form> | ||
| 437 | + } else { | ||
| 438 | + <p class="muted">(a(lang, AK::BackupOff))</p> | ||
| 439 | + } | ||
| 440 | + </section> | ||
| 441 | + }) | ||
| 442 | +} | ||
| 443 | + | ||
| 392 | /// One person in the people table, with the form that sets their quota. | 444 | /// One person in the people table, with the form that sets their quota. |
| 393 | #[component] | 445 | #[component] |
| 394 | async fn person_row(lang: Lang, member: &Member, csrf: &str) -> Result<impl View> { | 446 | async fn person_row(lang: Lang, member: &Member, csrf: &str) -> Result<impl View> { |
| @@ -637,6 +689,7 @@ async fn admin_body( | |||
| 637 | probes: Probes, | 689 | probes: Probes, |
| 638 | build_sha: &str, | 690 | build_sha: &str, |
| 639 | storage: &StorageStatus, | 691 | storage: &StorageStatus, |
| 692 | + backups: Option<&BackupStatus>, | ||
| 640 | members: &[Member], | 693 | members: &[Member], |
| 641 | services: &[(Service, Usage)], | 694 | services: &[(Service, Usage)], |
| 642 | defaults: &[DefaultRow], | 695 | defaults: &[DefaultRow], |
| @@ -656,6 +709,7 @@ async fn admin_body( | |||
| 656 | } | 709 | } |
| 657 | health_section(lang: lang, probes: probes, build_sha: build_sha) | 710 | health_section(lang: lang, probes: probes, build_sha: build_sha) |
| 658 | storage_section(lang: lang, status: storage) | 711 | storage_section(lang: lang, status: storage) |
| 712 | + backup_section(lang: lang, status: backups, csrf: csrf) | ||
| 659 | people_section(lang: lang, members: members, csrf: csrf) | 713 | people_section(lang: lang, members: members, csrf: csrf) |
| 660 | services_section(lang: lang, rows: services, csrf: csrf) | 714 | services_section(lang: lang, rows: services, csrf: csrf) |
| 661 | defaults_section(lang: lang, rows: defaults, csrf: csrf) | 715 | defaults_section(lang: lang, rows: defaults, csrf: csrf) |
| @@ -680,6 +734,11 @@ async fn admin_page(cx: &Cx) -> Result<impl View> { | |||
| 680 | } | 734 | } |
| 681 | let defaults = default_rows(state).await?; | 735 | let defaults = default_rows(state).await?; |
| 682 | let storage = storage_status(state).await; | 736 | let storage = storage_status(state).await; |
| 737 | + let backups = state.config.backup.as_ref().map(|config| BackupStatus { | ||
| 738 | + dir: config.dir.display().to_string(), | ||
| 739 | + interval_hours: config.interval_hours, | ||
| 740 | + keep: config.keep, | ||
| 741 | + }); | ||
| 683 | let probes = probe_all(state).await; | 742 | let probes = probe_all(state).await; |
| 684 | let page = state | 743 | let page = state |
| 685 | .store | 744 | .store |
| @@ -715,6 +774,7 @@ async fn admin_page(cx: &Cx) -> Result<impl View> { | |||
| 715 | probes: probes, | 774 | probes: probes, |
| 716 | build_sha: build, | 775 | build_sha: build, |
| 717 | storage: &storage, | 776 | storage: &storage, |
| 777 | + backups: backups.as_ref(), | ||
| 718 | members: &members, | 778 | members: &members, |
| 719 | services: &services, | 779 | services: &services, |
| 720 | defaults: &defaults, | 780 | defaults: &defaults, |
| @@ -850,6 +910,31 @@ async fn save_defaults(cx: &Cx, Form(form): Form<DefaultsForm>) -> Result<SeeOth | |||
| 850 | } | 910 | } |
| 851 | } | 911 | } |
| 852 | 912 | ||
| 913 | +/// `POST /admin/backup`: asks the backup worker for a backup now. Refused when backups are off. The administrator | ||
| 914 | +/// and the CSRF token are checked first. A request made while a run is pending or running starts nothing, and | ||
| 915 | +/// gets the same notice. The request is audited. | ||
| 916 | +#[route(POST "/admin/backup")] | ||
| 917 | +async fn back_up_now(cx: &Cx, Form(form): Form<BackupForm>) -> Result<SeeOther> { | ||
| 918 | + let state = app_context::<AppState>(cx); | ||
| 919 | + let viewer = require_admin_csrf(cx, &form.csrf).await?; | ||
| 920 | + if state.config.backup.is_none() { | ||
| 921 | + return Ok(see_other(with_param("/admin", "refusal", "backup_off"))); | ||
| 922 | + } | ||
| 923 | + let started = state.backup.request(); | ||
| 924 | + audit::record( | ||
| 925 | + state, | ||
| 926 | + Entry { | ||
| 927 | + actor: Some(&viewer.member.sub), | ||
| 928 | + action: "backup.requested", | ||
| 929 | + target_kind: "backup", | ||
| 930 | + target_id: None, | ||
| 931 | + data: json!({ "started": started }), | ||
| 932 | + }, | ||
| 933 | + ) | ||
| 934 | + .await; | ||
| 935 | + Ok(see_other("/admin?notice=backup_started")) | ||
| 936 | +} | ||
| 937 | + | ||
| 853 | /// `GET /live/admin`: a data-less `changed` event whenever an audit entry is written. It ends when the viewer | 938 | /// `GET /live/admin`: a data-less `changed` event whenever an audit entry is written. It ends when the viewer |
| 854 | /// is no longer an administrator. | 939 | /// is no longer an administrator. |
| 855 | #[route(GET "/live/admin")] | 940 | #[route(GET "/live/admin")] |
crates/in-web/src/state.rsmodified+3 -0
| @@ -7,6 +7,7 @@ use in_core::blob::BlobStore; | |||
| 7 | use in_core::config::Config; | 7 | use in_core::config::Config; |
| 8 | use in_core::store::Store; | 8 | use in_core::store::Store; |
| 9 | 9 | ||
| 10 | +use crate::backup::BackupTrigger; | ||
| 10 | use crate::family::FamilyCache; | 11 | use crate::family::FamilyCache; |
| 11 | use crate::live::LiveHub; | 12 | use crate::live::LiveHub; |
| 12 | use crate::oidc::OidcClient; | 13 | use crate::oidc::OidcClient; |
| @@ -27,6 +28,8 @@ pub struct AppState { | |||
| 27 | pub key: Arc<[u8; 32]>, | 28 | pub key: Arc<[u8; 32]>, |
| 28 | /// The change counters of the live pages. | 29 | /// The change counters of the live pages. |
| 29 | pub live: Arc<LiveHub>, | 30 | pub live: Arc<LiveHub>, |
| 31 | + /// Asks the backup worker for a run now (the admin page). Shared with the worker. | ||
| 32 | + pub backup: Arc<BackupTrigger>, | ||
| 30 | /// The OpenID Connect client of `im`. `None` when the configuration has no `[oidc]` table, in which | 33 | /// The OpenID Connect client of `im`. `None` when the configuration has no `[oidc]` table, in which |
| 31 | /// case nobody can sign in and the sign-in routes say so. | 34 | /// case nobody can sign in and the sign-in routes say so. |
| 32 | pub oidc: Option<Arc<OidcClient>>, | 35 | pub oidc: Option<Arc<OidcClient>>, |
docs/architecture.mdmodified+3 -2
| @@ -143,7 +143,7 @@ The sweeper removes unreferenced blobs older than 24 h. | |||
| 143 | | `GET /links`, `POST /links`, `/links/{id}/revoke`, `/links/{id}/remint` | my share links | | 143 | | `GET /links`, `POST /links`, `/links/{id}/revoke`, `/links/{id}/remint` | my share links | |
| 144 | | `GET/POST /s/{token}` (anonymous) | public share page, folder browsing, password gate, download, media, thumbs | | 144 | | `GET/POST /s/{token}` (anonymous) | public share page, folder browsing, password gate, download, media, thumbs | |
| 145 | | `GET /settings` + POST | preferences (theme; language follows `im` unless overridden), storage usage | | 145 | | `GET /settings` + POST | preferences (theme; language follows `im` unless overridden), storage usage | |
| 146 | -| `GET /admin` + POSTs (instance admin) | people with quotas (set quota), services with quotas, default quotas, storage backend status, base URL, audit log, health | | 146 | +| `GET /admin` + POSTs (instance admin) | people with quotas (set quota), services with quotas, default quotas, storage backend status, backup schedule and `POST /admin/backup` (back up now), base URL, audit log, health | |
| 147 | | `GET /avatar/{sub}` | photo proxy with disk cache (contracts.md 3.2) | | 147 | | `GET /avatar/{sub}` | photo proxy with disk cache (contracts.md 3.2) | |
| 148 | | `GET /auth/login`, `/auth/callback`, `POST /auth/logout`, `POST /auth/backchannel-logout` | OIDC client | | 148 | | `GET /auth/login`, `/auth/callback`, `POST /auth/logout`, `POST /auth/backchannel-logout` | OIDC client | |
| 149 | | `GET /-/alive`, live streams, `/healthz`, `/assets/*` | | | 149 | | `GET /-/alive`, live streams, `/healthz`, `/assets/*` | | |
| @@ -238,7 +238,8 @@ and has no sessions. | |||
| 238 | which retries failed removals), seen JTIs, trash past | 238 | which retries failed removals), seen JTIs, trash past |
| 239 | `purge_after_days`, audit rows past 365 days, unreferenced blobs older than 24 h, quota drift check (nightly). | 239 | `purge_after_days`, audit rows past 365 days, unreferenced blobs older than 24 h, quota drift check (nightly). |
| 240 | - **backup** (optional): snapshot + blob tree manifest, HMAC-SHA256 over the manifest (`manifest.mac`, keyed from | 240 | - **backup** (optional): snapshot + blob tree manifest, HMAC-SHA256 over the manifest (`manifest.mac`, keyed from |
| 241 | - `in.key`, checked before a restore parses anything), keep N, dead-man pings. | 241 | + `in.key`, checked before a restore parses anything), keep N, dead-man pings. The worker also runs when the admin |
| 242 | + page asks (`BackupTrigger`: one busy flag and a `Notify` the worker waits on); at most one run is pending or running. | ||
| 242 | 243 | ||
| 243 | --- | 244 | --- |
| 244 | 245 | ||