备份脚本:

cat >> backup.sh << EOF
DATE=$(date +%Y%m%d-%H%M%S)
containerd_bak=/var/backups
PGDUMPBACK_DIR=/apps/postgresql/bak
PGDUMPBACK_LOG=$PGDUMPBACK_DIR/log/pg_dump_${DATE}.log
export PGPASSWORD=Kevin_1128
startTime=`date +"%Y-%m-%d %H:%M:%S"`
startTime_s=`date +%s`
cur_time=$(date '+%Y-%m-%d') #当天
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d) #前一天
tendays_time=$(date -d -3days '+%Y-%m-%d') #前三天
ls $PGDUMPBACK_DIR/log/ > /dev/null 2>&1
if [ $? -eq 0 ];
    then echo "检查:备份目录 ... 已存在!"
else
    mkdir -p $PGDUMPBACK_DIR/log/
fi

touch $PGDUMPBACK_DIR/log/pg_dump_${DATE}.log
exec &> $PGDUMPBACK_LOG
echo "Starting Backup PostgreSQL:" $startTime "$PGDUMPBACK_DIR Size:" `du -sh $PGDUMPBACK_DIR | awk '{print $1}'` && docker exec -it postgres-master pg_dumpall -U postgres -p 5432 -c -f "$containerd_bak/pgsql-backup.$cur_time.dmp"
tar -zcvf - "$PGDUMPBACK_DIR/pgsql-backup.$cur_time.dmp" | split -b 3G -d -a 3 --additional-suffix=.tar.gz - "$PGDUMPBACK_DIR/pgsql-backup.$cur_time."
echo "$PGDUMPBACK_DIR Size: `du -sh $PGDUMPBACK_DIR | awk '{print \$1}'`"
echo "Remove temp file ... `date +"%Y-%m-%d %H:%M:%S"`"
rm -rf $PGDUMPBACK_DIR/pgsql-backup.*.dmp
rm -rf $PGDUMPBACK_DIR/pgsql-backup.${YESTERDAY}.tar-*.gz
echo "Finish Backup ... `date +"%Y-%m-%d %H:%M:%S"` $PGDUMPBACK_DIR Size: `du -sh $PGDUMPBACK_DIR | awk '{print $1}'`"
endTime=`date +"%Y-%m-%d %H:%M:%S"`
endTime_s=`date +%s`
sumTime=$[ $endTime_s - $startTime_s ]
echo "本次运行时间: $sumTime s"
EOF

备份打包目录:

ls /apps/postgresql/bak/
log  pgsql-backup.2024-05-22.000.tar.gz

备份执行过程日志:

root@postgresql:~#  ls /apps/postgresql/bak/log
pg_dump_20240522-152138.log
root@postgresql:~#  cat /apps/postgresql/bak/log/pg_dump_20240522-152138.log 
Starting Backup PostgreSQL: 2024-05-22 15:21:38 /apps/postgresql/bak Size: 12K
tar: Removing leading `/' from member names
/apps/postgresql/bak/pgsql-backup.2024-05-22.dmp
/apps/postgresql/bak Size: 20K
Remove temp file ... 2024-05-22 15:21:39
Finish Backup ... 2024-05-22 15:21:39 /apps/postgresql/bak Size: 16K
本次运行时间: 1 s
最后修改:2024 年 05 月 25 日
如果觉得我的文章对你有用,请随意赞赏