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

Add FTP as a backup target and schedule nightly backup job #2806

Open
wants to merge 7 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
7 changes: 7 additions & 0 deletions openwb.conf
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,10 @@ soc_aiwayslp2_user=''
soc_aiwayslp2_pass=''
soc_aiwayslp2_vin=''
soc_aiwayslp2_intervall=''
backuptarget=ftp
ftphost=192.168.170.1
ftppath=/UBUNTU_20_0
ftpuser=ftp
ftppass=bh220540
nightlybackup=1
shortbackupfilename=1
21 changes: 21 additions & 0 deletions runs/cronnightly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,24 @@ done
# monthly . csv updaten
echo "Trigger update of logfiles..."
python3 /var/www/html/openWB/runs/csvcalc.py --input /var/www/html/openWB/web/logging/data/daily/ --output /var/www/html/openWB/web/logging/data/v001/ --partial /var/www/html/openWB/ramdisk/ --mode A >> /var/www/html/openWB/ramdisk/csvcalc.log 2>&1 &

#check if backup is configured - if yes, let's go
if [[ $nightlybackup == 1 ]]; then
echo "Nightly backup turned on. Performing backup."
port=$WEB_PORT
if [[ -z "$port" ]]; then
port=80
fi

if (( $shortbackupfilename == 0)); then
shortbackupfilename=1
else
shortbackupfilename=0
fi

url="http://127.0.0.1:$port/openWB/web/settings/savebackup.php?extendedFilename=$shortbackupfilename"
echo "Using url $url"
curl -s -o /dev/null $url
else
echo "Nightly backup turned off. Doing nothing."
fi
170 changes: 133 additions & 37 deletions web/settings/backup.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
<?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" ;
}

// 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/");
?>
<!DOCTYPE html>
<html lang="de">

Expand All @@ -26,9 +6,8 @@

<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Backup erstellen</title>
<meta name="description" content="Control your charge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>openWB Einstellungen</title>
<meta name="author" content="Kevin Wieland, Michael Ortenstein" />
<!-- Favicons (created with http://realfavicongenerator.net/)-->
<link rel="apple-touch-icon" sizes="57x57" href="img/favicons/apple-touch-icon-57x57.png">
Expand All @@ -45,52 +24,169 @@
<link rel="stylesheet" type="text/css" href="css/bootstrap-4.4.1/bootstrap.min.css">
<!-- Normalize -->
<link rel="stylesheet" type="text/css" href="css/normalize-8.0.1.css">
<link rel="stylesheet" type="text/css" href="fonts/font-awesome-5.8.2/css/all.css">
<!-- include settings-style -->
<link rel="stylesheet" type="text/css" href="css/settings_style.css">

<!-- important scripts to be loaded -->
<script src="js/jquery-3.6.0.min.js"></script>
<script src="js/bootstrap-4.4.1/bootstrap.bundle.min.js"></script>
<!-- load helper functions -->
<script src = "settings/helperFunctions.js?ver=20210329" ></script>
<script src = "settings/helperFunctions.js?ver=202103291" ></script>
</head>

<body>
<?php
$lines = file($_SERVER['DOCUMENT_ROOT'] . '/openWB/openwb.conf');
foreach($lines as $line) {
list($key, $value) = explode("=", $line, 2);
${$key."old"} = trim( $value, " '\t\n\r\0\x0B" ); // remove all garbage and single quotes
}
?>
<div id="nav"></div> <!-- placeholder for navbar -->

<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.
<h1>OpenWB Backup</h1>
<div class="card border-secondary">
<form class="form" id="backupform" action="./settings/saveconfig.php" method="POST">
<div class="card-header bg-secondary">
Backup-Ziel

</div>
<div class="card-body">
<div class="form-group mb-1">
<div class="form-row mb-1">
<label for="backuptarget" class="col-md-4 col-form-label">Backup-Ziel</label>
<div class="col">
<select name="backuptarget" id="backuptarget" class="form-control">
<option value="local" <?php if ($backuptargetold == "local") echo "selected" ?>>/var/www/html/openWB/backup</option>
<option value="ftp" <?php if ($backuptargetold == "ftp") echo "selected" ?>>FTP</option>
</select>
</div>
</div>
<div class="form-row mb-1">
<label for="nightlybackup" class="col-md-4 col-form-label">Nächtliches Backup</label>
<div class="col">
<input type="hidden" value="0" name="nightlybackup" />
<input id="nightlybackupchk" name="nightlybackup" class="" type="checkbox" value="1" <?php if ($nightlybackupold == 1) echo "checked=\"checked\"" ?> />
</div>
</div>
<div class="form-row mb-1" id="shortfilename">
<label for="nightlybackup" class="col-md-4 col-form-label">Kurzer Dateiname</label>
<div class="col">
<input type="hidden" value="0" name="shortbackupfilename" />
<input id="shortbackupfilenamechk" name="shortbackupfilename" class="" type="checkbox" value="1" <?php if ($shortbackupfilenameold == 1) echo "checked=\"checked\"" ?> />
<span class="form-text small">Wenn aktiviert, wird die Datei unter den Namen backup.tar.gz am Zielort abgelegt. Wenn nicht aktiviert, wird die Datei unter dem Muster "openWB_backup_$jahr-$monat-$tag-$hourofday-$minute.tar.gz" abgelegt (Beispiel: openWB_backup_2023-11-30_17-19-11.tar.gz)</span>
</div>
</div>
</div>
</div>
<div class="card-body" id="ftpcreds">
<div class="form-group">
<div class="form-row mb-1">
<label for="ftphost" class="col-md-4 col-form-label">FTP Host</label>
<div class="col">
<input id="ftphost" class="form-control" type="text" name="ftphost" value="<?= $ftphostold ?>">
<span class="form-text small">Der FTP-Host als voller DNS-Name oder IP-Adresse</span>
</div>
</div>
<div class="form-row mb-1">
<label for="ftppath" class="col-md-4 col-form-label">Remote Pfad auf FTP-Server</label>
<div class="col">

<input id="ftppath" name="ftppath" value="<?= $ftppathold ?>" class="form-control" type="text" />
<span class="form-text small">Der Pfad auf dem Zielsystem, zB /Backups/openwb</span>
</div>
</div>
</div>
<div class="form-group">
<div class="form-row mb-1">
<label for="ftpuser" class="col-md-4 col-form-label">FTP Username</label>
<div class="col">
<input id="ftpuser" name="ftpuser" value="<?= $ftpuserold ?>" class="form-control" />
<span class="form-text small">Der Nutzername für das FTP-System</span>
</div>
</div>
<div class="form-row mb-1">
<label for="ftppass" class="col-md-4 col-form-label">FTP Passwort</label>
<div class="col">
<input id="ftppass" name="ftppass" class="form-control" type="password" value="<?= $ftppassold ?>" />
<span class="form-text small">Das FTP-Passwort, gespeichert in der openwb.conf</span>
</div>
</div>

</div>
</div>
<div class="card-footer text-center">
<button type="button" id="savecreds" class="btn btn-primary" onclick="location.href='./settings/savebackup.php';">Backup jetzt erstellen</button> <button type="button" id="savecreds" class="btn btn-success" onclick="checkFTP()">Einstellungen speichern</button>
</div>
</form>
</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>

</div> <!-- container -->

<footer class="footer bg-dark text-light font-small">
<div class="container text-center">
<small>Sie befinden sich hier: System/Backup erstellen</small>
<small>Sie befinden sich hier: System/Backup</small>
</div>
</footer>

<script>

$('#backuptarget').on('change', function() {
if (this.value == "ftp") {
showSection($("#ftpcreds"));
} else {
hideSection($("#ftpcreds"));
}
});

$('#nightlybackupchk').on('change', function() {
if (this.checked) {
showSection($("#shortfilename"));
} else {
hideSection($("#shortfilename"));
}

});

function checkFTP() {
$.get(
{ url: "./settings/testftp.php?ftphost="+$("#ftphost").val()+"&ftppath="+$("#ftppath").val()+"&ftpuser="+$("#ftpuser").val()+"&ftppass="+$("#ftppass").val(), cache: false },
function(data){
if (data == 0) {
alert("Nutzername/Passwort oder Host-Adresse nicht korrekt. Einstellungen können nicht gespeichert werden!");
return;
} else if (data == 1) {
alert("Nutzername/Passwort korrekt, aber Verzeichnis kann nicht gefunden bzw. in das Verzeichnis kann nicht gewechselt werden. Einstellungen können nicht gespeichert werden!");
return;
}
//submit!
$('#backupform').submit();
}
);
}

$(document).ready(function(){
if ($('#backuptarget').val() == "local") {
hideSection($("#ftpcreds"));
}
console.log("Check: ", $('#nightlybackupchk').is(':checked'));
if (!$('#nightlybackupchk').is(':checked')) {
hideSection($("#shortfilename"));
}
});

$.get(
{ url: "settings/navbar.html", cache: false },
function(data){
$("#nav").replaceWith(data);
// disable navbar entry for current page
$('#navBackup').addClass('disabled');
$('#navOpenwbCloud').addClass('disabled');
}
);

);
</script>

</body>
Expand Down
2 changes: 1 addition & 1 deletion web/settings/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
System
</a>
<div class="dropdown-menu">
<a class="dropdown-item" id="navBackup" data-toggle="modal" data-target="#backupConfirmationModal" style="cursor: pointer;">Backup erstellen</a>
<a class="dropdown-item" id="navBackup" style="cursor: pointer;" href="./settings/backup.php">Backup</a>
<a class="dropdown-item" id="navWiederherstellen" data-toggle="modal" data-target="#restoreConfirmationModal" style="cursor: pointer;">Backup wiederherstellen</a>
<a class="dropdown-item" id="navReboot" data-toggle="modal" data-target="#rebootConfirmationModal" style="cursor: pointer;">Reboot</a>
<a class="dropdown-item" id="navShutdown" data-toggle="modal" data-target="#shutdownConfirmationModal" style="cursor: pointer;">Shutdown</a>
Expand Down
Loading