# Roll back one customer, not everyone.

Revert Acme's database to just before yesterday's bad import while every other customer keeps working.

- [Set up tenant backups](https://litebackup.com/sqlite-database-for-every-tenant-or-agent)
- [All docs](https://litebackup.com/docs)

## Back up every tenant

Install your [Litebackup server](/install), then join the app machine and back up its tenant databases under tenants/.

### CLI

```bash
# Join this machine and back up every tenant database.
# Use the token from Machines, Add machine in your console.
curl --fail --silent --show-error --location https://litebackup.com/cli.sh | bash
litebackup install service --api-key YOUR_JOIN_TOKEN@HOST
litebackup start '/var/lib/app/tenants/*.sqlite' tenants/
```

### TypeScript

```ts
// Install with npm install @litebackup/sdk.
// Use the token from Machines, Add machine in your console.
// export LITEBACKUP_API_KEY=YOUR_JOIN_TOKEN@HOST
import { Litebackup } from "@litebackup/sdk";

const litebackup = new Litebackup();
await litebackup.backups.start("/var/lib/app/tenants/*.sqlite", { prefix: "tenants/" });
```

### Go

```go
// Install the client and Go SDK, then join this machine.
// curl --fail --silent --show-error --location https://litebackup.com/cli.sh | bash
// litebackup install service --api-key YOUR_JOIN_TOKEN@HOST
// go get github.com/litebackup/sdk/litebackup
package main

import (
	"context"
	"log"

	"github.com/litebackup/sdk/litebackup"
)

func main() {
	ctx := context.Background()
	client, err := litebackup.Default(ctx)
	if err != nil {
		log.Fatal(err)
	}
	_, err = client.StartBackup(ctx, "/var/lib/app/tenants/*.sqlite", &litebackup.StartBackupArgs{
		Prefix: "tenants/",
	})
	if err != nil {
		log.Fatal(err)
	}
}
```

### Python

```python
# Install with python -m pip install litebackup.
# Use the token from Machines, Add machine in your console.
# export LITEBACKUP_API_KEY=YOUR_JOIN_TOKEN@HOST
from litebackup import Litebackup

client = Litebackup()
client.backups.start("/var/lib/app/tenants/*.sqlite", prefix="tenants/")
```

### Ruby

```ruby
# Install with gem install litebackup.
# Use the token from Machines, Add machine in your console.
# export LITEBACKUP_API_KEY=YOUR_JOIN_TOKEN@HOST
require "litebackup"

client = Litebackup::Client.new
client.backups.start("/var/lib/app/tenants/*.sqlite", prefix: "tenants/")
```

### PHP

```php
<?php
// curl --fail --silent --show-error --location https://litebackup.com/cli.sh | bash
// composer require litebackup/sdk
// Use the token from Machines, Add machine in your console.
// export LITEBACKUP_API_KEY=YOUR_JOIN_TOKEN@HOST
require __DIR__ . "/vendor/autoload.php";

use Litebackup\Client;

$client = new Client();
$client->backups->start("/var/lib/app/tenants/*.sqlite", prefix: "tenants/");
```

### Rust

```rust
// curl --fail --silent --show-error --location https://litebackup.com/cli.sh | bash
// cargo add litebackup
// cargo add tokio --features macros,rt-multi-thread
// Use the token from Machines, Add machine in your console.
// export LITEBACKUP_API_KEY=YOUR_JOIN_TOKEN@HOST
use litebackup::{BackupOptions, Client};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::builder().connect().await?;
    client
        .backups()
        .start(
            "/var/lib/app/tenants/*.sqlite",
            BackupOptions::default().prefix("tenants/"),
        )
        .await?;
    client.close();
    Ok(())
}
```

## Recover one customer at a time

These examples use a backed up database named tenants/acme.sqlite, an SDK client on the local daemon, and version 120 as the retained version from before the mistake.

- **Shared tables make recovery a merge**. When customers share tables, recovering one account can mean restoring the whole database into a temporary instance, extracting that customer’s rows across related tables, and merging them back while preserving relationships and everyone else’s newer changes.
- **One database per customer makes recovery one operation.** With one SQLite database per tenant, each customer has separate versions, so a bad import, a buggy migration, or an accidental delete can be rolled back for that customer without changing another customer’s database.

## 1. Find the version before the mistake

List Acme's retained versions with their times and pick the one from before the import.

### CLI

```bash
# List Acme's retained versions with their times.
# Use --stdout-format json if you also need digests.
litebackup versions tenants/acme.sqlite
```

### TypeScript

```ts
const versions = await litebackup.databases.versions("tenants/acme.sqlite");
```

### Go

```go
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/litebackup/sdk/litebackup"
)

func main() {
	ctx := context.Background()
	client, err := litebackup.Default(ctx)
	if err != nil {
		log.Fatal(err)
	}
	versions, err := client.Versions(ctx, "tenants/acme.sqlite", nil)
	if err != nil {
		log.Fatal(err)
	}
	for _, version := range versions {
		fmt.Println(version.Version, version.Time)
	}
}
```

### Python

```python
versions = client.databases.versions("tenants/acme.sqlite")
```

### Ruby

```ruby
versions = client.databases.versions("tenants/acme.sqlite")
```

### PHP

```php
$versions = $client->databases->versions("tenants/acme.sqlite");
```

### Rust

```rust
let versions = client.databases().versions("tenants/acme.sqlite", Default::default()).await?;
```

## 2. Preview Acme’s database before the import

Restore the chosen version to a new file and inspect it while the live database stays untouched.

### CLI

```bash
# Preview the version chosen from Acme’s list.
litebackup restore tenants/acme.sqlite /tmp/acme-before-import.sqlite --version 120
```

### TypeScript

```ts
await litebackup.databases.restore("tenants/acme.sqlite", {
  path: "/tmp/acme-before-import.sqlite",
  version: 120,
});
```

### Go

```go
_, err = client.Restore(ctx, "tenants/acme.sqlite", &litebackup.RestoreArgs{
	Path:    "/tmp/acme-before-import.sqlite",
	Version: 120,
})
if err != nil {
	return err
}
```

### Python

```python
client.databases.restore(
    "tenants/acme.sqlite", path="/tmp/acme-before-import.sqlite", version=120
)
```

### Ruby

```ruby
client.databases.restore(
  "tenants/acme.sqlite", path: "/tmp/acme-before-import.sqlite", version: 120
)
```

### PHP

```php
$client->databases->restore(
    "tenants/acme.sqlite", path: "/tmp/acme-before-import.sqlite", version: 120
);
```

### Rust

```rust
client.databases().restore(
    "tenants/acme.sqlite",
    "/tmp/acme-before-import.sqlite",
    litebackup::RestoreOptions::default().version(120),
).await?;
```

## 3. Roll back Acme’s account

Once the preview checks out, pause Acme's work, close its connections, let its writes sync, and revert on its writer machine.

### CLI

```bash
# Make the checked version Acme’s current database again.
litebackup revert tenants/acme.sqlite --version 120
```

### TypeScript

```ts
await litebackup.databases.revert("tenants/acme.sqlite", { version: 120 });
```

### Go

```go
_, err = client.Revert(ctx, "tenants/acme.sqlite", &litebackup.RevertArgs{Version: 120})
if err != nil {
	return err
}
```

### Python

```python
client.databases.revert("tenants/acme.sqlite", version=120)
```

### Ruby

```ruby
client.databases.revert("tenants/acme.sqlite", version: 120)
```

### PHP

```php
$client->databases->revert("tenants/acme.sqlite", version: 120);
```

### Rust

```rust
client.databases().revert(
    "tenants/acme.sqlite", litebackup::RevertOptions::default().version(120),
).await?;
```

## Preview without changing the live database

`restore` writes a new file and runs SQLite’s `quick_check` before returning. The destination directory must exist and the file must not exist. Open the copy separately to check Acme’s records and test the app against the old schema before deciding to revert.

## Choose a time instead

Both `restore` and `revert` accept `--time` with an RFC 3339 timestamp instead of `--version`. They select the newest retained version at or before that time. Replace this example time with a moment before Acme’s import.

```bash
# Preview Acme’s database at or before the chosen time.
litebackup restore tenants/acme.sqlite /tmp/acme-before-import.sqlite --time 2026-11-28T09:00:00Z
```

After inspecting the preview, pause Acme as in step 3 before applying the same time.

```bash
# Roll back Acme’s database to the chosen time.
litebackup revert tenants/acme.sqlite --time 2026-11-28T09:00:00Z
```

These are alternatives to the version examples. Each SDK also accepts a time in place of a version. See the [restore guide](/restore-sqlite-to-any-version) and [SDK reference](/litebackup-sdk-and-command-line-reference).

## Undo the rollback

A revert replaces Acme’s whole database with the chosen version, including its schema. Valid changes made after that version are absent from the current database too.

Revert puts the chosen content back under Acme’s database name as a new head, one version beyond the current head. Like `git revert` adding a commit, it adds to the version list rather than erasing later versions. Those versions remain restorable for as long as retention keeps them. Revert to the version from before the rollback to undo it.

The revert changes only Acme’s database. Other tenants keep their current databases and changes. Resume Acme’s work after the revert succeeds and the app can use the restored database.

## Offer customers an undo action

An admin or support action in your SaaS can use the same SDK calls. Your app checks who may recover the account and maps that account to its database name. List that database’s versions so the person can choose one to preview.

```ts
const versions = await litebackup.databases.versions("tenants/acme.sqlite");
```

After the person checks the preview and confirms the rollback, pause Acme as in step 3 and revert to the chosen version on its writer machine.

```ts
await litebackup.databases.revert("tenants/acme.sqlite", { version: 120 });
```

Your app supplies the customer interface, permission checks, and coordination with its workers. Litebackup supplies the version list, preview restore, and revert. A database per tenant makes the rollback one operation without extracting or merging shared tables.

## Questions

### What happens to Acme’s writes after the bad import?

A revert replaces Acme’s whole database with the chosen version, including its schema. Valid changes made after that version are absent from the current database too. Later captured versions remain restorable under retention. If Acme needs those newer changes, use the preview to work out a selective repair instead of reverting the whole database. The [deleted rows guide](/restore-deleted-sqlite-row) shows how to copy selected rows back.

### What if the app is writing during revert?

Pause Acme as in step 3. Revert refuses an open file with `still_open`. If the file holds writes the server lacks, it refuses with `conflict` and leaves the file untouched. This protection prevents overwriting unsynced writes. It does not preserve newer changes that have already synced when you choose an older version.

### How far back can I go?

Only as far as the versions Litebackup captured and still retains. Choose retention in the console under **Retention**, and use `litebackup versions tenants/acme.sqlite` to see what is available now. A time selects a retained version at or before it. It does not reconstruct individual transactions between captured versions.

### Can I do this with customers in shared tables?

Litebackup restores or reverts a whole SQLite database. It does not select a tenant’s rows, track dependencies across tables, or merge them into a shared database. A separate database per customer is what makes the per-customer rollback a single operation.

## Install

[Install Litebackup](/install), then [back up your tenant databases](/sqlite-database-for-every-tenant-or-agent). The examples above show the calls after setup. Backups must already contain a version from before the mistake. See [pricing](/pricing) for plans.
