ssh 会话关闭时 azcopy 同步身份验证失败

问题描述 投票:0回答:0

我正在努力将 NFS 文件系统从 azure vm 同步到我计划通过 crontab 运行的 blob 存储帐户自动化

当脚本在后台运行时,我遇到身份验证问题,一旦 ssh 会话关闭,azcopy sync 就会出错

#ran the script in this way

nohup ./script &

Cannot perform sync due to error: Login Credentials missing. No SAS token or OAuth token is present and the resource is not public

我首先尝试了无法在后台工作的托管身份,然后尝试了也有同样问题的主要机密

这是我的剧本

#!/bin/bash

# Set variables
SRC_BASE_DIR=/shared2/attachments
DEST_BASE_BLOB_URL=https://stgontadapreprodgennlp.blob.core.windows.net/fileattachments
LOG_DIR=/root/.azcopy
LOG_FILE="$LOG_DIR/azcopy_sync_$(date '+%Y-%m-%d_%H-%M-%S').log"
[email protected]
FILESYSTEM_USAGE_THRESHOLD=85
SCRIPT_START=$(date '+%Y-%m-%d %H:%M:%S')

#echo "$DEST_BASE_BLOB_URL"

# Get the client ID, client secret, and tenant ID from the app registration
CLIENT_ID=cfe10752-d606-46f8-a427-ef8956711234
CLIENT_SECRET=kP28Q~rtZrNdW4mU5Z6Ho.sgXbcU5Hq55Kb1234
TENANT_ID=da67ef1b-ca59-4db2-9a8c-aa8d9412345

# Authenticate AzCopy with VM's managed identity
#azcopy login --identity
#azcopy login --service-principal --tenant-id “$TENANT_ID” --application-id “$CLIENT_ID” --client-secret “$CLIENT_SECRET”
export AZCOPY_SPA_CLIENT_SECRET=$CLIENT_SECRET
azcopy login --service-principal --tenant-id $TENANT_ID --application-id $CLIENT_ID


# Iterate over directories starting with /shared2/attachments/as
for src_dir in /shared2/attachments/as*/; do

  # Get the directory name (e.g. as01, as02) from the path
  src_dir_name=$(basename "$src_dir")
  echo "in main directory" $src_dir_name

 # Check the filesystem usage before syncing
#echo $LOG_DIR
#echo $LOG_FILE
echo "Filesystem Usage:"$filesystem_usage
echo "Filesystem Threshold:" $FILESYSTEM_USAGE_THRESHOLD
  
    filesystem_usage=$(df -h "$LOG_DIR" | awk 'NR==2{print $5}'|sed 's/%//g')
    if ((filesystem_usage >= FILESYSTEM_USAGE_THRESHOLD)); then
        echo "Filesystem usage for $LOG_DIR is over $FILESYSTEM_USAGE_THRESHOLD%, removing old log files..." >> "$LOG_FILE"
        rm "$LOG_DIR"/*.log
    fi

  # Iterate over subdirectories under the current as directory
  for subdir in "$src_dir"*/; do
    # Get the subdirectory name from the path
    subdir_name=$(basename "$subdir")
    echo "in sub directory $subdir_name and main directory $src_dir_name"

    # Construct the source and destination URLs
    SRC_URL="$SRC_BASE_DIR/$src_dir_name/$subdir_name/"
    DEST_URL="$DEST_BASE_BLOB_URL/$src_dir_name/$subdir_name/"

    # Sync the directories with AzCopy
    azcopy sync "$SRC_URL" "$DEST_URL" --recursive --log-level=ERROR

echo "Source:"$SRC_URL
echo "Destination:" $DEST_URL
#echo $DEST_BASE_BLOB_URL

  done
done

# Log out from AzCopy
azcopy logout

SCRIPT_END=$(date '+%Y-%m-%d %H:%M:%S')
SCRIPT_DURATION=$(( $(date -d "$SCRIPT_END" '+%s') - $(date -d "$SCRIPT_START" '+%s') ))
SCRIPT_DURATION_MINUTES=$(( $SCRIPT_DURATION / 60 ))

echo $SCRIPT_DURATION_MINUTES


# Send email with start and end times, and duration
#echo "Subject: AzCopy Sync Script Completed" | cat - <(echo -e "From: [email protected]\nTo: $EMAIL_RECIPIENT\n") <(echo -e "AzCopy Sync Script completed.\nStart time: $SCRIPT_START\nEnd time: $SCRIPT_END\nDuration: $SCRIPT_DURATION seconds ($SCRIPT_DURATION_MINUTES minutes).") | sendmail -t

任何想法如何在活动会话关闭时解决此身份验证问题

azure shell cloud blob azcopy
© www.soinside.com 2019 - 2024. All rights reserved.