Advertisement
abhinav777

Powershell - Example of mysqldump

May 14th, 2024
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $databasename = 'parks_and_recreation'
  2. $today = Get-Date -UFormat "%Y-%m-%d"
  3. $backuppath = 'C:\db-backups\'
  4. $backupfilename = $backuppath + $databasename + '-' + $today + '.sql'
  5. $compressfilename = $backuppath + $databasename + '-' + $today + '.zip'
  6. $errorlog = $backuppath + 'error.log'
  7.  
  8. # Ensure the backup directory exists
  9. if (-not (Test-Path -Path $backuppath)) {
  10.     New-Item -Path $backuppath -ItemType Directory
  11. }
  12.  
  13. # Full path to mysqldump
  14. $mysqldumpPath = 'C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqldump.exe'
  15.  
  16. # Perform the database backup
  17. & $mysqldumpPath -u 'root' -p'NinjaBackup2024!' --log-error=$errorlog --result-file=$backupfilename --databases $databasename
  18. Compress-Archive -Path $backupfilename -DestinationPath $compressfilename
  19. Remove-Item -Path $backupfilename
  20.  
  21. # Remove old backups (keep last 7)
  22. $oldBackups = Get-ChildItem $backuppath -Filter '*.zip' | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 7
  23. foreach ($file in $oldBackups) {
  24.     Remove-Item -Path $file.FullName
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement