iz
Clone
https://git.tahinli.com/Tahinli/iz.git
0 watching0 forksRustAGPL-3.0567.3 KiB3950aceTahinli27 days ago34 commits
| 1 | //! The scheduled backup worker. It wakes every minute and writes one backup set under `[backup] dir` when a run |
| 2 | //! is due: no attempt is recorded yet, or the last attempt is at least `interval_hours` old. Then it keeps the |
| 3 | //! newest `keep` finished sets and deletes the rest. It runs only when the config has a `[backup]` section, and |
| 4 | //! stops with the stop flag. |
| 5 | //! |
| 6 | //! The attempt time is recorded before a set is written. So a failing run is retried at the interval and not |
| 7 | //! every minute, and a restart does not reset the timer: the next run is due from the recorded attempt. |
| 8 | //! |
| 9 | //! A set is a directory named by its UTC stamp (for example `20261028T093000Z`). It holds `iz.db` (a |
| 10 | //! `VACUUM INTO` snapshot), `attachments/<id>` for every attachment whose bytes are local, and |
| 11 | //! `manifest.json` and `manifest.mac` written last (`iz_core::manifest`), the MAC keyed by the instance key. A set is built in `.tmp-<ulid>` and renamed to its |
| 12 | //! stamp only when complete, so a directory with a manifest is always a whole set. A failed set is removed. |
| 13 | //! Only directories this worker created, or names that match the stamp pattern and hold a manifest, are ever |
| 14 | //! deleted. Nothing is written in place. |
| 15 | //! |
| 16 | //! The admin settings page can ask for a run now through [`BackupTrigger`], which runs at once. At most one run |
| 17 | //! is pending or running: a request made meanwhile starts nothing. Scheduled and requested runs both hold the |
| 18 | //! busy flag, so they never overlap. |
| 19 | //! |
| 20 | //! Every run records its outcome in the `settings` table (the attempt time, the last success, and the error of |
| 21 | //! the latest attempt), so a failing backup is visible on the Backups card. [`status`] reads it back and |
| 22 | //! [`is_stale`] says when no success is recent enough to warn about. |
| 23 | |
| 24 | use DirBuilderExt; |
| 25 | use ; |
| 26 | use ; |
| 27 | use Duration; |
| 28 | |
| 29 | use ; |
| 30 | use BackupConfig; |
| 31 | use ; |
| 32 | use ; |
| 33 | use ; |
| 34 | use now_stamp; |
| 35 | use ; |
| 36 | use Timestamp; |
| 37 | use ; |
| 38 | |
| 39 | use crateAppState; |
| 40 | |
| 41 | /// Asks the backup worker for a run now. A run is pending or running at most once: a request made while one is |
| 42 | /// pending or running starts nothing. |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | /// Prefix of a set that is still being written. Reason: a crash leaves a visibly unfinished directory. |
| 79 | const TMP_PREFIX: &str = ".tmp-"; |
| 80 | /// The database copy inside a set. |
| 81 | const DB_FILE: &str = "iz.db"; |
| 82 | /// How often the worker checks whether a run is due. Reason: a due run starts at most a minute late, and a check |
| 83 | /// is one settings read. |
| 84 | const TICK: Duration = from_secs; |
| 85 | /// How long a healthcheck ping may take, in seconds. Reason: a slow ping must not hold the worker. |
| 86 | const PING_TIMEOUT: Duration = from_secs; |
| 87 | /// The stamp format of a set's directory name. Reason: no colons, so the name is portable. |
| 88 | const SET_STAMP_FORMAT: &str = "%Y%m%dT%H%M%SZ"; |
| 89 | /// Length of a set's directory name, which is the stamp format above. |
| 90 | const SET_NAME_LEN: usize = 16; |
| 91 | |
| 92 | /// Settings key: when a run was last attempted (a UTC stamp). |
| 93 | pub const LAST_ATTEMPT_SETTING: &str = "backup_last_attempt"; |
| 94 | /// Settings key: when a run last succeeded (a UTC stamp). Never cleared, so the card shows the last good set. |
| 95 | pub const LAST_SUCCESS_SETTING: &str = "backup_last_success"; |
| 96 | /// Settings key: the text of the error of the latest attempt. Empty after a success. |
| 97 | pub const LAST_ERROR_SETTING: &str = "backup_last_error"; |
| 98 | /// Time without a success after which the settings page warns. Reason: two days lets one scheduled run be |
| 99 | /// missed without a warning, and a longer gap means the backups are not happening. |
| 100 | const STALE_AFTER: Duration = from_secs; |
| 101 | /// Longest error text kept, in characters. Reason: the settings page shows it on one line. |
| 102 | const ERROR_TEXT_MAX: usize = 200; |
| 103 | |
| 104 | /// The recorded outcome of the backup runs, as the settings page reads it. Stamps are UTC; `None` when no |
| 105 | /// value was recorded. |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | /// Reads the recorded outcome. Empty values count as not recorded. |
| 117 | pub async |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | /// Whether the backups are overdue at `now`: no success was ever recorded, or the last one is older than |
| 135 | /// [`STALE_AFTER`] or two backup intervals, whichever is longer, so a weekly schedule is not always overdue. |
| 136 | /// A stamp that does not parse counts as no success. |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | /// Starts the backup worker when the config has a `[backup]` section. Without one this does nothing. |
| 148 | /// Every minute it checks whether a run is due (see [`is_due`]). An administrator's request runs at once (see |
| 149 | /// [`BackupTrigger`]). |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | /// Whether a run is due now: see [`is_due`]. A recorded attempt that cannot be read means no run this tick. |
| 183 | async |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | /// Whether a run is due at `now`: none was attempted yet (no stamp, or one that does not parse), or the last |
| 194 | /// attempt is at least `interval_hours` old. A failed attempt counts too, so it is retried at the interval. |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | /// One backup: record the attempt, write and publish a set, prune the old sets, ping the healthcheck, and |
| 206 | /// record the outcome. A failure is logged and recorded, and leaves no partial set. A stop request cancels a |
| 207 | /// set in progress and removes it. When the attempt cannot be recorded nothing runs, so the worker does not |
| 208 | /// retry every tick against a failing store. |
| 209 | async |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | /// Writes one set in a temporary directory and publishes it under its stamp. Returns the set's name and its |
| 247 | /// file count. On failure this removes the temporary directory, by its exact path. |
| 248 | async |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | /// Stores one outcome value. A failure is logged and never fails or stops the run. |
| 273 | async |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | /// The short text kept for a failed run and shown on the settings page. It never holds a database message, |
| 280 | /// since those can echo statement text, and it names no path: a filesystem error keeps only its OS reason. |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | /// Writes the files of one set into `tmp` and the manifest and its MAC last. Returns the number of files |
| 293 | /// listed, not counting the manifest. The caller removes `tmp` on error. |
| 294 | async |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | /// Creates the backup directory and any missing parents, unless it already exists. |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | /// Renames a complete set to its stamp and makes the rename durable. Refuses to replace an existing name. |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | /// Deletes all finished sets except the newest `keep`. Only directories named like a set and holding a |
| 348 | /// manifest are candidates, so an unrelated directory in the backup dir is never touched. Returns the count. |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | /// Whether `name` has the shape of a set's directory name: `YYYYMMDDTHHMMSSZ`. |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | /// Whether `path` is a real directory (not a link) that holds a manifest. |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | /// Creates a directory with mode 0700. `recursive` also creates missing parents. |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | /// Pings the healthcheck once. A failure is logged without the URL, because the URL can carry a secret key. |
| 393 | async |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 |