Data, SQLite and backups
Updated:
DATA_DIR — the only persistent location
A bot container's file system is read-only. The only writable place — and the only one that survives redeploys, restarts, token changes and the bot going to sleep — is the folder in the DATA_DIR environment variable (/data). Writing anywhere else (for example, inside the project folder) fails. So keep all persistent data — SQLite databases, Excel exports, uploaded files — inside DATA_DIR only (sample code).
SQLite — the supported database
Botxona officially supports one database: SQLite, a plain file inside DATA_DIR. No separate server, password or setup: the database runs alongside your bot, is backed up automatically every day, and its size is shown in the dashboard. All of the libraries below work out of the box.
Python
The standard sqlite3 module, aiosqlite, SQLAlchemy, Tortoise ORM, peewee and others.
import os, sqlite3
DATA_DIR = os.environ.get("DATA_DIR", "./data")
os.makedirs(DATA_DIR, exist_ok=True)
db = sqlite3.connect(os.path.join(DATA_DIR, "bot.db"))
db.execute("PRAGMA journal_mode=WAL")
db.execute("PRAGMA busy_timeout=5000")
db.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)")
In SQLAlchemy an absolute path needs four slashes: sqlite:////data/bot.db. Rather than typing the path by hand, build it from DATA_DIR: create_engine("sqlite:///" + os.path.join(DATA_DIR, "bot.db")).
Node.js
better-sqlite3 and sqlite3 (native modules) — the platform spots them in package.json and installs them automatically in a separate build stage that has compilers. Node 22's built-in node:sqlite (DatabaseSync) works too (it prints an ExperimentalWarning on startup, which is harmless).
const fs = require("fs")
const path = require("path")
const Database = require("better-sqlite3")
const DATA_DIR = process.env.DATA_DIR || "./data"
fs.mkdirSync(DATA_DIR, { recursive: true })
const db = new Database(path.join(DATA_DIR, "bot.db"))
db.pragma("journal_mode = WAL")
db.pragma("busy_timeout = 5000")
db.exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)")
Go
We recommend the pure-Go modernc.org/sqlite — builds are fast. The cgo-based github.com/mattn/go-sqlite3 works as well: the platform builds it with a C toolchain and links it statically; only the first build takes longer.
import (
"database/sql"
"log"
"os"
"path/filepath"
_ "modernc.org/sqlite"
)
dsn := filepath.Join(os.Getenv("DATA_DIR"), "bot.db") +
"?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)"
db, err := sql.Open("sqlite", dsn)
if err != nil {
log.Fatal(err)
}
db.SetMaxOpenConns(1) // writes run one at a time
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)`)
Good practices
- Always build the path from
DATA_DIR:os.path.join(DATA_DIR, "bot.db"),path.join(process.env.DATA_DIR, "bot.db"),filepath.Join(os.Getenv("DATA_DIR"), "bot.db"). - Turn on WAL and busy_timeout:
PRAGMA journal_mode=WALlets reads and writes stay out of each other's way, andPRAGMA busy_timeout=5000waits up to 5 seconds instead of failing with "database is locked". - One connection: keep a single connection per process (or serialize writes) — don't open a new connection for every message.
- Create tables on startup: run
CREATE TABLE IF NOT EXISTSor your migrations when the bot starts, so it also comes up cleanly with an emptyDATA_DIR. - Don't store large media in the database: for photos and videos, save the Telegram
file_id— it's a few bytes and all you need to send the file again. - Keep state (FSM/sessions) in the database: aiogram's
MemoryStorageand grammY's in-memory sessions are lost when the bot restarts and when an idle bot goes to sleep. Persist important state in SQLite.
Data quota
The size of DATA_DIR (SQLite databases and other files combined) is limited per plan:
| Plan | DATA_DIR quota |
|---|---|
| Free | 100 MB |
| Start | 1 GB |
| Freelancer | 1 GB (per bot) |
| Pro | 5 GB |
| Business | 10 GB |
Usage is measured about every 5 minutes and shown in Bot → Backups as a "used / limit" bar.
- At 90% the bot owner gets a warning in Telegram.
- At 20% over the quota the bot is stopped automatically, and the owner and support staff are notified.
You can start the bot again yourself, but if the data is still over the ceiling it will be stopped again within about 30 minutes. So free up space — delete old rows and run VACUUM (SQLite only gives deleted space back after that) — or move to a bigger plan.
Daily automatic backup
Every day at 03:00 (Tashkent time), the DATA_DIR folder of every active bot is archived in full, encrypted with AES-256-GCM and stored separately. Bots with no activity since their last backup are skipped, so no resources are wasted.
How SQLite databases are backed up
Every SQLite file in DATA_DIR (detected by its file header, whatever the extension) is copied with VACUUM INTO — a transactionally consistent snapshot taken while the bot keeps running and writing (changes still in the WAL are included). There's no need to stop the bot. The snapshot is then verified with PRAGMA quick_check.
If a database is damaged, it is still saved as a raw copy, the backup is marked "Damaged", and the bot owner gets a Telegram message once a day suggesting a restore from an earlier healthy backup.
In Bot → Backups, every backup carries a badge:
| Badge | Meaning |
|---|---|
| ✓ N databases | N SQLite databases were copied consistently and passed the check |
| Damaged | at least one database failed the check (a raw copy was kept) |
| Raw copy | files were copied as-is — the snapshot helper was unavailable at that moment |
| No databases | no SQLite databases were found in DATA_DIR |
Retention
| Plan | Retention |
|---|---|
| Free | 3 days |
| Start, Freelancer | 7 days |
| Pro | 14 days |
| Business | 30 days |
Expired backups are deleted automatically. In the dashboard, Bot → Backups lists every available backup with its date and size.
Manual backup
You can take an extra backup at any time with the "Back up now" button — for example, before a big change.
Restoring from a backup
When you press Restore, the current state is first saved automatically as a "pre-restore backup" (so nothing is lost even if you clicked by mistake), then DATA_DIR is rolled back to the selected backup and the bot restarts briefly.
Downloading a backup
The "Download" button creates a signed link valid for 24 hours — use it to save the encrypted file to your computer. If the link has expired or been tampered with, the download will not work.
Why not PostgreSQL, MongoDB or Redis?
Update: on a paid plan a bot can reach external servers, databases included — conditions. The advice below still stands: SQLite is the most reliable home for a bot's own data (backups, speed, no dependency on an outside service). During the trial and on the free plan, bot containers have no internet access: they run on an isolated network and can only reach the Telegram API through the platform proxy. That means PostgreSQL, MongoDB, Redis or MySQL servers (Supabase, Neon, MongoDB Atlas and the like) can't be reached. When the check finds such a client library or connection URL (postgres://, mongodb://, REDIS_URL, etc.), it shows a yellow warning on rule 5.
That's nothing to worry about: SQLite is more than enough for a typical Telegram bot. It handles thousands of reads and hundreds of writes per second with ease, bots with large audiences run on it comfortably — and Botxona backs your database up for you. If your project currently uses an external database, move it to SQLite: with ORMs like SQLAlchemy or Tortoise, changing the connection URL is usually enough (Prisma also needs provider = "sqlite"), then migrate your existing data once.
Next step
Code rules that prevent data loss: 6 rules — rule 5. Why data survives when a bot sleeps and wakes up: Sleep and wake-up. Every numeric limit: Limits.