X-Git-Url: https://git.p6c8.net/psmysqlbackup.git/blobdiff_plain/0d9b112c85892de9511250eb3747a4adf6e60bf1..HEAD:/psmysqlbackup.ps1?ds=inline diff --git a/psmysqlbackup.ps1 b/psmysqlbackup.ps1 index cdc6afa..b728bb0 100644 --- a/psmysqlbackup.ps1 +++ b/psmysqlbackup.ps1 @@ -9,31 +9,37 @@ # Config # MySQL host -$configMysqlHost = "localhost" +$configMysqlHost = 'localhost' # Port of MySQL host $configMysqlPort = 3306 # MySQL user using to connect to MySQL -$configMysqlUser = "backup" +$configMysqlUser = 'backup' # Password for MySQL user -$configMysqlPassword = "backup" +$configMysqlPassword = 'backup' # Path to MySQL CLI program -$configMysqlCli = "C:\Program Files\MariaDB 10.5\bin\mysql.exe" +$configMysqlCli = 'mysql.exe' # Path to mysqldump CLI program -$configMysqldumpCli = "C:\Program Files\MariaDB 10.5\bin\mysqldump.exe" +$configMysqldumpCli = 'mysqldump.exe' # Directory where to store the backups -$configBackupDir = "backup" +$configBackupDir = 'backup' # Number of backups to keep, set to 0 to keep all backups -$configRotate = 7 +$configBackupRotate = 7 -$configLogDir = "log" +# Compress backups (limited to 2 GB due to usage of Compress-Archive) +$configBackupCompress = $False + +# Directory where to store the logfiles +$configLogDir = 'log' +# Number of logfiles to keep, set to 0 to keep all logfiles +# You should set this to at least the same as $configBackupRotate $configLogRotate = 7 # Databases to backup, leave empty to backup all databases $configDbBackup = @() # If $configDbBackup is empty, don't backup the databases defined here -$configDbExclude = @("test") +$configDbExclude = @('test') # If $configDbBackup is empty, don't backup the databases matching these patterns $configDbExcludePattern = @() @@ -167,43 +173,60 @@ function Get-Databases() { return $databases } -function Create-Backup([String]$database, [String]$target) { +function Invoke-DatabaseBackup([String]$database, [String]$target) { & $configMysqldumpCli --host=$configMysqlHost --port=$configMysqlPort --user=$configMysqlUser --password=$configMysqlPassword --single-transaction --result-file=$target $database if($LastExitCode -ne 0) { throw "mysqldump exited with Exit code $LastExitCode" } -} -function Rotate-Backups($backupDir) { - if($configRotate -le 0) { + if($configBackupCompress) { + Compress-Archive -Path $target -DestinationPath "$target.zip" + Remove-Item -Path $target + } +} +function Invoke-FileRotation { + Param ( + $Dir, + $MaxFiles, + [Parameter(Mandatory=$false)] + $Pattern, + [Parameter(Mandatory=$false)] + $LogFile + ) + + if($MaxFiles -le 0) { return } - - $keepBackupsCount = $configRotate - Get-ChildItem $backupDir -File | Where-Object {($_.Name -match "^backup-.+-\d{8,}-\d{6}\.sql$")} | Sort-Object -Descending | + $keepFilesCount = $MaxFiles + + Get-ChildItem $Dir -File | Where-Object {($null -eq $Pattern -or $_.Name -match $Pattern)} | Sort-Object -Descending | Foreach-Object { - if($keepBackupsCount -ge 0) { - $keepBackupsCount-- + if($keepFilesCount -ge 0) { + $keepFilesCount-- } - if($keepBackupsCount -eq -1) { - Write-Output "Deleting backup $($_.FullName)" - #Write-Log "Deleting backup $($_.FullName)" + if($keepFilesCount -eq -1) { + if($null -ne $LogFile) { + Write-Log "Deleting file $($_.FullName)" -Path $LogFile + } Remove-Item -Force $_.FullName } } } -$defaultDbExclude = @("information_schema", "performance_schema") +$defaultDbExclude = @('information_schema', 'performance_schema') -$currDaytime = Get-Date -format "yyyyMMdd-HHmmss" +$patternBackupFile = '^backup-.+-\d{8,}-\d{6}\.sql(\.zip)?$' +$patternLogFile = '^log-\d{8,}-\d{6}\.log$' + +$currDaytime = Get-Date -format 'yyyyMMdd-HHmmss' $logFile = "$configLogDir\log-$currDaytime.log" -$startTime = Get-Date -format "yyyy-MM-dd HH:mm:ss" +$startTime = Get-Date -format 'yyyy-MM-dd HH:mm:ss' Write-Log "Started at $startTime" -Path $logFile # Get a list of all databases @@ -211,12 +234,9 @@ try { $databases = Get-Databases | Where-Object {!($_ -in $defaultDbExclude)} } catch { - Write-Log "Failed to get list of databases" -Path $logFile -Level Error + Write-Log 'Failed to get list of databases' -Path $logFile -Level Error Write-Log $_ -Path $logFile -Level Error - Write-Log "Exiting" -Path $logFile -Level Error - - Write-Output "Failed to get list of databases" - Write-Output $_ + Write-Log 'Exiting' -Path $logFile -Level Error exit 1 } @@ -232,7 +252,6 @@ if($configDbBackup -and $configDbBackup.count -gt 0) { } else { Write-Log "Not backing up database $cDb, because it does not exist" -Path $logFile -Level Warn - Write-Warning "Not backing up database $cDb, because it does not exist" } } } @@ -264,10 +283,7 @@ foreach($d in $databasesToBackup) { catch { Write-Log "Failed to create directory $databaseBackupDir" -Path $logFile -Level Error Write-Log $_ -Path $logFile -Level Error - Write-Log "Exiting" -Path $logFile -Level Error - - Write-Output "Failed to create directory $databaseBackupDir" - Write-Output $_ + Write-Log 'Exiting' -Path $logFile -Level Error exit 1 } @@ -275,21 +291,24 @@ foreach($d in $databasesToBackup) { $databaseBackupFile = Join-Path -Path $databaseBackupDir -ChildPath "backup-$d-$currDaytime.sql" - Write-Log "Backing up $d to $databaseBackupFile..." -Path $logFile - Write-Output "Backing up $d to $databaseBackupFile..." + if($configBackupCompress) { + Write-Log "Backing up $d to compressed file $databaseBackupFile.zip..." -Path $logFile + } + else { + Write-Log "Backing up $d to $databaseBackupFile..." -Path $logFile + } try { - Create-Backup $d $databaseBackupFile - Rotate-Backups $databaseBackupDir + Invoke-DatabaseBackup $d $databaseBackupFile + Invoke-FileRotation -Dir $databaseBackupDir -MaxFiles $configBackupRotate -Pattern $patternBackupFile -LogFile $logFile } catch { Write-Log "Could not backup database $d to $databaseBackupFile" -Path $logFile -Level Error Write-Log $_ -Path $logFile -Level Error - - Write-Output "Could not backup database $d to $databaseBackupFile" - Write-Output $_ } } -$endTime = Get-Date -format "yyyy-MM-dd HH:mm:ss" +Invoke-FileRotation -Dir $configLogDir -MaxFiles $configLogRotate -Pattern $patternLogFile -LogFile $logFile + +$endTime = Get-Date -format 'yyyy-MM-dd HH:mm:ss' Write-Log "Ended at $endTime" -Path $logFile \ No newline at end of file