# Schedule SQLite backups with Cron or Kubernetes

Run a backup script on a schedule when you need a job that backs up files
and exits. Each run syncs the files to your server; later writes stay in the
local SQLite files for the next run. Keep them on [persistent storage](/sdk-sqlite-backup#:~:text=Keep%20the%20files%20on%20persistent%20storage).
For apps that stay up, consider [continuous backup](/sdk-sqlite-backup).

## Prepare the database host

Install a [server](/sdk-litebackup-server) and the CLI. Give the job access to the database directory and set `LITEBACKUP_API_KEY` in its environment. An existing joined session reuses its machine identity while the variable holds the credential it joined with.

For disposable workers, select **Multiple machines** in **Machines → Add machine**.
Each foreground command that joins spends one token use. The script below runs
one command per path, so two paths on an hourly schedule need 48 uses per day,
plus any retries that join. Set an expiry covering the schedule.

```bash
litebackup backup ./main.sqlite scheduled/
```

## Back up a list with the CLI

The [script](https://github.com/litebackup/sdk/blob/v1.11.0/examples/cron/backup-sqlite.sh) backs up each path, waits for
your server, and releases its writer lock. Add `--durability cloud` to wait for the
bucket too. Install it somewhere
the cron user can run it, such as `/opt/app/backup-sqlite.sh`.

```bash
/opt/app/backup-sqlite.sh /var/lib/app/exports/main.sqlite /var/lib/app/exports/audit.sqlite
```

It uses `scheduled/` names, so these become `scheduled/main.sqlite` and
`scheduled/audit.sqlite`. Reserve those names for this job. Different input
paths with the same basename need a modified script with explicit names.
These export files must not be covered by the app's continuous backup; see
[one-shot jobs](/sdk-sqlite-backup-script) for waiting on an existing backup instead.

Create writable `/var/lib/app/run` and `/var/lib/app/log` directories, then
edit that user's crontab: Cron does not read your shell's `PATH`, so if
`litebackup` is in `~/.local/bin`, add that directory's full path to the
`PATH` line.

```cron
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
# Every hour; refuse overlap and cap total runtime on this Linux host.
0 * * * * /usr/bin/flock -n /var/lib/app/run/backup.lock /usr/bin/timeout --signal=TERM --kill-after=30s 75m /opt/app/backup-sqlite.sh /var/lib/app/exports/main.sqlite /var/lib/app/exports/audit.sqlite >> /var/lib/app/log/backup.log 2>&1
```

This uses Linux `flock` and GNU `timeout`. Verify their paths on your VM.
A skipped lock acquisition or failed command exits nonzero. Configure your
job monitoring to alert on failures and on missing successful runs. Cron's
schedule uses the host's timezone unless you configure it otherwise.

The script allows 30 minutes per file and 75 minutes for the two-file job.
The default archive delay is 15 minutes; leave time for uploads too. A forced kill may leave the remote lease active until expiry.

The [TypeScript runner](/sdk-sqlite-backup-script) works with the same cron setup.

## Kubernetes CronJob

The pod must mount the actual SQLite directory, including WAL files. A pod
on an unrelated node cannot access a path from another pod without an
appropriate volume. Check PVC access modes and placement: a `ReadWriteOnce`
volume may prevent a separate job from mounting while the app runs elsewhere.
Using the app's existing client on its host can be simpler.

Build the [pinned CLI image](/sdk-sqlite-docker#:~:text=Install%20a%20pinned%20CLI%20in%20your%20own%20Dockerfile) for the target architecture and matching SDK version, then use that image to build the job from the SDK repository root.

```bash
docker build --build-context cli=docker-image://litebackup-cli:pinned \
  --file examples/cron/Dockerfile --tag YOUR_REGISTRY/litebackup-job:latest .
# Push the image to your registry before applying the manifest.
```

Create a Secret named `litebackup-join` with key `token` through your secret
manager. Provision the `app-databases` PVC, replace
the image reference, and adapt volume permissions for your cluster:

```yaml
apiVersion: batch/v1
kind: CronJob  # vocabulary:allow Kubernetes's field
metadata:
  name: sqlite-backup
spec:
  schedule: "0 * * * *"
  timeZone: Etc/UTC
  concurrencyPolicy: Forbid
  startingDeadlineSeconds: 300
  jobTemplate:
    spec:
      activeDeadlineSeconds: 4500
      backoffLimit: 1
      template:
        spec:
          restartPolicy: Never
          securityContext:
            runAsNonRoot: true
            runAsUser: 1000
            runAsGroup: 1000
            fsGroup: 1000
          terminationGracePeriodSeconds: 30
          containers:
            - name: backup
              image: YOUR_REGISTRY/litebackup-job:latest
              args: ["/data/exports/main.sqlite", "/data/exports/audit.sqlite"]
              env:
                - name: LITEBACKUP_API_KEY
                  valueFrom:
                    secretKeyRef:
                      name: litebackup-join
                      key: token
              volumeMounts:
                - name: databases
                  mountPath: /data
          volumes:
            - name: databases
              persistentVolumeClaim:
                claimName: app-databases
```

The image runs as `node` (UID 1000). Set the pod's UID and GID to match your
app and grant that user access to the database volume. The app must create the
export files before this job runs.

`Forbid` only prevents overlap within this CronJob. It does not coordinate
other CronJobs or writers using the same backup names. A fresh pod spends one use of the token for each foreground job. Each foreground job has its own writer session. A successful pod has waited for your server and released its locks.

Kubernetes schedules are approximate and Jobs may be retried. Keep the job
idempotent, use stable names, and alert on failed Jobs. See the
[Kubernetes CronJob reference](https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/)
for scheduling and concurrency semantics.

[SDK guides](/sdk-guides) · [Source on GitHub](https://github.com/litebackup/sdk/blob/v1.11.0/docs/sqlite-cron-backup.md)
