Part 2: Compress and Backup

This tutorial explains how you can make a backup of SBFspot.db
Because we're backing up to USB memory, we will compress the file first

Create backup script:
sudo nano /usr/local/bin/sbfspot.3/backup_db.sh


#!/bin/sh
#

# If USB memory is almost full (90%), log a warning
df_threshold=90

logfile="/home/pi/smadata/logs/backup_db.log"

src="/home/pi/smadata/SBFspot.db"
dst="/home/pi/smadata/backup_usb/SBFspot_$(date +"%Y%m%d").tar.gz"

# Compress the db
tar cfz $dst -P $src

# Check if backup exists
if [ ! -f "$dst" ]; then
	echo "ERROR: Backup file $dst is not found. Something has gone wrong.">>$logfile
else
	echo "Backup completed successfully.">>$logfile
	# Get disk usage in percent
	df_used=$(df /home/pi/smadata/backup_usb | grep -o '[0-9]\{1,\}%')
	# Drop last char (%)
	df_used=${df_used%?}

	#If used space exceeds threshold, log it
	if [ $df_used -gt $df_threshold ]; then
		echo "WARNING: Backup disk has reached the $df_threshold% threshold. The actual used space is $df_used%">>$logfile
	fi
fi

Make script executable;
sudo chmod a+x /usr/local/bin/sbfspot.3/backup_db.sh

To run the script the end of each day, setup a cron job:
crontab -e
55 23 * * * /usr/local/bin/sbfspot.3/backup_db.sh

Next: Compress and Backup

Last edited May 18 at 9:40 PM by SBF, version 5