Metadata-Version: 2.4
Name: close1
Version: 0.11.0
Summary: Simple backups from servers over SSH
Author: Yaroslav Polyakov
License-Expression: MIT
Project-URL: Homepage, https://github.com/yaroslaff/close1
Project-URL: Repository, https://github.com/yaroslaff/close1
Project-URL: Issues, https://github.com/yaroslaff/close1/issues
Keywords: backup,ssh,cron,pull,sqlite,mysql
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: System Administrators
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: System :: Archiving :: Backup
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: PyYAML>=5.4
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Dynamic: license-file

# close1

A simple tool for daily backups of files and databases over SSH.

## Features
- Pulls files and directories (.tar.gz/.zip) from the servers to your machine; nothing is installed on the servers.
- Runs commands on the servers (for example, `mysqldump`, `.backup` in sqlite3, or stopping a web server for the duration of the backup and starting it again afterwards).
- Good SSH integration: uses your key through the SSH agent (even if it is passphrase-protected). ControlMaster for speed and for reliability on lossy networks.
- Tolerates missed cron runs (e.g. when the computer was off). You can run close1 from cron every hour and it still makes one backup per day.
- Careful rotation: you set how many backups to keep (say, 7). Rotation removes the oldest ones, but it will not delete a 10-day-old backup if there were no newer ones. Even in the worst case you still have some backups.

## Install

```sh
pipx install close1
```

## Usage

```sh
close1 --check                 # SSH agent, keys, host reachability (copies nothing)
close1                         # back up all hosts from the config
close1 blog -f -v              # only blog, overwrite today's backup, verbose
close1 log                     # show the log (-v: print its path first)
close1 status                  # hosts in storage: last backup, number of days (-v: sizes, file counts)
```

Config: `-c path`, otherwise the first one found of `$CLOSE1_CONFIG`, `~/.config/close1/close1.yml`, `/etc/close1.yml`
(no merging). See `close1-example.yml` (short) and `close1-full-example.yml` (every option explained).
Log: `~/.local/state/close1/close1.log` (root: `/var/log/close1.log`).

## Config

```yaml
_default:
  dest: ~/backups          # -> ~/backups/<host>/<YYYY-MM-DD>/
  keep: 7

blog:
  pre:
    - mysqldump blog | gzip > /tmp/blog-${DATE}.sql.gz
  files:
    # "name:" is optional, only if you want to set the local file name
    - blog.sql.gz: /tmp/blog-${DATE}.sql.gz
    - /var/www/blog/uploads
    - /etc/nginx/sites-enabled
  post:
    - rm /tmp/blog-${DATE}.sql.gz

"backup@bot:2222":
  files:
    - /opt/bot/config.yml
  stdout:
    packages.txt: dpkg -l
```

The section name is the host to connect to (`blog`, `backup@bot:2222`). To give a section any name, set `host:`
(same `user@host:port` format; `user:`/`port:` override it).

Several projects on one server go under `projects:`. Each project is a separate backup in its own directory and
inherits the server's options (it can override them, except `host`):

```yaml
echo:
  host: root@echo.example.com:12234
  keep: 30
  projects:
    shop:                    # -> ~/backups/echo/shop/<YYYY-MM-DD>/
      files:
        - /var/www/shop
    wiki:                    # -> ~/backups/echo/wiki/<YYYY-MM-DD>/
      keep: 7
      files:
        - /var/lib/wiki
```

`close1 echo` backs up all projects of the server, `close1 echo/shop` only one. `pre`/`files`/`stdout`/`post` at
the server level are not inherited: they make one more backup, into `~/backups/echo/<YYYY-MM-DD>/` (beside the
project directories). `close1 status` lists projects as `echo/shop`.

All options with explanations: [close1-full-example.yml](close1-full-example.yml).

### SQLite

Do not copy a live SQLite file (in WAL mode part of the data lives in `-wal`). Take a snapshot in `pre` and fetch it:

```yaml
pre:
  - umask 077 && sqlite3 /opt/app/app.db ".backup /tmp/app-${DATE}.db" && gzip -f /tmp/app-${DATE}.db
files:
  - app.db.gz: /tmp/app-${DATE}.db.gz
post:
  - rm -f /tmp/app-${DATE}.db.gz
```

## Unreliable connections

All commands to a host go through one SSH connection (ControlMaster), so a backup needs a single handshake.
After a connection error (ssh exit code 255) close1 retries the connection check, file checks and downloads
(`retries` times, `retry_delay` seconds apart). `pre`, `post` and `stdout` commands are not retried: after a dropped
connection it is unknown whether they ran. If they fail, the backup fails and the next cron run starts it again.

## cron and ssh-agent

close1 authenticates through ssh-agent. If `SSH_AUTH_SOCK` is not set (as under cron), it looks for the socket in
`/run/user/$UID/` (`openssh_agent`, `ssh-agent.socket`, `gcr/ssh`, `keyring/ssh`, `gnupg/S.gpg-agent.ssh`). ssh is always
called with `BatchMode=yes` and `ForwardAgent=no`. While you are not logged in or the key is not loaded into the agent,
the backup is skipped and retried on the next run.

By default the first agent that holds any key is used. If you have several agents (e.g. ssh-agent and gnome-keyring)
with different keys, set `ssh_key: SHA256:...` (globally or per host): close1 then uses the agent that holds this key
and passes it to ssh as `IdentityAgent`. If the key is in no agent, the host is skipped. `close1 --check` lists all
agents with their key fingerprints. The key comment (`user@host`) is not used for matching: it is arbitrary text.

```cron
17 * * * * $HOME/.local/bin/close1
```

Only errors go to stderr (and so to cron mail); everything else goes to the log.
