Script PowerShell per il backup dei database MySql

Dopo aver creato vari siti Joomla e WordPress su Microsoft Azure sfruttando i DB gratuiti di ClearDb, abbiamo attivato una VM (ovviamente su Azure) con MySQL server dove abbiamo spostato i database potendo così superare il limite di 20Mb e delle 4 connessioni.
Per mettere in sicurezza i database abbiamo creato il seguente script PowerShell:

 

cls

Function bkmysql ($x)
{

$server = "127.0.0.1"
$user = "xxx"
$password = "xxx"
$backupFolder = "F:\MYSQL\Backups\"
$dbName = $x
$MySQLDumpPath = "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe"
$limit = (Get-Date).AddDays(-7) # file rentention period.

#--- Format timestamp for the filename
$timestamp = Get-Date -format yyyyMMdd
#Write-Host $timestamp

write-host "Backing up database: " $dbName

#--- Set backup filename
$filename = $dbName + "_" + $timestamp + ".sql"
$fullPath = $backupFolder + "" + $filename

#--- Invoke backup Command.
cmd /c " ""$MySQLDumpPath"" -u $user --password=$password $dbname > $fullPath "
If (test-path($fullPath))
{
write-host "Backup created."
}

#--- Check if 7z is present. Alias for 7-zip. May need to change path below.
if (-not (test-path "C:\Program Files\7-Zip\7z.exe")) {throw "C:\Program Files\7-Zip\7z.exe required"}
set-alias sz "C:\Program Files\7-Zip\7z.exe"

$zipfile = $backupFolder + $dbName + "_" + $timestamp + '.zip'
$from = $fullPath

sz a -tzip $zipfile $from

remove-item "$from"

#--- Delete Files
Get-ChildItem -Path $backupFolder -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
}

bkmysql db1
bkmysql db2
bkmysql dbx

 

Lo script esegue il backup dei database, ne esegue la compressione e cancella i vecchi backup più vecchi di una certa data impostabile tramite variabile.

Contattaci

La tua crescita parte da qui
Per maggiori informazioni

Contattaci

    Iscriviti alla newsletter

      Tematiche d'interesse