我有一个备份 shell 脚本,它每晚都会运行 pg_basebackup。但是当我尝试运行它时,我收到此错误消息,指出 pg_basebackup 显然与 Postgres.app 版本 16.3 不兼容。
2024-07-25 14:38:37 - Backup script started.
2024-07-25 14:38:37 - PG_USER: postgres
2024-07-25 14:38:37 - BACKUP_DIR: /Volumes/Atoma/Backup
2024-07-25 14:38:37 - TEMP_BACKUP_DIR: /Volumes/Atoma/tmp_backup
2024-07-25 14:38:37 - DAY_OF_WEEK: Donnerstag
2024-07-25 14:38:37 - BACKUP_NAME: Donnerstag.tar.gz
2024-07-25 14:38:37 - LOG_FILE: /Users/tristan/Server_Backup/daily.log
2024-07-25 14:38:37 - Starting backup for Donnerstag.
2024-07-25 14:38:37 - Running pg_basebackup command.
pg_basebackup: error: incompatible server version 16.3 (Postgres.app)
pg_basebackup: removing contents of data directory "/Volumes/Atoma/tmp_backup"
2024-07-25 14:38:38 - Backup failed for Donnerstag.
2024-07-25 14:38:38 - Backup script finished.
我使用 macOS Sonoma 14.5 (23F79)。这是我的备份脚本:
#!/bin/zsh
# Variables
PG_USER="xyz"
PG_USER_PW="xyz"
PORT="5432"
HOST="localhost"
BACKUP_DIR="/Volumes/Atoma/Backup"
TEMP_BACKUP_DIR="/Volumes/Atoma/tmp_backup"
DAY_OF_WEEK=$(date +%A)
BACKUP_NAME="$DAY_OF_WEEK.tar.gz"
LOG_FILE="/Users/tristan/Server_Backup/daily.log"
# create temporary directory (needed for pg_basebackup)
mkdir -p $TEMP_BACKUP_DIR
# log with timestamp
log_with_timestamp() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> $LOG_FILE
}
# Start logging
log_with_timestamp "Backup script started."
# Logging variables
log_with_timestamp "PG_USER: $PG_USER"
log_with_timestamp "BACKUP_DIR: $BACKUP_DIR"
log_with_timestamp "TEMP_BACKUP_DIR: $TEMP_BACKUP_DIR"
log_with_timestamp "DAY_OF_WEEK: $DAY_OF_WEEK"
log_with_timestamp "BACKUP_NAME: $BACKUP_NAME"
log_with_timestamp "LOG_FILE: $LOG_FILE"
# Start backup log process
log_with_timestamp "Starting backup for $DAY_OF_WEEK."
# Set the PGPASSWORD environment variable
export PGPASSWORD=$PG_USER_PW
# Run pg_basebackup
log_with_timestamp "Running pg_basebackup command."
if pg_basebackup -h $HOST -p $PORT -U $PG_USER -D $TEMP_BACKUP_DIR -Ft -z -P -Xs 2>> $LOG_FILE; then
# Compress the backup and move it to the final destination
mv $TEMP_BACKUP_DIR/$BACKUP_NAME $BACKUP_DIR
log_with_timestamp "Backup completed successfully for $DAY_OF_WEEK."
else
log_with_timestamp "Backup failed for $DAY_OF_WEEK."
fi
# Clean up temporary backup directory
rm -rf $TEMP_BACKUP_DIR
# Unset the PGPASSWORD environment variable for security
unset PGPASSWORD
# End of script
log_with_timestamp "Backup script finished."
我使用 homebrew 安装了 PostgreSQL 14 和 15,并且我的脚本从 homebrew 安装中获取了 pg_basebackup,这显然是错误的版本。
我刚刚卸载了自制版本的 Postgres
brew uninstall postgresql@14
,brew uninstall postgresql@15
然后brew cleanup
。然后我将 Postgres.app 中的 bin 文件夹添加到 PATH
export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH
并将其放在脚本的顶部。