3 $configMysqlHost = "localhost"
4 $configMysqlPort = 3306
5 $configMysqlUser = "backup"
6 $configMysqlPassword = "backup"
8 $configMysqlCli = "C:\Program Files\MariaDB 10.5\bin\mysql.exe"
9 $configMysqldumpCli = "C:\Program Files\MariaDB 10.5\bin\mysqldump.exe"
11 $configBackupDir = "backup"
15 $configDbExclusions = @("test")
19 $defaultExclusions = @("information_schema", "performance_schema")
21 function Get-Databases() {
22 $databaseString = (& $configMysqlCli --host=$configMysqlHost --port=$configMysqlPort --user=$configMysqlUser --password=$configMysqlPassword --batch --skip-column-names -e "SHOW DATABASES;")
23 $databases = $databaseString.split([Environment]::NewLine)
28 function Create-Backup([String]$database, [String]$target) {
29 & $configMysqldumpCli --host=$configMysqlHost --port=$configMysqlPort --user=$configMysqlUser --password=$configMysqlPassword --single-transaction --result-file=$target $database
32 function Rotate-Backups($backupDir) {
33 if($configRotate -le 0) {
37 $keepBackupsCount = $configRotate
39 Get-ChildItem $backupDir -File | Where-Object {($_.Name -match "^backup-.+-\d{8,}-\d{6}\.sql$")} | Sort-Object -Descending |
41 if($keepBackupsCount -ge 0) {
45 if($keepBackupsCount -eq -1) {
46 Write-Output "Deleting backup $($_.FullName)"
47 Remove-Item -Force $_.FullName
52 $currDaytime = Get-Date -format "yyyyMMdd-HHmmss"
54 $databases = Get-Databases | Where-Object {!($_ -in $defaultExclusions -or $_ -in $configDbExclusions)}
56 $databasesToBackup = @()
58 if($configDbBackup -and $configDbBackup.count -gt 0) {
59 $databasesToBackup = $configDbBackup
62 $databasesToBackup = $databases
65 foreach($d in $databasesToBackup) {
66 $databaseBackupDir = Join-Path -Path $configBackupDir -ChildPath $d
68 if(!(Test-Path $databaseBackupDir)) {
69 New-Item -ItemType directory -Path $databaseBackupDir -ErrorAction Stop | Out-Null
72 $databaseBackupFile = Join-Path -Path $databaseBackupDir -ChildPath "backup-$d-$currDaytime.sql"
73 Write-Output "Backing up $d to $databaseBackupFile..."
74 Create-Backup $d $databaseBackupFile
75 Rotate-Backups $databaseBackupDir