Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-19 08:00:06

0001 #!/bin/sh
0002 
0003 if [ "$1" == "-h" ] ; then
0004     echo "Usage: `basename $0` [-h] <DB_FILE> <BACKUP_DIR> <NUM_BACKUP> 
0005 
0006 A script to backup sqlite3 database. Backup files are created when integrity_check is successful.
0007 Only the recent backup files are kept.
0008 
0009     -h         show this help text
0010     DB_FILE    path to the database file
0011     BACKUP_DIR directory to store backup files
0012     NUM_BACKUP the number of backup files to keep"
0013     exit 0
0014 fi
0015 
0016 db_file=$1
0017 backup_dir=$2
0018 nbackup=$3
0019 
0020 mkdir -p $backup_dir
0021 now="$(date +'%Y-%m-%d-%H:%M')"
0022 backup_file="$backup_dir/$(basename $db_file).backup.$now"
0023 
0024 echo $now -- Start.
0025 
0026 sqlite3 $db_file 'pragma integrity_check;'
0027 has_integrity=$?
0028 
0029 if [ $has_integrity -eq 0 ]; then
0030     sqlite3 $db_file ".backup $backup_file"
0031     echo $now -- Backup successful in $backup_file.
0032 else 
0033     # db is corrupt
0034     echo $now -- Integrity_check faild.
0035     exit 1
0036 fi
0037 
0038 # delete old files
0039 ls -tr $backup_dir/$(basename $db_file).* | head -n -$nbackup | xargs --no-run-if-empty rm