Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $databasename = 'parks_and_recreation'
- $today = Get-Date -UFormat "%Y-%m-%d"
- $backuppath = 'C:\db-backups\'
- $backupfilename = $backuppath + $databasename + '-' + $today + '.sql'
- $compressfilename = $backuppath + $databasename + '-' + $today + '.zip'
- $errorlog = $backuppath + 'error.log'
- # Ensure the backup directory exists
- if (-not (Test-Path -Path $backuppath)) {
- New-Item -Path $backuppath -ItemType Directory
- }
- # Full path to mysqldump
- $mysqldumpPath = 'C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqldump.exe'
- # Perform the database backup
- & $mysqldumpPath -u 'root' -p'NinjaBackup2024!' --log-error=$errorlog --result-file=$backupfilename --databases $databasename
- Compress-Archive -Path $backupfilename -DestinationPath $compressfilename
- Remove-Item -Path $backupfilename
- # Remove old backups (keep last 7)
- $oldBackups = Get-ChildItem $backuppath -Filter '*.zip' | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip 7
- foreach ($file in $oldBackups) {
- Remove-Item -Path $file.FullName
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement