07-28-2025, 01:00 PM
(Ten post był ostatnio modyfikowany: 07-29-2025, 11:51 AM przez stAch.
Powód edycji: opcja cron
)
jeden klik i zapisane nawet 100 backupów z LK3 w różnych lokalizacjach - nic prostszego.
zapisać na serwerze ...edytować ip i porty do pobrania plików backups oraz globalne hasło i usera czyli fragment kodu poniżej.
jeśli wszystkie maja takie samo to nie musicie go podawać dla każdego z osobna wystarczy je podać jako globalne
skrypt pobiera zarówno z lokal ip:80 jak i po external ip:port można dodać go do crona by sam cyklicznie robił kopie na serwerze.
pytania lub problemy chętnie p/odpowiem...
Kod PHP:
<?php
error_reporting(E_ALL);
parse_str($_SERVER['QUERY_STRING']);
if (isset($_GET['cron'])) {
$_POST['backups'] = 1;
$_SERVER['REQUEST_METHOD'] = 'POST';
}
$folder = "backups";
$pattern = '/_(\d{4}-\d{2}-\d{2})_setting\.bin$/';
// Usuwanie plików (na podstawie daty z POST)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['date'])) {
$dateToDelete = $_POST['date'];
foreach (scandir($folder) as $file) {
if (preg_match($pattern, $file, $matches) && $matches[1] === $dateToDelete) {
unlink($folder . DIRECTORY_SEPARATOR . $file);
}
}
echo json_encode(['success' => true]);
exit;
}
// Backup i lista
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['backups'])) {
$messages = "";
// ? Dane logowania globalne
$globalUser = 'admin';
$globalPass = 'xxxxxxx';
// ? Lista hostów
$sources = [
['ip' => '192.168.1.20', 'port' => 97, 'name' => 'SOL'],
['ip' => '192.168.1.20', 'port' => 98, 'name' => 'CWU', 'user' => 'admin', 'pass' => 'xxxxxx'],
['ip' => '192.168.1.20', 'port' => 99, 'name' => 'GSM'],
['ip' => '192.168.1.20', 'port' => 100, 'name' => 'PCO'],
];
$remotePath = "/lk3_settings.bin";
$backupDir = __DIR__ . '/backups';
if (!is_dir($backupDir)) {
mkdir($backupDir, 0777, true);
}
$date = date("_Y-m-d");
foreach ($sources as $source) {
$ip = $source['ip'];
$port = $source['port'];
$name = $source['name'];
$user = $source['user'] ?? $globalUser;
$pass = $source['pass'] ?? $globalPass;
$url = "http://$ip:$port$remotePath";
$auth = base64_encode("$user:$pass");
$opts = [
"http" => [
"method" => "GET",
"header" => "Authorization: Basic $auth\r\n"
]
];
$context = stream_context_create($opts);
$content = @file_get_contents($url, false, $context);
if ($content === false) {
$messages .= "<div>? Błąd pobierania: $url</div>";
continue;
}
$filename = "{$name}{$date}_setting.bin";
$filepath = $backupDir . '/' . $filename;
if (file_put_contents($filepath, $content) !== false) {
$messages .= "<div>✔ Zapisano: $filename</div>";
} else {
$messages .= "<div>? Błąd zapisu: $filename</div>";
}
}
// ? Lista backupów
ob_start();
$groups = [];
foreach (scandir($folder) as $file) {
if (preg_match($pattern, $file, $matches)) {
$date = $matches[1];
$groups[$date][] = $file;
}
}
if (empty($groups)) {
echo "<p>Brak plików.</p>";
} else {
foreach ($groups as $date => $files) {
echo "<div class='group' data-date='" . htmlspecialchars($date) . "'>";
echo "<button class='delete-btn' onclick='deleteGroup(\"$date\")'>Usuń grupę</button>";
echo "<h3>" . htmlspecialchars($date) . "</h3>";
foreach ($files as $file) {
$fileEnc = rawurlencode($file);
echo "<div class='file'>";
echo htmlspecialchars($file);
echo " <a class='download' href='backups/$fileEnc' download>Pobierz</a>";
echo "</div>";
}
echo "</div>";
}
}
$html = ob_get_clean();
echo json_encode([
'success' => true,
'html' => $html,
'messages' => $messages
]);
exit;
}
// Pierwsze wczytanie listy
$groups = [];
foreach (scandir($folder) as $file) {
if (preg_match($pattern, $file, $matches)) {
$date = $matches[1];
$groups[$date][] = $file;
}
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>LK3 Backups</title>
<style>
body { font-family: Arial; padding: 20px; }
.group { border: 1px solid #ccc; padding: 10px; margin-bottom: 20px; border-radius: 5px; }
.group h3 { margin: 0 0 10px 0; }
.file { margin-left: 20px; }
.delete-btn {
background: #d00;
color: white;
border: none;
padding: 5px 10px;
float: right;
cursor: pointer;
}
.download {
background: #4CAF50;
color: white;
padding: 3px 8px;
margin-left: 10px;
text-decoration: none;
border-radius: 3px;
}
.download:hover {
background: #45a049;
}
.info {
margin-top: 20px;
padding: 10px;
background: #f9f9f9;
border: 1px dashed #aaa;
white-space: pre-wrap;
}
button.refresh {
background: #007bff;
color: white;
border: none;
padding: 6px 12px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Kopie zapasowe LK3 wg. daty</h1>
<div id="backup-list">
<?php if (empty($groups)): ?>
<p>Brak plików.</p>
<?php else: ?>
<?php foreach ($groups as $date => $files): ?>
<div class="group" data-date="<?= htmlspecialchars($date) ?>">
<button class="delete-btn" onclick="deleteGroup('<?= $date ?>')">Usuń grupę</button>
<h3><?= htmlspecialchars($date) ?></h3>
<?php foreach ($files as $file): ?>
<div class="file">
<?= htmlspecialchars($file) ?>
<a class="download" href="backups/<?= rawurlencode($file) ?>" download>Pobierz</a>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<div class="info">
<button class="refresh" onclick="getBackups()">? Aktualizuj (pobierz backupy)</button>
</div>
<script>
function deleteGroup(date) {
if (!confirm(`Czy na pewno chcesz usunąć wszystkie pliki z datą ${date}?`)) return;
fetch('', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'date=' + encodeURIComponent(date)
})
.then(res => res.json())
.then(data => {
if (data.success) {
document.querySelector(`.group[data-date="${date}"]`).remove();
} else {
alert('Błąd przy usuwaniu.');
}
});
}
function getBackups() {
document.querySelector('.info').innerHTML = '⏳ Trwa pobieranie...';
fetch('', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'backups=1'
})
.then(res => res.json())
.then(data => {
if (data.success) {
if (data.html) {
document.querySelector('#backup-list').innerHTML = data.html;
}
if (data.messages) {
document.querySelector('.info').innerHTML = data.messages;
} else {
document.querySelector('.info').innerHTML = '✅ Gotowe.';
}
} else {
document.querySelector('.info').innerHTML = '❌ Błąd przy pobieraniu.';
}
})
.catch(err => {
console.error('Fetch error:', err);
document.querySelector('.info').innerHTML = '❌ Wystąpił błąd sieci.';
});
}
</script>
</body>
</html>
Kod:
// ? Dane logowania globalne
$globalUser = 'admin'; //globalny user
$globalPass = 'pass'; //globalne haslo
// ? Lista hostów
$sources = [
['ip' => '192.168.1.100', 'port' => 80, 'name' => 'SOL'],
['ip' => '132.138.3.220', 'port' => 98, 'name' => 'CWU', 'user' => 'user', 'pass' => '1234'],
['ip' => '132.138.3.220', 'port' => 99, 'name' => 'GSM'],
['ip' => '132.138.3.220', 'port' => 100, 'name' => 'PCO'],
];
skrypt pobiera zarówno z lokal ip:80 jak i po external ip:port można dodać go do crona by sam cyklicznie robił kopie na serwerze.
Kod:
0 2 * * * curl -s "http://twojadomena.pl/backup.php?cron=1" > /dev/null 2>&1
pytania lub problemy chętnie p/odpowiem...
e-chata.hyzne.com | LK1 - sterowniki akwarystyki | LK2 - sterowniki ihome | LK3 - niezliczone ilości wersji/nakładek na farmach PV i nie tylko | LK4 - testy bezprzewodowe