#!/bin/bash

# 服务器信息
SERVER_USER="root"
SERVER_HOST="106.14.30.150"
SERVER_PATH="/opt/1panel/apps/openresty/openresty/www/sites/aizk.ycymedu.com/index"
PRIVATE_KEY="ALIYUN.pem"
BACKUP_PATH="${SERVER_PATH}-backup-$(date +%Y%m%d%H%M%S).zip"
DINGDING_WEBHOOK="https://oapi.dingtalk.com/robot/send?access_token=fca104958fea6273c9c7ef3f08b3d552645c214f929066785e8caf6e1885a5a6"

# 在上传之前备份原来的文件并压缩
ssh -i $PRIVATE_KEY $SERVER_USER@$SERVER_HOST "cd $(dirname $SERVER_PATH) && zip -r $(basename $BACKUP_PATH) $(basename $SERVER_PATH)"

# 使用 scp 上传文件
scp -i $PRIVATE_KEY -r build/* $SERVER_USER@$SERVER_HOST:$SERVER_PATH

# 提示上传完成
if [ $? -eq 0 ]; then
  echo "上传成功！备份存储于 $BACKUP_PATH"
  
  # 发送钉钉通知
  curl -X POST "$DINGDING_WEBHOOK" \
       -H "Content-Type: application/json" \
       -d '{
             "msgtype": "text",
             "text": {
               "content": "SixAIGC html| upload success!!！ backup to'"$BACKUP_PATH"'"
             }
           }'
else
  echo "上传失败，请检查错误信息。"
  
  # 发送钉钉通知
  curl -X POST "$DINGDING_WEBHOOK" \
       -H "Content-Type: application/json" \
       -d '{
             "msgtype": "text",
             "text": {
               "content": "SixAIGC html|upload fail，please check error info。"
             }
           }'
fi
