注意:
1、windows本地编辑必须注意编码,建议使用Notepad++编辑。
2、必须赋予755执行权限。
3、宝塔异常进程监控会结束rm进程,建议关闭异常进程监控(宝塔系统加固),否则无法删除备份的数据库文件。
4、建议在网站配置中禁止访问 /MySQL_bak 目录。
5、宝塔后台执行计划任务格式:/root/脚本名称.sh
空格字符转换
sudo apt-get install dos2unix # 如果尚未安装
dos2unix /root/beifen1688.sh
#!/bin/bash
# Author:Tespera
# Blog: https://www.80tm.com
## 备份配置信息 ##
# 备份名称,用于标记
BACKUP_NAME="www.80tm.com"
# 备份目录,多个请空格分隔
BACKUP_SRC="/home/www "
# Mysql主机地址
MYSQL_SERVER="127.0.0.1"
# Mysql用户名
MYSQL_USER="user"
# Mysql密码
MYSQL_PASS="password"
# Mysql备份数据库,多个请空格分隔
MYSQL_DBS="blog"
# 备份文件存放目录
BACKUP_DIR="/Data/Backup"
# 备份文件压缩密码
BACKUP_FILE_PASSWD="www.80tm.com"
# 备份文件保留天数
SAVE_DAYS="30"
## 备份配置信息 End ##
## 以下内容无需修改 ##
NOW=$(date +"%Y%m%d%H%M%S") #精确到秒,同一秒内上传的文件会被覆盖
mkdir -p $BACKUP_DIR
mkdir -p $BACKUP_SRC/MySQL_bak
# 备份 MySQL
echo -e "\nStart dump MySQL ..."
for db_name in $MYSQL_DBS; do
if ! mysqldump --no-tablespaces -u $MYSQL_USER -h $MYSQL_SERVER -p"$MYSQL_PASS" "$db_name" > "$BACKUP_SRC/MySQL_bak/$BACKUP_NAME-$db_name-$NOW.sql"; then
echo "MySQL dump failed for database $db_name" | tee -a $BACKUP_DIR/backup.log
exit 1
fi
done
echo "Dump MySQL OK !"
# 打包备份文件
echo -e "\nStart tar ..."
BACKUP_FILENAME="$BACKUP_NAME-backup-$NOW.zip"
zip -q -r -P $BACKUP_FILE_PASSWD $BACKUP_DIR/$BACKUP_FILENAME $BACKUP_SRC/MySQL_bak/*.sql $BACKUP_SRC
echo "tar OK ! "
# 清理临时备份文件
echo -e "\nStart clean temp file ..."
rm -rf $BACKUP_SRC/MySQL_bak/*.sql
echo "Clean temp file OK !"
# 清理过期备份文件
echo -e "\nStart clean outdated file ..."
find $BACKUP_DIR -type f -name "*zip" -mtime +$SAVE_DAYS -exec rm -rf {} \;
echo "Clean outdated file OK !"
# 写入日志
echo "$NOW Backup $BACKUP_SRC successifully!" >> $BACKUP_DIR/backup.log 2>&1
# 备份结束
echo -e "\033[32m \nBackup Successifully! \n \033[0m"
参考自:https://github.com/Tespera/AutoBackupWebsite