hy-farm/muchang/mysql/mysqlbk.sh
2026-05-29 19:54:56 +08:00

32 lines
751 B
Bash

#!/bin/bash
databases=(farm)
# 备份文件要保存的目录
basepath='/home/wwwroot/farm/mysql/'
if [ ! -d "$basepath" ]; then
mkdir -p "$basepath"
fi
# 循环databases数组
for db in ${databases[*]}
do
# 备份数据库生成SQL文件
/bin/nice -n 19 /usr/bin/mysqldump -uroot -padmin123. $db > $basepath$db-$(date +%Y%m%d).sql
# 将生成的SQL文件压缩
/bin/nice -n 19 tar zPcf $basepath$db-$(date +%Y%m%d).sql.tar.gz $basepath$db-$(date +%Y%m%d).sql
# 删除7天之前的备份数据
find $basepath -mtime +7 -name "*.sql.tar.gz" -exec rm -rf {} \;
done
# 删除生成的SQL文件
rm -rf $basepath/*.sql
#crontab -e
#*/01 * * * * /bin/sh /home/wwwroot/farm/mysql/mysqlbk.sh
#service crond restart