当前位置: 代码网 > it编程>前端脚本>Python > 使用Python备份SQLite数据库的完整过程(附详细代码)

使用Python备份SQLite数据库的完整过程(附详细代码)

2026年02月10日 Python 我要评论
1. 引言:为什么备份 sqlite 数据库至关重要?数据是任何应用程序的核心资产。无论是个人项目还是商业应用,数据的丢失都可能带来灾难性后果。对于 sqlite 数据库而言,备份更是不可或缺的实践。

1. 引言:为什么备份 sqlite 数据库至关重要?

数据是任何应用程序的核心资产。无论是个人项目还是商业应用,数据的丢失都可能带来灾难性后果。对于 sqlite 数据库而言,备份更是不可或缺的实践。

1.1 sqlite 数据库的特点

  • 文件型数据库:sqlite 数据库以单个文件形式存储在文件系统中(通常是 .db.sqlite 扩展名)。
  • 零配置、嵌入式:无需独立的服务器进程,直接嵌入到应用程序中。
  • 高可用性:在应用程序内部直接访问数据,但在数据丢失时恢复复杂。

1.2 备份的常见场景与挑战

  • 数据损坏:硬件故障、操作系统崩溃、程序错误等都可能导致数据库文件损坏。
  • 意外删除或修改:用户或应用程序的错误操作可能导致数据丢失或错误修改。
  • 迁移与升级:在数据库迁移到新系统或进行 schema 升级前,备份是安全保障。
  • 版本控制:为数据库的不同状态创建快照。

对于 sqlite 这种文件型数据库,最简单的备份方式似乎是直接复制文件。然而,如果数据库在复制过程中处于活跃写入状态,直接复制可能导致备份文件不一致或损坏。这就是 sqlite3.connection.backup() 方法发挥作用的地方。

2. 核心方法:sqlite3.connection.backup()

python sqlite3 模块提供了一个名为 backup() 的强大方法,它允许你在数据库运行时进行安全、一致的备份。

2.1backup()方法的工作原理

backup() 方法实现了 sqlite 数据库的在线备份 api。它通过以下方式工作:

  1. 它在一个源数据库连接和一个目标数据库连接之间进行操作。
  2. 它会逐页地将源数据库的数据复制到目标数据库。
  3. 在复制过程中,它会确保数据的一致性,这意味着即使在备份期间源数据库有写入操作,备份文件也是一个在某个时间点上一致的快照。
  4. 这个过程是非阻塞的,源数据库在备份期间可以继续进行读写操作。

2.2backup()的优势:原子性、一致性、非阻塞

  • 原子性:备份操作要么完全成功,要么不成功(如果失败则不会留下损坏的备份文件)。
  • 一致性:生成的备份文件是源数据库在备份开始时的一个完整、一致的副本,即使在备份过程中源数据库被修改。
  • 非阻塞:源数据库可以继续接受读写请求,不会因为备份操作而被锁定。

2.3 方法签名与参数

source_connection.backup(target_connection, *, pages=-1, progress=none, name='main', sleep=0.250)

  • target_connection: 目标数据库的 sqlite3.connection 对象。备份的数据将写入这个连接。
  • pages: 每次迭代复制的最大页数。默认为 -1,表示一次性复制所有剩余页。对于大型数据库,可以设置一个较小的正整数来分批复制,以便在备份过程中进行其他操作或更新进度。
  • progress: 一个可选的 callable 对象 (函数或方法),用于报告备份进度。它会在每次复制一批页后被调用。该 callable 接受三个整数参数:
    1. status: 当前已复制的页数。
    2. remaining: 剩余未复制的页数。
    3. total: 数据库的总页数。
  • name: 要备份的源数据库的名称。默认为 'main'。这在处理附加数据库 (attached databases) 时有用。
  • sleep: 在每次 pages 批次复制后,进程休眠的时间(秒)。默认为 0.250 秒。这可以减少备份操作对源数据库性能的影响。

3. 分步实现:使用backup()进行备份

3.1准备:创建示例源数据库

首先,我们需要一个包含一些数据的 sqlite 数据库文件作为源,以便进行备份。

import sqlite3
import os

source_db = "my_app.db"
backup_db = "my_app_backup.db"

def create_sample_db(db_filepath):
    """创建并填充一个示例sqlite数据库。"""
    print(f"\n--- creating sample database: {db_filepath} ---")
    conn = sqlite3.connect(db_filepath)
    cursor = conn.cursor()
    cursor.execute('''
        create table if not exists users (
            id integer primary key autoincrement,
            name text not null,
            email text unique,
            age integer
        );
    ''')
    # 插入一些数据,如果表为空
    cursor.execute("select count(*) from users;")
    if cursor.fetchone()[0] == 0:
        cursor.execute("insert into users (name, email, age) values ('alice', 'alice@example.com', 30);")
        cursor.execute("insert into users (name, email, age) values ('bob', 'bob@example.com', 25);")
        cursor.execute("insert into users (name, email, age) values ('charlie', 'charlie@example.com', 35);")
        conn.commit()
        print(f"  inserted 3 sample users into '{db_filepath}'.")
    else:
        print(f"  '{db_filepath}' already contains data. skipping insertion.")
    conn.close()

def cleanup_files(*filenames):
    """删除指定的文件。"""
    print("\n--- cleaning up files ---")
    for filename in filenames:
        if os.path.exists(filename):
            os.remove(filename)
            print(f"  deleted file: {filename}")

3.2建立源数据库连接

打开源数据库文件,获得一个 sqlite3.connection 对象。

source_conn = sqlite3.connect(source_db)

3.3建立目标备份数据库连接

打开目标备份文件(如果文件不存在,sqlite3.connect() 会自动创建它)。

backup_conn = sqlite3.connect(backup_db)

3.4执行备份操作

调用源连接对象的 backup() 方法,并传入目标连接。

try:
    source_conn.backup(backup_conn)
    print(f"backup of '{source_db}' to '{backup_db}' completed successfully.")
except sqlite3.error as e:
    print(f"error during backup: {e}")

3.5关闭连接与资源管理

无论备份成功与否,都应该关闭两个数据库连接,释放资源。

finally:
    if source_conn:
        source_conn.close()
    if backup_conn:
        backup_conn.close()

4. python 代码示例

import sqlite3
import os

# --- configuration ---
source_db = "my_application.db"
backup_db = "my_application_backup.db"

# --- helper functions (defined above, repeated for clarity) ---
def create_sample_db(db_filepath):
    """创建并填充一个示例sqlite数据库。"""
    print(f"\n--- creating sample database: {db_filepath} ---")
    conn = sqlite3.connect(db_filepath)
    cursor = conn.cursor()
    cursor.execute('''
        create table if not exists users (
            id integer primary key autoincrement,
            name text not null,
            email text unique,
            age integer
        );
    ''')
    cursor.execute("select count(*) from users;")
    if cursor.fetchone()[0] == 0:
        cursor.execute("insert into users (name, email, age) values ('alice', 'alice@example.com', 30);")
        cursor.execute("insert into users (name, email, age) values ('bob', 'bob@example.com', 25);")
        cursor.execute("insert into users (name, email, age) values ('charlie', 'charlie@example.com', 35);")
        conn.commit()
        print(f"  inserted 3 sample users into '{db_filepath}'.")
    else:
        print(f"  '{db_filepath}' already contains data. skipping insertion.")
    conn.close()

def cleanup_files(*filenames):
    """删除指定的文件。"""
    print("\n--- cleaning up files ---")
    for filename in filenames:
        if os.path.exists(filename):
            os.remove(filename)
            print(f"  deleted file: {filename}")

# --- core backup function ---
def perform_backup(source_db_path, backup_db_path):
    """
    使用 sqlite3.connection.backup() 方法备份 sqlite 数据库。
    """
    print(f"\n--- starting backup from '{source_db_path}' to '{backup_db_path}' ---")
    source_conn = none
    backup_conn = none
    try:
        source_conn = sqlite3.connect(source_db_path)
        backup_conn = sqlite3.connect(backup_db_path)
        
        # 执行备份操作
        source_conn.backup(backup_conn)
        
        print(f"backup of '{source_db_path}' to '{backup_db_path}' completed successfully.")
        return true
    except sqlite3.error as e:
        print(f"error during backup: {e}")
        return false
    except exception as e:
        print(f"an unexpected error occurred: {e}")
        return false
    finally:
        if source_conn:
            source_conn.close()
        if backup_conn:
            backup_conn.close()

# --- verification function ---
def verify_backup(db_filepath):
    """验证备份数据库是否存在并包含数据。"""
    print(f"\n--- verifying backup: {db_filepath} ---")
    if not os.path.exists(db_filepath):
        print(f"  error: backup file '{db_filepath}' does not exist.")
        return false

    conn = none
    try:
        conn = sqlite3.connect(db_filepath)
        cursor = conn.cursor()
        cursor.execute("select count(*) from users;")
        row_count = cursor.fetchone()[0]
        print(f"  backup file '{db_filepath}' exists and contains {row_count} rows in 'users' table.")
        
        # 打印一些示例数据
        cursor.execute("select id, name, email, age from users limit 2;")
        sample_data = cursor.fetchall()
        print("  sample data from backup:")
        for row in sample_data:
            print(f"    id: {row[0]}, name: {row[1]}, email: {row[2]}, age: {row[3]}")
        return true
    except sqlite3.error as e:
        print(f"  error accessing backup database '{db_filepath}': {e}")
        return false
    finally:
        if conn:
            conn.close()

# --- main execution ---
# cleanup_files(source_db, backup_db) # clean up previous runs
# create_sample_db(source_db)
# if perform_backup(source_db, backup_db):
#     verify_backup(backup_db)
# cleanup_files(source_db, backup_db) # clean up after execution

5. 最佳实践与注意事项

5.1错误处理 (try-except)

始终将备份操作包装在 try...except sqlite3.error 块中,以捕获可能发生的数据库错误。如果使用 with 语句管理连接,它可以简化资源释放,但在 backup() 方法中,由于涉及两个连接,手动 finally 块确保关闭所有连接更为稳妥。

5.2备份进度监控 (使用progress回调)

对于大型数据库,备份可能需要一段时间。提供一个 progress 回调函数可以向用户显示备份进度。

def backup_progress(status, remaining, total):
    """
    备份进度回调函数。
    :param status: 已复制的页数。
    :param remaining: 剩余未复制的页数。
    :param total: 数据库的总页数。
    """
    print(f"\r  copied: {status} pages, remaining: {remaining} pages, total: {total} pages "
          f"({(status/total)*100:.2f}%)", end='')

# 在 perform_backup 函数中调用
# source_conn.backup(backup_conn, progress=backup_progress)
# print() # 备份完成后换行

5.3文件路径管理

  • 唯一文件名:为备份文件添加时间戳或版本号,以防止覆盖,并能够回溯到不同时间点的备份。例如 my_app_backup_2023-10-27_10-30-00.db
  • 备份目录:将所有备份存储在一个专门的备份目录中,方便管理和清理。

5.4替代方案:简单的文件复制 (shutil.copyfile)

如果你确定在备份时数据库没有被任何进程写入,或者数据库是一个只读文件,那么直接使用 shutil.copyfile() 是一种简单快速的方法。

import shutil

def simple_file_copy_backup(source_path, backup_path):
    """
    简单的文件复制备份,仅在源数据库未被写入时安全。
    """
    print(f"\n--- performing simple file copy backup from '{source_path}' to '{backup_path}' ---")
    if not os.path.exists(source_path):
        print(f"  error: source database '{source_path}' does not exist.")
        return false
    try:
        shutil.copyfile(source_path, backup_path)
        print(f"  successfully copied '{source_path}' to '{backup_path}'.")
        return true
    except exception as e:
        print(f"  error during file copy: {e}")
        return false

# --- 严重警告 ---
# 仅当确定源数据库在复制期间不会有任何写入操作时才使用此方法。
# 否则,你可能会得到一个损坏或不一致的备份文件。
# 对于正在使用的数据库,始终优先使用 sqlite3.connection.backup()。

5.5备份压缩(zipfile,shutil.make_archive)

sqlite 数据库文件可能很大。为了节省存储空间和方便传输,可以考虑对备份文件进行压缩。

import zipfile

def compress_backup(db_filepath, zip_filepath):
    """将数据库文件压缩成zip文件。"""
    print(f"\n--- compressing '{db_filepath}' to '{zip_filepath}' ---")
    try:
        with zipfile.zipfile(zip_filepath, 'w', zipfile.zip_deflated) as zf:
            zf.write(db_filepath, os.path.basename(db_filepath))
        print(f"  successfully compressed '{db_filepath}' to '{zip_filepath}'.")
        return true
    except exception as e:
        print(f"  error compressing backup: {e}")
        return false

# example usage:
# if perform_backup(source_db, backup_db):
#     compress_backup(backup_db, backup_db + ".zip")

5.6自动化与调度

在生产环境中,备份通常是自动化任务。你可以使用:

  • linux/macos: cron 任务调度器来定时执行 python 备份脚本。
  • windows: 任务计划程序 (task scheduler) 来定时执行 python 备份脚本。

5.7存储位置与恢复策略

  • 异地存储:将备份文件存储在与源数据库不同的物理位置(例如,网络共享、云存储、外部硬盘),以防止整个系统故障导致数据丢失。
  • 多个备份版本:保留多个时间点的备份,以便在需要时可以选择恢复到某个特定的历史状态。
  • 测试恢复:定期测试备份文件的可恢复性,确保备份有效。

5.8从备份恢复

恢复通常只是将备份文件复制回原始数据库的位置。但在复制之前,请确保原始数据库文件被关闭或删除,并且备份文件是正确的。

def restore_from_backup(backup_db_path, target_db_path):
    """
    从备份文件恢复数据库。
    警告:这将覆盖目标路径的现有数据库!
    """
    print(f"\n--- restoring from '{backup_db_path}' to '{target_db_path}' ---")
    if not os.path.exists(backup_db_path):
        print(f"  error: backup file '{backup_db_path}' does not exist.")
        return false
    try:
        # 确保目标数据库连接已关闭
        if os.path.exists(target_db_path):
            os.remove(target_db_path) # 删除旧的(或损坏的)数据库
            print(f"  deleted existing database at '{target_db_path}'.")
        shutil.copyfile(backup_db_path, target_db_path)
        print(f"  successfully restored '{backup_db_path}' to '{target_db_path}'.")
        return true
    except exception as e:
        print(f"  error during restoration: {e}")
        return false

6. 综合代码示例:包含进度监控和验证

import sqlite3
import os
import shutil # for file copy operations and cleanup
import datetime # for timestamping backup files

# --- configuration ---
source_db = "production_app.db"
backup_dir = "backups" # directory to store backups
zip_dir = "compressed_backups" # directory to store compressed backups

# --- 1. helper functions ---
def create_sample_db(db_filepath):
    """creates and populates a sample sqlite database."""
    print(f"\n--- creating sample database: {db_filepath} ---")
    conn = none
    try:
        conn = sqlite3.connect(db_filepath)
        cursor = conn.cursor()
        cursor.execute('''
            create table if not exists inventory (
                id integer primary key autoincrement,
                item_name text not null,
                quantity integer default 0,
                last_updated text
            );
        ''')
        cursor.execute("select count(*) from inventory;")
        if cursor.fetchone()[0] == 0:
            cursor.execute("insert into inventory (item_name, quantity, last_updated) values ('laptop', 150, '2023-10-26 10:00:00');")
            cursor.execute("insert into inventory (item_name, quantity, last_updated) values ('monitor', 200, '2023-10-26 11:30:00');")
            cursor.execute("insert into inventory (item_name, quantity, last_updated) values ('keyboard', 300, '2023-10-26 12:00:00');")
            conn.commit()
            print(f"  inserted 3 sample items into '{db_filepath}'.")
        else:
            print(f"  '{db_filepath}' already contains data. skipping insertion.")
    except sqlite3.error as e:
        print(f"  database error during sample db creation: {e}")
    finally:
        if conn:
            conn.close()

def cleanup_dirs(*dirs):
    """deletes directories and their contents."""
    print("\n--- cleaning up directories ---")
    for dir_path in dirs:
        if os.path.exists(dir_path):
            shutil.rmtree(dir_path)
            print(f"  deleted directory: {dir_path}")

def cleanup_files(*filenames):
    """deletes specified files."""
    for filename in filenames:
        if os.path.exists(filename):
            os.remove(filename)
            print(f"  deleted file: {filename}")

def get_backup_filename(base_name, timestamp=true, extension=".db"):
    """生成带时间戳的备份文件名。"""
    if timestamp:
        current_time = datetime.datetime.now().strftime("%y%m%d_%h%m%s")
        return f"{base_name}_{current_time}{extension}"
    return f"{base_name}{extension}"

# --- 2. backup progress callback ---
def backup_progress_callback(status, remaining, total):
    """prints backup progress to the console."""
    percentage = (status / total) * 100 if total > 0 else 0
    print(f"\r  progress: {status}/{total} pages ({percentage:.2f}%) "
          f"remaining: {remaining} pages", end='', flush=true)

# --- 3. core backup function ---
def perform_db_backup(source_db_path, backup_dir_path):
    """
    使用 sqlite3.connection.backup() 方法备份 sqlite 数据库,并包含进度监控。
    备份文件将存储在 backup_dir_path 目录下,并带有时间戳。
    """
    if not os.path.exists(backup_dir_path):
        os.makedirs(backup_dir_path)
        print(f"  created backup directory: {backup_dir_path}")

    base_name = os.path.basename(source_db_path).replace(".db", "")
    backup_filename = get_backup_filename(base_name)
    target_backup_path = os.path.join(backup_dir_path, backup_filename)
    
    print(f"\n--- starting online backup from '{source_db_path}' to '{target_backup_path}' ---")
    source_conn = none
    backup_conn = none
    try:
        source_conn = sqlite3.connect(source_db_path)
        backup_conn = sqlite3.connect(target_backup_path)
        
        # 执行备份操作,包含进度回调
        source_conn.backup(backup_conn, pages=1, progress=backup_progress_callback, sleep=0.05) # small pages for visible progress
        
        print("\nbackup completed successfully.")
        return target_backup_path
    except sqlite3.error as e:
        print(f"\nerror during backup: {e}")
        if os.path.exists(target_backup_path):
            os.remove(target_backup_path) # clean up partial backup on error
            print(f"  removed incomplete backup file: {target_backup_path}")
        return none
    except exception as e:
        print(f"\nan unexpected error occurred during backup: {e}")
        return none
    finally:
        if source_conn:
            source_conn.close()
        if backup_conn:
            backup_conn.close()

# --- 4. verification function ---
def verify_db_backup(db_filepath):
    """verifies the backup database exists and contains data."""
    print(f"\n--- verifying backup: {db_filepath} ---")
    if not os.path.exists(db_filepath):
        print(f"  error: backup file '{db_filepath}' does not exist.")
        return false

    conn = none
    try:
        conn = sqlite3.connect(db_filepath)
        cursor = conn.cursor()
        cursor.execute("select count(*) from inventory;")
        row_count = cursor.fetchone()[0]
        print(f"  backup file '{db_filepath}' exists and contains {row_count} rows in 'inventory' table.")
        
        cursor.execute("select id, item_name, quantity from inventory limit 2;")
        sample_data = cursor.fetchall()
        print("  sample data from backup:")
        for row in sample_data:
            print(f"    id: {row[0]}, item: {row[1]}, quantity: {row[2]}")
        return true
    except sqlite3.error as e:
        print(f"  error accessing backup database '{db_filepath}': {e}")
        return false
    except exception as e:
        print(f"  an unexpected error occurred during verification: {e}")
        return false
    finally:
        if conn:
            conn.close()

# --- 5. compression function ---
def compress_db_backup(db_filepath, compressed_dir_path):
    """compresses a database file into a .zip archive."""
    if not os.path.exists(compressed_dir_path):
        os.makedirs(compressed_dir_path)
        print(f"  created compressed backup directory: {compressed_dir_path}")

    zip_filename = os.path.basename(db_filepath) + ".zip"
    target_zip_path = os.path.join(compressed_dir_path, zip_filename)
    
    print(f"\n--- compressing '{db_filepath}' to '{target_zip_path}' ---")
    try:
        with zipfile.zipfile(target_zip_path, 'w', zipfile.zip_deflated) as zf:
            zf.write(db_filepath, os.path.basename(db_filepath)) # write with just filename inside zip
        print(f"  successfully compressed '{db_filepath}' to '{target_zip_path}'.")
        return target_zip_path
    except exception as e:
        print(f"  error compressing backup: {e}")
        return none

# --- main program execution ---
def main():
    cleanup_dirs(backup_dir, zip_dir)
    cleanup_files(source_db) # ensure source db is clean if it was left from previous run

    create_sample_db(source_db) # create the source database with some data

    # perform backup using sqlite3.connection.backup()
    backup_file_path = perform_db_backup(source_db, backup_dir)
    
    if backup_file_path:
        verify_db_backup(backup_file_path) # verify the created backup
        
        # optionally, compress the backup file
        compressed_file_path = compress_db_backup(backup_file_path, zip_dir)
        if compressed_file_path:
            print(f"\ncompressed backup available at: {compressed_file_path}")
            
    print("\n--- program finished ---")
    # cleanup_dirs(backup_dir, zip_dir) # uncomment to delete backup files after run
    # cleanup_files(source_db) # uncomment to delete source db after run

if __name__ == "__main__":
    main()

7. 总结

为您详尽解析了在 python 中使用 sqlite3 模块备份 sqlite 数据库的方法。

核心要点回顾:

  • sqlite3.connection.backup() 是在数据库活跃时进行安全、一致备份的最佳实践,它保证了原子性和非阻塞性。
  • 进度监控:使用 progress 回调函数可以为大型数据库备份提供用户反馈。
  • 错误处理:始终捕获 sqlite3.error 以确保程序的健壮性。
  • 文件名和目录管理:为备份文件添加时间戳,并组织到专门的备份目录中,便于管理。
  • 文件复制 (shutil.copyfile) 仅适用于确定数据库未被写入的场景,否则存在数据损坏风险。
  • 压缩:使用 zipfile 等模块可以有效减小备份文件大小。
  • 自动化和异地存储:考虑将备份过程自动化,并将备份文件存储在安全、独立的位置。

通过掌握这些方法和最佳实践,您将能够构建一个可靠的备份策略,有效保护您的 sqlite 数据库数据免受意外丢失。

到此这篇关于使用python备份sqlite数据库的文章就介绍到这了,更多相关python备份sqlite数据库内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com