Compare commits
52 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9a3bb99d12 | |||
| c38cc47534 | |||
| dfd3f38d8c | |||
| 92e693cfe6 | |||
| bec070b1b5 | |||
| 89a126999f | |||
| 1b986c9d31 | |||
| 025bbfbb62 | |||
| 8ed7455619 | |||
| 30a250bc11 | |||
| 9c06ab2841 | |||
| d659322e60 | |||
| 7726c179f0 | |||
| df732f185a | |||
| b3c8068f8d | |||
| 0a9bd963fb | |||
| 379b81db02 | |||
| 828f3f142a | |||
| c146d700e8 | |||
| 8e273ac259 | |||
| 6385925845 | |||
| 894258f836 | |||
| 54750e9742 | |||
| fe92ef70bf | |||
| b3e3054737 | |||
| c775e27dd2 | |||
| d8e772137f | |||
| bcfec17cfe | |||
| b309081d1a | |||
| 5f8d5faee7 | |||
| 65f14b4380 | |||
| 4fa7cbf6ef | |||
| f4abfe0a8d | |||
| fe1a289c2d | |||
| ab3265e42c | |||
| 466cff8857 | |||
| 6bba0b59fc | |||
| a01aa16ccd | |||
| cc20b9174f | |||
| 239aa1b923 | |||
| 62766e1e71 | |||
| 9775cdc475 | |||
| a8da8cb54a | |||
| bfd54447b0 | |||
| 0b50bb6b81 | |||
| f415bab519 | |||
| 27eb0b32fc | |||
| b1d4f6a469 | |||
| fe7ad9b4b5 | |||
| 695d972785 | |||
| 972a52e665 | |||
| 7c08a10621 |
+147
@@ -7,6 +7,148 @@ the app store accepts a `-beta` suffix, but Nextcloud servers then only offer th
|
||||
beta/daily update channel, which would hide the app from most admins. The stability stage is
|
||||
instead carried by the app description, the git tag, the release, and this file.
|
||||
|
||||
## [0.9.6-beta.7] - 2026-10-14
|
||||
|
||||
Bugfix release for SMB locations, with `occ backup:cancel` to stop a backup that does not get
|
||||
anywhere. Tested on a production instance: a first full backup of 151,021 files (807 GiB) to OVH
|
||||
S3 completed, and a backup to a Hetzner Storage Box over SMB that had slowed to 12 GiB in 35
|
||||
hours ran at about 5.6 MiB/s after the update, while the S3 backup was still reading.
|
||||
|
||||
### Added
|
||||
- The list of backup runs (web interface and `occ backup:status`) shows the total size of each
|
||||
backup next to what it uploaded: an incremental backup of 1 TB that uploads 800 MB now shows
|
||||
both. `occ backup:status` shows sizes in GiB where that reads better than MiB.
|
||||
- `occ backup:cancel <run>` stops a running backup; `occ backup:status` shows the run numbers.
|
||||
No new step of the run starts, the step in progress finishes (usually within a minute), then
|
||||
the process working on the run (`occ backup:run` or cron) stops. What it already stored is
|
||||
removed by the next cleanup. A backup started by an earlier version is stopped as well: after
|
||||
two minutes it is marked cancelled while it works, and its process stops at the end of the
|
||||
step.
|
||||
|
||||
### Fixed
|
||||
- Backups to SMB locations, and to other slow locations with many packs, slowed to a crawl: each
|
||||
30-second step opened the location again, and reading the list of index files took longer
|
||||
than the step itself. Each step then stored a single 4 MiB pack, which made the list longer
|
||||
still. A first full backup of 0.8 TB to a Hetzner Storage Box over SMB managed 12 GiB in 35
|
||||
hours. `occ backup:run` and system cron now open the location once per run, opening no longer
|
||||
counts towards the step's time, and SMB folders are listed in one request instead of one per
|
||||
file. A backup to an SMB location that is not getting anywhere keeps running in its old
|
||||
process after the update: update first, then stop it with `occ backup:cancel <run>` and start
|
||||
it again. The new backup reuses the packs the old one stored.
|
||||
|
||||
## [0.9.5-beta.6] - 2026-10-12
|
||||
|
||||
Bugfix release for SFTP locations and large repositories. Tested on a production instance with a
|
||||
1 TB repository on a Hetzner Storage Box over SFTP: a check in 5.5 minutes, a deep check of all
|
||||
316,529 blobs (815 GiB) in 11 hours, and an incremental backup in under 8 minutes.
|
||||
|
||||
### Added
|
||||
- Progress of a running backup in the web interface and `occ backup:status`, e.g.
|
||||
"running (files) 68%". The total is the size of the previous snapshot; for a first backup it
|
||||
is estimated from Nextcloud's file cache (users' files, versions and trash) and shown as
|
||||
"~68%".
|
||||
- Edit a backup location (web interface and `occ backup:target:edit`): change the password, host,
|
||||
folder, connection type or name without losing its history. Before this the only option was a
|
||||
new location, which starts an empty repository and, with a retention lock at the provider,
|
||||
keeps paying for the old one. The new settings are only saved when they lead to the same
|
||||
repository; stored passwords are never sent to the browser, and an empty password field keeps
|
||||
the stored one. Administrators are notified of the change.
|
||||
- `occ backup:verify` reports how far it is every 30 seconds, and `occ backup:status` shows the
|
||||
progress of a running check and how the last check of each location ended. On a large
|
||||
repository a check takes hours, a deep one days.
|
||||
|
||||
### Changed
|
||||
- Checking and opening a large repository over SFTP is much faster: the index files (one per
|
||||
pack, 27,547 for 1 TB) are downloaded 32 at a time instead of one after the other, 114 instead
|
||||
of 6 files per second to a Hetzner Storage Box (about 4 minutes instead of 77 for 1 TB). A check
|
||||
of a repository with more than 256 packs also lists the pack folders (256 requests) instead of
|
||||
asking for every pack.
|
||||
- App store listing: the author's e-mail address is shown.
|
||||
- App store listing: the homepage link points to the GitHub repository instead of back to the
|
||||
app store page itself.
|
||||
- The notification for a location without a successful backup read "No successful backup to …
|
||||
since never" when there had never been one; it now reads "No successful backup to … yet".
|
||||
|
||||
### Fixed
|
||||
- 0.9.4 could not open a repository at the root of an SFTP location (empty path): its faster
|
||||
SFTP reads sent their first request before phpseclib had opened the SFTP channel, which only
|
||||
happens on the first SFTP call after login. Every backup then failed with "No repository at
|
||||
this location". The channel is now opened first.
|
||||
- A check (`occ backup:verify`) did not renew its hold on the location while it downloaded the
|
||||
index; on a large repository that takes longer than the hold lasts, so a weekly cleanup could
|
||||
have started during the check.
|
||||
- A long first backup could end as failed although its snapshot was complete: when its final
|
||||
step took longer than the step's lease (150 s with `occ backup:run`), the background job took
|
||||
the run over, finished it a second time and failed on the snapshot it had already recorded,
|
||||
overwriting the result. The lease now lasts at least 15 minutes, a step writes the run only if
|
||||
no other process wrote it since the step loaded it (in one database statement), and recording
|
||||
a snapshot that is already recorded is no longer an error.
|
||||
- `occ backup:verify` on a large repository failed with "SFTP connection failed while opening
|
||||
snapshots/…" or "Not found: snapshots/…". A Hetzner Storage Box drops an SFTP connection that
|
||||
carried a lot of data (some 40 MB) and then stayed quiet for a few minutes, and the check went
|
||||
quiet after downloading the index: it refreshed the local index cache, first in 29 minutes (a
|
||||
pass over all blobs for every pack: 27,547 packs, 400,000 blobs for 1 TB), then still in more
|
||||
than 6 minutes (a database transaction per pack on PostgreSQL). Nextcloud's SFTP storage keeps
|
||||
using a dropped connection, on which every file looks missing. Now:
|
||||
- an SFTP connection that was not used for a minute is checked first, and one that turns out
|
||||
to be dead after a failed request is replaced, once per request; uploads, moves and deletes
|
||||
are only checked first, never repeated;
|
||||
- the index cache is written in one transaction (27,547 packs in 11 seconds on a test server,
|
||||
against about a minute row by row);
|
||||
- after a check the cache holds the index files as they are: before, a blob that was in two
|
||||
packs was only cached for one of them.
|
||||
|
||||
## [0.9.4-beta.5] - 2026-10-09
|
||||
|
||||
Bugfix release: faster restores.
|
||||
|
||||
### Fixed
|
||||
- Restores and `occ backup:verify --deep` fetched every blob with its own request, so on a
|
||||
remote location they ran far below the speed of the connection. The blobs of a pack are now
|
||||
read from one open stream, in the order they were written. Restoring 86 MB in 202 files from a
|
||||
Hetzner Storage Box over SMB went from 0.78 MB/s to 4.2 MB/s, the speed of reading the packs
|
||||
as plain files over that connection.
|
||||
- Reading from an SFTP location waited a full round trip for every 256 KiB, whatever the line
|
||||
speed. Reads now keep up to 8 requests in flight. Restoring 1.97 GB in 500 files from a Hetzner
|
||||
Storage Box (47 ms away) took 694 s before the fix above, 499 s with it and 142 s with both
|
||||
(2.8 → 3.9 → 13.9 MB/s); a full 51 GB restore ran at 13.0 MB/s. When a server error
|
||||
interrupts such a read, it goes on from the same position with Nextcloud's own SFTP stream;
|
||||
after three failing files in a row, or on a Nextcloud whose SFTP library lacks what this needs,
|
||||
only Nextcloud's own stream is used.
|
||||
- README: a WebDAV location on a slow upload needs a higher `davstorage.request_timeout`.
|
||||
|
||||
## [0.9.3-beta.4] - 2026-10-08
|
||||
|
||||
Bugfix release.
|
||||
|
||||
### Fixed
|
||||
- A backup failed with "cannot execute INSERT in a read-only transaction" on PostgreSQL as soon
|
||||
as the database dump filled a whole pack (32 MiB): the pack's index cache row was written
|
||||
inside the read-only dump transaction. On other databases that row was rolled back silently.
|
||||
- Backups to a remote location slowed down as the repository grew: every backup step listed the
|
||||
repository's index with a round trip per file. Over SFTP to a Hetzner Storage Box a 60 GiB
|
||||
repository already lost a large part of each step to it; backing up continuously
|
||||
(`occ backup:run`) went from 45 KiB/s to 5.8 MB/s with the fix.
|
||||
|
||||
## [0.9.2-beta.3] - 2026-10-08
|
||||
|
||||
Bugfix release.
|
||||
|
||||
### Added
|
||||
- README: notes on using a Hetzner Storage Box over WebDAV and SMB.
|
||||
|
||||
### Fixed
|
||||
- Adding a backup location in the web interface failed with "Request failed with status code 403"
|
||||
when the login was more than 30 minutes old: the form did not ask for password confirmation.
|
||||
- Settings page: the "Backup and restore" / "Settings" tabs line up with the sections below them.
|
||||
- Backups: the runs table scrolls horizontally on narrow screens instead of running off the page.
|
||||
- `occ backup:verify --deep`: a blob that could not be read (network or storage error) was reported
|
||||
as "corrupted or tampered". Reads are now retried, and a read that keeps failing is reported
|
||||
separately as unreadable, with the error (run the check again; if the same blobs stay unreadable,
|
||||
their pack may be damaged or truncated). Only a blob that was read but fails decryption or its
|
||||
hash check counts as corrupted. Found on a Hetzner Storage Box over WebDAV, where the blob
|
||||
turned out to be intact.
|
||||
|
||||
## [0.9.1-beta.2] - 2026-10-07
|
||||
|
||||
Second beta, the first release in the Nextcloud app store (signed).
|
||||
@@ -103,6 +245,11 @@ and not implemented yet.
|
||||
- See the "Storage backend experiences" section of the README for provider-specific quirks found
|
||||
so far (e.g. Hetzner Storage Box sub-accounts).
|
||||
|
||||
[0.9.6-beta.7]: https://github.com/dillardblom/ng_backup/releases/tag/v0.9.6-beta.7
|
||||
[0.9.5-beta.6]: https://github.com/dillardblom/ng_backup/releases/tag/v0.9.5-beta.6
|
||||
[0.9.4-beta.5]: https://github.com/dillardblom/ng_backup/releases/tag/v0.9.4-beta.5
|
||||
[0.9.3-beta.4]: https://github.com/dillardblom/ng_backup/releases/tag/v0.9.3-beta.4
|
||||
[0.9.2-beta.3]: https://github.com/dillardblom/ng_backup/releases/tag/v0.9.2-beta.3
|
||||
[0.9.1-beta.2]: https://github.com/dillardblom/ng_backup/releases/tag/v0.9.1-beta.2
|
||||
[0.9.0-beta.1]: https://github.com/dillardblom/ng_backup/releases/tag/v0.9.0-beta.1
|
||||
[0.1.0-alpha.1]: https://github.com/dillardblom/ng_backup/releases/tag/v0.1.0-alpha.1
|
||||
|
||||
@@ -7,7 +7,7 @@ installations and hosted Nextcloud. (Choosing SMB as a storage backend is the on
|
||||
see "Storage backend experiences" below — that requirement comes from files_external, not from
|
||||
ng_backup, and applies to any Nextcloud app that writes to an SMB share.)
|
||||
|
||||
**Status: beta (0.9.1).** Tested thoroughly, usable alongside your current backup, but not yet a
|
||||
**Status: beta (0.9.6).** Tested thoroughly, usable alongside your current backup, but not yet a
|
||||
drop-in replacement for it. See "Known limitations" below, `PLAN.md` for the design and
|
||||
`docs/HOWTO.md` for a step-by-step guide from setup to a tested restore.
|
||||
|
||||
@@ -126,6 +126,21 @@ Contributions welcome via PR — add what you ran into with your own provider.
|
||||
verified whether this also applies to a Hetzner Storage Box **main** account (only tested with a
|
||||
sub-account so far) — if you've tried a main account, please say so in a PR.
|
||||
|
||||
- **Hetzner Storage Box over WebDAV and SMB (sub-accounts, 2026-10-07):** both work end to end
|
||||
(backup, incremental run, restore of a folder, verify), tested from outside the Hetzner network
|
||||
with about 66 MiB of data. In the Hetzner Console, enable WebDAV or SMB support for the
|
||||
sub-account, and "External reachability" when your server is not in the Hetzner network.
|
||||
WebDAV: host `https://uXXXXX-subN.your-storagebox.de`, and the `root`
|
||||
must be a folder that already exists (or empty): WebDAV does not create missing parent folders,
|
||||
so a root that doesn't exist yet fails `backup:target:add` with `Sabre\HTTP\ClientHttpException:
|
||||
Conflict`. SMB: share = the sub-account name, port 445 must be reachable from your server (it was
|
||||
from a home connection here, but some providers block it), and the `smbclient` PECL extension is
|
||||
required (see the SMB entry below). Both have a noticeable fixed cost per run: an incremental run
|
||||
that uploads nothing took about 20 s over WebDAV and 50 s over SMB. Over WebDAV, Nextcloud gives
|
||||
every request at most 30 s by default, also an upload of a whole 32 MiB pack: on an upload
|
||||
slower than about 9 Mbit/s a backup fails with `cURL error 28: Operation timed out`. Raise the
|
||||
limit with `occ config:system:set davstorage.request_timeout --value=600 --type=integer`.
|
||||
|
||||
- **FTP, server-dependent absolute-path handling:** files_external's FTP backend always builds
|
||||
absolute paths (e.g. `MKD /repo`) for the configured root, never relative ones. `atmoz/sftp`-style
|
||||
`vsftpd` test servers (chrooted) can reject that with a generic "create directory operation
|
||||
@@ -178,8 +193,9 @@ On the backlog; none of these block the beta or 1.0.0, but know them before you
|
||||
|---|---|
|
||||
| `backup:key:init`, `backup:key:kit`, `backup:key:confirm` | create the key, recovery kit, confirmation |
|
||||
| `backup:key:slots`, `backup:key:slot:add/replace/remove` | passphrase slots |
|
||||
| `backup:target:backends/add/list/remove/append-only` | backup locations |
|
||||
| `backup:target:backends/add/edit/list/remove/append-only` | backup locations |
|
||||
| `backup:run`, `backup:status`, `backup:list` | back up, status, restore points |
|
||||
| `backup:cancel <run>` | stop a running backup |
|
||||
| `backup:browse`, `backup:restore` | browse and restore |
|
||||
| `backup:retention`, `backup:prune`, `backup:trash`, `backup:trash:restore` | retention, cleanup, trash |
|
||||
| `backup:verify` | checksum audit of a restore point (`--deep` downloads and decrypts everything) |
|
||||
|
||||
+5
-3
@@ -9,15 +9,15 @@ directory to S3, SFTP, WebDAV, FTP, SMB or a local path, with restore of files a
|
||||
single users (via user_migration) and of the whole server onto a fresh installation. Tested
|
||||
thoroughly and usable alongside your current backup, but not yet a drop-in replacement for it.
|
||||
See the README for known limitations and the recovery kit before relying on it.]]></description>
|
||||
<version>0.9.1</version>
|
||||
<version>0.9.6</version>
|
||||
<licence>agpl</licence>
|
||||
<author>Dillard Blom</author>
|
||||
<author mail="dillard.blom@ensembia.com">Dillard Blom</author>
|
||||
<namespace>NgBackup</namespace>
|
||||
<documentation>
|
||||
<admin>https://github.com/dillardblom/ng_backup/blob/main/docs/HOWTO.md</admin>
|
||||
</documentation>
|
||||
<category>tools</category>
|
||||
<website>https://apps.nextcloud.com/apps/ng_backup</website>
|
||||
<website>https://github.com/dillardblom/ng_backup</website>
|
||||
<bugs>https://github.com/dillardblom/ng_backup/issues</bugs>
|
||||
<repository type="git">https://github.com/dillardblom/ng_backup</repository>
|
||||
<screenshot>https://raw.githubusercontent.com/dillardblom/ng_backup/main/screenshots/1-backups.png</screenshot>
|
||||
@@ -37,9 +37,11 @@ See the README for known limitations and the recovery kit before relying on it.]
|
||||
<command>OCA\NgBackup\Command\KeyConfirm</command>
|
||||
<command>OCA\NgBackup\Command\TargetBackends</command>
|
||||
<command>OCA\NgBackup\Command\TargetAdd</command>
|
||||
<command>OCA\NgBackup\Command\TargetEdit</command>
|
||||
<command>OCA\NgBackup\Command\TargetList</command>
|
||||
<command>OCA\NgBackup\Command\TargetRemove</command>
|
||||
<command>OCA\NgBackup\Command\RunBackup</command>
|
||||
<command>OCA\NgBackup\Command\CancelRun</command>
|
||||
<command>OCA\NgBackup\Command\Status</command>
|
||||
<command>OCA\NgBackup\Command\ListSnapshots</command>
|
||||
<command>OCA\NgBackup\Command\Browse</command>
|
||||
|
||||
@@ -18,6 +18,8 @@ return [
|
||||
['name' => 'AdminApi#untrash', 'url' => '/api/targets/{targetId}/trash/{snapshotId}', 'verb' => 'POST'],
|
||||
['name' => 'AdminApi#backends', 'url' => '/api/backends', 'verb' => 'GET'],
|
||||
['name' => 'AdminApi#addTarget', 'url' => '/api/targets', 'verb' => 'POST'],
|
||||
['name' => 'AdminApi#targetSettings', 'url' => '/api/targets/{id}/settings', 'verb' => 'GET'],
|
||||
['name' => 'AdminApi#updateTarget', 'url' => '/api/targets/{id}', 'verb' => 'PUT'],
|
||||
['name' => 'AdminApi#removeTarget', 'url' => '/api/targets/{id}', 'verb' => 'DELETE'],
|
||||
['name' => 'AdminApi#testTarget', 'url' => '/api/targets/{id}/test', 'verb' => 'POST'],
|
||||
['name' => 'AdminApi#setTargetLimit', 'url' => '/api/targets/{id}/limit', 'verb' => 'PUT'],
|
||||
|
||||
@@ -50,6 +50,22 @@ occ backup:target:list
|
||||
occ backup:status
|
||||
```
|
||||
|
||||
A backup that hangs or takes far too long can be stopped. `backup:status` shows the run number;
|
||||
the command waits until the step in progress is finished, then the process working on the run
|
||||
stops, and the next cleanup removes what it stored:
|
||||
|
||||
```
|
||||
occ backup:cancel 7
|
||||
```
|
||||
|
||||
Changed password, new host name, or moved the repository? Edit the location instead of adding a
|
||||
new one, so its history stays (in the web interface: **Edit** next to the location). The new
|
||||
settings must lead to the same repository; options you do not pass keep their value:
|
||||
|
||||
```
|
||||
occ backup:target:edit nas -o password=<new pw>
|
||||
```
|
||||
|
||||
## 3. Run the first backup
|
||||
|
||||
```
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// SPDX-FileCopyrightText: 2026 Dillard Blom
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
namespace OCA\NgBackup\Backend;
|
||||
|
||||
/** IBackend::getMany() for a backend where reading files one after the other costs nothing extra. */
|
||||
trait GetsOneByOne {
|
||||
/**
|
||||
* @param list<string> $paths
|
||||
* @return \Generator<string, string>
|
||||
*/
|
||||
public function getMany(array $paths): \Generator {
|
||||
foreach ($paths as $path) {
|
||||
$fh = $this->get($path);
|
||||
try {
|
||||
$data = stream_get_contents($fh);
|
||||
} finally {
|
||||
fclose($fh);
|
||||
}
|
||||
if ($data === false) {
|
||||
throw new BackendException('Read failed for ' . $path);
|
||||
}
|
||||
yield $path => $data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,16 @@ interface IBackend {
|
||||
/** @return resource readable stream of the whole object */
|
||||
public function get(string $path);
|
||||
|
||||
/**
|
||||
* Many small objects whole, in the order given (the repository's index files). A backend
|
||||
* where every request costs a round trip can fetch them several at a time.
|
||||
*
|
||||
* @param list<string> $paths
|
||||
* @return \Generator<string, string> contents by path
|
||||
* @throws BackendException for an object that cannot be read
|
||||
*/
|
||||
public function getMany(array $paths): \Generator;
|
||||
|
||||
/** @return string bytes [$offset, $offset + $length) of the object (used to fetch single blobs from a pack) */
|
||||
public function getRange(string $path, int $offset, int $length): string;
|
||||
|
||||
@@ -37,5 +47,15 @@ interface IBackend {
|
||||
/** @return list<string> object paths below $prefix (recursive) */
|
||||
public function list(string $prefix): array;
|
||||
|
||||
/**
|
||||
* Object paths directly in $dir, for a folder that only holds objects (index/, snapshots/,
|
||||
* catalog/, trash/info/). One listing, without checking the type of every entry: on a remote
|
||||
* storage list() costs a round trip per entry, which for index/ (one file per pack) grows
|
||||
* with the size of the repository.
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
public function listFiles(string $dir): array;
|
||||
|
||||
public function delete(string $path): void;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ declare(strict_types=1);
|
||||
namespace OCA\NgBackup\Backend;
|
||||
|
||||
final class LocalBackend implements IBackend {
|
||||
use GetsOneByOne;
|
||||
|
||||
public function __construct(
|
||||
private string $root,
|
||||
) {
|
||||
@@ -107,6 +109,25 @@ final class LocalBackend implements IBackend {
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function listFiles(string $dir): array {
|
||||
$base = $this->abs($dir);
|
||||
if (!is_dir($base)) {
|
||||
return [];
|
||||
}
|
||||
$result = [];
|
||||
$names = scandir($base);
|
||||
if ($names === false) {
|
||||
throw new BackendException('Cannot list ' . $dir);
|
||||
}
|
||||
foreach ($names as $name) {
|
||||
if ($name !== '.' && $name !== '..' && !StorageBackend::isTemporary($name)) {
|
||||
$result[] = trim($dir, '/') . '/' . $name;
|
||||
}
|
||||
}
|
||||
sort($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function delete(string $path): void {
|
||||
@unlink($this->abs($path));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,628 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// SPDX-FileCopyrightText: 2026 Dillard Blom
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
namespace OCA\NgBackup\Backend;
|
||||
|
||||
/**
|
||||
* Read stream for files_external's SFTP storage that keeps several READ requests in flight.
|
||||
*
|
||||
* files_external's own read stream asks for the next 256 KiB only after the previous one has
|
||||
* arrived, so every request costs a full round trip: about 5 MB/s at 47 ms to a Hetzner Storage
|
||||
* Box, whatever the line speed. With 8 requests in flight the same connection reached 15 MB/s.
|
||||
* phpseclib's get() pipelines too, but only into memory or a local file, and only from offset 0.
|
||||
*
|
||||
* Talks to the storage's phpseclib connection through the same internal methods files_external's
|
||||
* read stream uses (_send_sftp_packet, _get_sftp_packet, _realpath); open() returns null when the
|
||||
* connection does not offer them (phpseclib 3 does not), and StorageBackend then falls back to the
|
||||
* storage's own stream. A restore must rather be slow than fail: when a read fails halfway, the
|
||||
* stream goes on from the same position with the storage's own stream. After MAX_FAILURES streams
|
||||
* in a row failed, open() returns null for the rest of the process; one that works resets the count.
|
||||
* Like that stream, it owns the connection while open: no other request may go over the same
|
||||
* connection until it is closed (PackReaderGuard takes care of that for the repository).
|
||||
*/
|
||||
final class SftpReadStream {
|
||||
private const PROTOCOL = 'ngbsftpread';
|
||||
/** Requests in flight once a stream reads sequentially; it starts at one and doubles. */
|
||||
public const DEPTH = 8;
|
||||
/** Files read at once by readMany(): each holds a handle on the server while its batch runs. */
|
||||
public const BATCH = 32;
|
||||
/** Larger files are left to the usual read by readMany(), which holds every file in memory. */
|
||||
private const MANY_MAX_BYTES = 16 * 1048576;
|
||||
/** OpenSSH and the Hetzner Storage Box both answer at most 255 KiB per READ. */
|
||||
private const CHUNK = 261120;
|
||||
/** Every SFTP server must handle packets of this size (draft-ietf-secsh-filexfer-02, section 3). */
|
||||
private const MIN_CHUNK = 32768;
|
||||
|
||||
private const FXP_OPEN = 3;
|
||||
private const FXP_CLOSE = 4;
|
||||
private const FXP_READ = 5;
|
||||
private const FXP_STATUS = 101;
|
||||
private const FXP_HANDLE = 102;
|
||||
private const FXP_DATA = 103;
|
||||
private const FXF_READ = 1;
|
||||
private const FX_OK = 0;
|
||||
private const FX_EOF = 1;
|
||||
|
||||
/**
|
||||
* READ size per connection, lowered when a server answers less than asked and then goes on, so
|
||||
* later streams over that connection ask for no more than it gives.
|
||||
*
|
||||
* @var \WeakMap<object, int>|null
|
||||
*/
|
||||
private static ?\WeakMap $chunks = null;
|
||||
private static int $requestId = 0x4e470000;
|
||||
/**
|
||||
* Connections whose server did not confirm closing a batch's files: readMany() is not used on
|
||||
* them again, so handles cannot pile up.
|
||||
*
|
||||
* @var \WeakMap<object, true>|null
|
||||
*/
|
||||
private static ?\WeakMap $noBatches = null;
|
||||
/** Streams in a row that failed; at MAX_FAILURES only the storage's own stream is used. */
|
||||
private static int $failures = 0;
|
||||
public const MAX_FAILURES = 3;
|
||||
/** Set by stream_open() when the connection failed rather than the file. */
|
||||
private static bool $openBroke = false;
|
||||
/** Why the last stream_open() failed on the connection. */
|
||||
private static string $openCause = '';
|
||||
|
||||
/** @var resource|null set by PHP for stream wrappers */
|
||||
public $context;
|
||||
|
||||
/**
|
||||
* The phpseclib connection. PHP creates stream wrappers without constructor arguments; it is
|
||||
* set in stream_open(), and no other method is called when that fails.
|
||||
*
|
||||
* @psalm-suppress PropertyNotSetInConstructor
|
||||
*/
|
||||
private object $sftp;
|
||||
/** The storage and the path on it, to fall back to the storage's own stream. */
|
||||
private ?object $storage = null;
|
||||
private string $file = '';
|
||||
/** @var resource|null the storage's own stream, once this one has failed */
|
||||
private $fallback = null;
|
||||
private string $handle = '';
|
||||
/** Position of the reader: the first byte of $buffer. */
|
||||
private int $pos = 0;
|
||||
/** Offset of the next READ to send. */
|
||||
private int $next = 0;
|
||||
private string $buffer = '';
|
||||
/** @var list<array{int, int, int}> request id, offset, length, in the order sent */
|
||||
private array $inFlight = [];
|
||||
private int $depth = 1;
|
||||
private bool $eof = false;
|
||||
/** This stream gave up on its own requests. */
|
||||
private bool $failed = false;
|
||||
/** The connection is in an unknown state: nothing more is sent over it or read from it. */
|
||||
private bool $broken = false;
|
||||
/** Length of the last short answer, until the next answer tells whether it was the end of the file. */
|
||||
private ?int $shortAnswer = null;
|
||||
|
||||
/**
|
||||
* @param object $storage files_external's SFTP storage (getConnection(), getRoot())
|
||||
* @return resource|false|null the stream, false when the file cannot be opened, null when the
|
||||
* connection does not offer what this stream needs
|
||||
* @throws BackendException when the connection failed after the OPEN request went out: the
|
||||
* storage's own stream must not be tried on it, as it would take a
|
||||
* late answer to that request for its own
|
||||
*/
|
||||
public static function open(object $storage, string $path) {
|
||||
if (self::$failures >= self::MAX_FAILURES) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
$sftp = self::connection($storage);
|
||||
if ($sftp === null) {
|
||||
return null;
|
||||
}
|
||||
if (!in_array(self::PROTOCOL, stream_get_wrappers(), true)) {
|
||||
stream_wrapper_register(self::PROTOCOL, self::class);
|
||||
}
|
||||
$root = rtrim((string)$storage->getRoot(), '/');
|
||||
$context = stream_context_create([self::PROTOCOL => [
|
||||
'session' => $sftp,
|
||||
'path' => $root . '/' . ltrim($path, '/'),
|
||||
'storage' => $storage,
|
||||
'file' => $path,
|
||||
]]);
|
||||
self::$openBroke = false;
|
||||
$fh = @fopen(self::PROTOCOL . '://file', 'r', false, $context);
|
||||
} catch (\Throwable) {
|
||||
// nothing was sent yet
|
||||
self::$failures++;
|
||||
return null;
|
||||
}
|
||||
if ($fh === false && self::openBroke()) {
|
||||
throw new BackendException('SFTP connection failed while opening ' . $path . ': ' . self::$openCause);
|
||||
}
|
||||
return $fh;
|
||||
}
|
||||
|
||||
/**
|
||||
* The storage's phpseclib connection, with its SFTP channel open, when it offers what these
|
||||
* streams need; null otherwise.
|
||||
*/
|
||||
private static function connection(object $storage): ?object {
|
||||
if (!is_callable([$storage, 'getConnection']) || !is_callable([$storage, 'getRoot'])) {
|
||||
return null;
|
||||
}
|
||||
$sftp = $storage->getConnection();
|
||||
// is_callable and get_object_vars from here only see what is public: phpseclib 3 has
|
||||
// methods of these names neither public nor at all.
|
||||
if (!is_object($sftp) || !is_callable([$sftp, '_send_sftp_packet']) || !is_callable([$sftp, '_get_sftp_packet'])
|
||||
|| !is_callable([$sftp, '_realpath']) || !is_callable([$sftp, 'pwd'])
|
||||
|| !array_key_exists('packet_type', get_object_vars($sftp))) {
|
||||
return null;
|
||||
}
|
||||
// phpseclib opens the SFTP channel on its first SFTP call, not at login: on a fresh
|
||||
// connection a packet sent directly would go nowhere. pwd() opens it like any call does.
|
||||
if ($sftp->pwd() === false) {
|
||||
return null;
|
||||
}
|
||||
return $sftp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read many small files whole, BATCH at a time: the OPEN, READ and CLOSE requests of a batch
|
||||
* all go out before their answers are read, so a batch costs a few round trips instead of
|
||||
* four per file. Meant for files of up to a few hundred KiB (the repository's index files):
|
||||
* every file is held in memory.
|
||||
*
|
||||
* @param list<string> $paths below the storage's root
|
||||
* @return array<string, string|null>|null contents by path; null for a file this could not
|
||||
* read (missing, or a server error), so the caller
|
||||
* reads it the usual way; null for all when the
|
||||
* connection does not offer what this needs
|
||||
* @throws BackendException when the connection failed while requests were out: the storage's
|
||||
* own stream must not be tried on it
|
||||
*/
|
||||
public static function readMany(object $storage, array $paths): ?array {
|
||||
if (self::$failures >= self::MAX_FAILURES) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
$sftp = self::connection($storage);
|
||||
$root = $sftp === null ? '' : rtrim((string)$storage->getRoot(), '/');
|
||||
} catch (\Throwable) {
|
||||
self::$failures++;
|
||||
return null;
|
||||
}
|
||||
if ($sftp === null || self::$noBatches?->offsetExists($sftp) === true) {
|
||||
return null;
|
||||
}
|
||||
$result = array_fill_keys($paths, null);
|
||||
foreach (array_chunk($paths, self::BATCH) as $batch) {
|
||||
try {
|
||||
[$read, $closed] = self::readBatch($sftp, $root, $batch);
|
||||
} catch (\Throwable $e) {
|
||||
self::$failures++;
|
||||
// Answers to requests already sent may still arrive, and files_external would take
|
||||
// them for its own: end the connection rather than leave it out of step.
|
||||
self::disconnect($sftp);
|
||||
throw $e instanceof BackendException ? $e : new BackendException('SFTP connection failed while reading ' . count($batch) . ' files: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
foreach ($read as $path => $contents) {
|
||||
$result[$path] = $contents;
|
||||
}
|
||||
if (!$closed) {
|
||||
// The server did not confirm closing these files: no more batches on this
|
||||
// connection, so handles cannot pile up. The rest is read the usual way.
|
||||
self::noMoreBatches($sftp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
self::$failures = 0;
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function noMoreBatches(object $sftp): void {
|
||||
self::$noBatches ??= new \WeakMap();
|
||||
self::$noBatches[$sftp] = true;
|
||||
}
|
||||
|
||||
private static function disconnect(object $sftp): void {
|
||||
try {
|
||||
if (is_callable([$sftp, 'disconnect'])) {
|
||||
$sftp->disconnect();
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
}
|
||||
}
|
||||
|
||||
/** Requests of one kind for a whole batch, then their answers in order. @return list<array{int, string}> */
|
||||
private static function exchange(object $sftp, int $type, array $packets): array {
|
||||
$ids = [];
|
||||
foreach ($packets as $packet) {
|
||||
$id = self::nextId();
|
||||
if (!$sftp->_send_sftp_packet($type, $packet, $id)) {
|
||||
throw new BackendException('SFTP connection failed while sending a request');
|
||||
}
|
||||
$ids[] = $id;
|
||||
}
|
||||
$answers = [];
|
||||
foreach ($ids as $id) {
|
||||
$response = $sftp->_get_sftp_packet($id);
|
||||
if (!is_string($response)) {
|
||||
throw new BackendException('SFTP connection failed while waiting for an answer');
|
||||
}
|
||||
$answers[] = [$sftp->packet_type, $response];
|
||||
}
|
||||
return $answers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $batch
|
||||
* @return array{0: array<string, string>, 1: bool} the files read whole, and whether the
|
||||
* server confirmed closing every handle
|
||||
*/
|
||||
private static function readBatch(object $sftp, string $root, array $batch): array {
|
||||
$opens = [];
|
||||
$openPaths = [];
|
||||
foreach ($batch as $path) {
|
||||
$remote = $sftp->_realpath($root . '/' . ltrim($path, '/'));
|
||||
if (is_string($remote)) {
|
||||
$opens[] = pack('Na*N2', strlen($remote), $remote, self::FXF_READ, 0);
|
||||
$openPaths[] = $path;
|
||||
}
|
||||
// a path that cannot be resolved is left to the caller's usual read
|
||||
}
|
||||
$handles = []; // path => handle
|
||||
foreach (self::exchange($sftp, self::FXP_OPEN, $opens) as $i => [$type, $response]) {
|
||||
if ($type === self::FXP_HANDLE) {
|
||||
$handles[$openPaths[$i]] = substr($response, 4);
|
||||
}
|
||||
// any other answer (missing, no permission): the caller reads it the usual way
|
||||
}
|
||||
$data = array_fill_keys(array_keys($handles), '');
|
||||
$reading = $handles;
|
||||
while ($reading !== []) {
|
||||
$length = self::chunk($sftp);
|
||||
$reads = [];
|
||||
foreach ($reading as $path => $handle) {
|
||||
$offset = strlen($data[$path]);
|
||||
$reads[] = pack('Na*N3', strlen($handle), $handle, intdiv($offset, 4294967296), $offset % 4294967296, $length);
|
||||
}
|
||||
$paths = array_keys($reading);
|
||||
foreach (self::exchange($sftp, self::FXP_READ, $reads) as $i => [$type, $response]) {
|
||||
$path = $paths[$i];
|
||||
$declared = strlen($response) >= 4 ? unpack('N', $response) : false;
|
||||
if ($type === self::FXP_DATA && is_array($declared) && $declared[1] > 0 && $declared[1] === strlen($response) - 4
|
||||
&& $declared[1] <= $length && strlen($data[$path]) + $declared[1] <= self::MANY_MAX_BYTES) {
|
||||
$data[$path] .= substr($response, 4); // more may follow: read on from there
|
||||
continue;
|
||||
}
|
||||
unset($reading[$path]);
|
||||
$status = $type === self::FXP_STATUS && is_array($declared) ? $declared[1] : null;
|
||||
if ($status !== self::FX_EOF) {
|
||||
// a server error, a malformed answer or a file too large for this: the caller
|
||||
// reads it the usual way
|
||||
unset($data[$path]);
|
||||
}
|
||||
}
|
||||
}
|
||||
$closes = [];
|
||||
foreach ($handles as $handle) {
|
||||
$closes[] = pack('Na*', strlen($handle), $handle);
|
||||
}
|
||||
$closed = true;
|
||||
foreach (self::exchange($sftp, self::FXP_CLOSE, $closes) as [$type, $response]) {
|
||||
$status = $type === self::FXP_STATUS && strlen($response) >= 4 ? unpack('N', $response) : false;
|
||||
if (!is_array($status) || $status[1] !== self::FX_OK) {
|
||||
$closed = false;
|
||||
}
|
||||
}
|
||||
return [$data, $closed];
|
||||
}
|
||||
|
||||
/** Test hook: forget the learned READ sizes and earlier failures. */
|
||||
public static function reset(): void {
|
||||
self::$chunks = null;
|
||||
self::$noBatches = null;
|
||||
self::$failures = 0;
|
||||
}
|
||||
|
||||
public function stream_open(string $url, string $mode, int $options, ?string &$openedPath): bool {
|
||||
$options = stream_context_get_options($this->context ?? stream_context_get_default())[self::PROTOCOL] ?? [];
|
||||
if (!isset($options['session'], $options['path']) || !is_object($options['session'])) {
|
||||
return false;
|
||||
}
|
||||
$this->sftp = $options['session'];
|
||||
$storage = $options['storage'] ?? null;
|
||||
$this->storage = is_object($storage) ? $storage : null;
|
||||
$this->file = (string)($options['file'] ?? '');
|
||||
try {
|
||||
$remote = $this->sftp->_realpath((string)$options['path']);
|
||||
if (!is_string($remote)) {
|
||||
return false;
|
||||
}
|
||||
$id = self::nextId();
|
||||
if (!$this->sftp->_send_sftp_packet(self::FXP_OPEN, pack('Na*N2', strlen($remote), $remote, self::FXF_READ, 0), $id)) {
|
||||
return $this->connectionFailed('the request could not be sent');
|
||||
}
|
||||
$response = $this->sftp->_get_sftp_packet($id);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->connectionFailed(get_class($e) . ': ' . $e->getMessage());
|
||||
}
|
||||
if (!is_string($response)) {
|
||||
return $this->connectionFailed('no answer');
|
||||
}
|
||||
if ($this->sftp->packet_type !== self::FXP_HANDLE) {
|
||||
return false; // an answer, but no handle: the file cannot be opened
|
||||
}
|
||||
$this->handle = substr($response, 4);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Not a missing file but a connection that does not behave as expected. */
|
||||
private function connectionFailed(string $cause): bool {
|
||||
self::$failures++;
|
||||
self::$openBroke = true;
|
||||
self::$openCause = $cause . self::connectionState($this->sftp);
|
||||
// unknown state: closed, so that nothing else uses it (and the backend can build a new one)
|
||||
self::disconnect($this->sftp);
|
||||
return false;
|
||||
}
|
||||
|
||||
/** What phpseclib knows about the connection, for an error message. */
|
||||
private static function connectionState(object $sftp): string {
|
||||
$state = [];
|
||||
try {
|
||||
if (is_callable([$sftp, 'isConnected'])) {
|
||||
$state[] = 'connected: ' . ($sftp->isConnected() ? 'yes' : 'no');
|
||||
}
|
||||
if (is_callable([$sftp, 'getLastError'])) {
|
||||
$last = (string)$sftp->getLastError();
|
||||
if ($last !== '') {
|
||||
$state[] = 'ssh: ' . $last;
|
||||
}
|
||||
}
|
||||
if (is_callable([$sftp, 'getLastSFTPError'])) {
|
||||
$last = (string)$sftp->getLastSFTPError();
|
||||
if ($last !== '') {
|
||||
$state[] = 'sftp: ' . $last;
|
||||
}
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
}
|
||||
return $state === [] ? '' : ' (' . implode('; ', $state) . ')';
|
||||
}
|
||||
|
||||
/** Whether the last stream_open() failed on the connection; a call, as fopen() sets it. */
|
||||
private static function openBroke(): bool {
|
||||
return self::$openBroke;
|
||||
}
|
||||
|
||||
public function stream_read(int $count): string|false {
|
||||
if ($this->fallback !== null) {
|
||||
return fread($this->fallback, $count);
|
||||
}
|
||||
while (strlen($this->buffer) < $count && !$this->eof) {
|
||||
if (!$this->receive()) {
|
||||
return $this->useFallback() ? $this->stream_read($count) : false;
|
||||
}
|
||||
}
|
||||
$data = substr($this->buffer, 0, $count);
|
||||
$this->buffer = substr($this->buffer, strlen($data));
|
||||
$this->pos += strlen($data);
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function stream_eof(): bool {
|
||||
if ($this->fallback !== null) {
|
||||
return feof($this->fallback);
|
||||
}
|
||||
return $this->eof && $this->buffer === '';
|
||||
}
|
||||
|
||||
public function stream_tell(): int {
|
||||
if ($this->fallback !== null) {
|
||||
$pos = ftell($this->fallback);
|
||||
return $pos === false ? $this->pos : $pos;
|
||||
}
|
||||
return $this->pos;
|
||||
}
|
||||
|
||||
public function stream_seek(int $offset, int $whence = SEEK_SET): bool {
|
||||
if ($this->fallback !== null) {
|
||||
return fseek($this->fallback, $offset, $whence) === 0;
|
||||
}
|
||||
if ($whence === SEEK_CUR) {
|
||||
$offset += $this->pos;
|
||||
} elseif ($whence !== SEEK_SET) {
|
||||
return false; // the size is never asked for, to save a round trip
|
||||
}
|
||||
if ($offset < 0) {
|
||||
return false;
|
||||
}
|
||||
if ($this->failed) {
|
||||
$this->pos = $offset;
|
||||
return $this->useFallback();
|
||||
}
|
||||
if ($offset >= $this->pos && $offset - $this->pos <= strlen($this->buffer)) {
|
||||
$this->buffer = substr($this->buffer, $offset - $this->pos);
|
||||
} else {
|
||||
$this->drain();
|
||||
$this->buffer = '';
|
||||
$this->next = $offset;
|
||||
$this->eof = false;
|
||||
$this->depth = 1;
|
||||
$this->shortAnswer = null;
|
||||
}
|
||||
$this->pos = $offset;
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return array<int|string, int> */
|
||||
public function stream_stat(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
public function stream_close(): void {
|
||||
if (!$this->failed) {
|
||||
self::$failures = 0;
|
||||
}
|
||||
$fallback = $this->fallback;
|
||||
$this->fallback = null;
|
||||
if ($fallback !== null) {
|
||||
fclose($fallback);
|
||||
}
|
||||
$this->closeHandle();
|
||||
}
|
||||
|
||||
/**
|
||||
* Go on with the storage's own stream from the current position, after a server error on this
|
||||
* one. The data still buffered is dropped and read again. False after a connection error, or
|
||||
* when that stream cannot be opened or positioned: then the read fails as it would have
|
||||
* without this class. Reopening by path is safe for a repository: packs and index files are
|
||||
* never rewritten, and every object is authenticated when it is decrypted.
|
||||
*/
|
||||
private function useFallback(): bool {
|
||||
$this->closeHandle();
|
||||
$this->buffer = '';
|
||||
// files_external's stream takes whatever answer comes next on the connection: after a
|
||||
// connection error, answers to this stream's requests may still be on their way.
|
||||
if ($this->broken || $this->storage === null || !is_callable([$this->storage, 'fopen'])) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$fh = $this->storage->fopen($this->file, 'r');
|
||||
if (!is_resource($fh)) {
|
||||
return false;
|
||||
}
|
||||
if ($this->pos > 0 && fseek($fh, $this->pos) !== 0) {
|
||||
fclose($fh);
|
||||
return false;
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
return false;
|
||||
}
|
||||
$this->fallback = $fh;
|
||||
return true;
|
||||
}
|
||||
|
||||
private function closeHandle(): void {
|
||||
$this->drain();
|
||||
if ($this->handle !== '' && !$this->broken) {
|
||||
try {
|
||||
$id = self::nextId();
|
||||
if (!$this->sftp->_send_sftp_packet(self::FXP_CLOSE, pack('Na*', strlen($this->handle), $this->handle), $id)
|
||||
|| !is_string($this->sftp->_get_sftp_packet($id))) {
|
||||
$this->broken = true;
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
$this->broken = true;
|
||||
}
|
||||
if ($this->broken) {
|
||||
self::disconnect($this->sftp);
|
||||
}
|
||||
}
|
||||
$this->handle = '';
|
||||
}
|
||||
|
||||
/** Keep the pipeline full, then take the oldest answer. False on a connection or server error. */
|
||||
private function receive(): bool {
|
||||
if ($this->failed) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return $this->receiveOne();
|
||||
} catch (\Throwable) {
|
||||
$this->broken = true;
|
||||
return $this->fail();
|
||||
}
|
||||
}
|
||||
|
||||
private function receiveOne(): bool {
|
||||
while (count($this->inFlight) < $this->depth) {
|
||||
$id = self::nextId();
|
||||
$length = self::chunk($this->sftp);
|
||||
$packet = pack('Na*N3', strlen($this->handle), $this->handle, intdiv($this->next, 4294967296), $this->next % 4294967296, $length);
|
||||
if (!$this->sftp->_send_sftp_packet(self::FXP_READ, $packet, $id)) {
|
||||
$this->broken = true;
|
||||
return $this->fail();
|
||||
}
|
||||
$this->inFlight[] = [$id, $this->next, $length];
|
||||
$this->next += $length;
|
||||
}
|
||||
[$id, $offset, $length] = array_shift($this->inFlight);
|
||||
$response = $this->sftp->_get_sftp_packet($id);
|
||||
if (!is_string($response)) {
|
||||
$this->broken = true;
|
||||
return $this->fail();
|
||||
}
|
||||
if ($this->sftp->packet_type === self::FXP_DATA) {
|
||||
$data = substr($response, 4);
|
||||
if ($data === '') {
|
||||
return $this->fail();
|
||||
}
|
||||
$this->buffer .= $data;
|
||||
$this->depth = min(self::DEPTH, $this->depth * 2);
|
||||
if ($this->shortAnswer !== null) {
|
||||
// More data after a short answer: that one was the server's cap, not the end.
|
||||
// Never below what every server must support: a one-off small answer is no cap.
|
||||
self::$chunks ??= new \WeakMap();
|
||||
self::$chunks[$this->sftp] = max(self::MIN_CHUNK, min(self::chunk($this->sftp), $this->shortAnswer));
|
||||
$this->shortAnswer = null;
|
||||
}
|
||||
if (strlen($data) < $length) {
|
||||
// Fewer bytes than asked: the end of the file, or a server that caps a READ. The
|
||||
// requests behind this one assumed a full answer, so drop them and ask again from here.
|
||||
$this->drain();
|
||||
$this->next = $offset + strlen($data);
|
||||
$this->shortAnswer = strlen($data);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
$status = $this->sftp->packet_type === self::FXP_STATUS ? unpack('N', $response) : false;
|
||||
if (is_array($status) && ($status[1] ?? null) === self::FX_EOF) {
|
||||
$this->eof = true;
|
||||
$this->shortAnswer = null;
|
||||
$this->drain();
|
||||
return true;
|
||||
}
|
||||
return $this->fail();
|
||||
}
|
||||
|
||||
private function fail(): bool {
|
||||
$this->drain();
|
||||
if (!$this->failed) {
|
||||
self::$failures++;
|
||||
}
|
||||
$this->failed = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Read and drop the answers still on their way, so the connection is free again. */
|
||||
private function drain(): void {
|
||||
$inFlight = $this->inFlight;
|
||||
$this->inFlight = [];
|
||||
try {
|
||||
foreach ($this->broken ? [] : $inFlight as [$id]) {
|
||||
if (!is_string($this->sftp->_get_sftp_packet($id))) {
|
||||
$this->broken = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
$this->broken = true;
|
||||
}
|
||||
if ($this->broken) {
|
||||
// unknown state: closed, so that nothing else uses it (and the backend can build a new one)
|
||||
self::disconnect($this->sftp);
|
||||
}
|
||||
}
|
||||
|
||||
private static function chunk(object $sftp): int {
|
||||
$learned = self::$chunks?->offsetExists($sftp) === true ? self::$chunks->offsetGet($sftp) : null;
|
||||
return is_int($learned) ? $learned : self::CHUNK;
|
||||
}
|
||||
|
||||
private static function nextId(): int {
|
||||
self::$requestId = self::$requestId >= 0x7fffffff ? 0x4e470000 : self::$requestId + 1;
|
||||
return self::$requestId;
|
||||
}
|
||||
}
|
||||
@@ -17,15 +17,30 @@ use OCP\Files\Storage\IWriteStreamStorage;
|
||||
final class StorageBackend implements IBackend {
|
||||
/** Object stores write whole objects atomically; other storages get a temp name + rename. */
|
||||
private bool $atomicPut;
|
||||
/** SFTP: read through SftpReadStream, which keeps several requests in flight. */
|
||||
private bool $sftpReads;
|
||||
/** Seconds without a request after which an SFTP connection is checked before it is used. */
|
||||
public const IDLE_CHECK = 60;
|
||||
private float $lastUsed;
|
||||
/** Times the storage was built anew after its connection died (for tests and diagnostics). */
|
||||
public int $reconnects = 0;
|
||||
|
||||
/**
|
||||
* @param (\Closure():IStorage)|null $reconnect builds the same storage anew, with a new
|
||||
* connection
|
||||
*/
|
||||
public function __construct(
|
||||
private IStorage $storage,
|
||||
private string $base = '',
|
||||
bool $createBase = true,
|
||||
private ?\Closure $reconnect = null,
|
||||
) {
|
||||
$this->lastUsed = microtime(true);
|
||||
$this->base = trim($base, '/');
|
||||
self::validate($this->base, true);
|
||||
$this->atomicPut = $storage->instanceOfStorage(\OCA\Files_External\Lib\Storage\AmazonS3::class);
|
||||
if ($this->base !== '') {
|
||||
$this->sftpReads = $storage->instanceOfStorage(\OCA\Files_External\Lib\Storage\SFTP::class);
|
||||
if ($createBase && $this->base !== '') {
|
||||
$this->mkdirs($this->base);
|
||||
}
|
||||
}
|
||||
@@ -36,6 +51,8 @@ final class StorageBackend implements IBackend {
|
||||
}
|
||||
|
||||
public function put(string $path, $stream): void {
|
||||
// not repeated on a new connection: the stream is (partly) read
|
||||
$this->idleCheck();
|
||||
$expected = self::remaining($stream);
|
||||
$target = $this->abs($path);
|
||||
$this->mkdirs(dirname($target));
|
||||
@@ -79,6 +96,19 @@ final class StorageBackend implements IBackend {
|
||||
}
|
||||
|
||||
public function get(string $path) {
|
||||
return $this->call(fn () => $this->open($path));
|
||||
}
|
||||
|
||||
/** @return resource */
|
||||
private function open(string $path) {
|
||||
if ($this->sftpReads) {
|
||||
$fh = SftpReadStream::open($this->storage, $this->abs($path));
|
||||
if (is_resource($fh)) {
|
||||
return $fh;
|
||||
}
|
||||
// null: not supported or failing for now; false: not opened, which need not mean
|
||||
// missing. Either way the storage's own stream decides.
|
||||
}
|
||||
$fh = $this->storage->fopen($this->abs($path), 'r');
|
||||
if ($fh === false) {
|
||||
throw new BackendException('Not found: ' . $path);
|
||||
@@ -86,7 +116,40 @@ final class StorageBackend implements IBackend {
|
||||
return $fh;
|
||||
}
|
||||
|
||||
/** Over SFTP several at a time (SftpReadStream::readMany()); elsewhere one after the other. */
|
||||
public function getMany(array $paths): \Generator {
|
||||
foreach (array_chunk($paths, 8 * SftpReadStream::BATCH) as $chunk) {
|
||||
$fast = null;
|
||||
if ($this->sftpReads) {
|
||||
$abs = array_map(fn (string $p): string => $this->abs($p), $chunk);
|
||||
$fast = $this->call(fn () => SftpReadStream::readMany($this->storage, $abs));
|
||||
$fast = $fast === null ? null : array_combine($chunk, array_map(fn (string $a): ?string => $fast[$a] ?? null, $abs));
|
||||
}
|
||||
foreach ($chunk as $path) {
|
||||
$data = $fast[$path] ?? null;
|
||||
if ($data === null) {
|
||||
// not read in the batch (or no batches here): the usual way, which also
|
||||
// reports a missing file as such
|
||||
$fh = $this->get($path);
|
||||
try {
|
||||
$data = stream_get_contents($fh);
|
||||
} finally {
|
||||
fclose($fh);
|
||||
}
|
||||
if ($data === false) {
|
||||
throw new BackendException('Read failed for ' . $path);
|
||||
}
|
||||
}
|
||||
yield $path => $data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getRange(string $path, int $offset, int $length): string {
|
||||
return $this->call(fn () => $this->readRange($path, $offset, $length));
|
||||
}
|
||||
|
||||
private function readRange(string $path, int $offset, int $length): string {
|
||||
$fh = $this->get($path);
|
||||
try {
|
||||
if (fseek($fh, $offset) !== 0) {
|
||||
@@ -109,23 +172,40 @@ final class StorageBackend implements IBackend {
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether the storage accepts the connection settings (files_external's own check). */
|
||||
public function canConnect(): bool {
|
||||
try {
|
||||
return !method_exists($this->storage, 'test') || $this->storage->test();
|
||||
} catch (\Throwable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function exists(string $path): bool {
|
||||
return $this->storage->file_exists($this->abs($path));
|
||||
$abs = $this->abs($path);
|
||||
return $this->call(fn () => $this->storage->file_exists($abs), fn (bool $found) => !$found);
|
||||
}
|
||||
|
||||
public function list(string $prefix): array {
|
||||
$result = [];
|
||||
$this->listInto($this->abs($prefix), $result);
|
||||
$abs = $this->abs($prefix);
|
||||
$result = $this->call(function () use ($abs): array {
|
||||
$result = [];
|
||||
$this->listInto($abs, $result);
|
||||
return $result;
|
||||
}, fn (array $found) => $found === []);
|
||||
sort($result);
|
||||
$cut = $this->base === '' ? 0 : strlen($this->base) + 1;
|
||||
return array_map(fn ($p) => substr($p, $cut), $result);
|
||||
}
|
||||
|
||||
public function delete(string $path): void {
|
||||
$this->idleCheck();
|
||||
$this->storage->unlink($this->abs($path));
|
||||
}
|
||||
|
||||
public function move(string $from, string $to): void {
|
||||
// not repeated on a new connection: the server may have done it before the connection died
|
||||
$this->idleCheck();
|
||||
$target = $this->abs($to);
|
||||
$this->mkdirs(dirname($target));
|
||||
if (!$this->storage->rename($this->abs($from), $target)) {
|
||||
@@ -133,6 +213,172 @@ final class StorageBackend implements IBackend {
|
||||
}
|
||||
}
|
||||
|
||||
public function listFiles(string $dir): array {
|
||||
$abs = $this->abs($dir);
|
||||
return $this->call(fn () => $this->filesIn($dir, $abs), fn (array $found) => $found === []);
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
private function filesIn(string $dir, string $abs): array {
|
||||
if (!$this->storage->is_dir($abs)) {
|
||||
return []; // not created yet
|
||||
}
|
||||
$cut = $this->base === '' ? 0 : strlen($this->base) + 1;
|
||||
$names = $this->smbNames($abs);
|
||||
if ($names !== null) {
|
||||
$result = [];
|
||||
foreach ($names as $name) {
|
||||
if (!self::isTemporary($name)) {
|
||||
$result[] = substr($abs . '/' . $name, $cut);
|
||||
}
|
||||
}
|
||||
sort($result);
|
||||
return $result;
|
||||
}
|
||||
$dh = $this->storage->opendir($abs);
|
||||
if ($dh === false) {
|
||||
// An existing folder read as empty would look like a new repository.
|
||||
throw new BackendException('Cannot list ' . $dir);
|
||||
}
|
||||
$result = [];
|
||||
while (($name = readdir($dh)) !== false) {
|
||||
if ($name !== '.' && $name !== '..' && !self::isTemporary($name)) {
|
||||
$result[] = substr($abs . '/' . $name, $cut);
|
||||
}
|
||||
}
|
||||
closedir($dh);
|
||||
sort($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* The names in an SMB folder, read in one listing; null for other storages, with access
|
||||
* control lists on, or when the storage's internals are not as expected. The SMB storage's
|
||||
* opendir() asks the server for the attributes of each entry to leave out hidden ones: with
|
||||
* thousands of index files that took minutes. Hidden entries are still left out: names
|
||||
* starting with a dot, and, after asking, any name a repository does not use (letters,
|
||||
* digits, - and _), such as desktop.ini or Thumbs.db. The repository's own files have such
|
||||
* names: they are not checked one by one, which was the cost.
|
||||
*
|
||||
* @return list<string>|null
|
||||
*/
|
||||
private function smbNames(string $abs): ?array {
|
||||
if (!$this->storage instanceof \OCA\Files_External\Lib\Storage\SMB) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
/** @var list<string>|null */
|
||||
return (function (string $path): ?array {
|
||||
/** @psalm-suppress UndefinedThisPropertyFetch */
|
||||
if ($this->checkAcl) {
|
||||
return null;
|
||||
}
|
||||
$names = [];
|
||||
/** @psalm-suppress UndefinedThisPropertyFetch, UndefinedMethod */
|
||||
foreach ($this->share->dir(ltrim($this->buildPath($path), '/')) as $info) {
|
||||
$name = (string)$info->getName();
|
||||
/** @psalm-suppress UndefinedThisPropertyFetch */
|
||||
if (!$this->showHidden && (str_starts_with($name, '.')
|
||||
|| (preg_match('/^[A-Za-z0-9_-]+$/', $name) !== 1 && $info->isHidden()))) {
|
||||
continue;
|
||||
}
|
||||
$names[] = $name;
|
||||
}
|
||||
return $names;
|
||||
})->call($this->storage, $abs);
|
||||
} catch (\Throwable) {
|
||||
return null; // another SMB storage version, or an error: the usual way reports it
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run $op; when it throws, or $failed says its result may come from a dead connection, and
|
||||
* the SFTP connection turns out to be dead, run it once more on a new connection.
|
||||
*
|
||||
* @template T
|
||||
* @param \Closure():T $op
|
||||
* @param (\Closure(T):bool)|null $failed
|
||||
* @return T
|
||||
*/
|
||||
private function call(\Closure $op, ?\Closure $failed = null): mixed {
|
||||
$this->idleCheck();
|
||||
try {
|
||||
$result = $op();
|
||||
} catch (\Throwable $e) {
|
||||
if (!$this->reopenIfDead()) {
|
||||
throw $e;
|
||||
}
|
||||
return $op();
|
||||
}
|
||||
if ($failed !== null && $failed($result) && $this->reopenIfDead()) {
|
||||
return $op();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* A Hetzner Storage Box drops an SFTP connection that carried a lot of data and then stayed
|
||||
* quiet for a few minutes, while phpseclib still takes it for open; files_external never
|
||||
* builds a new one, and every request on it fails as if the file were missing. So after
|
||||
* such a pause the connection is checked (one round trip) before it is used again.
|
||||
*/
|
||||
private function idleCheck(): void {
|
||||
$now = microtime(true);
|
||||
if ($this->reconnect !== null && $this->sftpReads && $now - $this->lastUsed >= self::IDLE_CHECK
|
||||
&& $this->alive(true) === false) {
|
||||
$this->reopen();
|
||||
}
|
||||
$this->lastUsed = microtime(true);
|
||||
}
|
||||
|
||||
private function reopenIfDead(): bool {
|
||||
if ($this->reconnect === null || !$this->sftpReads || $this->alive(false) !== false) {
|
||||
return false;
|
||||
}
|
||||
$this->reopen();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the storage's SFTP connection is alive; null when it cannot tell.
|
||||
* $ask: send a request instead of trusting what phpseclib saw last.
|
||||
*/
|
||||
private function alive(bool $ask): ?bool {
|
||||
try {
|
||||
if (!is_callable([$this->storage, 'getConnection'])) {
|
||||
return null;
|
||||
}
|
||||
$sftp = $this->storage->getConnection();
|
||||
if (!is_object($sftp) || !is_callable([$sftp, 'isConnected'])) {
|
||||
return null;
|
||||
}
|
||||
if (!$sftp->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
if (!$ask || !is_callable([$sftp, 'stat'])) {
|
||||
return true;
|
||||
}
|
||||
if (is_callable([$sftp, 'clearStatCache'])) {
|
||||
$sftp->clearStatCache(); // or stat() answers from memory
|
||||
}
|
||||
$sftp->stat('.');
|
||||
return (bool)$sftp->isConnected();
|
||||
} catch (\Throwable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function reopen(): void {
|
||||
/** @var \Closure():IStorage $reconnect */
|
||||
$reconnect = $this->reconnect;
|
||||
$this->storage = $reconnect();
|
||||
$this->reconnects++;
|
||||
$this->lastUsed = microtime(true);
|
||||
if (function_exists('OCP\Log\logger')) {
|
||||
\OCP\Log\logger('ng_backup')->info('The SFTP connection to the backup location was lost; reconnected');
|
||||
}
|
||||
}
|
||||
|
||||
private function listInto(string $dir, array &$result): void {
|
||||
if (!$this->storage->is_dir($dir)) {
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// SPDX-FileCopyrightText: 2026 Dillard Blom
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
namespace OCA\NgBackup\Command;
|
||||
|
||||
use OCA\NgBackup\Db\Run;
|
||||
use OCA\NgBackup\Db\RunMapper;
|
||||
use OCA\NgBackup\Service\BackupService;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
final class CancelRun extends Command {
|
||||
public function __construct(
|
||||
private RunMapper $runs,
|
||||
private BackupService $backups,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void {
|
||||
$this->setName('backup:cancel')
|
||||
->setDescription('Stop a running backup')
|
||||
->addArgument('run', InputArgument::REQUIRED, 'Run number, as shown by backup:status')
|
||||
->setHelp("No new step of the run starts; the step in progress finishes first (usually within a
|
||||
"
|
||||
. "minute), so a backup that is just being completed is not cut off. The process working on the
|
||||
"
|
||||
. "run (occ backup:run, or cron) then stops. A backup started by an earlier version is marked
|
||||
"
|
||||
. "cancelled after two minutes and stops at the end of its step. What it already stored is
|
||||
"
|
||||
. "removed by the next cleanup.");
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$id = (string)$input->getArgument('run');
|
||||
if (!ctype_digit($id)) {
|
||||
$output->writeln('<error>Give the run number, as shown by backup:status.</error>');
|
||||
return 1;
|
||||
}
|
||||
try {
|
||||
$run = $this->runs->find((int)$id);
|
||||
} catch (DoesNotExistException) {
|
||||
$output->writeln("<error>There is no run $id.</error>");
|
||||
return 1;
|
||||
}
|
||||
if ($run->getKind() !== BackupService::KIND_FULL) {
|
||||
$output->writeln("<error>Run $id is not a backup; only backups can be cancelled.</error>");
|
||||
return 1;
|
||||
}
|
||||
if ($run->getStatus() === Run::RUNNING) {
|
||||
$output->writeln("Waiting for the current step of run $id to finish...");
|
||||
}
|
||||
try {
|
||||
$run = $this->backups->cancel($run);
|
||||
} catch (\RuntimeException $e) {
|
||||
$output->writeln('<error>' . $e->getMessage() . '</error>');
|
||||
return 1;
|
||||
}
|
||||
if ($run->getStatus() !== Run::CANCELLED) {
|
||||
$output->writeln("Run $id is not running (status: {$run->getStatus()}); nothing to cancel.");
|
||||
return 0;
|
||||
}
|
||||
$output->writeln("Run $id is cancelled.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
+27
-3
@@ -10,6 +10,7 @@ namespace OCA\NgBackup\Command;
|
||||
use OCA\NgBackup\Db\RunMapper;
|
||||
use OCA\NgBackup\Service\KeyService;
|
||||
use OCA\NgBackup\Service\TargetService;
|
||||
use OCA\NgBackup\Service\VerifyService;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Helper\Table;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
@@ -20,6 +21,7 @@ final class Status extends Command {
|
||||
private KeyService $keys,
|
||||
private TargetService $targets,
|
||||
private RunMapper $runs,
|
||||
private VerifyService $verify,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
@@ -44,13 +46,35 @@ final class Status extends Command {
|
||||
$rows = [];
|
||||
foreach ($this->runs->findRecent(10) as $r) {
|
||||
$s = json_decode($r->getStats() ?? '{}', true)['files'] ?? [];
|
||||
$rows[] = [$r->getId(), $names[$r->getTargetId()] ?? '#' . $r->getTargetId(), $r->getKind(), $r->getStatus() . ($r->getStatus() === 'running' ? ' (' . $r->getPhase() . ')' : ''),
|
||||
$rows[] = [$r->getId(), $names[$r->getTargetId()] ?? '#' . $r->getTargetId(), $r->getKind(), $r->getStatus() . ($r->getStatus() === 'running' ? ' (' . $r->getPhase() . self::percent($r) . ')' : ''),
|
||||
date('Y-m-d H:i', $r->getStartedAt()), $r->getFinishedAt() ? gmdate('H:i:s', $r->getFinishedAt() - $r->getStartedAt()) : '',
|
||||
$s['files'] ?? '', isset($s['uploaded']) ? sprintf('%.1f MiB', $s['uploaded'] / 1048576) : '', mb_strimwidth((string)$r->getError(), 0, 50, '…')];
|
||||
$s['files'] ?? '', isset($s['bytes']) ? self::size((int)$s['bytes']) : '',
|
||||
isset($s['uploaded']) ? self::size((int)$s['uploaded']) : '', mb_strimwidth((string)$r->getError(), 0, 50, '…')];
|
||||
}
|
||||
if ($rows !== []) {
|
||||
(new Table($output))->setHeaders(['run', 'location', 'kind', 'status', 'started', 'took', 'files', 'uploaded', 'error'])->setRows($rows)->render();
|
||||
(new Table($output))->setHeaders(['run', 'location', 'kind', 'status', 'started', 'took', 'files', 'size', 'uploaded', 'error'])->setRows($rows)->render();
|
||||
}
|
||||
foreach ($this->verify->progress() as $targetId => $state) {
|
||||
$output->writeln(sprintf('Verify of %s on %s: %s', mb_strimwidth((string)($state['what'] ?? ''), 0, 40, '…'),
|
||||
$names[$targetId] ?? '#' . $targetId, VerifyService::describe($state)));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** " 68%", or " ~68%" when estimated from the file cache; empty without an estimate */
|
||||
private static function size(int $bytes): string {
|
||||
$b = (float)$bytes;
|
||||
foreach (['B', 'KiB', 'MiB', 'GiB'] as $u) {
|
||||
if ($b < 1024) {
|
||||
return $u === 'B' ? "$bytes B" : sprintf('%.1f %s', $b, $u);
|
||||
}
|
||||
$b /= 1024.0;
|
||||
}
|
||||
return sprintf('%.1f TiB', $b);
|
||||
}
|
||||
|
||||
private static function percent(\OCA\NgBackup\Db\Run $r): string {
|
||||
$p = \OCA\NgBackup\Job\BackupRun::progressOf($r);
|
||||
return $p === null ? '' : sprintf(' %s%d%%', $p['approximate'] ? '~' : '', (int)floor($p['fraction'] * 100.0));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// SPDX-FileCopyrightText: 2026 Dillard Blom
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
namespace OCA\NgBackup\Command;
|
||||
|
||||
use OCA\NgBackup\Service\AlertService;
|
||||
use OCA\NgBackup\Service\TargetService;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
final class TargetEdit extends Command {
|
||||
public function __construct(
|
||||
private TargetService $targets,
|
||||
private AlertService $alerts,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void {
|
||||
$this->setName('backup:target:edit')
|
||||
->setDescription('Change the connection settings or the name of a backup location, keeping its backups')
|
||||
->addArgument('target', InputArgument::REQUIRED, 'Location name or id')
|
||||
->addOption('name', null, InputOption::VALUE_REQUIRED, 'New name')
|
||||
->addOption('backend', 'b', InputOption::VALUE_REQUIRED, 'New files_external backend id (see backup:target:backends)')
|
||||
->addOption('auth', 'a', InputOption::VALUE_REQUIRED, 'New auth mechanism id')
|
||||
->addOption('option', 'o', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Option key=value to change (repeatable); other options keep their value')
|
||||
->addOption('path', 'p', InputOption::VALUE_REQUIRED, 'New folder on the location')
|
||||
->setHelp(<<<'HELP'
|
||||
The new settings must lead to the same repository as before, so the history of the location is
|
||||
kept. Use it after a password change, a new host name, or after moving the repository.
|
||||
|
||||
Example:
|
||||
occ backup:target:edit nas -o password=new-secret
|
||||
HELP);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
try {
|
||||
$t = $this->targets->get($input->getArgument('target'));
|
||||
$options = $this->targets->options($t);
|
||||
if ($input->getOption('backend') !== null || $input->getOption('auth') !== null) {
|
||||
$options = []; // a different connection type has its own options
|
||||
}
|
||||
foreach ($input->getOption('option') as $pair) {
|
||||
if (!str_contains($pair, '=')) {
|
||||
$output->writeln("<error>Option '$pair' must be key=value</error>");
|
||||
return 1;
|
||||
}
|
||||
$k = (string)strstr($pair, '=', true);
|
||||
$v = substr($pair, strlen($k) + 1);
|
||||
$options[$k] = match (strtolower($v)) { 'true' => true, 'false' => false, default => $v };
|
||||
}
|
||||
$before = $t->getName();
|
||||
$t = $this->targets->update($t, $input->getOption('name') ?? $t->getName(), $input->getOption('backend') ?? $t->getBackend(),
|
||||
$input->getOption('auth') ?? $t->getAuth(), $options, $input->getOption('path') ?? $t->getBasePath());
|
||||
} catch (\Throwable $e) {
|
||||
$output->writeln('<error>' . $e->getMessage() . '</error>');
|
||||
return 1;
|
||||
}
|
||||
$this->alerts->securityEvent('target_changed', ['target' => $before]);
|
||||
$output->writeln(sprintf('Location <info>%s</info> updated; repository %s found with the new settings.', $t->getName(), (string)$t->getRepositoryId()));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
+15
-3
@@ -37,6 +37,9 @@ blob is also downloaded and decrypted, which fails loudly on exactly that even i
|
||||
lists it -- the same verification an actual restore would do, run here deliberately instead of
|
||||
waiting to find out during one.
|
||||
|
||||
While it runs it prints how far it is every 30 seconds; occ backup:status shows the same, and
|
||||
how the last check of each location ended.
|
||||
|
||||
Run this periodically (cron, picking a sample of recent snapshots) rather than only right before
|
||||
you'd need to rely on a restore.
|
||||
|
||||
@@ -51,9 +54,12 @@ HELP);
|
||||
$target = $this->targets->get($input->getArgument('target'));
|
||||
$ref = (string)$input->getArgument('snapshot');
|
||||
$deep = (bool)$input->getOption('deep');
|
||||
$onProgress = static function (array $state) use ($output): void {
|
||||
$output->writeln(date('H:i:s') . ' ' . VerifyService::describe($state));
|
||||
};
|
||||
$result = str_starts_with($ref, 'users/')
|
||||
? $this->verify->verifyUserExport($target, $ref, $deep)
|
||||
: $this->verify->verify($target, $ref, $deep);
|
||||
? $this->verify->verifyUserExport($target, $ref, $deep, $onProgress)
|
||||
: $this->verify->verify($target, $ref, $deep, $onProgress);
|
||||
} catch (\Throwable $e) {
|
||||
$output->writeln('<error>' . $e->getMessage() . '</error>');
|
||||
return 1;
|
||||
@@ -66,7 +72,13 @@ HELP);
|
||||
if ($result['failed'] !== []) {
|
||||
$output->writeln('<error>' . count($result['failed']) . ' blob(s) failed verification (corrupted or tampered):</error> ' . implode(', ', $result['failed']));
|
||||
}
|
||||
if ($result['missing'] === [] && $result['failed'] === []) {
|
||||
if ($result['unreadable'] !== []) {
|
||||
$output->writeln('<error>' . count($result['unreadable']) . ' blob(s) could not be read from the location</error> (often a temporary storage '
|
||||
. 'or network error: run the check again; if the same blobs stay unreadable, their pack may be damaged or truncated): '
|
||||
. implode(', ', $result['unreadable']));
|
||||
$output->writeln('Last read error: ' . $result['readError']);
|
||||
}
|
||||
if ($result['missing'] === [] && $result['failed'] === [] && $result['unreadable'] === []) {
|
||||
$output->writeln('<info>OK</info>');
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -181,6 +181,28 @@ final class AdminApiController extends Controller {
|
||||
return new JSONResponse(['id' => $r['target']->getId(), 'created' => $r['created']]);
|
||||
}
|
||||
|
||||
/** The stored settings for the edit form; password values are left out (null). */
|
||||
#[PasswordConfirmationRequired]
|
||||
public function targetSettings(int $id): JSONResponse {
|
||||
$t = $this->targets->get((string)$id);
|
||||
return new JSONResponse(['name' => $t->getName(), 'backend' => $t->getBackend(), 'auth' => $t->getAuth(),
|
||||
'options' => $this->targets->editableOptions($t), 'path' => $t->getBasePath()]);
|
||||
}
|
||||
|
||||
/** Options with a null value keep their stored value. */
|
||||
#[PasswordConfirmationRequired]
|
||||
public function updateTarget(int $id, string $name, string $backend, string $auth, array $options = [], string $path = 'ng_backup'): JSONResponse {
|
||||
try {
|
||||
$t = $this->targets->get((string)$id);
|
||||
$before = $t->getName();
|
||||
$t = $this->targets->update($t, $name, $backend, $auth, $options, $path);
|
||||
} catch (\Throwable $e) {
|
||||
return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
|
||||
}
|
||||
$this->alerts->securityEvent('target_changed', ['target' => $before]);
|
||||
return new JSONResponse(['id' => $t->getId()]);
|
||||
}
|
||||
|
||||
/** Size limit in GiB; null or 0 = unlimited. */
|
||||
#[PasswordConfirmationRequired]
|
||||
public function setTargetLimit(int $id, ?float $maxGb = null): JSONResponse {
|
||||
@@ -283,6 +305,6 @@ final class AdminApiController extends Controller {
|
||||
return ['id' => $r->getId(), 'targetId' => $r->getTargetId(), 'kind' => $r->getKind(), 'status' => $r->getStatus(),
|
||||
'phase' => $r->getPhase(), 'snapshotId' => $r->getSnapshotId(), 'snapshot' => $snapshot, 'startedAt' => $r->getStartedAt(),
|
||||
'updatedAt' => $r->getUpdatedAt(), 'finishedAt' => $r->getFinishedAt(), 'error' => $r->getError(),
|
||||
'stats' => json_decode($r->getStats() ?? 'null', true)];
|
||||
'stats' => json_decode($r->getStats() ?? 'null', true), 'progress' => \OCA\NgBackup\Job\BackupRun::progressOf($r)];
|
||||
}
|
||||
}
|
||||
|
||||
+43
-36
@@ -72,50 +72,57 @@ final class DbDumper {
|
||||
public function dumpStep(Repository $repo, array $state, float $deadline, array &$stats): array {
|
||||
$state['steps']++;
|
||||
$sm = $this->db->createSchemaManager();
|
||||
$this->db->setTransactionIsolation(TransactionIsolationLevel::REPEATABLE_READ);
|
||||
$this->db->beginTransaction();
|
||||
// A pack that fills up during the dump is uploaded right away; its index cache row must
|
||||
// wait until the read-only transaction is over.
|
||||
$repo->holdIndexCache();
|
||||
try {
|
||||
if ($this->db->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) {
|
||||
$this->db->executeStatement('SET TRANSACTION READ ONLY');
|
||||
}
|
||||
while ($state['i'] < count($state['tables'])) {
|
||||
$table = $state['tables'][$state['i']];
|
||||
$schema = $sm->introspectTable($table);
|
||||
$writer = $repo->blobWriter($stats);
|
||||
$hash = $state['hash'] !== null ? unserialize(base64_decode($state['hash']), ['allowed_classes' => [\HashContext::class]]) : hash_init('sha256');
|
||||
if ($state['after'] === null && $state['segments'] === []) {
|
||||
$writer->write($this->header($schema) . "\n");
|
||||
$this->db->setTransactionIsolation(TransactionIsolationLevel::REPEATABLE_READ);
|
||||
$this->db->beginTransaction();
|
||||
try {
|
||||
if ($this->db->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) {
|
||||
$this->db->executeStatement('SET TRANSACTION READ ONLY');
|
||||
}
|
||||
$complete = true;
|
||||
foreach ($this->rowLines($schema, $state['after']) as [$key, $line]) {
|
||||
$writer->write($line . "\n");
|
||||
hash_update($hash, $line . "\n");
|
||||
$state['rows']++;
|
||||
$state['after'] = $key;
|
||||
if (microtime(true) >= $deadline) {
|
||||
$complete = false;
|
||||
while ($state['i'] < count($state['tables'])) {
|
||||
$table = $state['tables'][$state['i']];
|
||||
$schema = $sm->introspectTable($table);
|
||||
$writer = $repo->blobWriter($stats);
|
||||
$hash = $state['hash'] !== null ? unserialize(base64_decode($state['hash']), ['allowed_classes' => [\HashContext::class]]) : hash_init('sha256');
|
||||
if ($state['after'] === null && $state['segments'] === []) {
|
||||
$writer->write($this->header($schema) . "\n");
|
||||
}
|
||||
$complete = true;
|
||||
foreach ($this->rowLines($schema, $state['after']) as [$key, $line]) {
|
||||
$writer->write($line . "\n");
|
||||
hash_update($hash, $line . "\n");
|
||||
$state['rows']++;
|
||||
$state['after'] = $key;
|
||||
if (microtime(true) >= $deadline) {
|
||||
$complete = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$state['segments'][] = $writer->finish();
|
||||
$state['bytes'] += $writer->bytes();
|
||||
if (!$complete) {
|
||||
$state['hash'] = base64_encode(serialize($hash));
|
||||
break;
|
||||
}
|
||||
$state['result'][$table] = ['blobs' => array_merge(...$state['segments']), 'rows' => $state['rows'],
|
||||
'bytes' => $state['bytes'], 'sha256' => hash_final($hash)];
|
||||
$state = array_merge($state, ['i' => $state['i'] + 1, 'after' => null, 'segments' => [], 'rows' => 0, 'bytes' => 0, 'hash' => null]);
|
||||
if (microtime(true) >= $deadline && $state['i'] < count($state['tables'])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$state['segments'][] = $writer->finish();
|
||||
$state['bytes'] += $writer->bytes();
|
||||
if (!$complete) {
|
||||
$state['hash'] = base64_encode(serialize($hash));
|
||||
break;
|
||||
if ($state['i'] >= count($state['tables'])) {
|
||||
$state['sequences'] = $this->sequenceStates($state['tables']);
|
||||
$state['done'] = true;
|
||||
}
|
||||
$state['result'][$table] = ['blobs' => array_merge(...$state['segments']), 'rows' => $state['rows'],
|
||||
'bytes' => $state['bytes'], 'sha256' => hash_final($hash)];
|
||||
$state = array_merge($state, ['i' => $state['i'] + 1, 'after' => null, 'segments' => [], 'rows' => 0, 'bytes' => 0, 'hash' => null]);
|
||||
if (microtime(true) >= $deadline && $state['i'] < count($state['tables'])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($state['i'] >= count($state['tables'])) {
|
||||
$state['sequences'] = $this->sequenceStates($state['tables']);
|
||||
$state['done'] = true;
|
||||
} finally {
|
||||
$this->db->rollBack();
|
||||
}
|
||||
} finally {
|
||||
$this->db->rollBack();
|
||||
$repo->releaseIndexCache();
|
||||
}
|
||||
$repo->flushPacks();
|
||||
return $state;
|
||||
|
||||
+72
-7
@@ -30,19 +30,84 @@ final class DbIndexCache implements IIndexCache {
|
||||
}
|
||||
|
||||
public function put(string $repositoryId, string $packId, array $entries): void {
|
||||
try {
|
||||
$this->insert($repositoryId, $packId, $entries);
|
||||
} catch (Exception $e) {
|
||||
if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* In one transaction: on PostgreSQL with its default settings every statement of its own
|
||||
* waits for a disk sync, which made 27,547 rows take more than 6 minutes. Rows already there
|
||||
* are replaced. Should the transaction fail (another process writing the same rows), the
|
||||
* rows are written one by one, keeping rows that are already there.
|
||||
*/
|
||||
public function putMany(string $repositoryId, array $packs): void {
|
||||
if ($packs === []) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
$this->transaction(function () use ($repositoryId, $packs): void {
|
||||
$this->remove($repositoryId, array_keys($packs));
|
||||
$this->insertAll($repositoryId, $packs);
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
if ($this->db->inTransaction()) {
|
||||
throw $e; // a transaction of the caller's, which may be spoilt now
|
||||
}
|
||||
foreach ($packs as $packId => $entries) {
|
||||
$this->put($repositoryId, $packId, $entries);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** All or nothing: when it fails, the cache stays as it was. */
|
||||
public function replaceAll(string $repositoryId, array $packs): void {
|
||||
$this->transaction(function () use ($repositoryId, $packs): void {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->delete('ngb_index')->where($qb->expr()->eq('repository_id', $qb->createNamedParameter($repositoryId)));
|
||||
$qb->executeStatement();
|
||||
$this->insertAll($repositoryId, $packs);
|
||||
});
|
||||
}
|
||||
|
||||
/** Run $write in a transaction of its own, or as part of the caller's when there is one. */
|
||||
private function transaction(\Closure $write): void {
|
||||
if ($this->db->inTransaction()) {
|
||||
$write();
|
||||
return;
|
||||
}
|
||||
$this->db->beginTransaction();
|
||||
try {
|
||||
$write();
|
||||
$this->db->commit();
|
||||
} catch (\Throwable $e) {
|
||||
if ($this->db->inTransaction()) {
|
||||
$this->db->rollBack();
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/** @param array<string, list<array{id:string, offset:int, length:int, raw:int, flags:int}>> $packs */
|
||||
private function insertAll(string $repositoryId, array $packs): void {
|
||||
foreach ($packs as $packId => $entries) {
|
||||
$this->insert($repositoryId, $packId, $entries);
|
||||
}
|
||||
}
|
||||
|
||||
/** @param list<array{id:string, offset:int, length:int, raw:int, flags:int}> $entries */
|
||||
private function insert(string $repositoryId, string $packId, array $entries): void {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->insert('ngb_index')->values([
|
||||
'repository_id' => $qb->createNamedParameter($repositoryId),
|
||||
'pack_id' => $qb->createNamedParameter($packId),
|
||||
'entries' => $qb->createNamedParameter(json_encode($entries, JSON_THROW_ON_ERROR)),
|
||||
]);
|
||||
try {
|
||||
$qb->executeStatement();
|
||||
} catch (Exception $e) {
|
||||
if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
$qb->executeStatement();
|
||||
}
|
||||
|
||||
public function remove(string $repositoryId, array $packIds): void {
|
||||
|
||||
@@ -17,6 +17,36 @@ final class RunMapper extends QBMapper {
|
||||
parent::__construct($db, 'ngb_runs', Run::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write $run only if it is still running and unchanged since it was loaded (updated_at
|
||||
* $loadedAt), in one statement: two processes that loaded the same run cannot both write it.
|
||||
* The new updated_at is always later than $loadedAt, so every write is seen as a change.
|
||||
*
|
||||
* @return Run|null the written run, or null when another process wrote it first
|
||||
*/
|
||||
public function updateIfUnchanged(Run $run, int $loadedAt): ?Run {
|
||||
$run->setUpdatedAt(max(time(), $loadedAt + 1));
|
||||
$types = $run->getFieldTypes();
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->update($this->getTableName());
|
||||
foreach (array_keys($run->getUpdatedFields()) as $field) {
|
||||
if ($field === 'id') {
|
||||
continue;
|
||||
}
|
||||
$value = $run->{'get' . ucfirst($field)}();
|
||||
$type = ($types[$field] ?? 'string') === 'integer' ? IQueryBuilder::PARAM_INT : IQueryBuilder::PARAM_STR;
|
||||
$qb->set($run->propertyToColumn($field), $qb->createNamedParameter($value, $value === null ? IQueryBuilder::PARAM_NULL : $type));
|
||||
}
|
||||
$qb->where($qb->expr()->eq('id', $qb->createNamedParameter($run->getId(), IQueryBuilder::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter(Run::RUNNING)))
|
||||
->andWhere($qb->expr()->eq('updated_at', $qb->createNamedParameter($loadedAt, IQueryBuilder::PARAM_INT)));
|
||||
if ($qb->executeStatement() !== 1) {
|
||||
return null;
|
||||
}
|
||||
$run->resetUpdatedFields();
|
||||
return $run;
|
||||
}
|
||||
|
||||
public function find(int $id): Run {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->select('*')->from($this->getTableName())->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
|
||||
|
||||
@@ -160,6 +160,38 @@ final class BackupRun {
|
||||
return $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Progress of the files phase as a fraction (at most 0.99 until it is done), or null without an
|
||||
* estimate. Bytes of a large file that is still being read count too.
|
||||
*
|
||||
* @return array{fraction: float, approximate: bool}|null
|
||||
*/
|
||||
public static function progress(array $state): ?array {
|
||||
$estimate = $state['estimate'] ?? null;
|
||||
if (($state['phase'] ?? null) !== 'files' || !is_array($estimate) || ($estimate['bytes'] ?? 0) <= 0) {
|
||||
return null;
|
||||
}
|
||||
$done = (int)($state['stats']['bytes'] ?? 0) + (int)($state['cur']['offset'] ?? 0);
|
||||
return ['fraction' => min(0.99, $done / (int)$estimate['bytes']), 'approximate' => (bool)($estimate['approximate'] ?? true)];
|
||||
}
|
||||
|
||||
/**
|
||||
* progress() of a run, while it is in its files phase.
|
||||
*
|
||||
* @return array{fraction: float, approximate: bool}|null
|
||||
*/
|
||||
public static function progressOf(\OCA\NgBackup\Db\Run $run): ?array {
|
||||
if ($run->getStatus() !== \OCA\NgBackup\Db\Run::RUNNING || $run->getPhase() !== 'files') {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
$state = json_decode($run->getState(), true, 512, JSON_THROW_ON_ERROR);
|
||||
} catch (\JsonException) {
|
||||
return null;
|
||||
}
|
||||
return is_array($state['files'] ?? null) ? self::progress($state['files']) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A file or directory that could not be read: it is missing from this snapshot. Remembered (first
|
||||
* 1000, deduplicated across steps) so the run can report it instead of silently succeeding.
|
||||
|
||||
@@ -51,7 +51,10 @@ final class Notifier implements INotifier {
|
||||
$notification->setParsedMessage($l->t('Backups stop when the limit is reached. Raise the limit, or let the retention policy remove older backups.'));
|
||||
break;
|
||||
case 'backup_stale':
|
||||
$notification->setParsedSubject($l->t('No successful backup to %1$s since %2$s', [$p['target'], $p['since']]));
|
||||
// 'never' was stored by 0.9.4 and earlier
|
||||
$notification->setParsedSubject(in_array($p['since'] ?? '', ['', 'never'], true)
|
||||
? $l->t('No successful backup to %s yet', [$p['target']])
|
||||
: $l->t('No successful backup to %1$s since %2$s', [$p['target'], $p['since']]));
|
||||
$notification->setParsedMessage($l->t('Check the backup settings and the server log.'));
|
||||
break;
|
||||
case 'security_event':
|
||||
@@ -59,6 +62,7 @@ final class Notifier implements INotifier {
|
||||
$notification->setParsedSubject(match ($p['event'] ?? '') {
|
||||
'kit_downloaded' => $l->t('%s downloaded the backup recovery kit', [$who]),
|
||||
'target_removed' => $l->t('%1$s removed backup location %2$s', [$who, $p['target'] ?? '']),
|
||||
'target_changed' => $l->t('%1$s changed the connection settings of backup location %2$s', [$who, $p['target'] ?? '']),
|
||||
'append_only_off' => $l->t('%1$s turned off append-only for backup location %2$s', [$who, $p['target'] ?? '']),
|
||||
'retention_changed' => $l->t('%1$s changed the backup retention to %2$s', [$who, $p['policy'] ?? '']),
|
||||
'snapshots_forgotten' => $l->t('%1$s moved %2$s snapshots of %3$s to the trash; they are deleted permanently after %4$s unless restored', [$who, $p['count'] ?? '', $p['target'] ?? '', $p['until'] ?? '']),
|
||||
|
||||
+106
-25
@@ -25,7 +25,11 @@ final class BlobIndex {
|
||||
private array $pending = [];
|
||||
|
||||
private ?IIndexCache $cache = null;
|
||||
/** @var list<\Closure():void>|null cache writes held back by holdCache(), or null when writing directly */
|
||||
private ?array $heldCacheWrites = null;
|
||||
private string $repositoryId = '';
|
||||
/** Index files written to the cache per database transaction while loading. */
|
||||
private const CACHE_LOT = 256;
|
||||
/** @var int index files downloaded by the last load() (for tests and diagnostics) */
|
||||
public int $downloaded = 0;
|
||||
|
||||
@@ -40,52 +44,120 @@ final class BlobIndex {
|
||||
$this->repositoryId = $repositoryId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hold back cache writes until releaseCache(). The database dump reads inside a read-only
|
||||
* transaction on the same connection that the cache writes to: a write there fails on
|
||||
* PostgreSQL and would be rolled back with the dump transaction elsewhere.
|
||||
*/
|
||||
public function holdCache(): void {
|
||||
$this->heldCacheWrites ??= [];
|
||||
}
|
||||
|
||||
/** Write the cache changes held back since holdCache(). */
|
||||
public function releaseCache(): void {
|
||||
$writes = $this->heldCacheWrites ?? [];
|
||||
$this->heldCacheWrites = null;
|
||||
foreach ($writes as $write) {
|
||||
$write();
|
||||
}
|
||||
}
|
||||
|
||||
/** @param list<array{id:string, offset:int, length:int, raw:int, flags:int}> $entries */
|
||||
private function cachePut(string $packId, array $entries): void {
|
||||
if ($this->heldCacheWrites !== null) {
|
||||
$this->heldCacheWrites[] = function () use ($packId, $entries): void {
|
||||
$this->cache?->put($this->repositoryId, $packId, $entries);
|
||||
};
|
||||
return;
|
||||
}
|
||||
$this->cache?->put($this->repositoryId, $packId, $entries);
|
||||
}
|
||||
|
||||
/** @param list<string> $packIds */
|
||||
private function cacheRemove(array $packIds): void {
|
||||
if ($this->heldCacheWrites !== null) {
|
||||
$this->heldCacheWrites[] = function () use ($packIds): void {
|
||||
$this->cache?->remove($this->repositoryId, $packIds);
|
||||
};
|
||||
return;
|
||||
}
|
||||
$this->cache?->remove($this->repositoryId, $packIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the index: one listing of index/, entries from the local cache where available,
|
||||
* and only the index files that are not cached yet are downloaded. Cached packs that no
|
||||
* longer exist on the backend are dropped from the cache.
|
||||
*/
|
||||
public function load(bool $verify = false): void {
|
||||
public function load(bool $verify = false, ?callable $progress = null): void {
|
||||
$this->blobs = [];
|
||||
$cached = $verify ? [] : ($this->cache?->all($this->repositoryId) ?? []);
|
||||
$present = [];
|
||||
$this->downloaded = 0;
|
||||
foreach ($this->backend->list('index') as $path) {
|
||||
$order = []; // pack ids in listing order: where a blob is in two packs, the last listed wins
|
||||
$fetch = [];
|
||||
foreach ($this->backend->listFiles('index') as $path) {
|
||||
$packId = basename($path);
|
||||
$present[$packId] = true;
|
||||
if (isset($cached[$packId])) {
|
||||
$entries = $cached[$packId];
|
||||
} else {
|
||||
$fh = $this->backend->get($path);
|
||||
$entries = json_decode($this->cipher->decryptString((string)stream_get_contents($fh), 'index:' . $packId), true, 512, JSON_THROW_ON_ERROR);
|
||||
fclose($fh);
|
||||
$this->downloaded++;
|
||||
$this->cache?->put($this->repositoryId, $packId, $entries);
|
||||
}
|
||||
foreach ($entries as $e) {
|
||||
$this->blobs[$e['id']] = [$packId, $e['offset'], $e['length'], $e['raw'], $e['flags']];
|
||||
$order[] = $packId;
|
||||
if (!isset($cached[$packId])) {
|
||||
$fetch[] = $path;
|
||||
}
|
||||
}
|
||||
if ($verify && $this->cache !== null) {
|
||||
// Refresh the cache from what was just verified.
|
||||
$this->cache->remove($this->repositoryId, array_keys($this->cache->all($this->repositoryId)));
|
||||
foreach ($this->packs() as $packId => $_) {
|
||||
$entries = [];
|
||||
foreach ($this->blobs as $id => [$pack, $offset, $length, $raw, $flags]) {
|
||||
if ($pack === $packId) {
|
||||
$entries[] = ['id' => $id, 'offset' => $offset, 'length' => $length, 'raw' => $raw, 'flags' => $flags];
|
||||
}
|
||||
// The rest is downloaded several at a time where the location allows it: on a large
|
||||
// repository these are tens of thousands of small files. getMany() keeps their order, so
|
||||
// the cached packs listed before each one are added first.
|
||||
$next = 0;
|
||||
$total = count($fetch);
|
||||
$toCache = []; // written to the cache a few hundred at a time, each lot in one transaction
|
||||
foreach ($this->backend->getMany($fetch) as $path => $data) {
|
||||
$packId = basename($path);
|
||||
$entries = json_decode($this->cipher->decryptString($data, 'index:' . $packId), true, 512, JSON_THROW_ON_ERROR);
|
||||
$this->downloaded++;
|
||||
if ($this->cache !== null) {
|
||||
$toCache[$packId] = $entries;
|
||||
// a check writes the whole cache afresh at the end
|
||||
if (!$verify && count($toCache) >= self::CACHE_LOT) {
|
||||
$this->cache->putMany($this->repositoryId, $toCache);
|
||||
$toCache = [];
|
||||
}
|
||||
$this->cache->put($this->repositoryId, $packId, $entries);
|
||||
}
|
||||
for (; $next < count($order) && $order[$next] !== $packId; $next++) {
|
||||
$this->add($order[$next], $cached[$order[$next]] ?? []);
|
||||
}
|
||||
$this->add($packId, $entries);
|
||||
$next++;
|
||||
if ($progress !== null) {
|
||||
$progress($this->downloaded, $total);
|
||||
}
|
||||
}
|
||||
for (; $next < count($order); $next++) {
|
||||
$this->add($order[$next], $cached[$order[$next]] ?? []);
|
||||
}
|
||||
if ($verify && $this->cache !== null) {
|
||||
// Refresh the cache from what was just verified, as the index files list it: a blob in
|
||||
// two packs is in both rows, so that removing one of the packs leaves the other right.
|
||||
// In one transaction: one row at a time took more than 6 minutes on a 1 TB repository
|
||||
// (27,547 packs), long enough for the location to drop the connection.
|
||||
$this->cache->replaceAll($this->repositoryId, $toCache);
|
||||
return;
|
||||
}
|
||||
if ($toCache !== []) {
|
||||
$this->cache?->putMany($this->repositoryId, $toCache);
|
||||
}
|
||||
$stale = array_keys(array_diff_key($cached, $present));
|
||||
if ($stale !== []) {
|
||||
$this->cache?->remove($this->repositoryId, $stale);
|
||||
}
|
||||
}
|
||||
|
||||
/** @param list<array{id:string, offset:int, length:int, raw:int, flags:int}> $entries */
|
||||
private function add(string $packId, array $entries): void {
|
||||
foreach ($entries as $e) {
|
||||
$this->blobs[$e['id']] = [$packId, $e['offset'], $e['length'], $e['raw'], $e['flags']];
|
||||
}
|
||||
}
|
||||
|
||||
public function has(string $id): bool {
|
||||
return isset($this->blobs[$id]) || isset($this->pending[$id]);
|
||||
}
|
||||
@@ -112,7 +184,7 @@ final class BlobIndex {
|
||||
fwrite($stream, $this->cipher->encryptString(json_encode($entries, JSON_THROW_ON_ERROR), 'index:' . $packId));
|
||||
rewind($stream);
|
||||
$this->backend->put('index/' . $packId, $stream);
|
||||
$this->cache?->put($this->repositoryId, $packId, $entries);
|
||||
$this->cachePut($packId, $entries);
|
||||
foreach ($entries as $e) {
|
||||
$this->blobs[$e['id']] = [$packId, $e['offset'], $e['length'], $e['raw'], $e['flags']];
|
||||
unset($this->pending[$e['id']]);
|
||||
@@ -131,7 +203,7 @@ final class BlobIndex {
|
||||
/** Forget a pack: delete its index file and cache row. Blobs that now live elsewhere are kept. */
|
||||
public function removePack(string $packId): void {
|
||||
$this->backend->delete('index/' . $packId);
|
||||
$this->cache?->remove($this->repositoryId, [$packId]);
|
||||
$this->cacheRemove([$packId]);
|
||||
foreach ($this->blobs as $id => $loc) {
|
||||
if ($loc[0] === $packId) {
|
||||
unset($this->blobs[$id]);
|
||||
@@ -139,6 +211,15 @@ final class BlobIndex {
|
||||
}
|
||||
}
|
||||
|
||||
/** Number of packs that hold blobs of the index. */
|
||||
public function packCount(): int {
|
||||
$packs = [];
|
||||
foreach ($this->blobs as [$pack]) {
|
||||
$packs[$pack] = true;
|
||||
}
|
||||
return count($packs);
|
||||
}
|
||||
|
||||
/** Bytes of all blobs in the index (encrypted size, i.e. what is stored in packs). */
|
||||
public function storedBytes(): int {
|
||||
$total = 0;
|
||||
|
||||
@@ -128,7 +128,7 @@ final class Catalog {
|
||||
|
||||
/** @return list<int> */
|
||||
private function generations(): array {
|
||||
return array_map(fn ($p) => (int)basename($p), $this->backend->list('catalog'));
|
||||
return array_map(fn ($p) => (int)basename($p), $this->backend->listFiles('catalog'));
|
||||
}
|
||||
|
||||
private function path(int $gen): string {
|
||||
|
||||
@@ -15,6 +15,20 @@ interface IIndexCache {
|
||||
/** @param list<array{id:string, offset:int, length:int, raw:int, flags:int}> $entries */
|
||||
public function put(string $repositoryId, string $packId, array $entries): void;
|
||||
|
||||
/**
|
||||
* put() for many packs at once (one database transaction).
|
||||
*
|
||||
* @param array<string, list<array{id:string, offset:int, length:int, raw:int, flags:int}>> $packs packId => entries
|
||||
*/
|
||||
public function putMany(string $repositoryId, array $packs): void;
|
||||
|
||||
/**
|
||||
* Replace everything cached for the repository with $packs (one database transaction).
|
||||
*
|
||||
* @param array<string, list<array{id:string, offset:int, length:int, raw:int, flags:int}>> $packs packId => entries
|
||||
*/
|
||||
public function replaceAll(string $repositoryId, array $packs): void;
|
||||
|
||||
/** @param list<string> $packIds */
|
||||
public function remove(string $repositoryId, array $packIds): void;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// SPDX-FileCopyrightText: 2026 Dillard Blom
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
namespace OCA\NgBackup\Repository;
|
||||
|
||||
use OCA\NgBackup\Backend\IBackend;
|
||||
|
||||
/**
|
||||
* Reads blobs from packs. Restores and deep verifies read the blobs of a snapshot in the order
|
||||
* they were written, so the stream of the pack read last is kept open and read on from where it
|
||||
* is: one request per pack instead of one per blob (a round trip each, and on most remote
|
||||
* storages a new connection or file handle too). Anything else opens the pack at the offset,
|
||||
* as a single read always did.
|
||||
*
|
||||
* The open stream must not overlap other requests on the location (SFTP cannot read and write
|
||||
* on one connection at once): Repository reaches the location through a PackReaderGuard, which
|
||||
* closes it first.
|
||||
*/
|
||||
final class PackReader {
|
||||
/** A gap up to this size in the open stream is read past instead of opening the pack again. */
|
||||
private const SKIP_MAX = 8 * 1048576;
|
||||
private const CHUNK = 1048576;
|
||||
|
||||
/** @var resource|null */
|
||||
private $stream = null;
|
||||
private string $pack = '';
|
||||
private int $pos = 0;
|
||||
|
||||
public function __construct(
|
||||
private IBackend $backend,
|
||||
) {
|
||||
}
|
||||
|
||||
public function read(string $pack, int $offset, int $length): string {
|
||||
if ($this->stream !== null && $this->pack === $pack && $offset >= $this->pos && $offset - $this->pos <= self::SKIP_MAX) {
|
||||
try {
|
||||
$this->skipTo($offset);
|
||||
return $this->readExactly($length);
|
||||
} catch (\Exception) {
|
||||
// The open stream broke (e.g. a timeout); open the pack again below.
|
||||
}
|
||||
}
|
||||
$this->close();
|
||||
$fh = $this->backend->get('packs/' . substr($pack, 0, 2) . '/' . $pack);
|
||||
$this->stream = $fh;
|
||||
$this->pack = $pack;
|
||||
$this->pos = 0;
|
||||
try {
|
||||
if ($offset > 0 && (stream_get_meta_data($fh)['seekable'] ?? false) && fseek($fh, $offset) === 0) {
|
||||
$this->pos = $offset;
|
||||
}
|
||||
$this->skipTo($offset); // a stream that cannot seek is read up to the offset
|
||||
return $this->readExactly($length);
|
||||
} catch (\Exception $e) {
|
||||
$this->close();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function close(): void {
|
||||
$fh = $this->stream;
|
||||
$this->stream = null;
|
||||
if ($fh !== null) {
|
||||
@fclose($fh);
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
$this->close();
|
||||
}
|
||||
|
||||
private function skipTo(int $offset): void {
|
||||
while ($this->pos < $offset) {
|
||||
$this->readExactly(min($offset - $this->pos, self::CHUNK));
|
||||
}
|
||||
}
|
||||
|
||||
/** Read $length bytes from the open stream; a short read closes it. */
|
||||
private function readExactly(int $length): string {
|
||||
$fh = $this->stream ?? throw new RepositoryException('No open pack stream');
|
||||
$data = '';
|
||||
while (strlen($data) < $length) {
|
||||
$chunk = fread($fh, min($length - strlen($data), self::CHUNK));
|
||||
if ($chunk === false || $chunk === '') {
|
||||
$this->close();
|
||||
throw new RepositoryException('Short read in pack ' . $this->pack);
|
||||
}
|
||||
$data .= $chunk;
|
||||
}
|
||||
$this->pos += $length;
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// SPDX-FileCopyrightText: 2026 Dillard Blom
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
namespace OCA\NgBackup\Repository;
|
||||
|
||||
use OCA\NgBackup\Backend\IBackend;
|
||||
|
||||
/** The location as Repository sees it: closes the PackReader's open stream before every other request. */
|
||||
final class PackReaderGuard implements IBackend {
|
||||
public function __construct(
|
||||
private IBackend $inner,
|
||||
private PackReader $reader,
|
||||
) {
|
||||
}
|
||||
|
||||
/** @param resource $stream */
|
||||
public function put(string $path, $stream): void {
|
||||
$this->reader->close();
|
||||
$this->inner->put($path, $stream);
|
||||
}
|
||||
|
||||
public function get(string $path) {
|
||||
$this->reader->close();
|
||||
return $this->inner->get($path);
|
||||
}
|
||||
|
||||
public function getMany(array $paths): \Generator {
|
||||
$this->reader->close();
|
||||
return $this->inner->getMany($paths);
|
||||
}
|
||||
|
||||
public function getRange(string $path, int $offset, int $length): string {
|
||||
$this->reader->close();
|
||||
return $this->inner->getRange($path, $offset, $length);
|
||||
}
|
||||
|
||||
public function exists(string $path): bool {
|
||||
$this->reader->close();
|
||||
return $this->inner->exists($path);
|
||||
}
|
||||
|
||||
public function move(string $from, string $to): void {
|
||||
$this->reader->close();
|
||||
$this->inner->move($from, $to);
|
||||
}
|
||||
|
||||
public function list(string $prefix): array {
|
||||
$this->reader->close();
|
||||
return $this->inner->list($prefix);
|
||||
}
|
||||
|
||||
public function listFiles(string $dir): array {
|
||||
$this->reader->close();
|
||||
return $this->inner->listFiles($dir);
|
||||
}
|
||||
|
||||
public function delete(string $path): void {
|
||||
$this->reader->close();
|
||||
$this->inner->delete($path);
|
||||
}
|
||||
}
|
||||
+116
-28
@@ -28,12 +28,17 @@ final class Repository {
|
||||
public const FORMAT = 1;
|
||||
public const BLOB_SIZE = 4 * 1048576;
|
||||
private const FLAG_DEFLATE = 1;
|
||||
/** Reads per blob in a deep verify before it is reported as unreadable (not corrupted). */
|
||||
private const VERIFY_READ_ATTEMPTS = 3;
|
||||
|
||||
private StreamCipher $cipher;
|
||||
private BlobIndex $index;
|
||||
private ?PackWriter $runPacks = null;
|
||||
private ?int $maxBytes = null;
|
||||
private Catalog $catalog;
|
||||
private PackReader $packReader;
|
||||
/** verify() lists the pack folders from this many packs on (see presentPacks()); tests lower it. */
|
||||
public int $listPacksAbove = 256;
|
||||
|
||||
private function __construct(
|
||||
private IBackend $backend,
|
||||
@@ -41,7 +46,9 @@ final class Repository {
|
||||
private string $repositoryId,
|
||||
) {
|
||||
$this->cipher = new StreamCipher($keys);
|
||||
$this->index = new BlobIndex($backend, $this->cipher);
|
||||
$this->packReader = new PackReader($backend);
|
||||
$this->backend = new PackReaderGuard($backend, $this->packReader);
|
||||
$this->index = new BlobIndex($this->backend, $this->cipher);
|
||||
}
|
||||
|
||||
public static function init(IBackend $backend, #[\SensitiveParameter] string $passphrase): self {
|
||||
@@ -90,8 +97,8 @@ final class Repository {
|
||||
private function loadCatalog(?ICatalogAnchor $anchor): void {
|
||||
$this->catalog = new Catalog($this->backend, $this->cipher, $this->repositoryId, $anchor);
|
||||
$this->catalog->load(
|
||||
fn () => array_map('basename', $this->backend->list('snapshots')),
|
||||
fn () => array_map('basename', $this->backend->list('trash/info')),
|
||||
fn () => array_map('basename', $this->backend->listFiles('snapshots')),
|
||||
fn () => array_map('basename', $this->backend->listFiles('trash/info')),
|
||||
fn () => $this->backend->list('users'));
|
||||
}
|
||||
|
||||
@@ -260,6 +267,15 @@ final class Repository {
|
||||
});
|
||||
}
|
||||
|
||||
/** See BlobIndex::holdCache(): no database writes until releaseIndexCache(). */
|
||||
public function holdIndexCache(): void {
|
||||
$this->index->holdCache();
|
||||
}
|
||||
|
||||
public function releaseIndexCache(): void {
|
||||
$this->index->releaseCache();
|
||||
}
|
||||
|
||||
public function flushPacks(): void {
|
||||
$this->runPacks?->flush();
|
||||
}
|
||||
@@ -632,7 +648,7 @@ final class Repository {
|
||||
* @return list<string>
|
||||
*/
|
||||
public function listSnapshots(): array {
|
||||
$present = array_flip(array_map('basename', $this->backend->list('snapshots')));
|
||||
$present = array_flip(array_map('basename', $this->backend->listFiles('snapshots')));
|
||||
$listed = array_keys($this->catalog->snapshots);
|
||||
foreach ($listed as $id) {
|
||||
if (!isset($present[$id])) {
|
||||
@@ -696,8 +712,13 @@ final class Repository {
|
||||
}
|
||||
|
||||
private function loadBlob(string $id): string {
|
||||
[$pack, $offset, $length, $raw, $flags] = $this->index->get($id);
|
||||
$encrypted = $this->backend->getRange('packs/' . substr($pack, 0, 2) . '/' . $pack, $offset, $length);
|
||||
[$pack, $offset, $length] = $this->index->get($id);
|
||||
return $this->decodeBlob($id, $this->packReader->read($pack, $offset, $length));
|
||||
}
|
||||
|
||||
/** Decrypts and checks a blob read from its pack; throws when it is corrupted or tampered with. */
|
||||
private function decodeBlob(string $id, string $encrypted): string {
|
||||
[, , , $raw, $flags] = $this->index->get($id);
|
||||
$payload = $this->cipher->decryptString($encrypted, 'blob:' . $id);
|
||||
$data = ($flags & self::FLAG_DEFLATE) ? gzinflate($payload) : $payload;
|
||||
if ($data === false || strlen($data) !== $raw || $this->keys->blobId($data) !== $id) {
|
||||
@@ -731,17 +752,23 @@ final class Repository {
|
||||
* referenced by more than one file is only checked once; a missing database manifest is a
|
||||
* failure, not something to silently skip over.
|
||||
*
|
||||
* @param ?callable $heartbeat called after every blob (shallow) or every blob read (deep);
|
||||
* callers use it to keep a time-boxed lease alive during a large audit
|
||||
* @return array{filesChecked:int, blobsChecked:int, bytesChecked:int, missing:list<string>, failed:list<string>}
|
||||
* @param ?callable(array{phase:string, done:int, total:?int, blobs?:int, bytes?:int}):void $heartbeat
|
||||
* called often during the check, with how far it is: phase "index" (index files
|
||||
* downloaded), "packs" (pack folders listed), "files" and "database" (files of the
|
||||
* snapshot checked); callers use it to keep a time-boxed lease alive and to report
|
||||
* @return array{filesChecked:int, blobsChecked:int, bytesChecked:int, missing:list<string>, failed:list<string>, unreadable:list<string>, readError:string}
|
||||
*/
|
||||
public function verify(string $snapshotId, bool $deep = false, ?callable $heartbeat = null): array {
|
||||
$heartbeat ??= static function (): void {
|
||||
$heartbeat ??= static function (array $progress): void {
|
||||
};
|
||||
$this->index->load(true);
|
||||
$stats = ['filesChecked' => 0, 'blobsChecked' => 0, 'bytesChecked' => 0, 'missing' => [], 'failed' => []];
|
||||
$meta = $this->snapshotMeta($snapshotId);
|
||||
$filesTotal = isset($meta['stats']['files']) ? (int)$meta['stats']['files'] : null;
|
||||
$this->index->load(true, static function (int $done, int $total) use ($heartbeat): void {
|
||||
$heartbeat(['phase' => 'index', 'done' => $done, 'total' => $total]);
|
||||
});
|
||||
$stats = ['filesChecked' => 0, 'blobsChecked' => 0, 'bytesChecked' => 0, 'missing' => [], 'failed' => [], 'unreadable' => [], 'readError' => ''];
|
||||
$seen = [];
|
||||
$packSeen = []; // pack id => exists on the backend; avoids one exists() call per blob
|
||||
[$packSeen, $listedDirs] = $this->presentPacks($heartbeat);
|
||||
foreach ($this->streamEntries($snapshotId) as $entry) {
|
||||
$stats['filesChecked']++;
|
||||
foreach ($entry['b'] as $id) {
|
||||
@@ -749,11 +776,11 @@ final class Repository {
|
||||
continue;
|
||||
}
|
||||
$seen[$id] = true;
|
||||
$this->verifyBlob($id, $deep, $stats, $packSeen);
|
||||
$heartbeat();
|
||||
$this->verifyBlob($id, $deep, $stats, $packSeen, $listedDirs);
|
||||
$heartbeat(['phase' => 'files', 'done' => $stats['filesChecked'], 'total' => $filesTotal,
|
||||
'blobs' => $stats['blobsChecked'], 'bytes' => $stats['bytesChecked']]);
|
||||
}
|
||||
}
|
||||
$meta = $this->snapshotMeta($snapshotId);
|
||||
if (isset($meta['db']) && is_string($meta['db'])) {
|
||||
if (!$this->backend->exists($meta['db'])) {
|
||||
$stats['missing'][] = $meta['db'];
|
||||
@@ -765,8 +792,9 @@ final class Repository {
|
||||
continue;
|
||||
}
|
||||
$seen[$id] = true;
|
||||
$this->verifyBlob($id, $deep, $stats, $packSeen);
|
||||
$heartbeat();
|
||||
$this->verifyBlob($id, $deep, $stats, $packSeen, $listedDirs);
|
||||
$heartbeat(['phase' => 'database', 'done' => $stats['filesChecked'], 'total' => $filesTotal,
|
||||
'blobs' => $stats['blobsChecked'], 'bytes' => $stats['bytesChecked']]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -774,20 +802,56 @@ final class Repository {
|
||||
return $stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Which packs are on the location, from one listing per pack folder instead of a request per
|
||||
* pack. A folder that cannot be listed, or comes back empty (which a storage also reports when
|
||||
* it could not look), is left out: its packs are then checked one by one. With no more packs
|
||||
* than folders nothing is listed: a request per pack is fewer requests then, and on FTP every
|
||||
* request takes a data connection, of which a server may have only a few.
|
||||
*
|
||||
* @return array{0: array<string, bool>, 1: array<string, true>} pack id => present, and the
|
||||
* folders that were listed
|
||||
*/
|
||||
private function presentPacks(callable $heartbeat): array {
|
||||
$present = [];
|
||||
$listed = [];
|
||||
if ($this->index->packCount() <= $this->listPacksAbove) {
|
||||
return [$present, $listed];
|
||||
}
|
||||
for ($i = 0; $i < 256; $i++) {
|
||||
$dir = sprintf('%02x', $i);
|
||||
try {
|
||||
$files = $this->backend->listFiles('packs/' . $dir);
|
||||
} catch (BackendException) {
|
||||
$files = [];
|
||||
}
|
||||
foreach ($files as $path) {
|
||||
$present[basename($path)] = true;
|
||||
}
|
||||
if ($files !== []) {
|
||||
$listed[$dir] = true;
|
||||
}
|
||||
$heartbeat(['phase' => 'packs', 'done' => $i + 1, 'total' => 256]);
|
||||
}
|
||||
return [$present, $listed];
|
||||
}
|
||||
|
||||
/**
|
||||
* Same checks as verify(), for a user export: every blob its manifest references must be in
|
||||
* the freshly re-authenticated index with its pack present; $deep also decrypts each.
|
||||
*
|
||||
* @return array{filesChecked:int, blobsChecked:int, bytesChecked:int, missing:list<string>, failed:list<string>}
|
||||
* @return array{filesChecked:int, blobsChecked:int, bytesChecked:int, missing:list<string>, failed:list<string>, unreadable:list<string>, readError:string}
|
||||
*/
|
||||
public function verifyUserExport(string $manifestPath, bool $deep = false, ?callable $heartbeat = null): array {
|
||||
if (!$this->isRecordedUserExport($manifestPath)) {
|
||||
throw new RepositoryException("$manifestPath is not a recorded user export for this location");
|
||||
}
|
||||
$heartbeat ??= static function (): void {
|
||||
$heartbeat ??= static function (array $progress): void {
|
||||
};
|
||||
$this->index->load(true);
|
||||
$stats = ['filesChecked' => 0, 'blobsChecked' => 0, 'bytesChecked' => 0, 'missing' => [], 'failed' => []];
|
||||
$this->index->load(true, static function (int $done, int $total) use ($heartbeat): void {
|
||||
$heartbeat(['phase' => 'index', 'done' => $done, 'total' => $total]);
|
||||
});
|
||||
$stats = ['filesChecked' => 0, 'blobsChecked' => 0, 'bytesChecked' => 0, 'missing' => [], 'failed' => [], 'unreadable' => [], 'readError' => ''];
|
||||
$seen = [];
|
||||
$packSeen = [];
|
||||
$manifest = json_decode($this->getObject($manifestPath), true, 512, JSON_THROW_ON_ERROR);
|
||||
@@ -802,20 +866,27 @@ final class Repository {
|
||||
}
|
||||
$seen[$id] = true;
|
||||
$this->verifyBlob($id, $deep, $stats, $packSeen);
|
||||
$heartbeat();
|
||||
$heartbeat(['phase' => 'files', 'done' => $stats['filesChecked'], 'total' => null,
|
||||
'blobs' => $stats['blobsChecked'], 'bytes' => $stats['bytesChecked']]);
|
||||
}
|
||||
}
|
||||
return $stats;
|
||||
}
|
||||
|
||||
/** @param array<string, bool> $packSeen */
|
||||
private function verifyBlob(string $id, bool $deep, array &$stats, array &$packSeen): void {
|
||||
/**
|
||||
* @param array<string, bool> $packSeen pack id => present; filled in as packs are checked
|
||||
* @param array<string, true> $listedDirs pack folders whose listing is in $packSeen: a pack
|
||||
* not in it is not there
|
||||
*/
|
||||
private function verifyBlob(string $id, bool $deep, array &$stats, array &$packSeen, array $listedDirs = []): void {
|
||||
if (!$this->index->has($id)) {
|
||||
$stats['missing'][] = $id;
|
||||
return;
|
||||
}
|
||||
[$pack] = $this->index->get($id);
|
||||
$packExists = $packSeen[$pack] ??= $this->backend->exists('packs/' . substr($pack, 0, 2) . '/' . $pack);
|
||||
$packExists = $packSeen[$pack] ??= isset($listedDirs[substr($pack, 0, 2)])
|
||||
? false
|
||||
: $this->backend->exists('packs/' . substr($pack, 0, 2) . '/' . $pack);
|
||||
if (!$packExists) {
|
||||
$stats['missing'][] = $id;
|
||||
return;
|
||||
@@ -824,9 +895,26 @@ final class Repository {
|
||||
if (!$deep) {
|
||||
return;
|
||||
}
|
||||
// Reading and checking are kept apart: a read that fails is usually a network or
|
||||
// storage error, so it is retried and reported as unreadable (a pack that stays
|
||||
// unreadable may still be truncated). Only a blob that was read but does not
|
||||
// decrypt or match its id counts as failed: that is proof of damage.
|
||||
[, $offset, $length] = $this->index->get($id);
|
||||
$encrypted = null;
|
||||
for ($attempt = 1; $attempt <= self::VERIFY_READ_ATTEMPTS && $encrypted === null; $attempt++) {
|
||||
try {
|
||||
$encrypted = $this->packReader->read($pack, $offset, $length);
|
||||
} catch (\Exception $e) { // storage adapters throw their own exceptions; \Error (a bug) is not retried
|
||||
$stats['readError'] = $e->getMessage();
|
||||
}
|
||||
}
|
||||
if ($encrypted === null) {
|
||||
$stats['unreadable'][] = $id;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
$stats['bytesChecked'] += strlen($this->loadBlob($id));
|
||||
} catch (\Throwable) {
|
||||
$stats['bytesChecked'] += strlen($this->decodeBlob($id, $encrypted));
|
||||
} catch (\Exception) { // damage; an \Error is a bug and must not be reported as damage
|
||||
$stats['failed'][] = $id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ final class AlertService {
|
||||
}
|
||||
$this->appConfig->setValueString(Application::APP_ID, $key, date('Y-m-d'));
|
||||
$this->notifyAdmins('backup_stale', 'stale-' . $target->getId() . '-' . date('Y-m-d'),
|
||||
['target' => $target->getName(), 'since' => $last ? date('Y-m-d H:i', $lastTime) : 'never']);
|
||||
['target' => $target->getName(), 'since' => $last ? date('Y-m-d H:i', $lastTime) : '']);
|
||||
}
|
||||
|
||||
private function notifyAdmins(string $subject, string $objectId, array $params): void {
|
||||
|
||||
+202
-23
@@ -14,9 +14,12 @@ use OCA\NgBackup\Db\Snapshot;
|
||||
use OCA\NgBackup\Db\SnapshotMapper;
|
||||
use OCA\NgBackup\Db\Target;
|
||||
use OCA\NgBackup\Job\BackupRun;
|
||||
use OCA\NgBackup\Repository\Repository;
|
||||
use OCA\NgBackup\Repository\RepositoryException;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\App\IAppManager;
|
||||
use OCP\IConfig;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\Lock\ILockingProvider;
|
||||
use OCP\Lock\LockedException;
|
||||
@@ -32,6 +35,9 @@ use Psr\Log\LoggerInterface;
|
||||
final class BackupService {
|
||||
public const KIND_FULL = 'full';
|
||||
|
||||
/** @var \WeakMap<Repository, string> the location settings each kept repository was opened with */
|
||||
private \WeakMap $openedWith;
|
||||
|
||||
public function __construct(
|
||||
private TargetService $targets,
|
||||
private KeyService $keys,
|
||||
@@ -45,6 +51,9 @@ final class BackupService {
|
||||
private AlertService $alerts,
|
||||
private LeaseService $leases,
|
||||
) {
|
||||
/** @var \WeakMap<Repository, string> $opened */
|
||||
$opened = new \WeakMap();
|
||||
$this->openedWith = $opened;
|
||||
}
|
||||
|
||||
public function start(Target $target, string $label = ''): Run {
|
||||
@@ -68,6 +77,46 @@ final class BackupService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The expected size of the files phase, for the progress shown while it runs: the size of the
|
||||
* previous snapshot, or for a first backup the users' files, versions and trash as Nextcloud's
|
||||
* file cache knows them (approximate: other data in the data directory is not counted).
|
||||
*
|
||||
* @return array{bytes: int, approximate: bool}
|
||||
*/
|
||||
private function estimateBytes(Run $run, array $files): array {
|
||||
if (($files['parent'] ?? null) !== null) {
|
||||
try {
|
||||
return ['bytes' => $this->snapshots->findOne($run->getTargetId(), (string)$files['parent'])->getBytes() ?? 0, 'approximate' => false];
|
||||
} catch (\Throwable) {
|
||||
}
|
||||
}
|
||||
try {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->select('numeric_id')->from('storages')
|
||||
->where($qb->expr()->like('id', $qb->createNamedParameter('home::%')))
|
||||
->orWhere($qb->expr()->like('id', $qb->createNamedParameter('object::user:%')));
|
||||
$result = $qb->executeQuery();
|
||||
$storages = [];
|
||||
while (($id = $result->fetchOne()) !== false) {
|
||||
$storages[] = (int)$id;
|
||||
}
|
||||
$result->closeCursor();
|
||||
$bytes = 0;
|
||||
foreach (array_chunk($storages, 500) as $chunk) {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->select($qb->func()->sum('size'))->from('filecache')
|
||||
->where($qb->expr()->in('storage', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)))
|
||||
->andWhere($qb->expr()->in('path_hash', $qb->createNamedParameter(array_map('md5', ['files', 'files_versions', 'files_trashbin']), IQueryBuilder::PARAM_STR_ARRAY)))
|
||||
->andWhere($qb->expr()->gt('size', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
|
||||
$bytes += (int)$qb->executeQuery()->fetchOne();
|
||||
}
|
||||
return ['bytes' => $bytes, 'approximate' => true];
|
||||
} catch (\Throwable) {
|
||||
return ['bytes' => 0, 'approximate' => true];
|
||||
}
|
||||
}
|
||||
|
||||
private function createRun(Target $target, string $label): Run {
|
||||
$running = $this->runs->findRunning($target->getId(), self::KIND_FULL);
|
||||
if ($running !== []) {
|
||||
@@ -102,10 +151,22 @@ final class BackupService {
|
||||
/**
|
||||
* Do one step of a run until $deadline. Safe to call concurrently: a second caller finds the
|
||||
* run locked and returns immediately.
|
||||
*
|
||||
* @param Repository|null $repo the location's repository, opened by an earlier step of this run
|
||||
* in this process (see runToCompletion()); set when this step opens it
|
||||
*/
|
||||
public function step(Run $run, float $deadline): Run {
|
||||
public function step(Run $run, float $deadline, ?Repository &$repo = null): Run {
|
||||
// Leases expire on their own: a step killed by a time limit or OOM blocks nothing for long.
|
||||
$ttl = (int)min(3600, max(120, $deadline - microtime(true) + 120));
|
||||
// A step can run well past its deadline (the commit of a large snapshot writes its tree and
|
||||
// index), so the lease holds at least 15 minutes; should it still expire and another process
|
||||
// take the run over, the checks below keep the two from overwriting each other.
|
||||
$ttl = (int)min(3600, max(900, $deadline - microtime(true) + 600));
|
||||
// No new step once a cancel has begun: it waits for the step in progress, if any.
|
||||
try {
|
||||
$this->leases->release($this->leases->acquire(self::cancelKey($run->getId()), LeaseService::SHARED, 60));
|
||||
} catch (LockedException) {
|
||||
return $run;
|
||||
}
|
||||
try {
|
||||
$runLease = $this->leases->acquire('run/' . $run->getId(), LeaseService::EXCLUSIVE, $ttl);
|
||||
} catch (LockedException) {
|
||||
@@ -118,13 +179,28 @@ final class BackupService {
|
||||
$this->leases->release($runLease);
|
||||
return $run;
|
||||
}
|
||||
$loadedAt = null;
|
||||
$savedBefore = $run->getUpdatedAt();
|
||||
try {
|
||||
$run = $this->runs->find($run->getId()); // fresh state after acquiring the lock
|
||||
if ($run->getStatus() !== Run::RUNNING || $run->getKind() !== self::KIND_FULL) {
|
||||
return $run;
|
||||
}
|
||||
$loadedAt = $run->getUpdatedAt();
|
||||
$target = $this->targets->get((string)$run->getTargetId());
|
||||
$repo = $this->targets->repository($target);
|
||||
// Reopen when another process took a step since ours (its packs are not in our copy of
|
||||
// the index) or the location was edited (password, folder, size limit).
|
||||
$settings = json_encode([$target->getBackend(), $target->getAuth(), $target->getOptions(),
|
||||
$target->getBasePath(), $target->getRepositoryId(), $target->getMaxBytes()], JSON_THROW_ON_ERROR);
|
||||
if ($repo === null || $loadedAt !== $savedBefore || ($this->openedWith[$repo] ?? null) !== $settings) {
|
||||
// Opening reads the index listing, which takes minutes on a slow location with many
|
||||
// packs: that time does not count, or each step would store one blob and stop.
|
||||
$opening = microtime(true);
|
||||
$repo = null;
|
||||
$repo = $this->targets->repository($target);
|
||||
$this->openedWith[$repo] = $settings;
|
||||
$deadline += microtime(true) - $opening;
|
||||
}
|
||||
$state = json_decode($run->getState(), true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
if ($run->getPhase() === 'db') {
|
||||
@@ -147,6 +223,7 @@ final class BackupService {
|
||||
$state['dump'] = $dump;
|
||||
}
|
||||
} else {
|
||||
$state['files']['estimate'] ??= $this->estimateBytes($run, $state['files']);
|
||||
$state['files'] = BackupRun::step($repo, $state['files'], $deadline);
|
||||
if ($state['files']['phase'] === 'commit') {
|
||||
// Catalog write under the catalog lock, from a repository opened inside it: a per-user
|
||||
@@ -162,14 +239,20 @@ final class BackupService {
|
||||
}
|
||||
if ($state['files']['phase'] === 'done') {
|
||||
$this->recordSnapshot($run, $state['files']);
|
||||
$this->alerts->storedBytes($target, $repo->storedBytes());
|
||||
$stats = $state['files']['stats'];
|
||||
if (($stats['unreadable'] ?? 0) > 0 || ($stats['changing'] ?? 0) > 0) {
|
||||
$warnings = BackupRun::warnings($state['files']);
|
||||
$this->logger->warning(sprintf('NG Backup run %d to %s: %d unreadable (missing from the snapshot): %s; %d changed while being read: %s',
|
||||
$run->getId(), $target->getName(), $stats['unreadable'] ?? 0, implode(', ', $warnings['unreadable']),
|
||||
$stats['changing'] ?? 0, implode(', ', $warnings['changing'])));
|
||||
$this->alerts->runWarnings($run, $target->getName(), (int)($stats['unreadable'] ?? 0), (int)($stats['changing'] ?? 0), $warnings);
|
||||
// The snapshot is committed: what follows only reports on it and must not turn
|
||||
// the run into a failure.
|
||||
try {
|
||||
$this->alerts->storedBytes($target, $repo->storedBytes());
|
||||
$stats = $state['files']['stats'];
|
||||
if (($stats['unreadable'] ?? 0) > 0 || ($stats['changing'] ?? 0) > 0) {
|
||||
$warnings = BackupRun::warnings($state['files']);
|
||||
$this->logger->warning(sprintf('NG Backup run %d to %s: %d unreadable (missing from the snapshot): %s; %d changed while being read: %s',
|
||||
$run->getId(), $target->getName(), $stats['unreadable'] ?? 0, implode(', ', $warnings['unreadable']),
|
||||
$stats['changing'] ?? 0, implode(', ', $warnings['changing'])));
|
||||
$this->alerts->runWarnings($run, $target->getName(), (int)($stats['unreadable'] ?? 0), (int)($stats['changing'] ?? 0), $warnings);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->warning('NG Backup run ' . $run->getId() . ': reporting on the finished backup failed: ' . $e->getMessage(), ['exception' => $e]);
|
||||
}
|
||||
$run->setStatus(Run::DONE);
|
||||
$run->setPhase('done');
|
||||
@@ -178,15 +261,33 @@ final class BackupService {
|
||||
}
|
||||
$run->setState(json_encode($state, JSON_THROW_ON_ERROR));
|
||||
$run->setStats(json_encode(['db' => $state['dbStats'] ?? null, 'files' => $state['files']['stats']], JSON_THROW_ON_ERROR));
|
||||
$run->setUpdatedAt(time());
|
||||
return $this->runs->update($run);
|
||||
// Another process may have taken the run over after this step's lease expired: then its
|
||||
// writes stand and this step's are dropped.
|
||||
$saved = $this->runs->updateIfUnchanged($run, $loadedAt);
|
||||
if ($saved === null) {
|
||||
$repo = null; // the run is another process's now: its packs are not in our index
|
||||
return $this->runs->find($run->getId());
|
||||
}
|
||||
return $saved;
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->error('NG Backup run ' . $run->getId() . ' failed: ' . $e->getMessage(), ['exception' => $e]);
|
||||
$repo = null; // may hold blobs of a half-written pack: the next step opens it afresh
|
||||
if ($loadedAt === null) {
|
||||
// The run could not even be loaded: there is nothing fresh to mark as failed, and
|
||||
// the caller's copy may be stale.
|
||||
$this->logger->error('NG Backup run ' . $run->getId() . ': step could not load the run: ' . $e->getMessage(), ['exception' => $e]);
|
||||
return $run;
|
||||
}
|
||||
$run->setStatus(Run::FAILED);
|
||||
$run->setError($e->getMessage());
|
||||
$run->setFinishedAt(time());
|
||||
$run->setUpdatedAt(time());
|
||||
$run = $this->runs->update($run);
|
||||
$failed = $this->runs->updateIfUnchanged($run, $loadedAt);
|
||||
if ($failed === null) {
|
||||
// Another process that took the run over may have finished it: its result stands.
|
||||
$this->logger->warning('NG Backup run ' . $run->getId() . ': a step that another process had taken over ended with: ' . $e->getMessage());
|
||||
return $this->runs->find($run->getId());
|
||||
}
|
||||
$run = $failed;
|
||||
$this->logger->error('NG Backup run ' . $run->getId() . ' failed: ' . $e->getMessage(), ['exception' => $e]);
|
||||
try {
|
||||
$this->alerts->runFailed($run, isset($target) ? $target->getName() : '#' . $run->getTargetId());
|
||||
} catch (\Throwable) {
|
||||
@@ -200,14 +301,16 @@ final class BackupService {
|
||||
|
||||
/** Run to completion in this process (occ, system cron), reporting progress. */
|
||||
public function runToCompletion(Run $run, ?callable $progress = null, float $stepSeconds = 30): Run {
|
||||
$repo = null; // opened once, by the first step, and kept for the next ones
|
||||
while ($run->getStatus() === Run::RUNNING) {
|
||||
$before = $run->getUpdatedAt();
|
||||
$run = $this->step($run, microtime(true) + $stepSeconds);
|
||||
$run = $this->step($run, microtime(true) + $stepSeconds, $repo);
|
||||
if ($run->getStatus() === Run::RUNNING && $run->getUpdatedAt() === $before) {
|
||||
// Step was not possible (another process holds the run, or a crashed step's lease has
|
||||
// not expired yet): wait instead of spinning.
|
||||
sleep(5);
|
||||
$run = $this->runs->find($run->getId());
|
||||
$repo = null; // another process may have stored packs in the meantime
|
||||
}
|
||||
if ($progress !== null) {
|
||||
$progress($run);
|
||||
@@ -216,14 +319,78 @@ final class BackupService {
|
||||
return $run;
|
||||
}
|
||||
|
||||
public function cancel(Run $run): Run {
|
||||
$run->setStatus(Run::CANCELLED);
|
||||
$run->setFinishedAt(time());
|
||||
$run->setUpdatedAt(time());
|
||||
return $this->runs->update($run);
|
||||
/**
|
||||
* Stop a running backup. First no new step may start, then this waits (up to $wait seconds)
|
||||
* for the step in progress, so a step that is recording the snapshot finishes first. A process
|
||||
* running the backup then finds it cancelled and stops. The packs it stored stay until the
|
||||
* next cleanup.
|
||||
*
|
||||
* A process of an earlier version does not hold back its next step and may never leave a gap:
|
||||
* after $wait the run is marked cancelled while it works, unless it is recording the snapshot.
|
||||
* Its step then fails to save and the process stops.
|
||||
*/
|
||||
public function cancel(Run $run, int $wait = 120): Run {
|
||||
$id = $run->getId();
|
||||
$until = time() + $wait;
|
||||
$hold = null;
|
||||
for ($try = 0; $hold === null; $try++) {
|
||||
try {
|
||||
$hold = $this->leases->acquire(self::cancelKey($id), LeaseService::EXCLUSIVE, $wait + 300);
|
||||
} catch (LockedException) {
|
||||
if ($try >= 20) {
|
||||
throw new \RuntimeException("Run $id is being cancelled already");
|
||||
}
|
||||
usleep(100000); // a step checking whether it may start
|
||||
}
|
||||
}
|
||||
try {
|
||||
$lease = null;
|
||||
while ($lease === null && time() < $until) {
|
||||
try {
|
||||
$lease = $this->leases->acquire('run/' . $id, LeaseService::EXCLUSIVE, 60);
|
||||
} catch (LockedException) {
|
||||
sleep(2);
|
||||
}
|
||||
}
|
||||
try {
|
||||
for ($try = 0; $try < 10; $try++) {
|
||||
$run = $this->runs->find($id);
|
||||
if ($run->getStatus() !== Run::RUNNING) {
|
||||
return $run;
|
||||
}
|
||||
$state = json_decode($run->getState(), true) ?: [];
|
||||
if ($lease === null && ($state['files']['phase'] ?? '') === 'commit') {
|
||||
throw new \RuntimeException("Run $id is recording its snapshot; try again in a minute");
|
||||
}
|
||||
$loadedAt = $run->getUpdatedAt();
|
||||
$run->setStatus(Run::CANCELLED);
|
||||
$run->setFinishedAt(time());
|
||||
if (($cancelled = $this->runs->updateIfUnchanged($run, $loadedAt)) !== null) {
|
||||
return $cancelled;
|
||||
}
|
||||
usleep(200000); // a step of another process saved just now
|
||||
}
|
||||
throw new \RuntimeException("Run $id keeps changing; try again");
|
||||
} finally {
|
||||
if ($lease !== null) {
|
||||
$this->leases->release($lease);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
$this->leases->release($hold);
|
||||
}
|
||||
}
|
||||
|
||||
private static function cancelKey(int $runId): string {
|
||||
return 'run/' . $runId . '/cancel';
|
||||
}
|
||||
|
||||
private function recordSnapshot(Run $run, array $files): void {
|
||||
try {
|
||||
$this->snapshots->findOne($run->getTargetId(), $files['snapshot']);
|
||||
return; // already recorded, by an earlier attempt at this step
|
||||
} catch (DoesNotExistException) {
|
||||
}
|
||||
$s = new Snapshot();
|
||||
$s->setTargetId($run->getTargetId());
|
||||
$s->setSnapshotId($files['snapshot']);
|
||||
@@ -232,7 +399,19 @@ final class BackupService {
|
||||
$s->setCreatedAt(time());
|
||||
$s->setFiles($files['stats']['files']);
|
||||
$s->setBytes($files['stats']['bytes']);
|
||||
$this->snapshots->insert($s);
|
||||
try {
|
||||
$this->snapshots->insert($s);
|
||||
} catch (\OCP\DB\Exception $e) {
|
||||
// Recorded by another process between the check above and this insert?
|
||||
if ($e->getReason() !== \OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
||||
throw $e;
|
||||
}
|
||||
try {
|
||||
$this->snapshots->findOne($run->getTargetId(), $files['snapshot']);
|
||||
} catch (DoesNotExistException) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
|
||||
+104
-17
@@ -17,6 +17,7 @@ use OCA\NgBackup\Db\TargetMapper;
|
||||
use OCA\NgBackup\Repository\Repository;
|
||||
use OCP\App\IAppManager;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\Files\Storage\IStorage;
|
||||
use OCP\Security\ICrypto;
|
||||
use OCP\Server;
|
||||
|
||||
@@ -37,7 +38,7 @@ final class TargetService {
|
||||
) {
|
||||
}
|
||||
|
||||
/** @return list<array{id:string, name:string, auth:list<string>, parameters:array}> */
|
||||
/** @return list<array{id:string, name:string, auth:list<array{id:string, name:string, parameters:array}>, parameters:array}> */
|
||||
public function availableBackends(): array {
|
||||
return $this->factory()->describeBackends();
|
||||
}
|
||||
@@ -50,20 +51,8 @@ final class TargetService {
|
||||
* @return array{target: Target, created: bool}
|
||||
*/
|
||||
public function add(string $name, string $backend, string $auth, array $options, string $basePath): array {
|
||||
if (!preg_match('/^[A-Za-z0-9][A-Za-z0-9 _.-]{0,63}$/', $name)) {
|
||||
throw new \InvalidArgumentException('Name: letters, digits, space, _ . - (max 64)');
|
||||
}
|
||||
// Without the native extension files_external falls back to the smbclient binary, which
|
||||
// registers fine but cannot seek: the first backup then fails with "Seek failed". Refuse
|
||||
// up front instead of letting the admin find out from a failed run.
|
||||
if ($backend === 'smb' && !extension_loaded('smbclient')) {
|
||||
throw new \InvalidArgumentException('The SMB backend needs the PHP smbclient extension (pecl install smbclient); the smbclient binary alone cannot seek and every backup would fail');
|
||||
}
|
||||
try {
|
||||
$this->mapper->findByName($name);
|
||||
throw new \InvalidArgumentException("A target named '$name' already exists");
|
||||
} catch (\OCP\AppFramework\Db\DoesNotExistException) {
|
||||
}
|
||||
$this->validateName($name);
|
||||
$this->requireUsableBackend($backend);
|
||||
$keys = $this->keys->keyRing();
|
||||
$storageBackend = $this->buildBackend($backend, $auth, $options, $basePath);
|
||||
if ($storageBackend->exists('config')) {
|
||||
@@ -97,6 +86,103 @@ final class TargetService {
|
||||
return ['target' => $target, 'created' => $created];
|
||||
}
|
||||
|
||||
/**
|
||||
* Change how an existing location is reached (credentials, host, folder, even the connection
|
||||
* type) or its name. The new settings must lead to the same repository: the history stays
|
||||
* and a typo cannot silently start a new, empty repository. A null option value keeps the
|
||||
* stored one, so the web interface never has to send stored passwords back.
|
||||
*
|
||||
* @param array<string, mixed> $options
|
||||
*/
|
||||
public function update(Target $target, string $name, string $backend, string $auth, array $options, string $basePath): Target {
|
||||
$this->validateName($name, $target->getId());
|
||||
$this->requireUsableBackend($backend);
|
||||
$stored = $this->options($target);
|
||||
foreach ($options as $key => $value) {
|
||||
if ($value === null) {
|
||||
if (array_key_exists($key, $stored)) {
|
||||
$options[$key] = $stored[$key];
|
||||
} else {
|
||||
unset($options[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Do not create the folder: a typo in the path must not leave an empty folder behind.
|
||||
$storageBackend = $this->buildBackend($backend, $auth, $options, $basePath, false);
|
||||
if (!$storageBackend->exists('config')) {
|
||||
if (!$storageBackend->canConnect()) {
|
||||
throw new \InvalidArgumentException('Cannot connect with these settings. Check the host, user name and password');
|
||||
}
|
||||
throw new \InvalidArgumentException("No NG Backup repository found in folder '" . trim($basePath, '/') . "'. To start an empty repository, add a new location instead");
|
||||
}
|
||||
$repo = Repository::openWithKey($storageBackend, $this->keys->keyRing(), $this->indexCache, $this->anchor);
|
||||
if ($target->getRepositoryId() !== null && $repo->id() !== $target->getRepositoryId()) {
|
||||
throw new \InvalidArgumentException('These settings lead to a different repository than the one of this location');
|
||||
}
|
||||
$target->setName($name);
|
||||
$target->setBackend($backend);
|
||||
$target->setAuth($auth);
|
||||
$target->setOptions($this->crypto->encrypt(json_encode($options, JSON_THROW_ON_ERROR)));
|
||||
$target->setBasePath(trim($basePath, '/'));
|
||||
return $this->mapper->update($target);
|
||||
}
|
||||
|
||||
/**
|
||||
* The stored options for an edit form: password-type values are replaced by null, which
|
||||
* update() reads as "keep the stored value".
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function editableOptions(Target $target): array {
|
||||
$secret = [];
|
||||
foreach ($this->availableBackends() as $b) {
|
||||
if ($b['id'] !== $target->getBackend()) {
|
||||
continue;
|
||||
}
|
||||
$params = $b['parameters'];
|
||||
foreach ($b['auth'] as $a) {
|
||||
if ($a['id'] === $target->getAuth()) {
|
||||
$params += $a['parameters'];
|
||||
}
|
||||
}
|
||||
foreach ($params as $key => $p) {
|
||||
if (($p['type'] ?? 0) === 2) {
|
||||
$secret[$key] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
$options = $this->options($target);
|
||||
foreach ($options as $key => $value) {
|
||||
if (isset($secret[$key])) {
|
||||
$options[$key] = null;
|
||||
}
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
|
||||
private function validateName(string $name, ?int $ownId = null): void {
|
||||
if (!preg_match('/^[A-Za-z0-9][A-Za-z0-9 _.-]{0,63}$/', $name)) {
|
||||
throw new \InvalidArgumentException('Name: letters, digits, space, _ . - (max 64)');
|
||||
}
|
||||
try {
|
||||
$other = $this->mapper->findByName($name);
|
||||
} catch (DoesNotExistException) {
|
||||
return;
|
||||
}
|
||||
if ($other->getId() !== $ownId) {
|
||||
throw new \InvalidArgumentException("A target named '$name' already exists");
|
||||
}
|
||||
}
|
||||
|
||||
private function requireUsableBackend(string $backend): void {
|
||||
// Without the native extension files_external falls back to the smbclient binary, which
|
||||
// registers fine but cannot seek: the first backup then fails with "Seek failed". Refuse
|
||||
// up front instead of letting the admin find out from a failed run.
|
||||
if ($backend === 'smb' && !extension_loaded('smbclient')) {
|
||||
throw new \InvalidArgumentException('The SMB backend needs the PHP smbclient extension (pecl install smbclient); the smbclient binary alone cannot seek and every backup would fail');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A target's options are encrypted with Nextcloud's own instance secret (ICrypto), not
|
||||
* ng_backup's key, so changing that secret (occ backup:restore:full merging config.php)
|
||||
@@ -193,8 +279,9 @@ final class TargetService {
|
||||
return json_decode($this->crypto->decrypt($target->getOptions()), true, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
private function buildBackend(string $backend, string $auth, array $options, string $basePath): StorageBackend {
|
||||
return new StorageBackend($this->factory()->create($backend, $auth, $options), $basePath);
|
||||
private function buildBackend(string $backend, string $auth, array $options, string $basePath, bool $createBase = true): StorageBackend {
|
||||
$create = fn (): IStorage => $this->factory()->create($backend, $auth, $options);
|
||||
return new StorageBackend($create(), $basePath, $createBase, $create);
|
||||
}
|
||||
|
||||
private ?ExternalStorageFactory $factory = null;
|
||||
|
||||
+123
-13
@@ -7,42 +7,152 @@ declare(strict_types=1);
|
||||
|
||||
namespace OCA\NgBackup\Service;
|
||||
|
||||
use OCA\NgBackup\AppInfo\Application;
|
||||
use OCA\NgBackup\Db\Target;
|
||||
use OCP\IAppConfig;
|
||||
use OCP\Lock\LockedException;
|
||||
|
||||
/** Checksum audit of a snapshot (occ backup:verify), see Repository::verify(). */
|
||||
/**
|
||||
* Checksum audit of a snapshot (occ backup:verify), see Repository::verify(). On a large
|
||||
* repository a check takes hours (a deep one days), so how far it is gets stored every
|
||||
* PROGRESS_SECONDS, together with how it ended: `occ backup:status` shows it.
|
||||
*/
|
||||
final class VerifyService {
|
||||
public const PROGRESS_SECONDS = 30;
|
||||
private const KEY_PREFIX = 'verify_progress_';
|
||||
|
||||
public function __construct(
|
||||
private TargetService $targets,
|
||||
private LeaseService $leases,
|
||||
private IAppConfig $appConfig,
|
||||
) {
|
||||
}
|
||||
|
||||
/** @return array{filesChecked:int, blobsChecked:int, bytesChecked:int, missing:list<string>, failed:list<string>} */
|
||||
public function verify(Target $target, string $snapshotId, bool $deep = false): array {
|
||||
return $this->run($target, $deep, fn (\OCA\NgBackup\Repository\Repository $repo, callable $heartbeat): array => $repo->verify($snapshotId, $deep, $heartbeat));
|
||||
/**
|
||||
* @param ?callable(array):void $onProgress called with the stored progress every PROGRESS_SECONDS
|
||||
* @return array{filesChecked:int, blobsChecked:int, bytesChecked:int, missing:list<string>, failed:list<string>, unreadable:list<string>, readError:string}
|
||||
*/
|
||||
public function verify(Target $target, string $snapshotId, bool $deep = false, ?callable $onProgress = null): array {
|
||||
return $this->run($target, $snapshotId, $deep, $onProgress, fn (\OCA\NgBackup\Repository\Repository $repo, callable $heartbeat): array => $repo->verify($snapshotId, $deep, $heartbeat));
|
||||
}
|
||||
|
||||
/** @return array{filesChecked:int, blobsChecked:int, bytesChecked:int, missing:list<string>, failed:list<string>} */
|
||||
public function verifyUserExport(Target $target, string $manifestPath, bool $deep = false): array {
|
||||
return $this->run($target, $deep, fn (\OCA\NgBackup\Repository\Repository $repo, callable $heartbeat): array => $repo->verifyUserExport($manifestPath, $deep, $heartbeat));
|
||||
/**
|
||||
* @param ?callable(array):void $onProgress called with the stored progress every PROGRESS_SECONDS
|
||||
* @return array{filesChecked:int, blobsChecked:int, bytesChecked:int, missing:list<string>, failed:list<string>, unreadable:list<string>, readError:string}
|
||||
*/
|
||||
public function verifyUserExport(Target $target, string $manifestPath, bool $deep = false, ?callable $onProgress = null): array {
|
||||
return $this->run($target, $manifestPath, $deep, $onProgress, fn (\OCA\NgBackup\Repository\Repository $repo, callable $heartbeat): array => $repo->verifyUserExport($manifestPath, $deep, $heartbeat));
|
||||
}
|
||||
|
||||
private function run(Target $target, bool $deep, callable $fn): array {
|
||||
/** One line on a check's progress or outcome, for occ (see progress()). */
|
||||
public static function describe(array $s): string {
|
||||
$size = static fn (int $b): string => $b >= 1073741824 ? sprintf('%.1f GiB', (float)$b / 1073741824.0) : sprintf('%.1f MiB', (float)$b / 1048576.0);
|
||||
$count = static fn (int $done, mixed $total): string => number_format($done)
|
||||
. (is_int($total) && $total > 0 ? '/' . number_format($total) . sprintf(' (%d%%)', intdiv(100 * $done, $total)) : '');
|
||||
$deep = ($s['deep'] ?? false) === true;
|
||||
$kind = $deep ? 'deep check' : 'check';
|
||||
if (($s['status'] ?? '') === 'running') {
|
||||
$phase = match ($s['phase'] ?? '') {
|
||||
'index' => 'index files ' . $count((int)$s['done'], $s['total'] ?? null),
|
||||
'packs' => 'pack folders ' . $count((int)$s['done'], $s['total'] ?? null),
|
||||
'files', 'database' => 'files ' . $count((int)$s['done'], $s['total'] ?? null) . ', ' . number_format((int)($s['blobs'] ?? 0)) . ' blobs'
|
||||
. ($deep ? ', ' . $size((int)($s['bytes'] ?? 0)) . ' decrypted' : ''),
|
||||
default => 'starting',
|
||||
};
|
||||
$age = time() - (int)($s['updated'] ?? 0);
|
||||
return sprintf('%s running since %s: %s; updated %s ago%s', $kind, date('Y-m-d H:i', (int)$s['started']), $phase,
|
||||
$age < 120 ? $age . ' s' : intdiv($age, 60) . ' min',
|
||||
$age > 600 ? ' (no progress for a while: the process may have stopped)' : '');
|
||||
}
|
||||
$took = gmdate('H:i:s', max(0, (int)($s['finished'] ?? 0) - (int)($s['started'] ?? 0)));
|
||||
return match ($s['status'] ?? '') {
|
||||
'ok' => sprintf('%s OK on %s (took %s): %s files, %s blobs%s', $kind, date('Y-m-d H:i', (int)$s['finished']), $took,
|
||||
number_format((int)($s['files'] ?? 0)), number_format((int)($s['blobs'] ?? 0)), $deep ? ', ' . $size((int)($s['bytes'] ?? 0)) . ' decrypted' : ''),
|
||||
'problems' => sprintf('%s found PROBLEMS on %s: %d missing, %d failed, %d unreadable blob(s); run occ backup:verify again for the details',
|
||||
$kind, date('Y-m-d H:i', (int)$s['finished']), (int)($s['missing'] ?? 0), (int)($s['failedBlobs'] ?? 0), (int)($s['unreadable'] ?? 0)),
|
||||
default => sprintf('%s FAILED on %s: %s', $kind, date('Y-m-d H:i', (int)($s['finished'] ?? 0)), (string)($s['error'] ?? 'unknown error')),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The stored state of the last check per location: running (with how far it is) or how it
|
||||
* ended.
|
||||
*
|
||||
* @return array<int, array<string, mixed>> by location id
|
||||
*/
|
||||
public function progress(): array {
|
||||
$result = [];
|
||||
foreach ($this->appConfig->getKeys(Application::APP_ID) as $key) {
|
||||
if (!str_starts_with($key, self::KEY_PREFIX)) {
|
||||
continue;
|
||||
}
|
||||
$state = json_decode($this->appConfig->getValueString(Application::APP_ID, $key, '', lazy: true), true);
|
||||
if (is_array($state)) {
|
||||
$result[(int)substr($key, strlen(self::KEY_PREFIX))] = $state;
|
||||
}
|
||||
}
|
||||
ksort($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function run(Target $target, string $what, bool $deep, ?callable $onProgress, callable $fn): array {
|
||||
$ttl = $deep ? 600 : 300;
|
||||
$state = ['what' => $what, 'deep' => $deep, 'status' => 'running', 'started' => time(), 'updated' => time(),
|
||||
'pid' => getmypid(), 'phase' => 'starting', 'done' => 0, 'total' => null, 'blobs' => 0, 'bytes' => 0];
|
||||
try {
|
||||
return $this->leases->with(PruneService::lockKey($target), LeaseService::SHARED, $ttl, function (callable $refresh) use ($target, $deep, $ttl, $fn): array {
|
||||
$last = time();
|
||||
$heartbeat = function () use ($refresh, $ttl, &$last): void {
|
||||
if (time() - $last >= intdiv($ttl, 2)) {
|
||||
$this->store($target, $state);
|
||||
} catch (\Throwable) {
|
||||
// progress reports are a convenience; the check itself goes ahead
|
||||
}
|
||||
try {
|
||||
$result = $this->leases->with(PruneService::lockKey($target), LeaseService::SHARED, $ttl, function (callable $refresh) use ($target, $ttl, $fn, $onProgress, &$state): array {
|
||||
$lastRefresh = time();
|
||||
$lastStore = time();
|
||||
$heartbeat = function (?array $progress = null) use ($refresh, $ttl, $target, $onProgress, &$lastRefresh, &$lastStore, &$state): void {
|
||||
$now = time();
|
||||
if ($now - $lastRefresh >= intdiv($ttl, 2)) {
|
||||
$refresh();
|
||||
$last = time();
|
||||
$lastRefresh = $now;
|
||||
}
|
||||
if ($progress !== null && $now - $lastStore >= self::PROGRESS_SECONDS) {
|
||||
$state = array_merge($state, $progress, ['updated' => $now]);
|
||||
try {
|
||||
$this->store($target, $state);
|
||||
} catch (\Throwable) {
|
||||
// a progress report that cannot be stored must not stop the check
|
||||
}
|
||||
$lastStore = $now;
|
||||
if ($onProgress !== null) {
|
||||
$onProgress($state);
|
||||
}
|
||||
}
|
||||
};
|
||||
return $fn($this->targets->repository($target), $heartbeat);
|
||||
});
|
||||
} catch (LockedException) {
|
||||
$this->finish($target, $state, 'failed', ['error' => 'A cleanup of the location was in progress']);
|
||||
throw new \RuntimeException('A cleanup of ' . $target->getName() . ' is in progress; try again later');
|
||||
} catch (\Throwable $e) {
|
||||
$this->finish($target, $state, 'failed', ['error' => $e->getMessage()]);
|
||||
throw $e;
|
||||
}
|
||||
$problems = count($result['missing']) + count($result['failed']) + count($result['unreadable']);
|
||||
$this->finish($target, $state, $problems === 0 ? 'ok' : 'problems', [
|
||||
'phase' => 'done', 'files' => $result['filesChecked'], 'blobs' => $result['blobsChecked'], 'bytes' => $result['bytesChecked'],
|
||||
'missing' => count($result['missing']), 'failedBlobs' => count($result['failed']), 'unreadable' => count($result['unreadable']),
|
||||
]);
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function finish(Target $target, array $state, string $status, array $extra): void {
|
||||
try {
|
||||
$this->store($target, array_merge($state, $extra, ['status' => $status, 'updated' => time(), 'finished' => time()]));
|
||||
} catch (\Throwable) {
|
||||
// the check's own result counts; its stored summary is a convenience
|
||||
}
|
||||
}
|
||||
|
||||
private function store(Target $target, array $state): void {
|
||||
$this->appConfig->setValueString(Application::APP_ID, self::KEY_PREFIX . $target->getId(), json_encode($state, JSON_THROW_ON_ERROR), lazy: true);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-7
@@ -395,7 +395,7 @@
|
||||
</file>
|
||||
<file src="lib/Service/BackupService.php">
|
||||
<InvalidOperand>
|
||||
<code><![CDATA[$deadline - microtime(true) + 120]]></code>
|
||||
<code><![CDATA[$deadline - microtime(true) + 600]]></code>
|
||||
</InvalidOperand>
|
||||
<UndefinedClass>
|
||||
<code><![CDATA[\OC]]></code>
|
||||
@@ -474,12 +474,6 @@
|
||||
</RedundantCastGivenDocblockType>
|
||||
</file>
|
||||
<file src="lib/Service/TargetService.php">
|
||||
<InvalidReturnStatement>
|
||||
<code><![CDATA[$this->factory()->describeBackends()]]></code>
|
||||
</InvalidReturnStatement>
|
||||
<InvalidReturnType>
|
||||
<code><![CDATA[list<array{id:string, name:string, auth:list<string>, parameters:array}>]]></code>
|
||||
</InvalidReturnType>
|
||||
<UndefinedDocblockClass>
|
||||
<code><![CDATA[Server::get(\OCA\Files_External\Service\BackendService::class)]]></code>
|
||||
</UndefinedDocblockClass>
|
||||
|
||||
@@ -82,6 +82,7 @@ onBeforeUnmount(() => clearTimeout(timer))
|
||||
.ngb-tabs {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
margin: 0 0 12px;
|
||||
/* Same inline margin as NcSettingsSection, so the tabs line up with the section content */
|
||||
margin: 0 calc(var(--default-grid-baseline) * 7) 12px;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -27,6 +27,8 @@ export default {
|
||||
confirmKit: (accepted, phrase, code) => call('post', '/key/confirm', { accepted, phrase, code }),
|
||||
backends: () => call('get', '/backends'),
|
||||
addTarget: (target) => call('post', '/targets', target),
|
||||
targetSettings: (id) => call('get', `/targets/${id}/settings`),
|
||||
updateTarget: (id, target) => call('put', `/targets/${id}`, target),
|
||||
removeTarget: (id) => call('delete', `/targets/${id}`),
|
||||
testTarget: (id) => call('post', `/targets/${id}/test`),
|
||||
setTargetLimit: (id, maxGb) => call('put', `/targets/${id}/limit`, { maxGb }),
|
||||
|
||||
+31
-24
@@ -30,30 +30,34 @@
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<table v-if="status.runs.length" class="ngb-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ t('ng_backup', 'Started') }}</th>
|
||||
<th>{{ t('ng_backup', 'Location') }}</th>
|
||||
<th>{{ t('ng_backup', 'Kind') }}</th>
|
||||
<th>{{ t('ng_backup', 'Restore point') }}</th>
|
||||
<th>{{ t('ng_backup', 'Status') }}</th>
|
||||
<th>{{ t('ng_backup', 'Files') }}</th>
|
||||
<th>{{ t('ng_backup', 'Uploaded') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="run in status.runs" :key="run.id" :title="run.error || ''">
|
||||
<td>{{ new Date(run.startedAt * 1000).toLocaleString() }}</td>
|
||||
<td>{{ targetName(run.targetId) }}</td>
|
||||
<td>{{ run.kind }}</td>
|
||||
<td>{{ run.snapshot ? new Date(run.snapshot.createdAt * 1000).toLocaleString() + (run.snapshot.label ? ' – ' + run.snapshot.label : '') : '' }}</td>
|
||||
<td :class="'ngb-' + run.status">{{ run.status }}{{ run.status === 'running' ? ' (' + run.phase + ')' : '' }}{{ run.error ? ': ' + run.error : '' }}</td>
|
||||
<td>{{ run.stats?.files?.files ?? run.stats?.restored ?? '' }}</td>
|
||||
<td>{{ run.stats?.files ? formatSize(run.stats.files.uploaded) : '' }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div v-if="status.runs.length" class="ngb-table-wrap">
|
||||
<table class="ngb-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ t('ng_backup', 'Started') }}</th>
|
||||
<th>{{ t('ng_backup', 'Location') }}</th>
|
||||
<th>{{ t('ng_backup', 'Kind') }}</th>
|
||||
<th>{{ t('ng_backup', 'Restore point') }}</th>
|
||||
<th>{{ t('ng_backup', 'Status') }}</th>
|
||||
<th>{{ t('ng_backup', 'Files') }}</th>
|
||||
<th :title="t('ng_backup', 'Total size of the files in this backup, unchanged files included')">{{ t('ng_backup', 'Size') }}</th>
|
||||
<th>{{ t('ng_backup', 'Uploaded') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="run in status.runs" :key="run.id" :title="run.error || ''">
|
||||
<td>{{ new Date(run.startedAt * 1000).toLocaleString() }}</td>
|
||||
<td>{{ targetName(run.targetId) }}</td>
|
||||
<td>{{ run.kind }}</td>
|
||||
<td>{{ run.snapshot ? new Date(run.snapshot.createdAt * 1000).toLocaleString() + (run.snapshot.label ? ' – ' + run.snapshot.label : '') : '' }}</td>
|
||||
<td :class="'ngb-' + run.status">{{ run.status }}{{ run.status === 'running' ? ' (' + run.phase + percent(run) + ')' : '' }}{{ run.error ? ': ' + run.error : '' }}</td>
|
||||
<td>{{ run.stats?.files?.files ?? run.stats?.restored ?? '' }}</td>
|
||||
<td>{{ run.stats?.files ? formatSize(run.stats.files.bytes) : '' }}</td>
|
||||
<td>{{ run.stats?.files ? formatSize(run.stats.files.uploaded) : '' }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</NcSettingsSection>
|
||||
</template>
|
||||
|
||||
@@ -99,6 +103,8 @@ async function untrash(target, item) {
|
||||
}
|
||||
}
|
||||
|
||||
// " 68%", or " ~68%" when the total is estimated from Nextcloud's file cache (first backup)
|
||||
const percent = (run) => run.progress ? ' ' + (run.progress.approximate ? '~' : '') + Math.floor(run.progress.fraction * 100) + '%' : ''
|
||||
const isRunning = (targetId) => props.status.runs.some(r => r.targetId === targetId && r.kind === 'full' && r.status === 'running')
|
||||
const targetName = (id) => props.status.targets.find(tg => tg.id === id)?.name ?? '#' + id
|
||||
|
||||
@@ -126,6 +132,7 @@ async function saveSchedule() {
|
||||
<style scoped>
|
||||
.ngb-row { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 12px; }
|
||||
.ngb-time { max-width: 260px; }
|
||||
.ngb-table-wrap { overflow-x: auto; }
|
||||
.ngb-table { width: 100%; border-collapse: collapse; }
|
||||
.ngb-table th, .ngb-table td { text-align: start; padding: 4px 8px; border-bottom: 1px solid var(--color-border); }
|
||||
.ngb-failed { color: var(--color-error-text); }
|
||||
|
||||
@@ -14,12 +14,16 @@
|
||||
{{ tests[target.id].ok ? t('ng_backup', 'OK, {n} snapshots', { n: tests[target.id].snapshots }) : tests[target.id].error }}
|
||||
</span>
|
||||
<NcButton variant="tertiary" @click="test(target)">{{ t('ng_backup', 'Test') }}</NcButton>
|
||||
<NcButton variant="tertiary" @click="startEdit(target)">{{ t('ng_backup', 'Edit') }}</NcButton>
|
||||
<NcButton variant="tertiary" @click="remove(target)">{{ t('ng_backup', 'Remove') }}</NcButton>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<NcButton v-if="!adding" @click="startAdd">{{ t('ng_backup', 'Add location') }}</NcButton>
|
||||
<form v-else class="ngb-form" @submit.prevent="add">
|
||||
<NcButton v-if="!adding" @click="startAdd()">{{ t('ng_backup', 'Add location') }}</NcButton>
|
||||
<form v-else class="ngb-form" @submit.prevent="save">
|
||||
<NcNoteCard v-if="editing" type="info">
|
||||
{{ t('ng_backup', 'Editing "{name}". The new settings must lead to the same repository, so its backups and history are kept. Leave a password field empty to keep the stored password.', { name: editing.name }) }}
|
||||
</NcNoteCard>
|
||||
<NcTextField v-model="form.name" :label="t('ng_backup', 'Name')" />
|
||||
<NcSelect v-model="backend" :options="backends" label="name" :input-label="t('ng_backup', 'Type')" :clearable="false" />
|
||||
<NcSelect v-if="backend" v-model="auth" :options="backend.auth" label="name" :input-label="t('ng_backup', 'Authentication')" :clearable="false" />
|
||||
@@ -32,8 +36,8 @@
|
||||
<NcTextField v-model="form.maxGb" type="number" min="0" :label="t('ng_backup', 'Maximum size in GiB')"
|
||||
:helper-text="form.maxGb ? t('ng_backup', 'Backups stop at this size; administrators are warned at 80%.') : t('ng_backup', 'Empty = unlimited: everything stored is billed by your provider, and nothing stops a backup that keeps growing.')" />
|
||||
<div class="ngb-row">
|
||||
<NcButton type="submit" variant="primary" :disabled="busy || !form.name || !backend || !auth">{{ t('ng_backup', 'Connect and add') }}</NcButton>
|
||||
<NcButton @click="adding = false">{{ t('ng_backup', 'Cancel') }}</NcButton>
|
||||
<NcButton type="submit" variant="primary" :disabled="busy || !form.name || !backend || !auth">{{ editing ? t('ng_backup', 'Connect and save') : t('ng_backup', 'Connect and add') }}</NcButton>
|
||||
<NcButton @click="adding = false; editing = null">{{ t('ng_backup', 'Cancel') }}</NcButton>
|
||||
</div>
|
||||
</form>
|
||||
<NcNoteCard v-if="error" type="error">{{ error }}</NcNoteCard>
|
||||
@@ -41,7 +45,7 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed, reactive, ref, watch } from 'vue'
|
||||
import { computed, nextTick, reactive, ref, watch } from 'vue'
|
||||
import { t } from '@nextcloud/l10n'
|
||||
import NcButton from '@nextcloud/vue/components/NcButton'
|
||||
import NcCheckboxRadioSwitch from '@nextcloud/vue/components/NcCheckboxRadioSwitch'
|
||||
@@ -58,6 +62,7 @@ defineProps({ status: { type: Object, required: true } })
|
||||
const emit = defineEmits(['changed'])
|
||||
|
||||
const adding = ref(false)
|
||||
const editing = ref(null) // the target being edited, or null while adding
|
||||
const busy = ref(false)
|
||||
const error = ref('')
|
||||
const backends = ref([])
|
||||
@@ -83,34 +88,83 @@ const toFields = (params) => Object.entries(params || {})
|
||||
.map(([key, p]) => ({ key, label: p.value, type: p.type, optional: !!(p.flags & 1) }))
|
||||
const fields = computed(() => [...toFields(backend.value?.parameters), ...toFields(auth.value?.parameters)])
|
||||
|
||||
let prefilling = false
|
||||
watch(backend, (b) => {
|
||||
if (prefilling) {
|
||||
return
|
||||
}
|
||||
auth.value = b?.auth?.[0] ?? null
|
||||
form.options = {}
|
||||
})
|
||||
|
||||
async function startAdd() {
|
||||
adding.value = true
|
||||
error.value = ''
|
||||
async function loadBackends() {
|
||||
if (!backends.value.length) {
|
||||
try {
|
||||
backends.value = await api.backends()
|
||||
} catch (e) {
|
||||
error.value = e.message
|
||||
}
|
||||
backends.value = await api.backends()
|
||||
}
|
||||
}
|
||||
|
||||
async function add() {
|
||||
busy.value = true
|
||||
async function startAdd() {
|
||||
error.value = ''
|
||||
editing.value = null
|
||||
Object.assign(form, { name: '', path: 'ng_backup', options: {}, maxGb: '' })
|
||||
backend.value = null
|
||||
adding.value = true
|
||||
try {
|
||||
await loadBackends()
|
||||
} catch (e) {
|
||||
error.value = e.message
|
||||
}
|
||||
}
|
||||
|
||||
async function startEdit(target) {
|
||||
error.value = ''
|
||||
try {
|
||||
await api.addTarget({ name: form.name, backend: backend.value.id, auth: auth.value.id, options: form.options, path: form.path,
|
||||
maxGb: form.maxGb === '' ? null : Number(form.maxGb) })
|
||||
await confirmPassword()
|
||||
await loadBackends()
|
||||
const settings = await api.targetSettings(target.id)
|
||||
prefilling = true
|
||||
backend.value = backends.value.find((b) => b.id === settings.backend) ?? null
|
||||
auth.value = backend.value?.auth.find((a) => a.id === settings.auth) ?? null
|
||||
Object.assign(form, { name: settings.name, path: settings.path, options: Object.fromEntries(Object.entries(settings.options).map(([k, v]) => [k, v ?? ''])),
|
||||
maxGb: target.maxBytes ? String(+(target.maxBytes / 1073741824).toFixed(2)) : '' })
|
||||
await nextTick()
|
||||
prefilling = false
|
||||
editing.value = target
|
||||
adding.value = true
|
||||
} catch (e) {
|
||||
prefilling = false
|
||||
error.value = e?.message || ''
|
||||
}
|
||||
}
|
||||
|
||||
async function save() {
|
||||
busy.value = true
|
||||
error.value = ''
|
||||
const maxGb = form.maxGb === '' ? null : Number(form.maxGb)
|
||||
try {
|
||||
await confirmPassword()
|
||||
if (editing.value) {
|
||||
// An empty password field sends null: the server keeps the stored password.
|
||||
const options = { ...form.options }
|
||||
for (const field of fields.value) {
|
||||
if (field.type === 2 && !options[field.key]) {
|
||||
options[field.key] = null
|
||||
}
|
||||
}
|
||||
await api.updateTarget(editing.value.id, { name: form.name, backend: backend.value.id, auth: auth.value.id, options, path: form.path })
|
||||
if (maxGb !== (editing.value.maxBytes ? Number((editing.value.maxBytes / 1073741824).toFixed(2)) : null)) {
|
||||
await api.setTargetLimit(editing.value.id, maxGb)
|
||||
}
|
||||
delete tests[editing.value.id]
|
||||
} else {
|
||||
await api.addTarget({ name: form.name, backend: backend.value.id, auth: auth.value.id, options: form.options, path: form.path, maxGb })
|
||||
}
|
||||
adding.value = false
|
||||
editing.value = null
|
||||
Object.assign(form, { name: '', path: 'ng_backup', options: {}, maxGb: '' })
|
||||
emit('changed')
|
||||
} catch (e) {
|
||||
error.value = e.message
|
||||
error.value = e?.message || ''
|
||||
} finally {
|
||||
busy.value = false
|
||||
}
|
||||
|
||||
@@ -0,0 +1,593 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\NgBackup\Tests\Unit\Backend;
|
||||
|
||||
use OCA\NgBackup\Backend\BackendException;
|
||||
use OCA\NgBackup\Backend\SftpReadStream;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* SftpReadStream against a fake of the phpseclib connection: an SFTP server that answers READ
|
||||
* requests from in-memory files, optionally capped. Every answer must be asked for by its request
|
||||
* id, as phpseclib matches them: a server may answer out of order.
|
||||
*/
|
||||
class SftpReadStreamTest extends TestCase {
|
||||
protected function setUp(): void {
|
||||
SftpReadStream::reset();
|
||||
}
|
||||
|
||||
public function testReadsAWholeFileWithSeveralRequestsInFlight(): void {
|
||||
$data = random_bytes(5 * 1048576 + 12345);
|
||||
$sftp = new FakeSftpConnection(['/home/repo/packs/ab/abcd' => $data]);
|
||||
$fh = SftpReadStream::open(new FakeSftpStorage($sftp), 'repo/packs/ab/abcd');
|
||||
|
||||
$this->assertIsResource($fh);
|
||||
$this->assertSame($data, stream_get_contents($fh));
|
||||
$this->assertTrue(feof($fh));
|
||||
fclose($fh);
|
||||
$this->assertSame(SftpReadStream::DEPTH, $sftp->maxInFlight);
|
||||
$this->assertSame(0, $sftp->pending(), 'nothing may be left on the connection after close');
|
||||
$this->assertSame([], $sftp->openHandles);
|
||||
}
|
||||
|
||||
public function testAServerThatCapsAReadStillGivesEveryByteInOrder(): void {
|
||||
$data = random_bytes(3 * 1048576 + 7);
|
||||
$sftp = new FakeSftpConnection(['/home/f' => $data], cap: 100000);
|
||||
$fh = SftpReadStream::open(new FakeSftpStorage($sftp), 'f');
|
||||
|
||||
$this->assertSame($data, stream_get_contents($fh));
|
||||
fclose($fh);
|
||||
$this->assertSame(0, $sftp->pending());
|
||||
|
||||
// The cap is learned: a next stream asks for no more than the server gives.
|
||||
$sftp->reads = [];
|
||||
$fh = SftpReadStream::open(new FakeSftpStorage($sftp), 'f');
|
||||
$this->assertSame($data, stream_get_contents($fh));
|
||||
fclose($fh);
|
||||
$this->assertSame([100000], array_values(array_unique($sftp->reads)));
|
||||
|
||||
// Only for that connection.
|
||||
$other = new FakeSftpConnection(['/home/f' => $data]);
|
||||
$fh = SftpReadStream::open(new FakeSftpStorage($other), 'f');
|
||||
$this->assertSame($data, stream_get_contents($fh));
|
||||
fclose($fh);
|
||||
$this->assertSame(261120, max($other->reads));
|
||||
}
|
||||
|
||||
public function testATinyAnswerDoesNotShrinkTheReadsBelowWhatEveryServerSupports(): void {
|
||||
$data = random_bytes(1048576);
|
||||
$sftp = new FakeSftpConnection(['/home/f' => $data], cap: 1000);
|
||||
$fh = SftpReadStream::open(new FakeSftpStorage($sftp), 'f');
|
||||
$this->assertSame($data, stream_get_contents($fh));
|
||||
fclose($fh);
|
||||
$this->assertSame(32768, min($sftp->reads));
|
||||
}
|
||||
|
||||
public function testSeekingReadsFromTheNewOffset(): void {
|
||||
$data = random_bytes(3 * 1048576);
|
||||
$sftp = new FakeSftpConnection(['/home/f' => $data]);
|
||||
$fh = SftpReadStream::open(new FakeSftpStorage($sftp), 'f');
|
||||
|
||||
$this->assertSame(0, fseek($fh, 2000000));
|
||||
$this->assertSame(2000000, ftell($fh));
|
||||
$this->assertSame(substr($data, 2000000, 1000), fread($fh, 1000));
|
||||
$this->assertSame(0, fseek($fh, 10)); // backwards
|
||||
$this->assertSame(substr($data, 10, 70000), $this->readExactly($fh, 70000));
|
||||
$this->assertSame(0, fseek($fh, 100, SEEK_CUR)); // within what is already buffered
|
||||
$this->assertSame(substr($data, 70110, 5000), $this->readExactly($fh, 5000));
|
||||
fclose($fh);
|
||||
$this->assertSame(0, $sftp->pending());
|
||||
}
|
||||
|
||||
public function testASmallReadDoesNotFetchTheWholePipelineAhead(): void {
|
||||
$sftp = new FakeSftpConnection(['/home/f' => random_bytes(10 * 1048576)]);
|
||||
$fh = SftpReadStream::open(new FakeSftpStorage($sftp), 'f');
|
||||
fread($fh, 100);
|
||||
fclose($fh);
|
||||
$this->assertCount(1, $sftp->reads, 'the first request goes alone, the pipeline grows while reading on');
|
||||
}
|
||||
|
||||
public function testAnEmptyFileIsAtEndAtOnce(): void {
|
||||
$sftp = new FakeSftpConnection(['/home/f' => '']);
|
||||
$fh = SftpReadStream::open(new FakeSftpStorage($sftp), 'f');
|
||||
$this->assertSame('', stream_get_contents($fh));
|
||||
$this->assertTrue(feof($fh));
|
||||
fclose($fh);
|
||||
}
|
||||
|
||||
public function testAMissingFileCannotBeOpened(): void {
|
||||
$sftp = new FakeSftpConnection([]);
|
||||
$this->assertFalse(SftpReadStream::open(new FakeSftpStorage($sftp), 'nope'));
|
||||
$this->assertSame(0, $sftp->pending());
|
||||
}
|
||||
|
||||
public function testAServerErrorGoesOnWithTheStoragesOwnStreamFromTheSamePosition(): void {
|
||||
$data = random_bytes(3 * 1048576);
|
||||
$sftp = new FakeSftpConnection(['/home/f' => $data], failAt: 1048576);
|
||||
$storage = new FakeSftpStorage($sftp);
|
||||
$fh = SftpReadStream::open($storage, 'f');
|
||||
|
||||
$this->assertSame($data, $this->readAll($fh));
|
||||
$this->assertSame(['f'], $storage->fopens);
|
||||
$this->assertSame(strlen($data), ftell($fh));
|
||||
fclose($fh);
|
||||
$this->assertSame(0, $sftp->pending(), 'the connection is clean for the storage\'s own stream');
|
||||
$this->assertSame([], $sftp->openHandles, 'the failed handle is closed');
|
||||
|
||||
// One failure is no reason to give up on the fast reads.
|
||||
$fh = SftpReadStream::open($storage, 'f');
|
||||
$this->assertIsResource($fh);
|
||||
fclose($fh);
|
||||
}
|
||||
|
||||
public function testOnlyFailuresInARowSwitchTheFastReadsOff(): void {
|
||||
$data = random_bytes(2 * 1048576);
|
||||
$failing = new FakeSftpStorage(new FakeSftpConnection(['/home/f' => $data], failAt: 1048576));
|
||||
$working = new FakeSftpStorage(new FakeSftpConnection(['/home/f' => $data]));
|
||||
for ($i = 1; $i < SftpReadStream::MAX_FAILURES; $i++) {
|
||||
$fh = SftpReadStream::open($failing, 'f');
|
||||
$this->assertSame($data, $this->readAll($fh));
|
||||
fclose($fh);
|
||||
}
|
||||
$fh = SftpReadStream::open($working, 'f'); // a stream that works resets the count
|
||||
$this->assertSame($data, $this->readAll($fh));
|
||||
fclose($fh);
|
||||
for ($i = 1; $i <= SftpReadStream::MAX_FAILURES; $i++) {
|
||||
$fh = SftpReadStream::open($failing, 'f');
|
||||
$this->assertIsResource($fh, "failure $i");
|
||||
$this->assertSame($data, $this->readAll($fh));
|
||||
fclose($fh);
|
||||
}
|
||||
$this->assertNull(SftpReadStream::open($working, 'f'));
|
||||
$this->assertSame(SftpReadStream::MAX_FAILURES * 2 - 1, count($failing->fopens), 'every failed stream still gave all its data');
|
||||
}
|
||||
|
||||
public function testAServerErrorAfterASeekGoesOnFromTheSeekedPosition(): void {
|
||||
$data = random_bytes(3 * 1048576);
|
||||
$sftp = new FakeSftpConnection(['/home/f' => $data], failAt: 2500000);
|
||||
$storage = new FakeSftpStorage($sftp);
|
||||
$fh = SftpReadStream::open($storage, 'f');
|
||||
$this->assertSame(0, fseek($fh, 2400000));
|
||||
$this->assertSame(substr($data, 2400000), $this->readAll($fh));
|
||||
$this->assertSame(0, fseek($fh, 100)); // seeks go to the storage's stream now
|
||||
$this->assertSame(substr($data, 100, 1000), $this->readExactly($fh, 1000));
|
||||
fclose($fh);
|
||||
}
|
||||
|
||||
public function testAConnectionErrorDoesNotFallBackOnTheSameConnection(): void {
|
||||
// After an exception, answers may still be on their way; files_external's stream would
|
||||
// take them for its own.
|
||||
$sftp = new FakeSftpConnection(['/home/f' => random_bytes(3 * 1048576)], throwAt: 1048576);
|
||||
$storage = new FakeSftpStorage($sftp);
|
||||
$fh = SftpReadStream::open($storage, 'f');
|
||||
$this->assertLessThan(3 * 1048576, strlen($this->readAll($fh)));
|
||||
fclose($fh);
|
||||
$this->assertSame([], $storage->fopens);
|
||||
}
|
||||
|
||||
public function testALostConnectionDoesNotFallBackEither(): void {
|
||||
$sftp = new FakeSftpConnection(['/home/f' => random_bytes(3 * 1048576)], loseAt: 1048576);
|
||||
$storage = new FakeSftpStorage($sftp);
|
||||
$fh = SftpReadStream::open($storage, 'f');
|
||||
$this->assertLessThan(3 * 1048576, strlen($this->readAll($fh)));
|
||||
fclose($fh);
|
||||
$this->assertSame([], $storage->fopens);
|
||||
}
|
||||
|
||||
public function testWhenTheStoragesOwnStreamFailsTooTheReadFails(): void {
|
||||
$sftp = new FakeSftpConnection(['/home/f' => random_bytes(3 * 1048576)], failAt: 1048576);
|
||||
$storage = new FakeSftpStorage($sftp, fopenFails: true);
|
||||
$fh = SftpReadStream::open($storage, 'f');
|
||||
$this->assertLessThan(3 * 1048576, strlen($this->readAll($fh)));
|
||||
fclose($fh);
|
||||
$this->assertSame(['f'], $storage->fopens);
|
||||
}
|
||||
|
||||
public function testAConnectionErrorWhileOpeningIsAnErrorNotAMissingFile(): void {
|
||||
// The OPEN request went out: the storage's own stream must not be tried on this connection.
|
||||
$storage = new FakeSftpStorage(new FakeSftpConnection(['/home/f' => 'x'], throwAt: -1));
|
||||
try {
|
||||
SftpReadStream::open($storage, 'f');
|
||||
$this->fail('expected a BackendException');
|
||||
} catch (BackendException $e) {
|
||||
$this->assertStringContainsString('connection', $e->getMessage());
|
||||
}
|
||||
$lost = new FakeSftpStorage(new FakeSftpConnection(['/home/f' => 'x'], loseAt: -1));
|
||||
$this->expectException(BackendException::class);
|
||||
SftpReadStream::open($lost, 'f');
|
||||
}
|
||||
|
||||
public function testAConnectionErrorWhileOpeningDoesNotStopTheNextFile(): void {
|
||||
try {
|
||||
SftpReadStream::open(new FakeSftpStorage(new FakeSftpConnection(['/home/f' => 'x'], throwAt: -1)), 'f');
|
||||
} catch (BackendException) {
|
||||
}
|
||||
$fh = SftpReadStream::open(new FakeSftpStorage(new FakeSftpConnection(['/home/f' => 'x'])), 'f');
|
||||
$this->assertSame('x', stream_get_contents($fh));
|
||||
fclose($fh);
|
||||
}
|
||||
|
||||
public function testAFailedCloseDoesNotFallBackOnTheSameConnection(): void {
|
||||
$sftp = new FakeSftpConnection(['/home/f' => random_bytes(3 * 1048576)], failAt: 1048576, closeFails: true);
|
||||
$storage = new FakeSftpStorage($sftp);
|
||||
$fh = SftpReadStream::open($storage, 'f');
|
||||
$this->assertLessThan(3 * 1048576, strlen($this->readAll($fh)));
|
||||
fclose($fh);
|
||||
$this->assertSame([], $storage->fopens);
|
||||
}
|
||||
|
||||
public function testAStorageThatThrowsIsNotUsed(): void {
|
||||
$storage = new class {
|
||||
public function getRoot(): string {
|
||||
return '/';
|
||||
}
|
||||
|
||||
public function getConnection(): object {
|
||||
throw new \RuntimeException('Login failed');
|
||||
}
|
||||
};
|
||||
$this->assertNull(SftpReadStream::open($storage, 'f'));
|
||||
}
|
||||
|
||||
public function testReadManyReadsSmallFilesInBatches(): void {
|
||||
$files = [];
|
||||
for ($i = 0; $i < 70; $i++) {
|
||||
// one empty file on purpose (random_bytes(0) throws)
|
||||
$files["/home/index/$i"] = $i === 0 ? '' : random_bytes(random_int(1, 5000));
|
||||
}
|
||||
$files['/home/index/big'] = random_bytes(600000); // more than one READ
|
||||
$sftp = new FakeSftpConnection($files);
|
||||
$paths = array_map(fn ($p) => substr($p, 6), array_keys($files));
|
||||
|
||||
$got = SftpReadStream::readMany(new FakeSftpStorage($sftp), $paths);
|
||||
|
||||
$this->assertSame(array_combine($paths, array_values($files)), $got);
|
||||
$this->assertSame(SftpReadStream::BATCH, $sftp->maxInFlight, 'a batch goes out at once');
|
||||
$this->assertSame(0, $sftp->pending());
|
||||
$this->assertSame([], $sftp->openHandles);
|
||||
}
|
||||
|
||||
public function testReadManyLeavesAMissingOrFailingFileToTheCaller(): void {
|
||||
$sftp = new FakeSftpConnection(['/home/a' => 'aaa', '/home/b' => random_bytes(400000), '/home/c' => 'ccc'], failAt: 300000);
|
||||
|
||||
$got = SftpReadStream::readMany(new FakeSftpStorage($sftp), ['a', 'missing', 'b', 'c']);
|
||||
|
||||
$this->assertSame(['a' => 'aaa', 'missing' => null, 'b' => null, 'c' => 'ccc'], $got);
|
||||
$this->assertSame(0, $sftp->pending());
|
||||
$this->assertSame([], $sftp->openHandles);
|
||||
}
|
||||
|
||||
public function testReadManyAcrossACappedServer(): void {
|
||||
$data = random_bytes(300000);
|
||||
$sftp = new FakeSftpConnection(['/home/a' => $data, '/home/b' => 'b'], cap: 100000);
|
||||
$this->assertSame(['a' => $data, 'b' => 'b'], SftpReadStream::readMany(new FakeSftpStorage($sftp), ['a', 'b']));
|
||||
}
|
||||
|
||||
public function testReadManyEndsTheConnectionOnAConnectionError(): void {
|
||||
// Answers to requests already out may still come: files_external must not get them.
|
||||
$sftp = new FakeSftpConnection(['/home/a' => random_bytes(10), '/home/b' => 'b'], loseAt: 0);
|
||||
try {
|
||||
SftpReadStream::readMany(new FakeSftpStorage($sftp), ['a', 'b']);
|
||||
$this->fail('expected a BackendException');
|
||||
} catch (BackendException) {
|
||||
}
|
||||
$this->assertTrue($sftp->disconnected);
|
||||
}
|
||||
|
||||
public function testReadManyStopsBatchingWhenCloseIsNotConfirmed(): void {
|
||||
$files = [];
|
||||
for ($i = 0; $i < 40; $i++) {
|
||||
$files["/home/$i"] = "file $i";
|
||||
}
|
||||
$sftp = new FakeSftpConnection($files, closeStatus: 4);
|
||||
$got = SftpReadStream::readMany(new FakeSftpStorage($sftp), array_map('strval', range(0, 39)));
|
||||
$this->assertSame('file 0', $got['0']);
|
||||
$this->assertSame('file 31', $got['31']);
|
||||
$this->assertNull($got['32'], 'no second batch after unconfirmed closes');
|
||||
$this->assertCount(SftpReadStream::BATCH, array_filter($got, fn ($v) => $v !== null));
|
||||
// Nor on a later call over the same connection.
|
||||
$this->assertNull(SftpReadStream::readMany(new FakeSftpStorage($sftp), ['0']));
|
||||
}
|
||||
|
||||
public function testReadManyLeavesAMalformedAnswerToTheCaller(): void {
|
||||
$sftp = new FakeSftpConnection(['/home/a' => 'aaaa', '/home/b' => 'bbbb'], malformed: '/home/a');
|
||||
$this->assertSame(['a' => null, 'b' => 'bbbb'], SftpReadStream::readMany(new FakeSftpStorage($sftp), ['a', 'b']));
|
||||
$this->assertSame([], $sftp->openHandles);
|
||||
}
|
||||
|
||||
public function testReadManyOnAnUnsupportedConnectionIsNull(): void {
|
||||
$this->assertNull(SftpReadStream::readMany(new \stdClass(), ['a']));
|
||||
$this->assertNull(SftpReadStream::readMany(new FakeSftpStorage(new FakeSftpConnection(['/home/a' => 'a'], channelFails: true)), ['a']));
|
||||
}
|
||||
|
||||
public function testAFreshConnectionHasItsSftpChannelOpenedFirst(): void {
|
||||
// phpseclib 2 logs in at connect time but opens the SFTP channel on the first SFTP call.
|
||||
$sftp = new FakeSftpConnection(['/home/config' => 'repository config']);
|
||||
$this->assertFalse($sftp->initialised);
|
||||
$fh = SftpReadStream::open(new FakeSftpStorage($sftp), 'config');
|
||||
$this->assertIsResource($fh);
|
||||
$this->assertSame('repository config', stream_get_contents($fh));
|
||||
fclose($fh);
|
||||
}
|
||||
|
||||
public function testAChannelThatCannotBeOpenedLeavesItToTheStoragesOwnStream(): void {
|
||||
$sftp = new FakeSftpConnection(['/home/config' => 'x'], channelFails: true);
|
||||
$this->assertNull(SftpReadStream::open(new FakeSftpStorage($sftp), 'config'));
|
||||
$this->assertSame(0, $sftp->sent, 'nothing may be sent on a connection without an SFTP channel');
|
||||
}
|
||||
|
||||
public function testAConnectionWithoutTheNeededMethodsIsNotUsed(): void {
|
||||
$storage = new class {
|
||||
public function getRoot(): string {
|
||||
return '/';
|
||||
}
|
||||
|
||||
public function getConnection(): object {
|
||||
return new \stdClass();
|
||||
}
|
||||
};
|
||||
$this->assertNull(SftpReadStream::open($storage, 'f'));
|
||||
$this->assertNull(SftpReadStream::open(new \stdClass(), 'f'));
|
||||
}
|
||||
|
||||
public function testAPhpseclib3ConnectionIsNotUsed(): void {
|
||||
// phpseclib 3 has no _-prefixed methods; its packet_type and packet methods are private.
|
||||
$v3 = new class {
|
||||
private int $packet_type = -1;
|
||||
|
||||
private function _send_sftp_packet(): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
private function _get_sftp_packet(): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
public function _realpath(string $path): string {
|
||||
return $path;
|
||||
}
|
||||
|
||||
public function realpath(string $path): string {
|
||||
return $path . (string)$this->packet_type . $this->_send_sftp_packet() . $this->_get_sftp_packet();
|
||||
}
|
||||
};
|
||||
$storage = new class($v3) {
|
||||
public function __construct(
|
||||
private object $connection,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getRoot(): string {
|
||||
return '/';
|
||||
}
|
||||
|
||||
public function getConnection(): object {
|
||||
return $this->connection;
|
||||
}
|
||||
};
|
||||
$this->assertNull(SftpReadStream::open($storage, 'f'));
|
||||
}
|
||||
|
||||
public function testAPrivatePacketTypeIsNotUsed(): void {
|
||||
$conn = new class {
|
||||
private int $packet_type = -1;
|
||||
|
||||
public function _send_sftp_packet(): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function _get_sftp_packet(): string {
|
||||
return (string)$this->packet_type;
|
||||
}
|
||||
|
||||
public function _realpath(string $path): string {
|
||||
return $path;
|
||||
}
|
||||
};
|
||||
$storage = new class($conn) {
|
||||
public function __construct(
|
||||
private object $connection,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getRoot(): string {
|
||||
return '/';
|
||||
}
|
||||
|
||||
public function getConnection(): object {
|
||||
return $this->connection;
|
||||
}
|
||||
};
|
||||
$this->assertNull(SftpReadStream::open($storage, 'f'));
|
||||
}
|
||||
|
||||
/** @param resource $fh */
|
||||
private function readAll($fh): string {
|
||||
$data = '';
|
||||
while (!feof($fh)) {
|
||||
$chunk = fread($fh, 65536);
|
||||
if ($chunk === false || $chunk === '') {
|
||||
break;
|
||||
}
|
||||
$data .= $chunk;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/** @param resource $fh */
|
||||
private function readExactly($fh, int $length): string {
|
||||
$data = '';
|
||||
while (strlen($data) < $length && !feof($fh)) {
|
||||
$data .= fread($fh, $length - strlen($data));
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
final class FakeSftpStorage {
|
||||
/** @var list<string> paths opened through the storage's own stream */
|
||||
public array $fopens = [];
|
||||
|
||||
public function __construct(
|
||||
private FakeSftpConnection $connection,
|
||||
private bool $fopenFails = false,
|
||||
) {
|
||||
}
|
||||
|
||||
/** files_external's own read stream: here the file from memory, seekable. */
|
||||
public function fopen(string $path, string $mode) {
|
||||
$this->fopens[] = $path;
|
||||
$data = $this->connection->file('/home/' . $path);
|
||||
if ($this->fopenFails || $data === null) {
|
||||
return false;
|
||||
}
|
||||
$fh = fopen('php://memory', 'r+');
|
||||
fwrite($fh, $data);
|
||||
rewind($fh);
|
||||
return $fh;
|
||||
}
|
||||
|
||||
public function getRoot(): string {
|
||||
return '/home/';
|
||||
}
|
||||
|
||||
public function getConnection(): FakeSftpConnection {
|
||||
return $this->connection;
|
||||
}
|
||||
}
|
||||
|
||||
/** The part of phpseclib\Net\SFTP (2.x) that SftpReadStream uses. */
|
||||
final class FakeSftpConnection {
|
||||
public int $packet_type = -1;
|
||||
public int $maxInFlight = 0;
|
||||
/** @var list<int> lengths asked for by READ requests */
|
||||
public array $reads = [];
|
||||
/** @var array<string, string> */
|
||||
public array $openHandles = [];
|
||||
/** Whether the SFTP channel is open: phpseclib 2 opens it on the first SFTP call. */
|
||||
public bool $initialised = false;
|
||||
/** Packets sent */
|
||||
public int $sent = 0;
|
||||
/** @var array<int, array{int, string}> answers by request id, in the order the server sent them */
|
||||
private array $answers = [];
|
||||
private int $handles = 0;
|
||||
|
||||
/** @param array<string, string> $files */
|
||||
public function __construct(
|
||||
private array $files,
|
||||
private int $cap = 261120,
|
||||
private ?int $failAt = null,
|
||||
/** READ at or past this offset throws; -1: OPEN throws */
|
||||
private ?int $throwAt = null,
|
||||
/** READ at or past this offset finds the connection gone (phpseclib returns false); -1: OPEN */
|
||||
private ?int $loseAt = null,
|
||||
private bool $closeFails = false,
|
||||
private bool $channelFails = false,
|
||||
/** status code the server answers CLOSE with */
|
||||
private int $closeStatus = 0,
|
||||
/** a file whose DATA answers declare a wrong length */
|
||||
private ?string $malformed = null,
|
||||
) {
|
||||
}
|
||||
|
||||
public bool $disconnected = false;
|
||||
|
||||
public function disconnect(): void {
|
||||
$this->disconnected = true;
|
||||
}
|
||||
|
||||
public function pwd(): string|false {
|
||||
if ($this->channelFails) {
|
||||
return false;
|
||||
}
|
||||
$this->initialised = true;
|
||||
return '/home';
|
||||
}
|
||||
|
||||
public function file(string $path): ?string {
|
||||
return $this->files[$path] ?? null;
|
||||
}
|
||||
|
||||
public function pending(): int {
|
||||
return count($this->answers);
|
||||
}
|
||||
|
||||
public function _realpath(string $path): string {
|
||||
return '/' . trim(preg_replace('#/+#', '/', $path), '/');
|
||||
}
|
||||
|
||||
public function _send_sftp_packet(int $type, string $data, int $id = 1): bool {
|
||||
$this->sent++;
|
||||
if (!$this->initialised) {
|
||||
return false; // like phpseclib 2: no SFTP channel yet
|
||||
}
|
||||
switch ($type) {
|
||||
case 3: // OPEN
|
||||
if ($this->throwAt === -1) {
|
||||
throw new \RuntimeException('Connection closed by server');
|
||||
}
|
||||
if ($this->loseAt === -1) {
|
||||
return false;
|
||||
}
|
||||
$path = substr($data, 4, unpack('N', $data)[1]);
|
||||
if (!isset($this->files[$path])) {
|
||||
$this->answers[$id] = [101, pack('N', 2)];
|
||||
break;
|
||||
}
|
||||
$handle = 'h' . ++$this->handles;
|
||||
$this->openHandles[$handle] = $path;
|
||||
$this->answers[$id] = [102, pack('Na*', strlen($handle), $handle)];
|
||||
break;
|
||||
case 4: // CLOSE
|
||||
if ($this->closeFails) {
|
||||
return false;
|
||||
}
|
||||
$handle = substr($data, 4, unpack('N', $data)[1]);
|
||||
unset($this->openHandles[$handle]);
|
||||
$this->answers[$id] = [101, pack('N', $this->closeStatus)];
|
||||
break;
|
||||
case 5: // READ
|
||||
$len = unpack('N', $data)[1];
|
||||
$handle = substr($data, 4, $len);
|
||||
['hi' => $hi, 'lo' => $lo, 'n' => $n] = unpack('Nhi/Nlo/Nn', substr($data, 4 + $len));
|
||||
$offset = $hi * 4294967296 + $lo;
|
||||
$this->reads[] = $n;
|
||||
if ($this->throwAt !== null && $this->throwAt >= 0 && $offset >= $this->throwAt) {
|
||||
throw new \RuntimeException('Connection closed by server');
|
||||
}
|
||||
if ($this->loseAt !== null && $this->loseAt >= 0 && $offset >= $this->loseAt) {
|
||||
$this->answers = [];
|
||||
return false;
|
||||
}
|
||||
$file = $this->files[$this->openHandles[$handle]];
|
||||
if ($this->failAt !== null && $offset >= $this->failAt) {
|
||||
$this->answers[$id] = [101, pack('N', 4)]; // FX_FAILURE
|
||||
} elseif ($offset >= strlen($file)) {
|
||||
$this->answers[$id] = [101, pack('N', 1)]; // FX_EOF
|
||||
} else {
|
||||
$chunk = substr($file, $offset, min($n, $this->cap));
|
||||
$declared = $this->openHandles[$handle] === $this->malformed ? strlen($chunk) + 7 : strlen($chunk);
|
||||
$this->answers[$id] = [103, pack('Na*', $declared, $chunk)];
|
||||
}
|
||||
$this->maxInFlight = max($this->maxInFlight, count($this->answers));
|
||||
break;
|
||||
default:
|
||||
throw new \LogicException("unexpected packet type $type");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function _get_sftp_packet(?int $id = null): string|false {
|
||||
if ($this->answers === []) {
|
||||
throw new \LogicException('waiting for an answer that will never come');
|
||||
}
|
||||
if ($id === null || !isset($this->answers[$id])) {
|
||||
throw new \LogicException("no answer for request $id");
|
||||
}
|
||||
[$this->packet_type, $payload] = $this->answers[$id];
|
||||
unset($this->answers[$id]);
|
||||
return $payload;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\NgBackup\Tests\Unit\Backend;
|
||||
|
||||
use OCA\NgBackup\Backend\BackendException;
|
||||
use OCA\NgBackup\Backend\StorageBackend;
|
||||
use OCA\NgBackup\Tests\Unit\TempDirTrait;
|
||||
use OCP\Files\Storage\IStorage;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
require_once __DIR__ . '/../../stubs/SmbStorage.php';
|
||||
|
||||
/** On a remote storage every is_dir() is a round trip: listing index/ must not cost one per pack. */
|
||||
class StorageBackendListTest extends TestCase {
|
||||
use TempDirTrait;
|
||||
|
||||
public function testListFilesChecksOnlyTheFolderItself(): void {
|
||||
$dir = $this->tempDir();
|
||||
foreach (['b1', 'a2', 'c3.part-0123abcd'] as $name) {
|
||||
touch("$dir/$name");
|
||||
}
|
||||
$isDirCalls = 0;
|
||||
$storage = $this->createMock(IStorage::class);
|
||||
$storage->method('instanceOfStorage')->willReturn(false);
|
||||
$storage->method('is_dir')->willReturnCallback(function (string $path) use (&$isDirCalls): bool {
|
||||
$isDirCalls++;
|
||||
return $path === 'repo' || $path === 'repo/index';
|
||||
});
|
||||
$storage->method('opendir')->willReturnCallback(fn (string $path) => $path === 'repo/index' ? opendir($dir) : false);
|
||||
|
||||
$backend = new StorageBackend($storage, 'repo');
|
||||
$isDirCalls = 0; // the constructor checks the base folder
|
||||
$this->assertSame(['index/a2', 'index/b1'], $backend->listFiles('index'));
|
||||
$this->assertSame(1, $isDirCalls);
|
||||
|
||||
$isDirCalls = 0;
|
||||
$this->assertSame(['index/a2', 'index/b1'], $backend->list('index'));
|
||||
$this->assertSame(4, $isDirCalls, 'list() checks every entry: the reason listFiles() exists');
|
||||
}
|
||||
|
||||
public function testListFilesOfAMissingFolderIsEmpty(): void {
|
||||
$storage = $this->createMock(IStorage::class);
|
||||
$storage->method('instanceOfStorage')->willReturn(false);
|
||||
$storage->method('is_dir')->willReturnCallback(fn (string $path): bool => $path === 'repo');
|
||||
$storage->expects($this->never())->method('opendir');
|
||||
$this->assertSame([], (new StorageBackend($storage, 'repo'))->listFiles('snapshots'));
|
||||
}
|
||||
|
||||
public function testAnUnreadableFolderIsAnErrorNotEmpty(): void {
|
||||
$storage = $this->createMock(IStorage::class);
|
||||
$storage->method('instanceOfStorage')->willReturn(false);
|
||||
$storage->method('is_dir')->willReturn(true);
|
||||
$storage->method('opendir')->willReturn(false);
|
||||
$this->expectException(BackendException::class);
|
||||
(new StorageBackend($storage, 'repo'))->listFiles('catalog');
|
||||
}
|
||||
public function testSmbFoldersAreListedInOneRequest(): void {
|
||||
$calls = new \ArrayObject();
|
||||
$share = new class($calls) {
|
||||
public function __construct(private \ArrayObject $calls) {
|
||||
}
|
||||
|
||||
public function dir(string $path): array {
|
||||
$this->calls[] = "dir $path";
|
||||
return array_map(fn (string $n) => new class($n, $this->calls) {
|
||||
public function __construct(private string $n, private \ArrayObject $calls) {
|
||||
}
|
||||
|
||||
public function getName(): string {
|
||||
return $this->n;
|
||||
}
|
||||
|
||||
public function isHidden(): bool {
|
||||
$this->calls[] = "attributes {$this->n}";
|
||||
return $this->n === 'desktop.ini';
|
||||
}
|
||||
}, ['b1', 'a2', '.hidden', 'desktop.ini', 'c3.part-0123abcd']);
|
||||
}
|
||||
};
|
||||
$storage = $this->getMockBuilder(\OCA\Files_External\Lib\Storage\SMB::class)->disableOriginalConstructor()->getMock();
|
||||
(fn () => $this->share = $share)->call($storage);
|
||||
$storage->method('instanceOfStorage')->willReturn(false);
|
||||
$storage->method('is_dir')->willReturn(true);
|
||||
$storage->expects($this->never())->method('opendir');
|
||||
|
||||
$this->assertSame(['index/a2', 'index/b1'], (new StorageBackend($storage, 'repo'))->listFiles('index'));
|
||||
$this->assertSame(['dir repo/index', 'attributes desktop.ini', 'attributes c3.part-0123abcd'], $calls->getArrayCopy(),
|
||||
'only names a repository does not use cost a request');
|
||||
}
|
||||
|
||||
public function testSmbWithAccessControlListsIsListedTheUsualWay(): void {
|
||||
$dir = $this->tempDir();
|
||||
touch("$dir/a1");
|
||||
$storage = $this->getMockBuilder(\OCA\Files_External\Lib\Storage\SMB::class)->disableOriginalConstructor()->getMock();
|
||||
(fn () => $this->checkAcl = true)->call($storage);
|
||||
$storage->method('instanceOfStorage')->willReturn(false);
|
||||
$storage->method('is_dir')->willReturn(true);
|
||||
$storage->expects($this->once())->method('opendir')->willReturnCallback(fn () => opendir($dir));
|
||||
$this->assertSame(['index/a1'], (new StorageBackend($storage, 'repo'))->listFiles('index'));
|
||||
}
|
||||
|
||||
public function testSmbFallsBackToTheUsualListing(): void {
|
||||
$dir = $this->tempDir();
|
||||
touch("$dir/a1");
|
||||
$storage = $this->getMockBuilder(\OCA\Files_External\Lib\Storage\SMB::class)->disableOriginalConstructor()->getMock(); // no share
|
||||
$storage->method('instanceOfStorage')->willReturn(false);
|
||||
$storage->method('is_dir')->willReturn(true);
|
||||
$storage->method('opendir')->willReturnCallback(fn () => opendir($dir));
|
||||
$this->assertSame(['index/a1'], (new StorageBackend($storage, 'repo'))->listFiles('index'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\NgBackup\Tests\Unit\Backend;
|
||||
|
||||
use OCA\NgBackup\Backend\BackendException;
|
||||
use OCA\NgBackup\Backend\StorageBackend;
|
||||
use OCP\Files\Storage\IStorage;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* A Hetzner Storage Box drops an SFTP connection that carried a lot of data and then stayed
|
||||
* quiet for a few minutes; files_external keeps using the dead client, on which every file
|
||||
* looks missing. The backend builds the storage anew instead.
|
||||
*/
|
||||
class StorageBackendReconnectTest extends TestCase {
|
||||
/** @var list<ReconnectFakeSftp> */
|
||||
private array $connections = [];
|
||||
/** New connections come up dead (the location is gone). */
|
||||
private bool $deadOnArrival = false;
|
||||
|
||||
private function storage(): IStorage {
|
||||
$sftp = new ReconnectFakeSftp();
|
||||
$sftp->dropped = $this->deadOnArrival;
|
||||
$this->connections[] = $sftp;
|
||||
$storage = $this->createMock(SftpLikeStorage::class);
|
||||
$storage->method('instanceOfStorage')->willReturnCallback(fn (string $class): bool => $class === \OCA\Files_External\Lib\Storage\SFTP::class);
|
||||
$storage->method('getConnection')->willReturn($sftp);
|
||||
$storage->method('is_dir')->willReturn(true);
|
||||
$storage->method('file_exists')->willReturnCallback(fn (string $path): bool => $sftp->request() && $path === 'repo/config');
|
||||
$storage->method('fopen')->willReturnCallback(function (string $path) use ($sftp) {
|
||||
if (!$sftp->request() || $path !== 'repo/config') {
|
||||
return false;
|
||||
}
|
||||
$fh = fopen('php://memory', 'w+b');
|
||||
fwrite($fh, 'config');
|
||||
rewind($fh);
|
||||
return $fh;
|
||||
});
|
||||
return $storage;
|
||||
}
|
||||
|
||||
private function backend(bool $reconnect = true): StorageBackend {
|
||||
return new StorageBackend($this->storage(), 'repo', false, $reconnect ? fn (): IStorage => $this->storage() : null);
|
||||
}
|
||||
|
||||
private static function idle(StorageBackend $backend): void {
|
||||
(new \ReflectionProperty($backend, 'lastUsed'))->setValue($backend, microtime(true) - StorageBackend::IDLE_CHECK - 1);
|
||||
}
|
||||
|
||||
public function testADeadConnectionIsReplacedAfterAPause(): void {
|
||||
$backend = $this->backend();
|
||||
$this->connections[0]->dropped = true;
|
||||
self::idle($backend);
|
||||
$this->assertTrue($backend->exists('config'));
|
||||
$this->assertSame(1, $backend->reconnects);
|
||||
$this->assertSame(1, $this->connections[0]->requests, 'only the check went over the old connection');
|
||||
}
|
||||
|
||||
public function testAPauseOnALiveConnectionCostsOneCheck(): void {
|
||||
$backend = $this->backend();
|
||||
self::idle($backend);
|
||||
$this->assertTrue($backend->exists('config'));
|
||||
$this->assertSame(0, $backend->reconnects);
|
||||
$this->assertSame(2, $this->connections[0]->requests);
|
||||
$this->assertTrue($backend->exists('config'));
|
||||
$this->assertSame(3, $this->connections[0]->requests, 'no check without a pause');
|
||||
}
|
||||
|
||||
public function testAFailureOnAConnectionThatJustDiedIsRepeated(): void {
|
||||
$backend = $this->backend();
|
||||
$this->connections[0]->dropped = true; // without a pause: not checked first
|
||||
$this->assertTrue($backend->exists('config'));
|
||||
$fh = $backend->get('config');
|
||||
$this->assertSame('config', stream_get_contents($fh));
|
||||
$this->assertSame(1, $backend->reconnects);
|
||||
}
|
||||
|
||||
public function testAMissingFileOnALiveConnectionIsJustMissing(): void {
|
||||
$backend = $this->backend();
|
||||
$this->assertFalse($backend->exists('nothing'));
|
||||
try {
|
||||
$backend->get('nothing');
|
||||
$this->fail('missing');
|
||||
} catch (BackendException $e) {
|
||||
$this->assertStringContainsString('Not found', $e->getMessage());
|
||||
}
|
||||
$this->assertSame(0, $backend->reconnects);
|
||||
$this->assertCount(1, $this->connections);
|
||||
}
|
||||
|
||||
public function testANewConnectionThatFailsToo(): void {
|
||||
$backend = $this->backend();
|
||||
$this->connections[0]->dropped = true;
|
||||
$backend->exists('config'); // reconnects
|
||||
$this->connections[1]->dropped = true;
|
||||
$this->deadOnArrival = true;
|
||||
self::idle($backend);
|
||||
try {
|
||||
$backend->get('config');
|
||||
$this->fail('no connection');
|
||||
} catch (BackendException) {
|
||||
}
|
||||
$this->assertSame(3, $backend->reconnects, 'once after the pause, once after the failure, no more');
|
||||
}
|
||||
|
||||
public function testWithoutAWayToReconnectNothingChanges(): void {
|
||||
$backend = $this->backend(false);
|
||||
$this->connections[0]->dropped = true;
|
||||
self::idle($backend);
|
||||
$this->assertFalse($backend->exists('config'));
|
||||
$this->assertCount(1, $this->connections);
|
||||
}
|
||||
}
|
||||
|
||||
interface SftpLikeStorage extends IStorage {
|
||||
public function getConnection(): object;
|
||||
}
|
||||
|
||||
/** The public phpseclib calls the backend uses to tell a dead connection. */
|
||||
final class ReconnectFakeSftp {
|
||||
public bool $dropped = false;
|
||||
private bool $connected = true;
|
||||
public int $requests = 0;
|
||||
|
||||
public function isConnected(): bool {
|
||||
return $this->connected;
|
||||
}
|
||||
|
||||
public function clearStatCache(): void {
|
||||
}
|
||||
|
||||
public function stat(string $path): array|false {
|
||||
return $this->request() ? ['type' => 2] : false;
|
||||
}
|
||||
|
||||
/** A request: false (and from then on disconnected) when the server has dropped the connection. */
|
||||
public function request(): bool {
|
||||
$this->requests++;
|
||||
if ($this->dropped) {
|
||||
$this->connected = false;
|
||||
}
|
||||
return $this->connected;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\NgBackup\Tests\Unit\Job;
|
||||
|
||||
use OCA\NgBackup\Job\BackupRun;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class BackupProgressTest extends TestCase {
|
||||
public function testCountsFinishedFilesAndTheFileBeingRead(): void {
|
||||
$state = ['phase' => 'files', 'estimate' => ['bytes' => 1000, 'approximate' => false],
|
||||
'stats' => ['bytes' => 500], 'cur' => ['offset' => 180]];
|
||||
$this->assertSame(['fraction' => 0.68, 'approximate' => false], BackupRun::progress($state));
|
||||
}
|
||||
|
||||
public function testStaysBelowHundredPercentWhenTheEstimateIsTooLow(): void {
|
||||
$state = ['phase' => 'files', 'estimate' => ['bytes' => 1000, 'approximate' => true], 'stats' => ['bytes' => 5000], 'cur' => null];
|
||||
$this->assertSame(['fraction' => 0.99, 'approximate' => true], BackupRun::progress($state));
|
||||
}
|
||||
|
||||
public function testNoProgressWithoutEstimateOrOutsideTheFilesPhase(): void {
|
||||
$this->assertNull(BackupRun::progress(['phase' => 'files', 'stats' => ['bytes' => 1]]));
|
||||
$this->assertNull(BackupRun::progress(['phase' => 'files', 'estimate' => ['bytes' => 0, 'approximate' => true], 'stats' => ['bytes' => 1]]));
|
||||
$this->assertNull(BackupRun::progress(['phase' => 'commit', 'estimate' => ['bytes' => 10, 'approximate' => false], 'stats' => ['bytes' => 1]]));
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,8 @@ class IndexCacheTest extends TestCase {
|
||||
public function all(string $r): array { return $this->data[$r] ?? []; }
|
||||
public function put(string $r, string $p, array $e): void { $this->data[$r][$p] = $e; }
|
||||
public function remove(string $r, array $ps): void { foreach ($ps as $p) { unset($this->data[$r][$p]); } }
|
||||
public function putMany(string $r, array $packs): void { foreach ($packs as $p => $e) { $this->put($r, (string)$p, $e); } }
|
||||
public function replaceAll(string $r, array $packs): void { $this->data[$r] = []; $this->putMany($r, $packs); }
|
||||
};
|
||||
$src = $this->tempDir();
|
||||
for ($i = 0; $i < 12; $i++) {
|
||||
@@ -51,4 +53,77 @@ class IndexCacheTest extends TestCase {
|
||||
$fourth->restore($r1['snapshot'], $target);
|
||||
$this->assertCount(12, self::hashTree($target));
|
||||
}
|
||||
|
||||
/** The database dump reads in a read-only transaction: a pack that fills up then must not write the cache yet. */
|
||||
public function testHeldCacheWritesWaitForRelease(): void {
|
||||
$cache = new class implements IIndexCache {
|
||||
public array $data = [];
|
||||
public bool $readOnly = false;
|
||||
public function all(string $r): array { return $this->data[$r] ?? []; }
|
||||
public function put(string $r, string $p, array $e): void {
|
||||
if ($this->readOnly) {
|
||||
throw new \RuntimeException('cannot execute INSERT in a read-only transaction');
|
||||
}
|
||||
$this->data[$r][$p] = $e;
|
||||
}
|
||||
public function remove(string $r, array $ps): void { foreach ($ps as $p) { unset($this->data[$r][$p]); } }
|
||||
public function putMany(string $r, array $packs): void { foreach ($packs as $p => $e) { $this->put($r, (string)$p, $e); } }
|
||||
public function replaceAll(string $r, array $packs): void { $this->data[$r] = []; $this->putMany($r, $packs); }
|
||||
};
|
||||
$backend = new LocalBackend($this->tempDir());
|
||||
$keys = KeyRing::generate();
|
||||
Repository::initWithKey($backend, $keys, $keys->wrap('pw'));
|
||||
$repo = Repository::openWithKey($backend, $keys, $cache);
|
||||
|
||||
$stats = [];
|
||||
$repo->holdIndexCache();
|
||||
$cache->readOnly = true;
|
||||
$writer = $repo->blobWriter($stats);
|
||||
for ($i = 0; $i < 40; $i++) {
|
||||
$writer->write(random_bytes(1048576)); // more than one 32 MiB pack
|
||||
}
|
||||
$writer->finish();
|
||||
$this->assertGreaterThan(0, count($backend->list('index')), 'a full pack was uploaded during the hold');
|
||||
$this->assertSame([], $cache->data);
|
||||
|
||||
$cache->readOnly = false;
|
||||
$repo->releaseIndexCache();
|
||||
$repo->flushPacks();
|
||||
$this->assertCount(count($backend->list('index')), array_merge(...array_values($cache->data)));
|
||||
$this->assertSame(0, Repository::openWithKey($backend, $keys, $cache)->indexDownloads());
|
||||
}
|
||||
|
||||
/** After a check the cache holds every index file as it is, also a blob that is in two packs. */
|
||||
public function testACheckCachesTheIndexFilesAsTheyAre(): void {
|
||||
$cache = new class implements IIndexCache {
|
||||
public array $data = [];
|
||||
public int $replaced = 0;
|
||||
public function all(string $r): array { return $this->data[$r] ?? []; }
|
||||
public function put(string $r, string $p, array $e): void { $this->data[$r][$p] = $e; }
|
||||
public function remove(string $r, array $ps): void { foreach ($ps as $p) { unset($this->data[$r][$p]); } }
|
||||
public function putMany(string $r, array $packs): void { foreach ($packs as $p => $e) { $this->put($r, (string)$p, $e); } }
|
||||
public function replaceAll(string $r, array $packs): void { $this->replaced++; $this->data[$r] = $packs; }
|
||||
};
|
||||
$backend = new LocalBackend($this->tempDir());
|
||||
$cipher = new \OCA\NgBackup\Crypto\StreamCipher(KeyRing::generate());
|
||||
$index = new \OCA\NgBackup\Repository\BlobIndex($backend, $cipher);
|
||||
$index->setCache($cache, 'r');
|
||||
$blob = static fn (string $id, int $offset): array => ['id' => $id, 'offset' => $offset, 'length' => 10, 'raw' => 8, 'flags' => 0];
|
||||
$index->addPack('pack1', [$blob('a', 0), $blob('shared', 10)]);
|
||||
$index->addPack('pack2', [$blob('shared', 0), $blob('b', 10)]);
|
||||
$cache->data = ['r' => ['gone' => [$blob('x', 0)]]];
|
||||
|
||||
$index->load(true);
|
||||
$this->assertSame(1, $cache->replaced);
|
||||
$this->assertSame(['pack1', 'pack2'], array_keys($cache->data['r']));
|
||||
$this->assertCount(2, $cache->data['r']['pack1'], 'the shared blob stays in the row of its first pack');
|
||||
|
||||
// the second pack goes: the shared blob is still found in the first
|
||||
$index->removePack('pack2');
|
||||
$again = new \OCA\NgBackup\Repository\BlobIndex($backend, $cipher);
|
||||
$again->setCache($cache, 'r');
|
||||
$again->load();
|
||||
$this->assertSame(0, $again->downloaded);
|
||||
$this->assertSame('pack1', $again->get('shared')[0]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\NgBackup\Tests\Unit\Repository;
|
||||
|
||||
use OCA\NgBackup\Backend\IBackend;
|
||||
use OCA\NgBackup\Backend\LocalBackend;
|
||||
use OCA\NgBackup\Crypto\KeyRing;
|
||||
use OCA\NgBackup\Repository\Repository;
|
||||
use OCA\NgBackup\Tests\Unit\TempDirTrait;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/** Restores and deep verifies read a pack as one stream instead of one request per blob. */
|
||||
class PackReadTest extends TestCase {
|
||||
use TempDirTrait;
|
||||
|
||||
private string $root;
|
||||
private KeyRing $keys;
|
||||
private string $src;
|
||||
private string $snapshot;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->root = $this->tempDir();
|
||||
$this->keys = KeyRing::generate();
|
||||
$this->src = $this->tempDir();
|
||||
for ($i = 0; $i < 200; $i++) {
|
||||
file_put_contents(sprintf('%s/f%03d', $this->src, $i), random_bytes(20000));
|
||||
}
|
||||
$backend = new LocalBackend($this->root);
|
||||
$repo = Repository::initWithKey($backend, $this->keys, $this->keys->wrap('pw'));
|
||||
$this->snapshot = $repo->backupDirectory($this->src)['snapshot'];
|
||||
}
|
||||
|
||||
private function open(PackOpenCountingBackend $backend): Repository {
|
||||
return Repository::openWithKey($backend, $this->keys, null, null);
|
||||
}
|
||||
|
||||
public function testFullRestoreOpensEachPackOnce(): void {
|
||||
$backend = new PackOpenCountingBackend(new LocalBackend($this->root));
|
||||
$target = $this->tempDir();
|
||||
|
||||
$this->assertSame(200, $this->open($backend)->restore($this->snapshot, $target));
|
||||
|
||||
for ($i = 0; $i < 200; $i++) {
|
||||
$name = sprintf('f%03d', $i);
|
||||
$this->assertSame(file_get_contents("$this->src/$name"), file_get_contents("$target/$name"));
|
||||
}
|
||||
// One pack: one stream instead of 200 requests.
|
||||
$this->assertSame(1, $backend->packOpens());
|
||||
}
|
||||
|
||||
public function testDeepVerifyOpensEachPackOnce(): void {
|
||||
$backend = new PackOpenCountingBackend(new LocalBackend($this->root));
|
||||
|
||||
$result = $this->open($backend)->verify($this->snapshot, deep: true);
|
||||
|
||||
$this->assertSame(200 * 20000, $result['bytesChecked']);
|
||||
$this->assertSame(1, $backend->packOpens());
|
||||
}
|
||||
|
||||
public function testReadingBackwardsOpensThePackAgain(): void {
|
||||
$backend = new PackOpenCountingBackend(new LocalBackend($this->root));
|
||||
$repo = $this->open($backend);
|
||||
|
||||
$this->assertSame(1, $repo->restore($this->snapshot, $this->tempDir(), 'f150'));
|
||||
$this->assertSame(1, $repo->restore($this->snapshot, $this->tempDir(), 'f050'));
|
||||
|
||||
$this->assertSame(2, $backend->packOpens());
|
||||
}
|
||||
|
||||
public function testThePackStreamIsClosedBeforeAnyOtherRequest(): void {
|
||||
$backend = new PackOpenCountingBackend(new LocalBackend($this->root));
|
||||
$repo = $this->open($backend);
|
||||
$this->assertSame(1, $repo->restore($this->snapshot, $this->tempDir(), 'f010'));
|
||||
$this->assertTrue($backend->packStreamOpen(), 'kept open for the next read');
|
||||
|
||||
$repo->putObject('objects/after-restore', 'data');
|
||||
|
||||
$this->assertSame([false], $backend->packStreamOpenAtPut);
|
||||
}
|
||||
|
||||
public function testAPackStreamThatCannotSeekIsReadUpToTheOffset(): void {
|
||||
$backend = new PackOpenCountingBackend(new LocalBackend($this->root), $this->root);
|
||||
$target = $this->tempDir();
|
||||
|
||||
$this->assertSame(1, $this->open($backend)->restore($this->snapshot, $target, 'f150'));
|
||||
|
||||
$this->assertSame(file_get_contents("$this->src/f150"), file_get_contents("$target/f150"));
|
||||
}
|
||||
|
||||
public function testDeepVerifyOfATruncatedPackStillChecksTheBlobsBeforeTheDamage(): void {
|
||||
$packs = (new LocalBackend($this->root))->list('packs');
|
||||
$this->assertCount(1, $packs);
|
||||
$pack = $this->root . '/' . $packs[0];
|
||||
$fh = fopen($pack, 'r+b');
|
||||
ftruncate($fh, intdiv(filesize($pack), 2));
|
||||
fclose($fh);
|
||||
|
||||
$result = $this->open(new PackOpenCountingBackend(new LocalBackend($this->root)))->verify($this->snapshot, deep: true);
|
||||
|
||||
$this->assertGreaterThan(80 * 20000, $result['bytesChecked']);
|
||||
$this->assertNotSame([], $result['unreadable']);
|
||||
$this->assertSame([], $result['failed']);
|
||||
}
|
||||
}
|
||||
|
||||
final class PackOpenCountingBackend implements IBackend {
|
||||
use \OCA\NgBackup\Backend\GetsOneByOne;
|
||||
|
||||
/** @var list<string> */
|
||||
public array $gets = [];
|
||||
/** @var list<bool> */
|
||||
public array $packStreamOpenAtPut = [];
|
||||
/** @var resource|null */
|
||||
private $lastPackStream = null;
|
||||
|
||||
/** @param ?string $pipeFrom serve packs as pipes (streams that cannot seek) from this local repository root */
|
||||
public function __construct(
|
||||
private IBackend $inner,
|
||||
private ?string $pipeFrom = null,
|
||||
) {
|
||||
}
|
||||
|
||||
public function packStreamOpen(): bool {
|
||||
return is_resource($this->lastPackStream);
|
||||
}
|
||||
|
||||
public function put(string $path, $stream): void {
|
||||
$this->packStreamOpenAtPut[] = $this->packStreamOpen();
|
||||
$this->inner->put($path, $stream);
|
||||
}
|
||||
|
||||
public function packOpens(): int {
|
||||
return count(array_filter($this->gets, fn (string $p): bool => str_starts_with($p, 'packs/')));
|
||||
}
|
||||
|
||||
public function get(string $path) {
|
||||
$this->gets[] = $path;
|
||||
if (!str_starts_with($path, 'packs/')) {
|
||||
return $this->inner->get($path);
|
||||
}
|
||||
$fh = $this->pipeFrom !== null
|
||||
? popen('cat ' . escapeshellarg($this->pipeFrom . '/' . $path), 'r')
|
||||
: $this->inner->get($path);
|
||||
$this->lastPackStream = $fh;
|
||||
return $fh;
|
||||
}
|
||||
|
||||
public function getRange(string $path, int $offset, int $length): string {
|
||||
$this->gets[] = $path;
|
||||
return $this->inner->getRange($path, $offset, $length);
|
||||
}
|
||||
|
||||
public function exists(string $path): bool {
|
||||
return $this->inner->exists($path);
|
||||
}
|
||||
|
||||
public function move(string $from, string $to): void {
|
||||
$this->inner->move($from, $to);
|
||||
}
|
||||
|
||||
public function list(string $prefix): array {
|
||||
return $this->inner->list($prefix);
|
||||
}
|
||||
|
||||
public function listFiles(string $dir): array {
|
||||
return $this->inner->listFiles($dir);
|
||||
}
|
||||
|
||||
public function delete(string $path): void {
|
||||
$this->inner->delete($path);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace OCA\NgBackup\Tests\Unit\Repository;
|
||||
|
||||
use OCA\NgBackup\Backend\BackendException;
|
||||
use OCA\NgBackup\Backend\IBackend;
|
||||
use OCA\NgBackup\Backend\LocalBackend;
|
||||
use OCA\NgBackup\Crypto\KeyRing;
|
||||
use OCA\NgBackup\Repository\Repository;
|
||||
@@ -76,6 +78,107 @@ class VerifyTest extends TestCase {
|
||||
$this->assertSame(['db/does-not-exist'], $result['missing']);
|
||||
}
|
||||
|
||||
public function testVerifyReportsHowFarItIs(): void {
|
||||
$repo = Repository::initWithKey($this->backend, $this->keys, $this->keys->wrap('pw'));
|
||||
$s = $repo->backupDirectory($this->src);
|
||||
$seen = [];
|
||||
|
||||
$open = $this->open();
|
||||
$open->listPacksAbove = 0;
|
||||
$open->verify($s['snapshot'], heartbeat: function (array $p) use (&$seen): void {
|
||||
$seen[] = $p;
|
||||
});
|
||||
|
||||
$phases = array_values(array_unique(array_column($seen, 'phase')));
|
||||
$this->assertSame(['index', 'packs', 'files'], $phases);
|
||||
$this->assertSame(['phase' => 'packs', 'done' => 256, 'total' => 256], $seen[array_key_last(array_filter($seen, fn ($p) => $p['phase'] === 'packs'))]);
|
||||
$last = end($seen);
|
||||
$this->assertSame(2, $last['done']);
|
||||
$this->assertNull($last['total'], 'this snapshot has no file count in its metadata (a backup run records one)');
|
||||
}
|
||||
|
||||
/** Lists the pack folders however few packs there are. */
|
||||
private static function listing(Repository $repo): Repository {
|
||||
$repo->listPacksAbove = 0;
|
||||
return $repo;
|
||||
}
|
||||
|
||||
public function testWithNoMorePacksThanFoldersEveryPackIsCheckedInstead(): void {
|
||||
// fewer requests; on FTP each takes a data connection, of which a server may have few
|
||||
$repo = Repository::initWithKey($this->backend, $this->keys, $this->keys->wrap('pw'));
|
||||
$s = $repo->backupDirectory($this->src);
|
||||
$counting = new ListingBackend($this->backend);
|
||||
|
||||
$result = Repository::openWithKey($counting, $this->keys, null, null)->verify($s['snapshot']);
|
||||
|
||||
$this->assertSame([], $result['missing']);
|
||||
$this->assertSame(0, $counting->packListings);
|
||||
$this->assertGreaterThan(0, $counting->packExists);
|
||||
}
|
||||
|
||||
public function testVerifyListsPackFoldersInsteadOfCheckingEveryPack(): void {
|
||||
$repo = Repository::initWithKey($this->backend, $this->keys, $this->keys->wrap('pw'));
|
||||
$s = $repo->backupDirectory($this->src);
|
||||
$counting = new ListingBackend($this->backend);
|
||||
|
||||
$result = self::listing(Repository::openWithKey($counting, $this->keys, null, null))->verify($s['snapshot']);
|
||||
|
||||
$this->assertSame([], $result['missing']);
|
||||
$this->assertSame(0, $counting->packExists);
|
||||
}
|
||||
|
||||
public function testAPackFolderThatCannotBeListedIsCheckedPackByPack(): void {
|
||||
$repo = Repository::initWithKey($this->backend, $this->keys, $this->keys->wrap('pw'));
|
||||
$s = $repo->backupDirectory($this->src);
|
||||
$counting = new ListingBackend($this->backend, unlistable: true);
|
||||
|
||||
$result = self::listing(Repository::openWithKey($counting, $this->keys, null, null))->verify($s['snapshot']);
|
||||
|
||||
$this->assertSame([], $result['missing']);
|
||||
$this->assertGreaterThan(0, $counting->packExists);
|
||||
}
|
||||
|
||||
public function testAnEmptyFolderListingIsNoProofThatItsPacksAreGone(): void {
|
||||
// A storage also reports an empty folder when it could not look (is_dir() false).
|
||||
$repo = Repository::initWithKey($this->backend, $this->keys, $this->keys->wrap('pw'));
|
||||
$s = $repo->backupDirectory($this->src);
|
||||
$counting = new ListingBackend($this->backend, emptyListings: true);
|
||||
|
||||
$result = self::listing(Repository::openWithKey($counting, $this->keys, null, null))->verify($s['snapshot']);
|
||||
|
||||
$this->assertSame([], $result['missing']);
|
||||
$this->assertGreaterThan(0, $counting->packExists);
|
||||
}
|
||||
|
||||
public function testAPackMissingFromAFolderThatIsNotEmptyIsReportedWithoutAskingForIt(): void {
|
||||
$repo = Repository::initWithKey($this->backend, $this->keys, $this->keys->wrap('pw'));
|
||||
$s = $repo->backupDirectory($this->src);
|
||||
$packs = $this->backend->list('packs');
|
||||
// Another file in the same folder keeps its listing non-empty.
|
||||
$this->backend->put(dirname($packs[0]) . '/0000other', fopen('data://text/plain,x', 'r'));
|
||||
foreach ($packs as $pack) {
|
||||
$this->backend->delete($pack);
|
||||
}
|
||||
$counting = new ListingBackend($this->backend);
|
||||
|
||||
$result = self::listing(Repository::openWithKey($counting, $this->keys, null, null))->verify($s['snapshot']);
|
||||
|
||||
$this->assertNotSame([], $result['missing']);
|
||||
$this->assertSame(0, $counting->packExists);
|
||||
}
|
||||
|
||||
public function testAPackMissingFromAnEmptyFolderIsReportedToo(): void {
|
||||
$repo = Repository::initWithKey($this->backend, $this->keys, $this->keys->wrap('pw'));
|
||||
$s = $repo->backupDirectory($this->src);
|
||||
foreach ($this->backend->list('packs') as $pack) {
|
||||
$this->backend->delete($pack);
|
||||
}
|
||||
|
||||
$result = self::listing(Repository::openWithKey(new ListingBackend($this->backend), $this->keys, null, null))->verify($s['snapshot']);
|
||||
|
||||
$this->assertNotSame([], $result['missing']);
|
||||
}
|
||||
|
||||
public function testDeepVerifyDetectsATamperedPack(): void {
|
||||
$repo = Repository::initWithKey($this->backend, $this->keys, $this->keys->wrap('pw'));
|
||||
$s = $repo->backupDirectory($this->src);
|
||||
@@ -92,4 +195,151 @@ class VerifyTest extends TestCase {
|
||||
|
||||
$this->assertNotSame([], $result['failed']);
|
||||
}
|
||||
|
||||
public function testDeepVerifyRetriesAReadThatFailsOnce(): void {
|
||||
$repo = Repository::initWithKey($this->backend, $this->keys, $this->keys->wrap('pw'));
|
||||
$s = $repo->backupDirectory($this->src);
|
||||
$flaky = new FlakyRangeBackend($this->backend, failures: 1);
|
||||
|
||||
$result = Repository::openWithKey($flaky, $this->keys, null, null)->verify($s['snapshot'], deep: true);
|
||||
|
||||
$this->assertSame(200000, $result['bytesChecked']);
|
||||
$this->assertSame([], $result['failed']);
|
||||
$this->assertSame([], $result['unreadable']);
|
||||
}
|
||||
|
||||
public function testDeepVerifyReportsAPersistentReadErrorAsUnreadableNotCorrupted(): void {
|
||||
$repo = Repository::initWithKey($this->backend, $this->keys, $this->keys->wrap('pw'));
|
||||
$s = $repo->backupDirectory($this->src);
|
||||
$broken = new FlakyRangeBackend($this->backend, failures: PHP_INT_MAX);
|
||||
|
||||
$result = Repository::openWithKey($broken, $this->keys, null, null)->verify($s['snapshot'], deep: true);
|
||||
|
||||
$this->assertSame([], $result['failed']);
|
||||
$this->assertNotSame([], $result['unreadable']);
|
||||
$this->assertSame('connection reset', $result['readError']);
|
||||
}
|
||||
|
||||
public function testDeepVerifyDoesNotHideAProgrammingError(): void {
|
||||
$repo = Repository::initWithKey($this->backend, $this->keys, $this->keys->wrap('pw'));
|
||||
$s = $repo->backupDirectory($this->src);
|
||||
$buggy = new FlakyRangeBackend($this->backend, failures: PHP_INT_MAX, error: new \TypeError('bug'));
|
||||
|
||||
$this->expectException(\TypeError::class);
|
||||
Repository::openWithKey($buggy, $this->keys, null, null)->verify($s['snapshot'], deep: true);
|
||||
}
|
||||
}
|
||||
|
||||
/** Delegates to a real backend, but reading a pack throws for the first $failures reads. */
|
||||
final class FlakyRangeBackend implements IBackend {
|
||||
use \OCA\NgBackup\Backend\GetsOneByOne;
|
||||
|
||||
public function __construct(
|
||||
private IBackend $inner,
|
||||
private int $failures,
|
||||
private ?\Throwable $error = null,
|
||||
) {
|
||||
}
|
||||
|
||||
public function put(string $path, $stream): void {
|
||||
$this->inner->put($path, $stream);
|
||||
}
|
||||
|
||||
public function get(string $path) {
|
||||
if (str_starts_with($path, 'packs/')) {
|
||||
$this->fail();
|
||||
}
|
||||
return $this->inner->get($path);
|
||||
}
|
||||
|
||||
public function getRange(string $path, int $offset, int $length): string {
|
||||
$this->fail();
|
||||
return $this->inner->getRange($path, $offset, $length);
|
||||
}
|
||||
|
||||
private function fail(): void {
|
||||
if ($this->failures > 0) {
|
||||
$this->failures--;
|
||||
throw $this->error ?? new BackendException('connection reset');
|
||||
}
|
||||
}
|
||||
|
||||
public function exists(string $path): bool {
|
||||
return $this->inner->exists($path);
|
||||
}
|
||||
|
||||
public function move(string $from, string $to): void {
|
||||
$this->inner->move($from, $to);
|
||||
}
|
||||
|
||||
public function list(string $prefix): array {
|
||||
return $this->inner->list($prefix);
|
||||
}
|
||||
|
||||
public function listFiles(string $dir): array {
|
||||
return $this->inner->listFiles($dir);
|
||||
}
|
||||
|
||||
public function delete(string $path): void {
|
||||
$this->inner->delete($path);
|
||||
}
|
||||
}
|
||||
|
||||
/** Delegates to a real backend; counts exists() on packs, and can refuse to list pack folders. */
|
||||
final class ListingBackend implements IBackend {
|
||||
use \OCA\NgBackup\Backend\GetsOneByOne;
|
||||
|
||||
public int $packExists = 0;
|
||||
public int $packListings = 0;
|
||||
|
||||
public function __construct(
|
||||
private IBackend $inner,
|
||||
private bool $unlistable = false,
|
||||
private bool $emptyListings = false,
|
||||
) {
|
||||
}
|
||||
|
||||
public function put(string $path, $stream): void {
|
||||
$this->inner->put($path, $stream);
|
||||
}
|
||||
|
||||
public function get(string $path) {
|
||||
return $this->inner->get($path);
|
||||
}
|
||||
|
||||
public function getRange(string $path, int $offset, int $length): string {
|
||||
return $this->inner->getRange($path, $offset, $length);
|
||||
}
|
||||
|
||||
public function exists(string $path): bool {
|
||||
if (str_starts_with($path, 'packs/')) {
|
||||
$this->packExists++;
|
||||
}
|
||||
return $this->inner->exists($path);
|
||||
}
|
||||
|
||||
public function move(string $from, string $to): void {
|
||||
$this->inner->move($from, $to);
|
||||
}
|
||||
|
||||
public function list(string $prefix): array {
|
||||
return $this->inner->list($prefix);
|
||||
}
|
||||
|
||||
public function listFiles(string $dir): array {
|
||||
if (str_starts_with($dir, 'packs/')) {
|
||||
$this->packListings++;
|
||||
}
|
||||
if ($this->unlistable && str_starts_with($dir, 'packs/')) {
|
||||
throw new BackendException('Cannot list ' . $dir);
|
||||
}
|
||||
if ($this->emptyListings && str_starts_with($dir, 'packs/')) {
|
||||
return [];
|
||||
}
|
||||
return $this->inner->listFiles($dir);
|
||||
}
|
||||
|
||||
public function delete(string $path): void {
|
||||
$this->inner->delete($path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,3 +3,13 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
// nextcloud/ocp ships the public interfaces without an autoloader; tests that mock them need one.
|
||||
spl_autoload_register(static function (string $class): void {
|
||||
if (str_starts_with($class, 'OCP\\')) {
|
||||
$file = __DIR__ . '/../vendor/nextcloud/ocp/' . str_replace('\\', '/', $class) . '.php';
|
||||
if (is_file($file)) {
|
||||
require $file;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -68,7 +68,11 @@ run_on ngb-frt-restore backup:target:add offsite local -a null::null -o datadir=
|
||||
run_on ngb-frt-restore backup:list offsite | grep -q "$SNAP" || fail "the origin's snapshot is not listed after connecting the location"
|
||||
ok "recovery kit imported, existing repository adopted, snapshot visible"
|
||||
|
||||
run_on ngb-frt-restore backup:restore:full offsite "$SNAP" --no-interaction >/dev/null || fail "restore:full"
|
||||
OUT=$(run_on ngb-frt-restore backup:restore:full offsite "$SNAP" --no-interaction 2>&1) || {
|
||||
printf '%s\n' "$OUT" | tail -40
|
||||
docker exec ngb-frt-restore sh -c 'tail -c 6000 /var/www/html/data/nextcloud.log' || true
|
||||
fail "restore:full"
|
||||
}
|
||||
ok "backup:restore:full completed"
|
||||
|
||||
# Everything below must work in a FRESH occ process (a new PHP process per docker exec), since
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// Stand-in for the files_external SMB storage in unit tests: only what StorageBackend uses.
|
||||
|
||||
namespace OCA\Files_External\Lib\Storage;
|
||||
|
||||
use OCP\Files\Storage\IStorage;
|
||||
|
||||
if (!class_exists(SMB::class)) {
|
||||
abstract class SMB implements IStorage {
|
||||
/** @var object */
|
||||
protected $share;
|
||||
protected bool $showHidden = false;
|
||||
protected bool $checkAcl = false;
|
||||
|
||||
protected function buildPath(string $path): string {
|
||||
return '/' . trim($path, '/');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user