侧边栏壁纸
博主头像
chao's blog博主等级

记录学习所得,以及日常生活

  • 累计撰写 11 篇文章
  • 累计创建 0 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

一个同步博客数据到onedrive上的脚本

Administrator
2024-08-30 / 0 评论 / 0 点赞 / 5 阅读 / 2074 字
#!/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

0

评论区