# Backups

*Settings > Backups* has the wall write an encrypted backup of itself to a folder every day:
everything it takes to bring the wall back on another computer, in one file that opens with
the backup key's recovery kit and with nothing else. An admin's to set up, as every setting is.

### What is in a backup

- The settings as the wall keeps them: the KVMs, servers, PCs and virtual machines with their
  passwords and remembered certificates; the saved actions; the accounts with their roles,
  the machines they may see, what their passwords open and their two-factor keys; the
  tokens and display links; the recovery key's record; the alert settings and destinations
  with their secrets; the identity provider as it is set up; the wall's own settings.
- The wall's data key, which every saved password in those settings is sealed with: the
  accounts' passwords open it, so a wall restored with another would lose them.
- The activity log, every file of it, with its key and the place its chain had come to. A
  backup begins with a seal of the log (why "backup"), and the restored log checks as the
  wall's did.
- The history, where *With the history* is ticked. It can be large, and a wall comes back
  without it.

Not in it: the backup key itself, this one or any other (a restored wall has none until its
admin makes one); the server storage key (a restore makes one of the new folder's own); the key
that opens the saved passwords at a start (`unlock.key`: the restored wall makes and fits its own
at the first sign-in); who is signed in; the wall's own HTTPS certificate (made anew, and
browsers ask once more); the program's log; the pictures kept for screen alerts.

A backup is not an export. An export ([Export and import](https://qkvm.com/docs/export-and-import)) is a file for a
person to take devices and settings to another wall, sealed with a passphrase they choose,
read entry by entry on the way in, and never holding accounts. A backup is the wall itself,
made by the clock, and restored only into an empty data folder, as it was.

### Setting it up

1. **Make the key.** *Make the backup key* asks for your password, then shows the key's
   recovery kit, once (see below). Print it, or save it as a file, and keep it apart from
   the wall.
2. **Name the folder:** a whole path on the computer qKVM runs on (`/srv/qkvm-backups`,
   `D:\qkvm-backups`). It may be a mounted share or a folder another tool copies elsewhere;
   not the data folder, which it would go with. A folder that is not there is made, its owner's
   alone, but only inside a folder that is there: a share that is not mounted leaves its mount
   point empty, and backups made below it would be on the computer's own disk. FTP, Dropbox,
   Drive and S3 are not built in.
3. **Choose** the hour (3 at night by the wall's clock, unless you choose another), how many
   to keep (7), and whether the history goes in. Switch them on and save.

The first backup is made the next time the hour comes; *Back up now* makes one at once. One
missed while qKVM was not running is made soon after it starts. On the night the clock is set
back, the hour shown twice runs at its first; on the night it is set on, an hour it skips runs
at the first minute after it. Times saved while the computer's clock was a day or more ahead
(set right since) would have held the next backup back until they came round: they are set to
now at the clock's next look, and the activity log says so. While backups are on, qKVM
keeps the saved passwords open after the last sign-out, as it does while alerts are on: a
backup at night seals with the backup key, which the vault keeps. Started with
`--locked-start`, nothing is made until somebody signs in, and every run says why it made none.

Each backup is one file, `qkvm-backup-<key id>-<date>-<time>.qkvmbackup` (the key it opens with,
by the id its kit names, and the time in UTC), the wall's account's alone. It is written under
a name of its own and takes that name only once it is whole, on the disk, and has been read
back with the key. Only then do the oldest of this key's backups beyond those to keep go: those
older than the new one, never the new one, and never one whose name is later than it (made
while the clock was ahead). A run cut off leaves no backup behind, and the next run takes away
what it left (a `.partial` file of this key's). Backups of another key are never touched: another
wall's in the same folder, or this wall's from before its key was made new. Settings says how
many there are; delete them yourself when they are no longer wanted. Nothing else in the
folder is touched either. *Verify the latest* reads this key's newest backup whole with the
key, restores nothing, and says what it holds: machines, accounts, saved secrets and whether
each opens, the activity log, the history. After the folder is changed, the latest is the new
folder's, and there is none until a backup is made there; the last run says where the backup
it made is.

**A folder that never answers** (a share whose server is gone, mounted so that it waits for
it for good) holds a run without end. After an hour the run is said to have failed, and alerted,
once, also where qKVM opened its listeners anew meanwhile (a changed address, HTTPS switched on);
its thread, which nothing can stop, goes on waiting for that folder. Settings says what waits
and since when, and once a day at the hour an alert says so again. No other backup is begun into
that folder meanwhile, by any name for it that this computer gives it: a junction or a link to it,
a short name, another spelling, and on Windows a mapped drive and the share's `\\server\share` name.
On a share whose server gives no file numbers, no backup is begun into any folder of its volume:
every share that reports the same volume number is held, of any server and by any name. Where
that number is 0 (some servers say 0 for every share of theirs) the share is told apart by its
root as well (on Windows its `\\server\share`, also behind a mapped drive; on Linux the source of
its mount, as `//server/share` or `server:/export`), so a share of another server is another;
where the root cannot be had, every share numbered 0 is held. Such a share numbered 0 and reached
by another name of its server (its address, say) is not known as the same. On Linux a second
mount of the same share usually has a device number of its own, and is not known as the same
folder either (a bind mount of it is). A backup begun beside the one that waits, in a folder not
known as the same, still leaves that one's file in the making alone: each run's is its own. Move
the backups to another folder and they are made there. A
restart of qKVM frees what waits; a run that ends by itself once its folder answers is said in
the activity log, and its backup is kept.

**A run that fails** is a line in the activity log, the last run in Settings says why, and
every alert destination that asks for *Backup failed* (severity error, ticked for a new
destination) is told; none while alerts are off. A run that fails within a pause, or within a
destination's quiet hours, is held as a machine's change is: told when they end where backups
still fail, and not at all where a backup was made meanwhile (a restart within them forgets
it; the next run that fails is told). The next backup that is made is told as back to normal
where the failure was told (when a pause or quiet hours end, where it is made within them), a
restart of qKVM between (who was told is kept with the settings). Every run, made or
not, is in the activity log with the folder it was made for, and so
are a key made, the settings changed and each Verify. A run that ends after its folder or
the key was changed (it was under way then) is said there as that folder's, and leaves the
last run, the latest backup and the next run in Settings as they were, and alerts nobody:
they are of the backups as they are set up now.

### The recovery kit

The backup key is 32 random bytes of the wall's own, apart from every other key it has. The
wall keeps it sealed in its vault, so that a backup at night needs nobody, and shows it once,
when it is made, as its recovery kit: the key in fourteen groups of four letters and digits,
what it opens, and how to restore. No page, answer, log or export ever holds it again. The kit
is all it takes to restore a backup on a new computer, and it opens every backup made with
that key: whoever has a backup and its kit has every device password in it. A backup also
carries the key the wall seals its own secrets with (accounts' passwords and second steps work
only with it), so a backup and its kit together open the wall's settings file too: the kit is
as strong as the wall's own key, and is kept as carefully. Keep it as a password is kept,
printed and put away or in a password manager, and not on the computer qKVM runs on or in the
folder of the backups.

*Make a new key* (it asks for your password) shows a new kit, once. Backups made from then on
open with the new kit, and those made before with the kit before: keep that one for as long as
you keep them. A backup being made at that moment takes its key with the settings it holds,
at one moment: what it holds of the wall from after the new key opens with the new kit only.
No backup holds a backup key, its own or another, so a kit opens the backups made with it and
nothing in them leads to a later key. The backups made before are no longer pruned (they are
another key's now): delete them yourself when they are no longer wanted. A new key changes
nothing in an older backup. Where an older kit may have been seen, delete the backups made
with it, and change the passwords of the devices.

A kit that is lost cannot be shown again: make a new key, and the backups made from then on
open with the new kit. The backups made before it open with nothing.

### Restoring

On the new computer, with qKVM installed and not running:

1. Copy the backup file there, and save the kit as a text file (or have the key at hand to
   type).
2. Run, with a data folder that is new or empty:

   ```
   python -m qkvm --restore qkvm-backup-43d96c035ccc455a-20261117-030000.qkvmbackup --backup-key kit.txt --data-dir /var/lib/qkvm
   ```

   Without `--backup-key` it asks for the key, where it runs in a terminal. The key itself is
   never given on the command line, where other users of the computer could see it.
3. It refuses a data folder with anything in it, a backup made with another key (it names the
   key the backup needs), one made by a later version of qKVM, and one in which anything was
   changed, cut off or added: it reads the whole file and takes back whatever it wrote before
   it found that. Then the wall is in the folder, its settings sealed with a server storage
   key of the new folder's own (beside it as `<data folder>.keys/vault.key`, or where
   `--vault-key-file` says: keep a separate private backup of it, as
   [How it is protected](https://qkvm.com/docs/how-it-is-protected) says). A restore that fails leaves the storage
   key where it is, whoever made it (another restore may have been given that file meanwhile),
   and the next restore into the folder uses it; a key is written whole before it is put in its
   place, so a restore stopped as it makes one leaves none or a whole one. On Linux and macOS the
   key's folder must be on a volume that takes hard links and keeps the key private to the
   server's account (not FAT or exFAT): elsewhere no key is made and the restore is refused, and
   `--vault-key-file` chooses another place; an empty `.qkvm-key.lock` stays there beside a key
   once one was made. On every system no key may be named so, nor `.qkvm-key-` and 16 hex digits
   and `.part`, the file a key is written into first. On Windows a `.qkvm-key.lock` carried there
   from Linux or macOS, or left by a power cut, keeps a first key from being made: remove it by
   hand while no qKVM is making a key there. A line in the activity
   log says it was restored, from which file, and the log is checked as `python -m qkvm.checklog --data-dir`
   checks it. While it runs, the folder is held for it (`qkvm.restoring` in it): a second
   restore into the same folder is refused until it ends, and takes nothing away. A restore
   that was stopped before it finished (the computer went down, it was killed) leaves that
   file, with what it had written: run the same command again, and it takes away what the
   stopped one wrote, and nothing else, and begins again. It writes the settings (`qkvm.json`)
   last, after its line in the activity log: a folder with them in it is a wall, which the same
   command refuses, and its line is there. Until then qKVM does not start on the folder: it says
   to run the same command again. Beside anything else in the folder it is refused as ever, and the
   folder is left as it is.
4. Start qKVM with that data folder and sign in as before. Until somebody signs in, the
   restored wall watches nothing and sends no alert (the key that opened its saved passwords
   at a start was the old computer's); from the first sign-in on, it carries on by itself as
   before, or as `--unlock-file` or `--locked-start` say.
5. Make a new backup key in *Settings > Backups* (the restored wall has none, since a backup
   holds no backup key) and keep its kit, then switch backups on again once this is the only
   wall writing to their folder. A restore leaves them off, so that two walls never both
   write, and prune, one folder. The kit you used opens only the backups made with it.

Everything else comes back as it was: every account with its role, its machines, its password
and its second step; the identity provider as it was set up, switched on or not; the tokens,
the destinations, the recovery key. A restore makes nothing more than it was.

### What a backup does not protect against

- **A lost kit.** Nobody can open the backups made with it, qKVM included.
- **Somebody with a backup and its kit.** They have every device password in it, and every
  account as it was.
- **One disk for both.** A folder on the disk the wall is on goes with it: keep the backups on
  another disk, a share, or a folder copied elsewhere.
- **What happened since.** A backup a day can be up to a day old.
- **Others who may change the folder.** They can neither read a backup nor change one
  unnoticed, but they can delete them. Keep the folder writable by qKVM's account alone (on a
  share, by the share's own rights).
- **What the machines hold.** The settings of the KVMs and controllers themselves, their
  firmware and the computers behind them are not in a backup.

### How a backup is sealed

Each file has a key of its own, derived from the backup key and random bytes in its head
(HKDF-SHA256); its content is compressed and sealed with AES-256-GCM in pieces of a mebibyte,
each bound to the head and to its place, so that a file cut short, added to or changed anywhere
is refused as a whole. The head says in clear that it is a backup of qKVM's, which version of
the format and of the program made it, when, and which key opens it, by a mark of the key that
gives nothing of it away; nothing of the wall can be read off a backup but its size.
