Skip to content

Instantly share code, notes, and snippets.

@sarim
Created September 6, 2013 08:14
Show Gist options
  • Save sarim/6460936 to your computer and use it in GitHub Desktop.
Save sarim/6460936 to your computer and use it in GitHub Desktop.
Sqlite db repair script
#!/bin/bash
DBSTATUS=$(sqlite3 "$1" "PRAGMA integrity_check")
if [ "$DBSTATUS" == "ok" ] ; then
echo DB ALREADY OK
else
echo FIXING CORRUPT DB
TMPDB=$(mktemp -t sarim)
echo ".mode insert
.dump" | sqlite3 "$1" > $TMPDB
rm "$1"
sed -i "" "s/ROLLBACK; -- due to errors/COMMIT;/" $TMPDB
sqlite3 "$1" < $TMPDB
rm $TMPDB
echo DB FIXED
fi
@gitthangbaby
Copy link

throwing really a lot of errors (multiline command isn't normal, /tmp can be full, sed should not have two expressions..), using this:


[ "$1" == "" ] && exit
DBSTATUS=$(sqlite3 "$1" "PRAGMA integrity_check")
if [ "$DBSTATUS" == "ok" ] ; then
    echo "DB ALREADY OK"
else
    echo "FIXING CORRUPT DB (status $DBSTATUS)"
    TMPDB=$1.tmp
    echo -e ".mode insert\n.dump" | sqlite3 "$1" > $TMPDB
    rm "$1"
    sed -i  "s/ROLLBACK; -- due to errors/COMMIT;/" $TMPDB
    sqlite3 "$1" < $TMPDB
    rm $TMPDB
    echo "DB FIXED"
fi

@LinuxforPunks
Copy link

Error: in prepare, database disk image is malformed (11)
repairsqlite.sh: 4: [: DBSTATUS: unexpected operator
FIXING CORRUPT DB
mktemp: too few X's in template ‘sarim’
repairsqlite.sh: 10: cannot create : Directory nonexistent
sed: can't read s/ROLLBACK; -- due to errors/COMMIT;/: No such file or directory
repairsqlite.sh: 13: cannot open : No such file
rm: missing operand
Try 'rm --help' for more information.
DB FIXED

It is saying the DB is FIXED without checking if it is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment