Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework backup and restore #2372

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion helperFunctions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ openwbDebugLog() {
export -f openwbDebugLog

openwbRunLoggingOutput() {
$1 2>&1 | while read -r line
"$@" 2>&1 | while read -r line
do
# use path to /var/log as link in ramdisk may not be set up yet!
echo "$(date +"%Y-%m-%d %H:%M:%S"): $1: $line" >> "/var/log/openWB.log"
Expand Down
41 changes: 41 additions & 0 deletions runs/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
OPENWBBASEDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
BACKUPDIR="$OPENWBBASEDIR/web/backup"
. "$OPENWBBASEDIR/helperFunctions.sh"

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

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

# create backup file
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"

openwbDebugLog MAIN 0 "backup finished"
}

useExtendedFilename=$1
if ((useExtendedFilename == 1)); then
FILENAME="openWB_backup_$(date +"%Y-%m-%d_%H:%M:%S").tar.gz"
else
FILENAME="backup.tar.gz"
fi

openwbRunLoggingOutput backup "$FILENAME"
# return our filename for further processing
echo "$FILENAME"
3 changes: 1 addition & 2 deletions runs/reboot.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/bash

sudo rm /var/log/openWB.log
sudo touch /var/log/openWB.log
sudo chmod 777 /var/log/openWB.log
$(sleep 5 && sudo reboot now)&
$(sleep 5 && sudo reboot now) &
68 changes: 49 additions & 19 deletions runs/restore.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
#!/bin/bash
echo "****************************************"
echo "Step 1: moving file to home directory..."
sudo cp /var/www/html/openWB/web/tools/upload/backup.tar.gz /home/pi/backup.tar.gz
cd /home/pi/
echo "****************************************"
echo "Step 2: extracting archive..."
sudo tar -vxf backup.tar.gz
echo "****************************************"
echo "Step 3: replacing old files..."
sudo cp -v -R /home/pi/var/www/html/openWB/* /var/www/html/openWB/
sudo chmod 777 /var/www/html/openWB/openwb.conf
echo "****************************************"
echo "Step 4: cleanup after restore..."
sudo rm /var/www/html/openWB/web/tools/upload/backup.tar.gz
sudo rm /home/pi/backup.tar.gz
sudo rm -R /home/pi/var
echo "****************************************"
echo "End: Restore finished."
echo "****************************************"
OPENWBBASEDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
RAMDISKDIR="$OPENWBBASEDIR/ramdisk"
SOURCEFILE="$OPENWBBASEDIR/web/tools/upload/backup.tar.gz"
WORKINGDIR="/home/pi/openwb_restore"
MOSQUITTODIR="/var/lib/mosquitto"
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"
# 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)$" |
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 --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"
exit 1
fi
echo "****************************************"
echo "Step 4: replacing old files..."
# we use cp not mv because of not empty directories in destination
sudo cp -v -p -r "${WORKINGDIR}${OPENWBBASEDIR}/." "${OPENWBBASEDIR}/"
echo "****************************************"
echo "Step 5: restoring 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"
sudo rm -R "$WORKINGDIR"
benderl marked this conversation as resolved.
Show resolved Hide resolved
echo "****************************************"
echo "$(date +"%Y-%m-%d %H:%M:%S") End: Restore finished."
echo "rebooting"
"$OPENWBBASEDIR/runs/reboot.sh"
echo "****************************************"
} >"$LOGFILE" 2>&1
39 changes: 16 additions & 23 deletions web/settings/backup.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
<?php
// if parameter extendedFilename is passed with value 1 the filename changes
// from backup.tar.gz to openWB_backup_YYYY-MM-DD_HH-MM-SS.tar.gz
$useExtendedFilename = false;
if( isset($_GET["extendedFilename"]) && $_GET["extendedFilename"] == "1") {
$useExtendedFilename = true;
}
$backupPath = "/var/www/html/openWB/web/backup/";
$timestamp = date("Y-m-d") . "_" . date("H-i-s");
if ( $useExtendedFilename ) {
$filename = "openWB_backup_" . $timestamp . ".tar.gz" ;
} else {
$filename = "backup.tar.gz" ;
}
$useExtendedFilename = isset($_GET["extendedFilename"]) && $_GET["extendedFilename"] == "1" ? 1 : 0;

// first empty backup-directory
array_map( "unlink", array_filter((array) glob($backupPath . "*") ) );
// then create new backup-file
exec("tar --exclude='/var/www/html/openWB/web/backup' --exclude='/var/www/html/openWB/.git' -czf ". $backupPath . $filename . " /var/www/html/");
// execute backup script
$filename = exec("sudo -u pi " . $_SERVER['DOCUMENT_ROOT'] . "/openWB/runs/backup.sh " . $useExtendedFilename, $output, $result);
?>
<!DOCTYPE html>
<html lang="de">
Expand Down Expand Up @@ -62,16 +50,21 @@
<div role="main" class="container" style="margin-top:20px">

<h1>Backup erstellen</h1>
<div class="alert alert-success">
Backup-Datei <?php echo $filename; ?> erfolgreich erstellt.
</div>

<div class="row">
<div class="col text-center">
<a class="btn btn-success" href="/openWB/web/backup/<?php echo $filename; ?>" target="_blank"><i class="fas fa-download"></i> Backup herunterladen</a>
<?php if ($filename !== false && $result == 0) { ?>
<div class="alert alert-success">
Backup-Datei <?php echo $filename; ?> erfolgreich erstellt.
</div>
</div>

<div class="row">
<div class="col text-center">
<a class="btn btn-success" href="/openWB/web/backup/<?php echo $filename; ?>" target="_blank"><i class="fas fa-download"></i> Backup herunterladen</a>
</div>
</div>
<?php } else { ?>
<div class="alert alert-danger">
Es gab einen Fehler beim Erstellen der Backup-Datei. Bitte die Logmeldungen prüfen!
</div>
<?php } ?>
</div> <!-- container -->

<footer class="footer bg-dark text-light font-small">
Expand Down
1 change: 1 addition & 0 deletions web/settings/restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<h1>openWB aus Backup wiederherstellen</h1>
<div class="alert alert-warning">
Wiederherstellen der openWB-Einstellungen und Log-Daten aus einer Backup-Datei (Dateiendung gz).<br>
Die openWB wird nach dem Wiederherstellen des Backups direkt neu gestartet! Bitte vorher alle Ladevorgänge beenden und Fahrzeuge zur Sicherheit abstecken.<br>
Sollte die Wiederherstellung fehlschlagen, bitte ein Update der openWB auf die gewünschte Version durchführen. Im Anschluss die openWB neu starten und das Wiederherstellen erneut versuchen.
</div>

Expand Down
5 changes: 2 additions & 3 deletions web/settings/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function parse_size($size) {
if ( $ext != "gz" ) {
?>
<div class="alert alert-danger">
Die Datei ist keine Zip-Datei, die Wiederherstellung wurde abgebrochen.
Die Datei ist keine Tar-GZip-Datei, die Wiederherstellung wurde abgebrochen.
</div>
<?php
} elseif ( $_FILES["fileToUpload"]["size"] > file_upload_max_size() ) {
Expand Down Expand Up @@ -139,8 +139,7 @@ function(data){
</script>
<?php
if($uploadOk === true) {
sleep(5);
exec($_SERVER['DOCUMENT_ROOT']."/openWB/runs/restore.sh >> ".$_SERVER['DOCUMENT_ROOT']."/openWB/web/tools/upload/restore.log");
exec("sudo -u pi " . escapeshellarg($_SERVER['DOCUMENT_ROOT']) . "/openWB/runs/restore.sh");
?>
<script>
setTimeout(function() { window.location = "index.php"; }, 15000);
Expand Down
Loading