Akonadi, Sqlite and Backups Part 1

Akonadi, Sqlite and Backups Part 1

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

On both my home PC and work laptop I use kontact for my email, calendaring, etc. I didn’t want to use mysql for the underlying database (I don’t particular like it) so decided to use sqlite instead. Generally I have found this to work well, with one or two exceptions.

However, searching for how to automate sqlite backups gives numerous results. The easiest way is to stop whatever is writing to it, in this case akonadi, and then copy the database. Something like:

/usr/bin/akonadictl stop
cp ~/.local/share/akonadi/akonadi.db ~/backups/akonadi.db.backup
/usr/bin/akonadictl start

This will work fine providing you are not trying to do it from inside cron. If you do you will end up with a whole bunch of dbus errors.

To get around this you need to create a shell script which looks something like this:

#!/bin/bash

START=$(date +%s)
echo Backing akonadi.db...
dbus_session_file=~/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0

if [ -e "$dbus_session_file" ]; then
    . "$dbus_session_file"
    export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID

    /usr/bin/akonadictl stop &>/dev/null
    sleep 10
    # What you use to backup sqlite
    /usr/bin/akonadictl start &>/dev/null
fi

echo Done.
FINISH=$(date +%s)
echo "Total Time: $(( ($FINISH-$START) / 60 )) minutes, $(( ($FINISH-$START) % 60 )) seconds"

You can now run this script from cron:

0 4 * * * /home/scott/bin/backup_sqlite.sh

However, there is an easier way…