Postgresql 为不同的集群单独备份

Postgresql separate backups for different clusters

我有这个备份 postgresql 数据库的脚本

#!/bin/bash
# Location to place backups.
backup_dir="/path/to/backups/"
#String to append to the name of the backup files
backup_date=`date +%Y-%m-%d`
#Numbers of days you want to keep copie of your databases
number_of_days=7
databases=`psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'`
for i in $databases; do
  if [ "$i" != "template0" ] && [ "$i" != "template1" ]; then
    echo Dumping $i to $backup_dir$i\_$backup_date
    pg_dump -Fc $i > $backup_dir$i\_$backup_date
  fi
done
find $backup_dir -type f -prune -mtime +$number_of_days -exec rm -f {} \;

服务器上只用了一个集群,一切正常。但是现在创建了一个新集群。所以我开始思考备份是否会正确完成,如果不能,如何确保它会为每个集群正确备份?

这个脚本现在会遍历每个集群并为所有集群中的所有数据库做备份吗?如果是这样,可能会出现名称冲突。

我如何确保它会在不同集群的不同目录中进行备份?

我通过创建第二个脚本来备份另一个集群数据库来解决这个问题。好吧,它不是很优雅,但它确实有效。如果有人可以编写更通用的脚本(并且该脚本可用于所有数据库备份)以便考虑不同的备份目录和集群,请 post 作为答案(因为这将是更好的解决方案)

第二个脚本如下所示(当然最好将两个脚本合并为一个):

#!/bin/bash
# Location to place backups.
        backup_dir="/path/to/backups2"
#String to append to the name of the backup files
        backup_date=`date +%Y-%m-%d`
#Numbers of days you want to keep copie of your databases
        number_of_days=1
        databases=` psql -p 5433 -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d' `
        for i in $databases; do
                if [ "$i" != "template0" ] && [ "$i" != "template1" ]; then
                        echo Dumping $i to $backup_dir$i\_$backup_date\.gz
                        pg_dump -p 5433 -Fc $i |gzip -f > $backup_dir$i\_$backup_date\.gz
                fi
        done
find $backup_dir -type f -prune -mtime +$number_of_days -exec rm -f {} \;