I have been working on setting up a script that will automate my postgres base backups and then use pg_archivecleanup to remove all the unneeded log files.
I want to ensure that the old base backup and the old WAL files are not deleted unless the new pg_basebackup is a success. I read that pg_basebackup will return 0 if successful and something else otherwise so I tried this in my script: (powershell)
if (!(pg_basebackup -h localhost -D $base_back_path -Fp -P -v -w 2>> $log_file)){
$backup_label = Get-Item("$($old_base_back_path)" + '/backup_label')
$b = Get-Content $backup_label
$last_keep = ($b[0].ToCharArray())[-2..-25]
[array]::Reverse($last_keep)
$last_keep_str = $last_keep -join ''
pg_archivecleanup -d $wal_archive_dir $last_keep_str 2>> $log_file
Remove-Item $to_delete_path -Recurse
Add-Content $log_file "Removed Oldest Base Backup"
}
However, when I accidentally ran this script as Administrator rather than postgres the backup command failed because there was no pg_hba entry for that user, but it still deleted the old files. Is there any simple way to ensure that the result of pg_basebackup is a consistent and successful backup in this script?