Skip to content

Commit

Permalink
make restore of mosquitto.db optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Lutz Bender committed May 30, 2023
1 parent b601e5e commit d6e697a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
13 changes: 8 additions & 5 deletions runs/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ BACKUPDIR="$OPENWBBASEDIR/web/backup"
. "$OPENWBBASEDIR/helperFunctions.sh"

backup() {
openwbDebugLog MAIN 0 "creating new backup: $FILENAME"
# $1: name for new backup file
# remove old backup files
openwbDebugLog MAIN 1 "deleting old backup files if present"
rm "$BACKUPDIR/"*
BACKUPFILE="$BACKUPDIR/$FILENAME"

# tell mosquitto to store all retained topics in db now
openwbDebugLog MAIN 1 "sending 'SIGUSR1' to mosquitto"
sudo pkill -e -SIGUSR1 mosquitto
sudo pkill --echo -SIGUSR1 mosquitto
# give mosquitto some time to finish
sleep 0.2

# create backup file
openwbDebugLog MAIN 1 "creating new backup file: $BACKUPFILE"
sudo tar --exclude="$BACKUPDIR" --exclude="$OPENWBBASEDIR/.git" -czf "$BACKUPFILE" "$OPENWBBASEDIR/" "/var/lib/mosquitto/"
BACKUPFILE="$BACKUPDIR/$1"
openwbDebugLog MAIN 0 "creating new backup file: $BACKUPFILE"
sudo tar --verbose --create --gzip --file="$BACKUPFILE" \
--exclude="$BACKUPDIR" \
--exclude="$OPENWBBASEDIR/.git" \
"$OPENWBBASEDIR/" "/var/lib/mosquitto/"
openwbDebugLog MAIN 1 "setting permissions of new backup file"
sudo chown pi:www-data "$BACKUPFILE"
sudo chmod 664 "$BACKUPFILE"
Expand Down
18 changes: 11 additions & 7 deletions runs/restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,33 @@ LOGFILE="$OPENWBBASEDIR/web/tools/upload/restore.log"
echo "$(date +"%Y-%m-%d %H:%M:%S") Restore of backup started..."
echo "****************************************"
echo "Step 1: setting flag 'update in progress' and wait for control loop to finish"
echo 1 > "$OPENWBBASEDIR/ramdisk/updateinprogress"
echo 1 >"$OPENWBBASEDIR/ramdisk/updateinprogress"
# Wait for regulation loop(s) and cron jobs to end, but with timeout in case a script hangs
pgrep -f "$OPENWBBASEDIR/(regel\\.sh|runs/cron5min\\.sh|runs/cronnightly\\.sh)$" | \
pgrep -f "$OPENWBBASEDIR/(regel\\.sh|runs/cron5min\\.sh|runs/cronnightly\\.sh)$" |
timeout 15 xargs -n1 -I'{}' tail -f --pid="{}" /dev/null
echo "****************************************"
echo "Step 2: creating working directory \"$WORKINGDIR\""
mkdir -p "$WORKINGDIR"
echo "****************************************"
echo "Step 3: extracting archive to working dir \"$WORKINGDIR\"..."
if ! sudo tar -vxf "$SOURCEFILE" -C "$WORKINGDIR"; then
if ! sudo tar --verbose --extract --file="$SOURCEFILE" --directory="$WORKINGDIR"; then
echo "something went wrong! aborting restore"
echo "Wiederherstellung fehlgeschlagen! Bitte Protokolldateien prüfen." >"$RAMDISKDIR/lastregelungaktiv"
echo 0 > "$OPENWBBASEDIR/ramdisk/updateinprogress"
echo 0 >"$OPENWBBASEDIR/ramdisk/updateinprogress"
exit 1
fi
echo "****************************************"
echo "Step 4: replacing old files..."
mv -v -f "${WORKINGDIR}${OPENWBBASEDIR}/." "${OPENWBBASEDIR}/"
echo "****************************************"
echo "Step 5: restoring mosquitto db..."
sudo systemctl stop mosquitto.service
sleep 2
sudo mv -v -f "${WORKINGDIR}${MOSQUITTODIR}/mosquitto.db" "$MOSQUITTODIR/mosquitto.db"
if [[ -f "${WORKINGDIR}${MOSQUITTODIR}/mosquitto.db" ]]; then
sudo systemctl stop mosquitto.service
sleep 2
sudo mv -v -f "${WORKINGDIR}${MOSQUITTODIR}/mosquitto.db" "$MOSQUITTODIR/mosquitto.db"
else
echo "Backup does not contain mosquitto.db. Skipping restore."
fi
echo "****************************************"
echo "Step 6: cleanup after restore..."
sudo rm "$SOURCEFILE"
Expand Down

0 comments on commit d6e697a

Please sign in to comment.