brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: sqlite3 .backup copies a live database safely where cp can catch it half-written

Database
~/tips/tip-database-sqlite3-backup-live-copy

sqlite3 app.db ".backup /backup/app-$(date +%F).db"

Makes a consistent copy of a SQLite database file while it is still in use.

Copying a SQLite file while an app is writing to it can give you a broken copy. SQLite has its own backup command.

sqlite3 app.db ".backup /backup/app-$(date +%F).db"
sqlite3 app.db "VACUUM INTO '/backup/app-small.db'"

.backup uses SQLite’s backup API, so the copy is consistent even while other connections write. VACUUM INTO (SQLite 3.27 and newer) writes a compacted copy and also leaves the original untouched.

In WAL mode the recent changes sit in a separate app.db-wal file, so a plain cp app.db can miss them. If you must copy files, stop the app first or use one of the two commands above.

Further reading: SQLite: Command Line Shell

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link