亲测可用,已使用在项目中
#!/bin/bash
# 基础路径
base_path="/home/sql_back/postgres/"
# 创建基础路径
mkdir -p $base_path
files=$(ls $base_path)
# 当前年月日格式时间
time=$(date "+%Y-%m-%d")
# 当前时间戳
todayTime=`date -d "$today" +%s`
# 保留天数
delete_days=5
#文件后缀
suffix="sql"
for filename in $files
do
# 文件名
name=${filename%.*}
# 文件后缀
extension=${filename##*.}
if ((extension==suffix))
then
fileDayTime=`date -d "$name" +%s`
dayTime=$[todayTime-fileDayTime]
if [[ $dayTime -gt $[delete_days*24*60*60] ]];
then
printf "`date +'%F %H:%M:%S'` delete file $filename \n"
rm -rf $base_path$filename
fi
fi
done
touch "$base_path$time.$suffix"
chmod 777 "$base_path$time.$suffix"
#sudo docker exec -i mysql_db1 mysqldump -umysql_user -p'123' --single-transaction jpa_test > "$base_path$time.$suffix"
sudo docker exec -i postgres pg_dump -U postgres -d postgres --encoding 'UTF8' > "$base_path$time.$suffix"
printf "sql back script exec down!!!!!!!!!!!! \n"