Improvement to backup system

This commit is contained in:
Pinga 2025-02-17 12:51:03 +02:00
parent 8e72f712b8
commit ad9b38acf1
3 changed files with 53 additions and 20 deletions

View file

@ -1,7 +1,7 @@
{
"storageType": "sftp",
"ftp": {
"host": "your_sftp_host",
"host": "your_ftp_host",
"username": "your_username",
"password": "your_password",
"port": 21,

View file

@ -133,32 +133,38 @@ function uploadFile($filesystem, $localPath, $remotePath, $logger, $retries, $de
}
}
// Current date and hour in the specified format
$currentDateHour = date('Ymd-H'); // Format: YYYYMMDD-HH
// Directory to check
$directory = $config['directory'];
$directory = "/srv/";
// Load patterns from config
$patterns = array_map(function ($pattern) use ($currentDateHour) {
return str_replace('{dateHour}', $currentDateHour, $pattern);
}, $config['patterns']);
// Define backup types (prefixes)
$backupTypes = ['database', 'registry', 'panel'];
// Scan directory and filter matching files
$backupFiles = [];
// Scan directory for matching files
$files = scandir($directory);
$filesFound = false; // Flag to track if any files are found
foreach ($files as $file) {
foreach ($patterns as $pattern) {
if (preg_match("/$pattern/", $file)) {
$filesFound = true;
$localPath = $directory . $file;
$remoteFileName = basename($file);
uploadFile($filesystem, $localPath, $remoteFileName, $log, $config['upload']['retries'], $config['upload']['delay']);
foreach ($backupTypes as $type) {
if (preg_match("/^{$type}-\d{8}-\d{4}\.sql\.bz2$/", $file)) {
$backupFiles[$type][$file] = filemtime($directory . $file);
}
}
}
// Upload the latest file for each type
foreach ($backupFiles as $type => $files) {
if (!empty($files)) {
// Get the latest file by modification time
arsort($files);
$latestFile = array_key_first($files);
// Upload file
$localPath = $directory . $latestFile;
$remoteFileName = basename($latestFile);
uploadFile($filesystem, $localPath, $remoteFileName, $log, $config['upload']['retries'], $config['upload']['delay']);
}
}
// Log if no files were found
if (!$filesFound) {
$log->info("No matching files found in directory: $directory for patterns: " . implode(', ', $patterns));

View file

@ -36,7 +36,7 @@
}
},
{
"name": "Files",
"name": "Registry",
"source": {
"type": "tar",
"options": {
@ -46,7 +46,34 @@
},
"target": {
"dirname": "/srv",
"filename": "files-%Y%m%d-%H%i.sql",
"filename": "registry-%Y%m%d-%H%i.sql",
"compress": "bzip2"
},
"checks": [
{
"type": "sizemin",
"value": "10M"
}
],
"cleanup": {
"type": "Capacity",
"options": {
"size": "750M"
}
}
},
{
"name": "Panel",
"source": {
"type": "tar",
"options": {
"path": "/var/www/cp",
"throttle": "5m"
}
},
"target": {
"dirname": "/srv",
"filename": "panel-%Y%m%d-%H%i.sql",
"compress": "bzip2"
},
"checks": [