Metadata-Version: 2.5
Name: resticbackups3
Version: 0.4.0
Summary: restic + S3-compatible storage (Cloudflare R2 first): init once, then back up from cron
License-Expression: MIT
License-File: LICENSE
Classifier: Environment :: Console
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: System :: Archiving :: Backup
Requires-Python: >=3.12
Requires-Dist: rich>=15
Provides-Extra: dev
Requires-Dist: pytest-xdist>=3.8; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: tzdata; extra == 'dev'
Description-Content-Type: text/markdown

# resticbackups3 (`rbs3`)

A command-line tool that turns [restic](https://restic.net/) and S3-compatible storage
(Cloudflare R2 first) into "init once, then back up from cron":

```
rbs3 init <profile>
rbs3 backup <profile> --mail
rbs3 cronjob <profile> "0 4 * * *" --mail --silent
```

**Status: early development.** `rbs3 init` and `rbs3 backup` (with retention, prune and check)
work; email reports, restore and cron come next.

## Create a profile

```
rbs3 init <profile> --env-file creds.env --config settings.toml
```

Credentials can come from an env file (`--env-file`), this shell's environment (`--env-var`),
flags (`--restic-password=…`) or prompts. They combine, with flags first, then the env file,
then the environment, and anything still missing is asked for. Prefer the env file or the prompts:
secrets given as flags end up in shell history.

| Key | Required |
|---|---|
| `RESTIC_REPOSITORY` | yes, `s3:https://<account>.r2.cloudflarestorage.com/<bucket>[/<prefix>]` |
| `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `RESTIC_PASSWORD` | yes |
| `AWS_DEFAULT_REGION` | no, default `auto` |
| `SMTP_SERVER` | no; empty means no email reports |
| `SMTP_PORT`, `SMTP_SECURITY` (`starttls`/`ssl`) | defaults 587 / starttls |
| `SMTP_USER`, `SMTP_PASSWORD`, `MAIL_TO` | when `SMTP_SERVER` is set |
| `MAIL_FROM` | no, default `SMTP_USER` |

Before saving, `init` checks everything it can and prints ✔ or ✖ with a reason for each:

- restic is installed and 0.16 or newer (if not, it prints the install command for your distro);
- the S3 keys can list, write and delete in the bucket (an R2 token with an IP allow-list is
  named as such, with this machine's public IP);
- the repository exists and the password opens it, or, if there is none yet, it offers to create
  one (`--yes` creates it without asking);
- the SMTP login works, if email is configured, and it can send a test email.

If a check fails, nothing is saved unless you answer "save anyway" at the prompt.

Without `--config`, the settings are asked for one by one. The profile is saved as
`/etc/resticbackups3/<profile>/.env` (root only, 0600) and `config.toml` (0644). After that you
may delete your own copies, but keep `RESTIC_PASSWORD` in a password manager: without it no
backup can be restored.

## Back up

```
rbs3 backup <profile> [--silent] [--dry-run]
```

All sources go into one snapshot, tagged `rbs3` and `profile:<profile>`. Before it starts, rbs3
removes stale repository locks and opens the repository; afterwards it shows the repository
size, the latest snapshot's restore size and the profile's snapshot count. A relative `exclude`
pattern is rooted at each source (`thumbs` skips `<source>/thumbs`, `**/*.tmp` any `.tmp` file
in it); an absolute one is used as-is. If `[freshness_check]` is set, a stale or missing matching
file is reported as a warning.

After each backup rbs3 maintains the repository:

- `restic forget` with the `[retention]` keep-* values runs every time. It looks at the
  profile's snapshots only (`--tag profile:<profile>`) as one group, so a new hostname or a
  changed source list does not start a separate retention series.
- `restic prune` runs only on a prune day: today is `maintenance.prune_weekday` (`""` = any day)
  **and** at least `prune_interval_days` calendar days have passed since the last successful
  prune (`0` = no minimum). With the defaults (`sun`, 7) that is every Sunday; a missed Sunday
  waits for the next one.
- `restic check --read-data-subset <check_subset>` runs right after each prune.
- backup, forget, prune and check each wait up to 2 minutes for a lock held by another restic
  run (a backup or prune of another profile in the same repository, a manual `restic` command).
- prune and check are stopped after 12 hours, so a hung one cannot hold the profile's lock (and
  block every later run) for ever. restic is stopped as Ctrl+C would, so it removes its lock.
- A failed forget, prune or check makes the run FAILED. A failed prune is retried on the next
  run. A last prune dated after today (a wrong clock) is not counted, with a warning.
- The last backup, prune and check (time and result) are kept in
  `/var/lib/resticbackups3/<profile>/state.json`.

- `--silent` prints nothing but errors; `--dry-run` shows what would be uploaded, and runs no
  maintenance.
- Each run writes `/var/log/resticbackups3/<profile>/backup/<time>.txt`, with secrets replaced
  by `***`. Logs older than `report.log_keep_days` are removed.
- Only one run per profile at a time: a second one exits at once.
- Exit codes: `0` OK, `3` warning (snapshot made, but some files could not be read), `1` failed,
  `75` another run of this profile is still going, `130` cancelled.

## Install

Needs Linux, Python 3.12 or newer, and restic 0.16 or newer.

| Tool | Install | Update |
|---|---|---|
| pipx | `pipx install resticbackups3` | `pipx upgrade resticbackups3` |
| uv | `uv tool install resticbackups3` | `uv tool upgrade resticbackups3` |
| pip (in a venv) | `pip install resticbackups3` | `pip install --upgrade resticbackups3` |

Run `rbs3` as your normal user; it asks for sudo itself when it needs it.

## Development

```
python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]" ruff mypy
git config core.hooksPath .githooks   # blocks commits containing strings from notes/deny-list.txt
ruff check . && ruff format --check . && mypy && pytest -n auto
```

`tests/test_integration.py` runs the checks against a real restic and S3 endpoint when
`RBS3_IT_ENDPOINT`, `RBS3_IT_BUCKET`, `RBS3_IT_KEY` and `RBS3_IT_SECRET` are set. CI starts
[Versity Gateway](https://github.com/versity/versitygw) for it.

Pull request titles follow [Conventional Commits](https://www.conventionalcommits.org/); a merged
`feat:` or `fix:` publishes a release.

## License

MIT
