litebackup / sdk
Continuous backups, live read replicas, and automatic failover for SQLite
Requires
- php: >=8.2
- ext-json: *
Requires (Dev)
- doctrine/dbal: 4.6.*
- doctrine/doctrine-bundle: 2.19.*
- friendsofphp/php-cs-fixer: 3.95.*
- laravel/framework: 12.74.*
- orchestra/testbench: 10.12.*
- phpstan/phpstan: 2.3.*
- phpunit/phpunit: 11.5.*
- symfony/framework-bundle: 7.4.*
- symfony/yaml: 7.4.*
Suggests
- ext-pdo_sqlite: for the PDO adapter, Litebackup\Integrations\PdoSqlite
- ext-sodium: to verify signed release metadata with Litebackup\Release::findRelease
- ext-sqlite3: for the SQLite3 adapter, Litebackup\Integrations\Sqlite3
Provides
None
Conflicts
None
Replaces
None
README
Use SQLite in production with PHP, Laravel, and Symfony. Litebackup adds continuous backups, live read replicas, and automatic failover while your app keeps its SQLite driver.
PHP 8.2 or later on Linux or
macOS. The package needs only ext-json; the PDO adapter needs
ext-pdo_sqlite, the SQLite3 adapter ext-sqlite3, and release
verification ext-sodium.
Install the Composer package litebackup/sdk from Packagist:
composer require litebackup/sdk export LITEBACKUP_API_KEY='litebackup_join_...@your-instance.litebackup.cloud'
LITEBACKUP_API_KEY is the value from Machines → Add machine, or an API
key (litebackup_key_...), then @ and your server address. The package carries no
litebackup command: install it with
curl -fsSL https://litebackup.com/cli.sh | bash, or set LITEBACKUP_PATH.
The SDK uses LITEBACKUP_PATH, then litebackup on PATH, then
~/.local/bin/litebackup.
Back up an app's database
<?php require __DIR__ . '/vendor/autoload.php'; use Litebackup\Client; use Litebackup\Integrations\PdoSqlite; if (!is_dir('data')) { mkdir('data'); } $db = new PDO('sqlite:data/app.sqlite'); $litebackup = new Client(); try { $rule = $litebackup->backups->start(PdoSqlite::databasePath($db)); echo $rule->id, ' ', $rule->name, PHP_EOL; } finally { $litebackup->close(); }
backups->start() returns the saved rule when the same path was registered
with the same settings, so call it on every start. Different settings for a
saved path raise rule_exists, with the saved rule in
$error->attributes['rule'].
Laravel and Symfony
In Laravel, publish the settings with
php artisan vendor:publish --tag=litebackup-config, name the connections
in config/litebackup.php, and run php artisan litebackup:start at every
deploy:
return [ 'connections' => ['sqlite' => []], ];
In Symfony, register Litebackup\Integrations\Symfony\LitebackupBundle in
config/bundles.php, name the Doctrine DBAL connections under
litebackup.connections, and run bin/console litebackup:start at every
deploy. Workers (queue:work, horizon:work, and schedule:work in
Laravel, messenger:consume in Symfony) register the backups once when they
start; HTTP requests never register.
Both take acquire, databases to acquire in order before the app opens
them, and drain_on_shutdown (false by default), which drains the session
when the outermost console command, such as a queue worker, finishes.
PHP-FPM keeps nothing between requests, so the integrations acquire in each
request; litebackup run --acquire saves that call.
PHP runs no code on SIGTERM, so PHP-FPM and other servers never drain: run
them under litebackup run, which acquires first and drains after the server
exits; a drain before run's is harmless. Under litebackup run --drain-durability cloud, leave drain_on_shutdown off.
ENTRYPOINT ["litebackup", "run", "--acquire", "app/main.sqlite=/var/www/database/database.sqlite", "--"] CMD ["php-fpm", "--nodaemonize"]
The PHP guide has the options.
Move a database between machines
A pool keeps a database open between calls. A lease locks the database for this machine, so no other machine writes it, and keeps the lock until the pool releases it:
<?php require __DIR__ . '/vendor/autoload.php'; use Litebackup\Client; use Litebackup\Integrations\Sqlite3; $litebackup = new Client(); $pool = $litebackup->databases->pool(Sqlite3::adapter()); try { $pool->lease('agents/42.sqlite', function (\SQLite3 $db): void { $db->exec('CREATE TABLE IF NOT EXISTS steps (body TEXT)'); $db->exec("INSERT INTO steps (body) VALUES ('tests passed')"); }, path: 'data/agents/42.sqlite', durability: 'server'); } finally { $pool->close(); $litebackup->close(); }
The adapters never commit: SQLite commits each statement outside a
transaction, and a transaction the work leaves open is rolled back when the
pool closes the database. Work that throws keeps the lease and never
offloads. A lease creates a database neither the server nor this machine
has; requireExisting: true makes it fail with not_found instead, as
existing: true does for $litebackup->databases->acquire(). PHP has no threads, so a pool releases an idle lease only when it
is called again or closed; the guide covers PHP-FPM and workers.
Errors
Every failure is a Litebackup\LitebackupError with errorCode, the
message, status, requestId (quote it when reporting a problem), field
(the request field at fault), and attributes. Litebackup\ErrorCode names the codes
worth matching:
use Litebackup\ErrorCode; use Litebackup\LitebackupError; try { $litebackup->databases->acquire('agents/42.sqlite', path: 'data/agents/42.sqlite'); } catch (LitebackupError $error) { if ($error->errorCode !== ErrorCode::LOCK_UNAVAILABLE) { throw $error; } error_log('another machine writes agents/42.sqlite; try again later'); }
Development
scripts/test runs PHPUnit, PHPStan, PHP-CS-Fixer, and the Laravel and
Symfony lanes in php:8.2.36-cli, php:8.3.37-cli, php:8.4.28-cli, and
php:8.5.13-cli as the calling user. scripts/test --php 8.4.28 runs one
version, scripts/test -- vendor/bin/phpunit tests/Unit/PoolTest.php runs a
command in the package's directory, and scripts/test generate regenerates
src/Generated from ../client-openapi.yaml. scripts/test --integration
also runs tests/Integration/IntegrationTest.php against
litebackup server run --development, with LITEBACKUP_PATH set to a
static litebackup binary. scripts/check-package builds the Composer
archive and installs it into a clean project without publishing anything. The SDK reference lists
every method, and the contract the behavior the
Go, TypeScript, Python, Ruby, and PHP SDKs share.