-[String]$configMysqlHost = "localhost"
-[Int32]$configMysqlPort = 3306
-[String]$configMysqlUser = "backup"
-[String]$configMysqlPassword = "backup"
+# 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
-[String]$configBackupDir = "backup"
-[Int32]$configRotate = 7
+$configMysqlHost = "localhost"
+$configMysqlPort = 3306
+$configMysqlUser = "backup"
+$configMysqlPassword = "backup"
+
+$configMysqlCli = "C:\Program Files\MariaDB 10.5\bin\mysql.exe"
+$configMysqldumpCli = "C:\Program Files\MariaDB 10.5\bin\mysqldump.exe"
+
+$configBackupDir = "backup"
+$configRotate = 7
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
+ }
+ }
- $databasesToBackup = $configDbBackup
+ foreach($cDb in $configDbBackup) {
+ if($cDb -in $databases) {
+ $databasesToBackup += $cDb
+ }
+ else {
+ Write-Warning "Not backing up database $cDb, because it does not exist"
+ }
+ }
$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..."
- Create-Backup $d $databaseBackupFile
- Rotate-Backups $databaseBackupDir
+
+ try {
+ Create-Backup $d $databaseBackupFile
+ Rotate-Backups $databaseBackupDir
+ }
+ catch {
+ Write-Output "Could not backup database $d to $databaseBackupFile"
+ Write-Output $_
+ }