Refactor backup scripts to include redundant backup directory creation, improved logging, and ensure successful backup copying; update run script to execute both database and volume backups sequentially.
This commit is contained in:
@@ -3,12 +3,22 @@ $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
|
||||
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
$backupDir = Join-Path $scriptDir "backups"
|
||||
$backupFile = Join-Path $backupDir "gitea-db-backup-$timestamp.sql"
|
||||
$redundantBackupDir = "D:\Pr00jects\gitea-docker"
|
||||
|
||||
# Ensure backup directory exists
|
||||
if (-not (Test-Path $backupDir)) {
|
||||
New-Item -ItemType Directory -Path $backupDir
|
||||
}
|
||||
|
||||
# Ensure redundant backup directory exists
|
||||
if (-not (Test-Path $redundantBackupDir)) {
|
||||
try {
|
||||
New-Item -ItemType Directory -Path $redundantBackupDir -Force
|
||||
} catch {
|
||||
"[$timestamp] Warning: Could not create redundant backup directory at $redundantBackupDir. Error: $_" | Out-File -Append -FilePath $logFile
|
||||
}
|
||||
}
|
||||
|
||||
# Log execution of the script
|
||||
$logFile = Join-Path $backupDir "backup-log.txt"
|
||||
"[$timestamp] Starting database backup..." | Out-File -Append -FilePath $logFile
|
||||
@@ -43,9 +53,23 @@ if ($LASTEXITCODE -eq 0 -and (Test-Path $backupFile) -and (Get-Item $backupFile)
|
||||
"[$timestamp] Backup completed successfully!" | Out-File -Append -FilePath $logFile
|
||||
|
||||
# Optional: Compress the backup file
|
||||
Compress-Archive -Path $backupFile -DestinationPath "$backupFile.zip" -Force
|
||||
$zipFileName = "$backupFile.zip"
|
||||
Compress-Archive -Path $backupFile -DestinationPath $zipFileName -Force
|
||||
Remove-Item $backupFile
|
||||
"[$timestamp] Backup compressed to $backupFile.zip" | Out-File -Append -FilePath $logFile
|
||||
"[$timestamp] Backup compressed to $zipFileName" | Out-File -Append -FilePath $logFile
|
||||
|
||||
# Copy backup to redundant location
|
||||
if (Test-Path $redundantBackupDir) {
|
||||
try {
|
||||
$redundantBackupFile = Join-Path $redundantBackupDir (Split-Path -Leaf $zipFileName)
|
||||
Copy-Item -Path $zipFileName -Destination $redundantBackupFile -Force
|
||||
"[$timestamp] Backup copied to redundant location: $redundantBackupFile" | Out-File -Append -FilePath $logFile
|
||||
} catch {
|
||||
"[$timestamp] Error copying backup to redundant location: $_" | Out-File -Append -FilePath $logFile
|
||||
}
|
||||
} else {
|
||||
"[$timestamp] Warning: Redundant backup directory not found: $redundantBackupDir" | Out-File -Append -FilePath $logFile
|
||||
}
|
||||
} else {
|
||||
"[$timestamp] Backup failed!" | Out-File -Append -FilePath $logFile
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user