+# PSMySQLBackup
+# PowerShell script for backing up MySQL / MariaDB databases on Windows
+#
+# Author: Patrick Canterino <patrick@patrick-canterino.de>
+# WWW: https://www.patrick-canterino.de/
+# https://github.com/pcanterino/dsmonrot
+# License: 2-Clause BSD License
+
function Get-Databases() {
$databaseString = (& $configMysqlCli --host=$configMysqlHost --port=$configMysqlPort --user=$configMysqlUser --password=$configMysqlPassword --batch --skip-column-names -e "SHOW DATABASES;")
function Get-Databases() {
$databaseString = (& $configMysqlCli --host=$configMysqlHost --port=$configMysqlPort --user=$configMysqlUser --password=$configMysqlPassword --batch --skip-column-names -e "SHOW DATABASES;")
function Create-Backup([String]$database, [String]$target) {
& $configMysqldumpCli --host=$configMysqlHost --port=$configMysqlPort --user=$configMysqlUser --password=$configMysqlPassword --single-transaction --result-file=$target $database
function Create-Backup([String]$database, [String]$target) {
& $configMysqldumpCli --host=$configMysqlHost --port=$configMysqlPort --user=$configMysqlUser --password=$configMysqlPassword --single-transaction --result-file=$target $database
- return
- }
-
- $keepBackupsCount = $configRotate
-
- Get-ChildItem $backupDir -File | Where-Object {($_.Name -match "^backup-.+-\d{8,}-\d{6}\.sql$")} | Sort-Object -Descending |
- Foreach-Object {
- if($keepBackupsCount -ge 0) {
- $keepBackupsCount--
- }
-
- if($keepBackupsCount -eq -1) {
- Write-Output "Deleting backup $($_.FullName)"
- Remove-Item -Force $_.FullName
- }
- }
+ return
+ }
+
+ $keepBackupsCount = $configRotate
+
+ Get-ChildItem $backupDir -File | Where-Object {($_.Name -match "^backup-.+-\d{8,}-\d{6}\.sql$")} | Sort-Object -Descending |
+ Foreach-Object {
+ if($keepBackupsCount -ge 0) {
+ $keepBackupsCount--
+ }
+
+ if($keepBackupsCount -eq -1) {
+ Write-Output "Deleting backup $($_.FullName)"
+ Remove-Item -Force $_.FullName
+ }
+ }
$databaseBackupFile = Join-Path -Path $databaseBackupDir -ChildPath "backup-$d-$currDaytime.sql"
Write-Output "Backing up $d to $databaseBackupFile..."
$databaseBackupFile = Join-Path -Path $databaseBackupDir -ChildPath "backup-$d-$currDaytime.sql"
Write-Output "Backing up $d to $databaseBackupFile..."