> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kiwifs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Backup sync

> Push your knowledge base's git history to a remote on a schedule, and restore it on a new machine.

Every write in KiwiFS is a git commit, so backing up means pushing that repository somewhere else. Backup sync pushes to a git remote on an interval while the server runs, and `kiwifs restore` clones it back.

Before each push, KiwiFS fetches the remote branch and rebases local commits onto it. This avoids the routine non-fast-forward failure you'd get when another server, laptop, or teammate pushed to the same branch first.

## Turn on backup sync

**1. Add a backup section**

```toml .kiwi/config.toml theme={null}
[backup]
remote = "git@github.com:org/knowledge.git"
interval = "5m"
branch = "main"
```

**2. Make sure the server can push**

The KiwiFS process pushes with your system `git`, so it needs SSH keys or credentials for the remote.

**3. Start the server**

```bash Terminal theme={null}
kiwifs serve --root ./knowledge
```

KiwiFS pushes once at startup, then on every interval, and once more on shutdown.

| Key | Default | Description |
| - | - | - |
| `remote` | | Git remote URL. Backup sync is off when it's empty. |
| `interval` | `5m` | Go duration between pushes. The minimum is `30s`. |
| `branch` | Current branch, then `main` | Branch to push |
| `rebase_before_push` | `true` | Fetch and rebase onto the remote branch before pushing |

In containers, set `KIWI_BACKUP_REMOTE`, `KIWI_BACKUP_INTERVAL`, and `KIWI_BACKUP_REBASE_BEFORE_PUSH` instead. They override the TOML values.

### Check the last push

```bash Terminal theme={null}
curl http://localhost:3333/api/kiwi/backup/status
```

```json Response theme={null}
{ "enabled": true, "status": { "last_push_at": "2026-10-19T18:46:15Z", "success": true } }
```

When a push fails, `success` is `false` and `error` holds the reason.

## Push once from the CLI

```bash Terminal theme={null}
kiwifs backup --root ./knowledge
```

Without `--remote`, the command reads `remote`, `branch`, and `rebase_before_push` from `[backup]` in `.kiwi/config.toml`.

| Flag | Default | Description |
| - | - | - |
| `--root`, `-r` | `./knowledge` | Knowledge root |
| `--remote` | From config | Remote URL. When set, KiwiFS ignores the config file. |
| `--branch` | Current branch | Branch to push |
| `--rebase-before-push` | `true` | Fetch and rebase before pushing |

## Restore from a remote

`kiwifs restore` clones a backup into an empty directory.

```bash Terminal theme={null}
kiwifs restore --from git@github.com:org/knowledge.git --to ./knowledge --branch main
kiwifs serve --root ./knowledge
```

| Flag | Description |
| - | - |
| `--from` | Remote URL to clone (required) |
| `--to` | Target directory. It must not exist or must be empty. (required) |
| `--branch` | Branch to check out. Defaults to the remote's default branch. |

Search indexes aren't part of the backup. KiwiFS rebuilds them in `.kiwi/state/` when the server starts.

## What happens before a push

With `rebase_before_push` on, each push:

1. Points a git remote named `backup` at your configured URL.
2. Resolves the branch from `[backup].branch`, then the current branch, then `main`.
3. Checks whether the branch exists on the remote. If it doesn't, KiwiFS skips the rebase and the push creates it.
4. Fetches `backup/<branch>`.
5. Refuses to rebase if the worktree has uncommitted changes.
6. Saves the current HEAD at `refs/kiwifs/backup-pre-rebase`, then rebases local commits onto `backup/<branch>`.
7. Pushes the branch.
8. Checks that the remote branch now matches the local HEAD, and logs a warning if it doesn't.

KiwiFS automates only this clean path. It never force-pushes and never resolves conflicts. If the rebase fails, KiwiFS aborts it and reports the error in the backup status.

Turn the rebase off only when another process owns git synchronization and you want a plain push:

```toml .kiwi/config.toml theme={null}
[backup]
rebase_before_push = false
```

```bash Environment theme={null}
KIWI_BACKUP_REBASE_BEFORE_PUSH=false kiwifs serve --root ./knowledge
```

```bash CLI theme={null}
kiwifs backup --root ./knowledge --rebase-before-push=false
```

## Troubleshooting

> [accordion] worktree has uncommitted changes; refusing automatic rebase
>
> Commit or discard the local changes, then push again.
>
> ```bash Terminal theme={null}
> cd ./knowledge
> git status --short
> git add .
> git commit -m "Save local knowledge changes"
> kiwifs backup --root .
> ```

> [accordion] Rebase conflict
>
> Two writers changed the same lines. Resolve the conflicted markdown files with normal git tools.
>
> ```bash Terminal theme={null}
> cd ./knowledge
> git status
> # Edit the conflicted files, then:
> git add <resolved-files>
> git rebase --continue
> kiwifs backup --root .
> ```
>
> Run `git rebase --abort` to give up and return to the pre-rebase state.

> [accordion] Recover the pre-rebase HEAD
>
> ```bash Terminal theme={null}
> git show refs/kiwifs/backup-pre-rebase
> git branch recover-before-backup-rebase refs/kiwifs/backup-pre-rebase
> ```

## Related

> [card] [Versioning](https://docs.kiwifs.com/concepts/versioning.md)
>
> How every write becomes a commit.

> [card] [Docker](https://docs.kiwifs.com/deploy/docker.md)
>
> Configure backup with environment variables.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.