#!/bin/bash
# 定义变量
SOURCE_DIR="/www/wwwroot/www.lcdd.net"
DATABASE_NAME="www_lcdd_net"
DB_USER="www_lcdd_net"
DB_PASSWORD="xxxxx" //数据库密码
BACKUP_DIR="/root/backup"
ONEDRIVE_REMOTE="onedrive:" # 确保在rclone中配置了名为"onedrive"的远程
ONEDRIVE_BACKUP_DIR="blog_backups"
LOG_DIR="/root/backuplog"
LOG_FILE="$LOG_DIR/backup.log"
DATE=$(date +"%Y%m%d%H%M")
# 创建备份目录
mkdir -p $BACKUP_DIR
# 创建日志目录(如果不存在)
mkdir -p $LOG_DIR
# 备份数据库
mysqldump -u $DB_USER -p$DB_PASSWORD $DATABASE_NAME > $BACKUP_DIR/$DATABASE_NAME-$DATE.sql
if [ $? -eq 0 ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") - Database backup successful" >> $LOG_FILE
else
echo "$(date +"%Y-%m-%d %H:%M:%S") - Database backup failed" >> $LOG_FILE
exit 1
fi
# 备份博客文件
rsync -avz $SOURCE_DIR $BACKUP_DIR/blog-files-$DATE
if [ $? -eq 0 ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") - Blog files backup successful" >> $LOG_FILE
else
echo "$(date +"%Y-%m-%d %H:%M:%S") - Blog files backup failed" >> $LOG_FILE
exit 1
fi
# 将备份文件上传到OneDrive
rclone copy $BACKUP_DIR $ONEDRIVE_REMOTE$ONEDRIVE_BACKUP_DIR/$DATE
if [ $? -eq 0 ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") - Upload to OneDrive successful" >> $LOG_FILE
else
echo "$(date +"%Y-%m-%d %H:%M:%S") - Upload to OneDrive failed" >> $LOG_FILE
exit 1
fi
# 删除OneDrive上的旧备份,只保留最新的三个
OLD_BACKUPS=$(rclone lsf $ONEDRIVE_REMOTE$ONEDRIVE_BACKUP_DIR --dirs-only | sort -r | sed '1,3d')
for backup in $OLD_BACKUPS
do
rclone purge $ONEDRIVE_REMOTE$ONEDRIVE_BACKUP_DIR/$backup
if [ $? -eq 0 ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") - Deleted old backup: $backup" >> $LOG_FILE
else
echo "$(date +"%Y-%m-%d %H:%M:%S") - Failed to delete old backup: $backup" >> $LOG_FILE
fi
done
# 删除本地备份文件
rm -rf $BACKUP_DIR
if [ $? -eq 0 ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") - Local backup cleanup successful" >> $LOG_FILE
else
echo "$(date +"%Y-%m-%d %H:%M:%S") - Local backup cleanup failed" >> $LOG_FILE
fi
版权归属:
Administrator
许可协议:
本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
评论区