def create_backup(dbpath: str, conn: sqlite3.Connection) -> int:
    """
    Creates a backup of the specified database, using the specified path dbpath,
    and an open database connection from which the backup is created.

    Backup will have the filepath "<dbpath>_backup". Will not overwrite existing backup.
    @param dbpath: Path + Filename of the original database
    @param conn: Active connection to said original database
    @return: 0 if a backup was created, 1 if it failed.
    """
    try:

        def progress(status, remaining, total):
            logger.debug(f"Copy status: {status}")
            logger.debug(f'Copied {total - remaining} of {total} pages...')

        backup_path = dbpath + '_backup'
        if os.path.exists(backup_path):
            logger.info(f"Previous backup already exists, skipping...")
            return 0

        logger.info(f"Creating a database backup at {backup_path}")
        bck = sqlite3.connect(backup_path)
        with bck:
            conn.backup(bck, pages=1, progress=progress)
        bck.close()
        return 0
    except (sqlite3.IntegrityError, sqlite3.OperationalError):
        logger.error(f"Failed to create backup of {dbpath}")
        logger.error(traceback.format_exc())
        return 1
Exemple #2
0
def sqlite_backup(*, source: sqlite3.Connection, dest: sqlite3.Connection, **kwargs) -> None:
    if sys.version_info[:2] >= (3, 7):
        source.backup(dest, **kwargs)
    else:
        # https://stackoverflow.com/a/10856450/706389
        import io
        tempfile = io.StringIO()
        for line in source.iterdump():
            tempfile.write('%s\n' % line)
        tempfile.seek(0)

        dest.cursor().executescript(tempfile.read())
        dest.commit()
Exemple #3
0
def backup_db(source: sqlite3.Connection):
    print("Backing up the database")
    s3_client = boto3.client("s3")

    day_of_month = datetime.datetime.today().day
    object_name = f"geocoding_cache/{day_of_month:02}.sqlite3"

    with tempfile.NamedTemporaryFile() as destination_file:
        dest = sqlite3.connect(destination_file.name)
        print("Making a copy of the database...")
        source.backup(dest)

        dest.close()

        print("Uploading to s3...")
        s3_client.upload_file(destination_file.name, "kijiji-apartments",
                              object_name)
        print("...done")
Exemple #4
0
def backup_database(conn: sqlite3.Connection, back_fp):
    with sqlite3.connect(back_fp) as back:
        conn.backup(back)