sh Postgres备份脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh Postgres备份脚本相关的知识,希望对你有一定的参考价值。

#! /bin/bash
set -E

display() {
	echo -e "\n----->" $*
}

abort() {
	display $* ; exit 77
}

# ensure root context
if [[ $EUID -ne 0 ]]; then
	echo "You must be a root user"
	exit 64 # NOTROOT
fi

# Define default values
dbname="vcenter"
pgversion="9.6"
pgport=5433
options="-v -Fc -C "
daily_backup_path="/backups/daily"
retention_path="/backups/retention"
enckey="merrymary"
backupsize=10485760 #Size is always in KB ( By defaul 10 GB)

# Print final values
echo "pgport:      ${pgport}"
echo "dbname:      ${dbname}"
echo "pgversion:   ${pgversion}"
echo "options:     ${options}"
echo "daily_backup_path:     ${daily_backup_path}"
echo "enckey:      ${enckey}"
echo "retention_path:     ${retention_path}"
echo "backup_size:     ${backupsize}"

echo "=========================================="

# Generate backupname
datestamp=$(date --iso-8601=seconds)
backupname="daily_${dbname}_${datestamp}.backup.enc"

# Set default log
log="/backups/log/backup_${datestamp}.log"
echo '' > $log

# Get space available in the retention path
space_available=$(df -k $retention_path | tail -1 | awk '{print $4}')

###### Main block #######
(
	# Move old daily backup into  retention folder
	daily=$(ls -t $daily_backup_path | tail -1)
	if [ -f "${daily_backup_path}/${daily}" ]
	then
		mv "${daily_backup_path}/${daily}" "${retention_path}/"
	fi

	# Making space for the new backup
	while [ $backupsize -gt $space_available ]
	do
		oldestenc=$(ls -t $retention_path | tail -1)
		display "Deleting old backups..${oldestenc}"
		rm "${retention_path}/${oldestenc}"
		space_available=$(df -k $retention_path | tail -1 | awk '{print $4}')
	done

	# Run the backup.
	display "Backing up the PostgreSQL database ${dbname}";
	su - postgres -c "/usr/lib/postgresql/$pgversion/bin/pg_dump -p ${pgport} -d ${dbname}  $options | openssl enc -e -aes-256-cbc -out ${daily_backup_path}/${backupname} -pass pass:${enckey}; echo \${PIPESTATUS[*]} > /tmp/dumpexit.code"
	# su - postgres -c "/usr/lib/postgresql/9.6/bin/pg_dump -p 5433 -d vcenter  -v -Fc -C  | openssl enc -e -aes-256-cbc -out /backups/daily/first.backup.enc -pass pass:merrymary; echo \${PIPESTATUS[*]} > /tmp/dumpexit.code"

	# Check return codes
	while IFS=" " read -r dumpexit encexit remainder
	do
		if [[ $dumpexit -ne 0 ]]
		then
				display "Dump failed"
				if [ -f "${daily_backup_path}/${backupname}" ]
				then
					display "Deleting failed backup ${daily_backup_path}/${backupname}"
					rm 	${daily_backup_path}/${backupname}
				fi
		else
				if [[ $encexit -ne 0 ]]
				then
						display "Enc failed"
						if [ -f "${daily_backup_path}/${backupname}" ]
						then
							display "Deleting failed backup ${daily_backup_path}/${backupname}"
							rm 	${daily_backup_path}/${backupname}
						fi
				else
					display "Backup success!!"
				fi
		fi
		echo "dumpexit: ${dumpexit}"
		echo "encexit:  ${encexit}"
	done < "/tmp/dumpexit.code"

) 2>&1 | tee -a $log

以上是关于sh Postgres备份脚本的主要内容,如果未能解决你的问题,请参考以下文章

sh Postgres备份

Centos7 备份 PostgreSQL 数据脚本

postgres数据库备份脚本

postgres procedura脚本不读取file.sh提供的输入

Oracle数据库RMAN的自动备份脚本简介

shell脚本定时备份日志===logBackup.sh