Skip to content

Instantly share code, notes, and snippets.

@sdellis
Created October 4, 2012 17:27
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save sdellis/3835123 to your computer and use it in GitHub Desktop.
Save sdellis/3835123 to your computer and use it in GitHub Desktop.
sqlite3 database backup script with integrity checking
#!/bin/bash
#
# sqlite3_backup.sh
# Script for backing up sqlite3 database with integrity checking
# Intended for use with cron for regular automated backups
#
# @author <shaune@princeton.edu>
#
DB="/path/to/file.db"
BKUP_PATH="/path/to/backup/dir"
LOG="/path/to/backup.log"
# check to see if $DB exists
if [ ! -f $DB ]
then
echo $(date +"%Y-%m-%d %T") -- cannot find the database at the following path: $DB >> $LOG
exit 1
fi
# check to make sure $BKUP_PATH exists and is writable
if [ ! -d $BKUP_PATH ]
then
# BKUP_PATH does not exist or is not a directory
echo $(date +"%Y-%m-%d %T") -- cannot find backup path: $BKUP_PATH >> $LOG
exit 1
else
# check to make sure it's writable
if [ ! -w $DB ]
then
echo $(date +"%Y-%m-%d %T") -- BKUP Path is not writable: $BKUP_PATH >> $LOG
exit 1
fi
fi
# check to see if md5 file exists
if [ ! -f $BKUP_PATH/$(basename $DB).md5 ]
then
# md5 file does not exist
# create md5 of DB and store it in BKUP_PATH
md5sum $DB > $BKUP_PATH/$(basename $DB).md5
exit 1
fi
# compare md5 file and DB md5
md5sum --status -c $BKUP_PATH/$(basename $DB).md5
match=$?
if [ $match -ne 0 ]; then
# if different, do integrity check to make sure it hasn't been corrupted
echo -ne $(date +"%Y-%m-%d %T --")" " >> $LOG
sqlite3 -line $DB 'pragma integrity_check;' >> $LOG
has_integrity=$?
if [ $has_integrity -eq 0 ]; then
# overwrite db file and make new checksum
md5sum $DB > $BKUP_PATH/$(basename $DB).md5
cp $DB $BKUP_PATH/$(basename $DB).bk
copied=$?
if [ $copied -ne 0 ]; then
#failed to copy
echo $(date +"%Y-%m-%d %T") -- Error writing backup file to $BKUP_PATH/$(basename $DB).bk >> $LOG
exit 1
fi
echo $(date +"%Y-%m-%d %T") -- $DB has changed! Backup successful. >> $LOG
exit 0
else
# db is corrupt
echo $(date +"%Y-%m-%d %T") -- $DB IS CORRUPT! Backup not overwritten. >> $LOG
exit 1
fi
fi
@coderjolly
Copy link

which lang is being used ?

@itech001
Copy link

itech001 commented Oct 9, 2018

@pkatzman the last else should be moved before 'elif' ???

@stevekm
Copy link

stevekm commented Nov 14, 2018

@coderjolly its a bash script.

This does not work on macOS; md5sum does not exist.

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