pythonsqlitepeewee

How to backup Peewee database (SqliteQueueDatabase) programatically?


I'm using Peewee in one of my projecs. Specifically, I'm using SqliteQueueDatabase and I need to create a backup (i.e. another *.db file) without stopping my application. I saw that there are two methods that could work for me (backup and backup_to_file) but they're methods from CSqliteExtDatabase, and SqliteQueueDatabase is subclass of SqliteExtDatabase. I've found solutions to manually create a dump of the file, but I need a *.db file (not a *.csv file, for example). Couldn't find any similar question or relevant answer.

Thanks!


Solution

  • You can just import the backup_to_file() helper from playhouse._sqlite_ext and pass it your connection and a filename:

    db = SqliteQueueDatabase('...')
    
    from playhouse._sqlite_ext import backup_to_file
    conn = db.connection()  # get the underlying pysqlite conn
    backup_to_file(conn, 'dest.db')
    

    Also, if you're using pysqlite3, then there are also backup methods available on the connection itself.