Copying a weewiki database

Copying a weewiki database

Sometimes it is ideal to copy over weewiki data from one SQLite database to another. This need came up for me while working on a sample database with an internal weewiki. Copying the whole file was very slow because the SQLar portion of the database was hundreds of megabytes. It made more sense to copy the whole file once, and then copy just the wiki data.

The script wiki_copy.sh makes use of the ATTACH command in SQLite to copy tables from one location' to another. It can be run in the following way:

./wiki_copy.sh source.db destination.db

It will copy the wiki data in source.db to destination.db. Note that this will blow away any previous weewiki data in the destination database. It also assumes that the weewiki tables already exist.

<<wiki_copy.sh>>=
#!/bin/sh

if [ "$#" -eq 0 ]
then
echo "Usage: $0 src dst"
exit
fi

SRC=$1
DST=$2

sqlite3 <<EOF
ATTACH "$SRC" as src;
ATTACH "$DST" as dst;

-- first, clear the old wiki pages in destination
DELETE FROM dst.wiki WHERE 1;
DELETE FROM dst.wikilinks WHERE 1;

-- then, copy over tables from source
INSERT INTO dst.wiki SELECT * from src.wiki;
INSERT INTO dst.wikilinks SELECT * from src.wikilinks;
EOF

home | index