Akonadi, Sqlite and Backups Part 2

Akonadi, Sqlite and Backups Part 2

Dec 1, 2014
Linux, Kubuntu, Akonadi, Sqlite, Backups

There are various ways you can backup sqlite. One of the ways is to use the sqlite3 command line utility:

/usr/bin/sqlite3 ~/.local/share/akonadi/akonadi.db
SQLite version 3.8.6 2014-08-15 11:46:33
Enter ".help" for usage hints.
sqlite> .backup /tmp/akonadi.db
sqlite> .quit

This will create a backup of akonadi.db in /tmp.

Sqlite also comes with a backup API - see https://www.sqlite.org/backup.html.

As python tends to be my scripting language of choice I was looking for something that I could use in python.

There were various python scripts which used the backup API, for example this and this. Both seemed overly complex for what I wanted. Browsing the web I did come across a ruby program (here) that looked good for what I wanted.

Converting that into python I ended up with:

...
def backup(src_db, dst_dir, dst_db):
    conn = sqlite3.connect(src_db, isolation_level='IMMEDIATE')
    backup_by_copy(src_db, dst_dir, dst_db)
    conn.commit()
    conn.close()
...

The backup does a file copy plus a few other bits and pieces. The advantage of this though is that the database is locked while it is happening, so no other processes are going to write to it. Not perfect, but good enough for a personal backup of the akonadi.db file.

Now it was also possible to simplify the shell script to be run from cron to just this:

#!/bin/bash

START=$(date +%s)
echo Backing akonadi.db...
/home/scott/bin/backup_sqlite.py --src_dir /home/scott/.local/share/akonadi --dst_dir /home/scott/.local/share/akonadi --dbname akonadi.db
echo Done.
FINISH=$(date +%s)
echo "Total Time: $(( ($FINISH-$START) / 60 )) minutes, $(( ($FINISH-$START) % 60 )) seconds"

So no dbus stuff needed.