Skip to content

Commit

Permalink
rotating sqlite backups
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyclenerd committed Mar 18, 2017
1 parent 825463b commit 8873452
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions backup/backup-sqlite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bash

#
# Create rotating SQLite backups
#

# Backup location
BACKUP_PATH='/private-backup/sqlite'

# Location of the database
MY_DATABASE="/var/lib/sqlite/users.sqlite"

# Maximum number of backups
ROT_PERIOD=30


# fetch biggest id in dir
cd "$BACKUP_PATH" || exit

BIG_ID=$( ls -1 *_dump.gz | tail -n 1 | cut -d '_' -f 1 ) &> /dev/null

if [ ! "$BIG_ID" ];
then
BIG_ID=0
fi

# rotation if at least 1_dump.gz
NEXT_ID=0
for i in $( seq 1 $BIG_ID );
do
NEXT_ID=$((i+1))
NEXT_FILENAME=$NEXT_ID'_dump.gz'
FILENAME="$i""_dump.gz"
if [ -e "$FILENAME" ];
then
# echo "$FILENAME exists"
if [ "$i" = "$ROT_PERIOD" ];
then
# echo "Removing oldest archive..."
rm "$i""_dump.gz"
else
# echo "Rotating $i..."
cp "$FILENAME" "$NEXT_FILENAME"
fi
fi
done

# Converting entire database to an ASCII text file
echo '.dump' | sqlite3 "$MY_DATABASE" | gzip -c >"$BACKUP_PATH/1_dump.gz"

0 comments on commit 8873452

Please sign in to comment.