Skip to content

Commit

Permalink
feat: enhanced the log output for backup tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Jul 7, 2024
1 parent 43461f3 commit c4c6b05
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
6 changes: 4 additions & 2 deletions app/Services/Backup/Tasks/AbstractBackupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function initializeBackup(): void
$this->backupTask->setScriptUpdateTime();
$this->backupTaskLog = $this->recordBackupTaskLog($this->backupTask->id, $this->logOutput);
$this->updateBackupTaskStatus($this->backupTask, BackupTaskModel::STATUS_RUNNING);
$this->logMessage('Backup task started.');
$this->logMessage('Backup task initiated.');
$this->updateBackupTaskLogOutput($this->backupTaskLog, $this->logOutput);
}

Expand All @@ -77,7 +77,7 @@ protected function initializeBackup(): void
*/
protected function finalizeSuccessfulBackup(): void
{
$this->logMessage('Backup task has finished successfully!');
$this->logMessage('Backup task has been completed.');
$this->backupTaskLog->setSuccessfulTime();
$this->updateBackupTaskLogOutput($this->backupTaskLog, $this->logOutput);
}
Expand All @@ -104,6 +104,8 @@ protected function cleanupBackup(): void
'size' => $this->backupSize,
]);

$this->logMessage("Backup summary: Operation completed in {$elapsedTime} seconds.");

Log::info("Completed backup task: {$this->backupTask->label} ({$this->backupTask->id}).");
}

Expand Down
8 changes: 5 additions & 3 deletions app/Services/Backup/Tasks/DatabaseBackupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ protected function performBackup(): void
throw new RuntimeException('Please provide a database password for the remote server.');
}

$this->logMessage('Attempting to connect to remote server.');
$sftp = $this->establishSFTPConnection($remoteServer, $this->backupTask);
$this->logMessage('SSH Connection established to the server.');
$this->logMessage('Secure SSH connection established with the remote server.');

$databaseType = $this->getDatabaseType($sftp);
$this->logMessage("Database type detected: {$databaseType}.");
$this->logMessage('Detected database type: ' . ucfirst($databaseType) . '.');

$this->backupTask->setScriptUpdateTime();

Expand All @@ -51,11 +52,12 @@ protected function performBackup(): void
if ($this->backupTask->isRotatingBackups() && ! $backupDestinationModel->isLocalConnection()) {
$backupDestination = $this->createBackupDestinationInstance($backupDestinationModel);
$this->rotateOldBackups($backupDestination, $this->backupTask->getAttribute('id'), $this->backupTask->getAttribute('maximum_backups_to_keep'), '.sql', 'backup_');
$this->logMessage("Initiating backup rotation. Retention limit: {$this->backupTask->getAttribute('maximum_backups_to_keep')} backups.");
}

$this->logMessage("Database backup has been uploaded to {$backupDestinationModel->label} - {$backupDestinationModel->type()}: {$dumpFileName}");

$sftp->delete($remoteDumpPath);
$this->logMessage('Cleaned up the temporary file on the server.');
$this->logMessage('Temporary server file removed after successful backup operation.');
}
}
20 changes: 14 additions & 6 deletions app/Services/Backup/Tasks/FileBackupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ protected function performBackup(): void
$sourcePath = $this->backupTask->getAttributeValue('source_path');
$storagePath = $this->backupTask->getAttributeValue('store_path');

$this->logMessage('Attempting to connect to remote server.');
$sftp = $this->establishSFTPConnection($remoteServer, $this->backupTask);
$this->logMessage('SSH Connection established to the server.');
$this->logMessage('Secure SSH connection established with the remote server.');

if (! $this->checkPathExists($sftp, $sourcePath)) {
throw new RuntimeException('The path specified does not exist.');
Expand All @@ -34,19 +35,25 @@ protected function performBackup(): void
$dirSize = $this->getRemoteDirectorySize($sftp, $sourcePath);
$this->backupSize = $dirSize;
$dirSizeInMB = number_format($dirSize / 1024 / 1024, 1);
$this->logMessage("Directory size of {$sourcePath}: {$dirSizeInMB} MB.");
$this->logMessage("Source directory '{$sourcePath}' size: {$dirSizeInMB} MB.");

if ($dirSize > BackupConstants::FILE_SIZE_LIMIT) {
throw new RuntimeException('Directory size exceeds the limit.');
}

$excludeDirs = $this->isLaravelDirectory($sftp, $sourcePath) ? ['node_modules', 'vendor'] : [];
$laravelProject = $this->isLaravelDirectory($sftp, $sourcePath);

if ($laravelProject) {
$this->logInfo('Laravel project detected. Optimizing backup process for Laravel-specific structure.');
}

$excludeDirs = $laravelProject ? ['node_modules', 'vendor'] : [];

$zipFileName = $this->generateBackupFileName('zip');
$remoteZipPath = "/tmp/{$zipFileName}";

$this->zipRemoteDirectory($sftp, $sourcePath, $remoteZipPath, $excludeDirs);
$this->logMessage("Directory has been zipped: {$remoteZipPath}");
$this->logMessage("Directory compression complete. Archive location: {$remoteZipPath}.");

$this->backupTask->setScriptUpdateTime();

Expand All @@ -60,11 +67,12 @@ protected function performBackup(): void
if ($this->backupTask->isRotatingBackups() && ! $backupDestinationModel->isLocalConnection()) {
$backupDestination = $this->createBackupDestinationInstance($backupDestinationModel);
$this->rotateOldBackups($backupDestination, $this->backupTask->getAttribute('id'), $this->backupTask->getAttribute('maximum_backups_to_keep'), '.zip', 'backup_');
$this->logMessage("Initiating backup rotation. Retention limit: {$this->backupTask->getAttribute('maximum_backups_to_keep')} backups.");
}

$this->logMessage("Backup has been uploaded to {$backupDestinationModel->label} - {$backupDestinationModel->type()}: {$zipFileName}");
$this->logMessage("File backup has been uploaded to {$backupDestinationModel->label} - {$backupDestinationModel->type()}: {$zipFileName}");

$sftp->delete($remoteZipPath);
$this->logMessage('Cleaned up the temporary zip file on server.');
$this->logMessage('Temporary server file removed after successful backup operation.');
}
}

0 comments on commit c4c6b05

Please sign in to comment.