Sling Academy
Home/SQLite/Using CLI Commands to Backup and Restore SQLite Databases

Using CLI Commands to Backup and Restore SQLite Databases

Last updated: December 07, 2024

SQLite, a lightweight disk-based database, is a prime choice for applications that require simple database management without a setup prerequisite. Nonetheless, ensuring your database is adequately backed up and restorable is critical for data integrity and recovery post-mishap. This article will guide you through using Command Line Interface (CLI) commands for this task, assuming a basic familiarity with terminal operations and SQLite.

Prerequisites

  • Ensure SQLite is installed on your machine. You can download it from the official website.
  • Basic knowledge of command-line operations.
  • A test SQLite database to work with.

Backing Up SQLite Databases

SQLite backs up databases using the `sqlite3` command-line tool. Below are the steps and command needed to perform a proper backup.

Step 1: Open Terminal

Before starting any operations, open your terminal or command prompt and navigate to the directory where your SQLite database is stored.

Step 2: Use the CLI to Create a Backup

The basic command to back up a database is:

sqlite3 path/to/your/database.db ".backup 'path/to/your/backup.db'"

In this command:

  • path/to/your/database.db: Path to your existing SQLite database.
  • path/to/your/backup.db: The location where you want to store the backup.

Using this command ensures that a snapshot of the entire database is safely copied to your specified backup file, which you can restore at any time.

Restoring SQLite Databases

Restoring a database involves a read operation from the backup to recreate the original database at the desired state. Here’s how you do it.

Step 1: Open Terminal

Launch the terminal or command prompt, and if necessary, navigate to the directory where your backup file exists.

Step 2: Use the CLI to Restore Your Database

The command to restore from a backup is essentially the same but reversed:

sqlite3 path/to/new/database.db ".restore 'path/to/your/backup.db'"

Here:

  • path/to/new/database.db: This is the file you want your restored database to be copied into. If the file exists, ensure that you're okay with overwriting it, as this operation will replace its content.
  • path/to/your/backup.db: The path to your existing backup.

This command helps reset your database to its state at the time of the last backup, effectively negating any corruptions or unwanted changes made since then.

Automating Backups with Shell Scripting

Frequent backups are crucial, and with a simple shell script, automating this process becomes straightforward:

#!/bin/bash

# Set up paths
SOURCE_DB="/path/to/your/database.db"
BACKUP_DB_PATH="/your/desired/backup/location"
DATE=$(date +"%Y-%m-%d_%H-%M-%S")

# Create a backup
sqlite3 "$SOURCE_DB" ".backup '$BACKUP_DB_PATH/backup_$DATE.db'"
echo "Backup completed for $SOURCE_DB at $DATE"

Save the script with a `.sh` extension and execute it via your terminal using:

chmod +x /path/to/your/script.sh
./path/to/your/script.sh

This script not only backs up your database but prefixes the backup file with a timestamp for unique identification.

By integrating these CLI commands and optional scripting into your database management routine, maintaining data continuity and mitigating the risk of loss becomes a manageable and efficient task. Your data remains secure, recoverable, and up-to-date, leaving you untroubled in case things go south.

Next Article: How to Ensure Data Integrity in SQLite Backups

Previous Article: Best Practices for Managing SQLite Backups in Production

Series: Backup and Restore Databases in SQLite

SQLite

You May Also Like

  • How to use regular expressions (regex) in SQLite
  • SQLite UPSERT tutorial (insert if not exist, update if exist)
  • What is the max size allowed for an SQLite database?
  • SQLite Error: Invalid Value for PRAGMA Configuration
  • SQLite Error: Failed to Load Extension Module
  • SQLite Error: Data Type Mismatch in INSERT Statement
  • SQLite Warning: Query Execution Took Longer Than Expected
  • SQLite Error: Cannot Execute VACUUM on Corrupted Database
  • SQLite Error: Missing Required Index for Query Execution
  • SQLite Error: FTS5 Extension Malfunction Detected
  • SQLite Error: R-Tree Node Size Exceeds Limit
  • SQLite Error: Session Extension: Invalid Changeset Detected
  • SQLite Error: Invalid Use of EXPLAIN Statement
  • SQLite Warning: Database Connection Not Closed Properly
  • SQLite Error: Cannot Attach a Database in Encrypted Mode
  • SQLite Error: Insufficient Privileges for Operation
  • SQLite Error: Cannot Bind Value to Parameter
  • SQLite Error: Maximum String or Blob Size Exceeded
  • SQLite Error: Circular Reference in Foreign Key Constraints