docker-postgres/fulldump

24 lines
770 B
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 获取脚本所在目录的绝对路径
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
# 切换工作目录到脚本所在目录
cd $SCRIPT_DIR
# 声明变量
container_name="postgres"
pg_database=$1 # 设置pg_database为第一个传参默认为postgres
pg_user="postgres"
backup_dir="./data/backup/"
current_datetime=$(date +"%Y%m%d_%H%M%S")
filename="${pg_database}_full_${current_datetime}"
# 清除几天前的备份
days=${2:-15}
# 删除符合条件的文件并记录到cleardump.log
find $backup_dir -maxdepth 1 -name "${pg_database}*" -mtime +$days -exec sh -c 'echo "$(date): $1" >> cleardump.log; rm -rf $1' sh {} \;
# 创建新备份
docker exec -i "$container_name" pg_dump -U postgres -Fd "$pg_database" -f "${backup_dir}${filename}" -j 4