]> git.p6c8.net - psmysqlbackup.git/blobdiff - psmysqlbackup.ps1
Before backing up a selection of database, check if the databases exist
[psmysqlbackup.git] / psmysqlbackup.ps1
index f67619bbb1cde54f42bdaf9f2e36322b01da1250..1e23c5e6c9c10416d31b9e58f47a424aa6aea6b4 100644 (file)
@@ -1,3 +1,11 @@
+# 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
+
 # Config
 
 $configMysqlHost = "localhost"
@@ -48,7 +56,7 @@ function Rotate-Backups($backupDir) {
         if($keepBackupsCount -ge 0) {
             $keepBackupsCount--
         }
-        
+
         if($keepBackupsCount -eq -1) {
             Write-Output "Deleting backup $($_.FullName)"
             Remove-Item -Force $_.FullName
@@ -72,7 +80,14 @@ catch {
 $databasesToBackup = @()
 
 if($configDbBackup -and $configDbBackup.count -gt 0) {
-    $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"
+        }
+    }
 }
 else {
     $databasesToBackup = $databases
@@ -90,11 +105,10 @@ foreach($d in $databasesToBackup) {
     
     try {
         Create-Backup $d $databaseBackupFile
+        Rotate-Backups $databaseBackupDir
     }
     catch {
         Write-Output "Could not backup database $d to $databaseBackupFile"
         Write-Output $_
     }
-    
-    Rotate-Backups $databaseBackupDir
 }
\ No newline at end of file

patrick-canterino.de