Skip to content

Commit

Permalink
rework backup and restore
Browse files Browse the repository at this point in the history
  • Loading branch information
Lutz Bender committed Aug 24, 2022
1 parent 4dd7a23 commit c8dbae0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 44 deletions.
40 changes: 40 additions & 0 deletions runs/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
OPENWBBASEDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
BACKUPDIR="$OPENWBBASEDIR/web/backup"
. "$OPENWBBASEDIR/helperFunctions.sh"

backup() {
openwbDebugLog MAIN 0 "creating new backup: $FILENAME"
# 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
for pid in $(pidof "mosquitto"); do
openwbDebugLog MAIN 1 "sending 'SIGUSR1' to mosquitto on pid '$pid'"
sudo kill -s SIGUSR1 "$pid"
done
# give mosquitto some time to finish
sleep 0.2

# create backup file
openwbDebugLog MAIN 1 "creating new backup file: $BACKUPFILE"
sudo tar --exclude="$OPENWBBASEDIR/web/backup" --exclude="$OPENWBBASEDIR/.git" -czf "$BACKUPFILE" "$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"
49 changes: 30 additions & 19 deletions runs/restore.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
#!/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)
SOURCEFILE="$OPENWBBASEDIR/web/tools/upload/backup.tar.gz"
WORKINGDIR="/home/pi/openwb_restore"
LOGFILE="$OPENWBBASEDIR/web/tools/upload/restore.log"

{
echo "$(date +"%Y-%m-%d %H:%M:%S") Restore of backup started..."
echo "****************************************"
echo "Step 1: creating working directory \"$WORKINGDIR\""
mkdir -p "$WORKINGDIR"
echo "****************************************"
echo "Step 2: extracting archive to working dir \"$WORKINGDIR\"..."
sudo tar -vxf "$SOURCEFILE" -C "$WORKINGDIR"
echo "****************************************"
echo "Step 3: replacing old files..."
cp -v -R -p "${WORKINGDIR}${OPENWBBASEDIR}/." "${OPENWBBASEDIR}R/"
echo "****************************************"
echo "Step 4: restoring mosquitto db..."
sudo systemctl stop mosquitto.service
sleep 2
sudo cp -v -p "$WORKINGDIR/var/lib/mosquitto/mosquitto.db" "/var/lib/mosquitto/mosquitto.db"
sudo systemctl start mosquitto.service
echo "****************************************"
echo "Step 5: cleanup after restore..."
sudo rm "$SOURCEFILE"
sudo rm -R "$WORKINGDIR"
echo "****************************************"
echo "$(date +"%Y-%m-%d %H:%M:%S") End: Restore finished."
echo "****************************************"
} >"$LOGFILE" 2>&1
39 changes: 17 additions & 22 deletions web/settings/backup.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
<?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;
$useExtendedFilename = 0;
if( isset($_GET["extendedFilename"]) && $_GET["extendedFilename"] == "1") {
$useExtendedFilename = true;
$useExtendedFilename = 1;
}
$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" ;
}

// 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 +52,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
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 " . $_SERVER['DOCUMENT_ROOT'] . "/openWB/runs/restore.sh");
?>
<script>
setTimeout(function() { window.location = "index.php"; }, 15000);
Expand Down

0 comments on commit c8dbae0

Please sign in to comment.