]> git.p6c8.net - psmysqlbackup.git/blob - psmysqlbackup.ps1
7ca585a28afe40a4736aae1ba3fbe79360883103
[psmysqlbackup.git] / psmysqlbackup.ps1
1 [String]$configMysqlHost = "localhost"
2 [String]$configMysqlUser = "backup"
3 [String]$configMysqlPassword = "backup"
4
5 [String]$configMysqlCli = "C:\Program Files\MariaDB 10.5\bin\mysql.exe"
6 [String]$configMysqldumpCli = "C:\Program Files\MariaDB 10.5\bin\mysqldump.exe"
7
8 [String]$configBackupDir = "backup"
9 [String]$configRotate = 7
10
11 function Get-Databases() {
12 $databaseString = (& $configMysqlCli --host=$configMysqlHost --user=$configMysqlUser --password=$configMysqlPassword --batch --skip-column-names -e "SHOW DATABASES;")
13 $databases = $databaseString.split([Environment]::NewLine)
14
15 return $databases
16 }
17
18 function Create-Backup([String]$database, [String]$target) {
19 & $configMysqldumpCli --host=$configMysqlHost --user=$configMysqlUser --password=$configMysqlPassword --single-transaction --result-file=$target $database
20 }
21
22 $currDaytime = Get-Date -format "yyyyMMdd-HHmmss"
23
24 $databases = Get-Databases | Where-Object { $_ -ne "information_schema" -and $_ -ne "performance_schema"}
25
26 foreach($d in $databases) {
27 $databaseBackupDir = Join-Path -Path $configBackupDir -ChildPath $d
28
29 if(!(Test-Path $databaseBackupDir)) {
30 New-Item -ItemType directory -Path $databaseBackupDir -ErrorAction Stop | Out-Null
31 }
32
33 $databaseBackupFile = Join-Path -Path $databaseBackupDir -ChildPath "backup-$d-$currDaytime.sql"
34 Write-Output "Backing up $d to $databaseBackupFile..."
35 Create-Backup $d $databaseBackupFile
36 }

patrick-canterino.de